Skip to content

Commit 14dbc75

Browse files
committed
Add E2E test cases to test fastmath attribute.
Ops tested: - math.exp - arith.maximumf
1 parent 30f2bf7 commit 14dbc75

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// RUN: mlir-opt %s --gpu-lower-to-xevm-pipeline="xegpu-op-level=workgroup" \
2+
// RUN: | mlir-runner \
3+
// RUN: --shared-libs=%mlir_levelzero_runtime \
4+
// RUN: --shared-libs=%mlir_runner_utils \
5+
// RUN: --entry-point-result=void \
6+
// RUN: | FileCheck %s
7+
8+
#map = #xegpu.layout<sg_layout = [8, 4], sg_data = [32, 32], inst_data = [8, 16]>
9+
module @gemm attributes {gpu.container_module} {
10+
func.func @test_fast_math(%input1: memref<256x256xf32>, %input2: memref<256x256xf32>) -> (memref<256x256xf32>, memref<256x256xf32>) attributes {llvm.emit_c_interface} {
11+
%c1 = arith.constant 1 : index
12+
%c4 = arith.constant 4 : index
13+
%c8 = arith.constant 8 : index
14+
%c16 = arith.constant 16 : index
15+
%c32 = arith.constant 32 : index
16+
%c64 = arith.constant 64 : index
17+
%c128 = arith.constant 128 : index
18+
%c512 = arith.constant 512 : index
19+
%input1_gpu = gpu.alloc () : memref<256x256xf32>
20+
gpu.memcpy %input1_gpu, %input2 : memref<256x256xf32>, memref<256x256xf32>
21+
%input2_gpu = gpu.alloc () : memref<256x256xf32>
22+
gpu.memcpy %input2_gpu, %input2 : memref<256x256xf32>, memref<256x256xf32>
23+
%result_gpu = gpu.alloc () : memref<256x256xf32>
24+
%result_gpu_with_fastmath = gpu.alloc () : memref<256x256xf32>
25+
// NOTE: Here we can't use [8, 64] wi threads following
26+
// the SG thread layout of [8, 4]. Because runtime will linearize
27+
// the x dimension first (we need y dimension to be linearized first).
28+
// So just use linearized thread layout of [512, 1] wi threads.
29+
gpu.launch_func @math_kernels::@gpu_maximumf blocks in (%c1, %c1, %c1) threads in (%c512, %c1, %c1) args(%input1_gpu : memref<256x256xf32>, %input2_gpu : memref<256x256xf32>, %result_gpu : memref<256x256xf32>)
30+
gpu.launch_func @math_kernels::@gpu_maximumf_with_fastmath blocks in (%c1, %c1, %c1) threads in (%c512, %c1, %c1) args(%input1_gpu : memref<256x256xf32>, %input2_gpu : memref<256x256xf32>, %result_gpu : memref<256x256xf32>)
31+
32+
%result_host = memref.alloc() : memref<256x256xf32>
33+
%result_host_with_fastmath = memref.alloc() : memref<256x256xf32>
34+
gpu.memcpy %result_host, %result_gpu : memref<256x256xf32>, memref<256x256xf32>
35+
gpu.dealloc %input_gpu : memref<256x256xf32>
36+
gpu.dealloc %result_gpu : memref<256x256xf32>
37+
return %result_host, %result_host_with_fastmath : memref<256x256xf32>, memref<256x256xf32>
38+
}
39+
40+
gpu.module @math_kernels {
41+
gpu.func @gpu_maximumf(%input1_gpu : memref<256x256xf32>, %input2_gpu : memref<256x256xf32>, %result_gpu : memref<256x256xf32>) kernel {
42+
%c256 = arith.constant 256 : index
43+
%block_id_x = gpu.block_id x
44+
%block_id_y = gpu.block_id y
45+
%m = arith.muli %block_id_x, %c256 : index
46+
%n = arith.muli %block_id_y, %c256 : index
47+
%input_tdesc_1 = xegpu.create_nd_tdesc %input1_gpu : memref<256x256xf32> -> !xegpu.tensor_desc<256x256xf32, #map>
48+
%input_val_1 = xegpu.load_nd %input_tdesc_1[%m, %n] : !xegpu.tensor_desc<256x256xf32, #map> -> vector<256x256xf32>
49+
%input_tdesc_2 = xegpu.create_nd_tdesc %input2_gpu : memref<256x256xf32> -> !xegpu.tensor_desc<256x256xf32, #map>
50+
%input_val_2 = xegpu.load_nd %input_tdesc_2[%m, %n] : !xegpu.tensor_desc<256x256xf32, #map> -> vector<256x256xf32>
51+
%result_val = arith.maximumf %input_val_1, %input_val_2 : vector<256x256xf32>
52+
%result_tdesc = xegpu.create_nd_tdesc %result_gpu : memref<256x256xf32> -> !xegpu.tensor_desc<256x256xf32, #map>
53+
xegpu.store_nd %result_val, %result_tdesc[%m, %n] : vector<256x256xf32>, !xegpu.tensor_desc<256x256xf32, #map>
54+
gpu.return
55+
}
56+
57+
// Kernel with fastmath attribute
58+
gpu.func @gpu_maximumf_with_fastmath(%input1_gpu : memref<256x256xf32>, %input2_gpu : memref<256x256xf32>, %result_gpu : memref<256x256xf32>) kernel {
59+
%c256 = arith.constant 256 : index
60+
%block_id_x = gpu.block_id x
61+
%block_id_y = gpu.block_id y
62+
%m = arith.muli %block_id_x, %c256 : index
63+
%n = arith.muli %block_id_y, %c256 : index
64+
%input_tdesc_1 = xegpu.create_nd_tdesc %input1_gpu : memref<256x256xf32> -> !xegpu.tensor_desc<256x256xf32, #map>
65+
%input_val_1 = xegpu.load_nd %input_tdesc_1[%m, %n] : !xegpu.tensor_desc<256x256xf32, #map> -> vector<256x256xf32>
66+
%input_tdesc_2 = xegpu.create_nd_tdesc %input2_gpu : memref<256x256xf32> -> !xegpu.tensor_desc<256x256xf32, #map>
67+
%input_val_2 = xegpu.load_nd %input_tdesc_2[%m, %n] : !xegpu.tensor_desc<256x256xf32, #map> -> vector<256x256xf32>
68+
%result_val = arith.maximumf %input_val_1, %input_val_2 fastmath<fast> : vector<256x256xf32>
69+
%result_tdesc = xegpu.create_nd_tdesc %result_gpu : memref<256x256xf32> -> !xegpu.tensor_desc<256x256xf32, #map>
70+
xegpu.store_nd %result_val, %result_tdesc[%m, %n] : vector<256x256xf32>, !xegpu.tensor_desc<256x256xf32, #map>
71+
gpu.return
72+
}
73+
}
74+
75+
func.func @main() attributes {llvm.emit_c_interface} {
76+
%c0 = arith.constant 0 : index
77+
%c1 = arith.constant 1 : index
78+
%c2_f32 = arith.constant 2.2 : f32
79+
%c256 = arith.constant 256 : index
80+
%input_1 = memref.alloc() : memref<256x256xf32>
81+
%input_2 = memref.alloc() : memref<256x256xf32>
82+
%cpu_ref_result = memref.alloc() : memref<256x256xf32>
83+
84+
scf.for %arg0 = %c0 to %c256 step %c1 {
85+
scf.for %arg1 = %c0 to %c256 step %c1 {
86+
memref.store %c2_f32, %input_1[%arg0, %arg1] : memref<256x256xf32>
87+
memref.store %c2_f32, %input_2[%arg0, %arg1] : memref<256x256xf32>
88+
}
89+
}
90+
91+
// Run CPU version
92+
scf.for %arg0 = %c0 to %c256 step %c1 {
93+
scf.for %arg1 = %c0 to %c256 step %c1 {
94+
%val_1 = memref.load %input_1[%arg0, %arg1] : memref<256x256xf32>
95+
%val_2 = memref.load %input_2[%arg0, %arg1] : memref<256x256xf32>
96+
%res_val = arith.maximumf %val_1, %val_2 : f32
97+
memref.store %res_val, %cpu_ref_result[%arg0, %arg1] : memref<256x256xf32>
98+
}
99+
}
100+
101+
// Run GPU version.
102+
%gpu_result, %gpu_result_fastmath = call @test_fast_math(%input_1, %input_2) : (memref<256x256xf32>, memref<256x256xf32>) -> (memref<256x256xf32>, memref<256x256xf32>)
103+
%gpu_result_cast = memref.cast %gpu_result : memref<256x256xf32> to memref<*xf32>
104+
// CHECK: Unranked Memref base@ = 0x{{[0-9a-f]+}}
105+
// CHECK-COUNT-256: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]
106+
call @printMemrefF32(%gpu_result_cast) : (memref<*xf32>) -> ()
107+
108+
memref.dealloc %input_1 : memref<256x256xf32>
109+
memref.dealloc %input_2 : memref<256x256xf32>
110+
memref.dealloc %cpu_ref_result : memref<256x256xf32>
111+
memref.dealloc %gpu_result : memref<256x256xf32>
112+
memref.dealloc %gpu_result_fastmath : memref<256x256xf32>
113+
return
114+
}
115+
func.func private @printMemrefF32(memref<*xf32>) attributes {llvm.emit_c_interface}
116+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// RUN: mlir-opt %s --gpu-lower-to-xevm-pipeline="xegpu-op-level=workgroup" \
2+
// RUN: | mlir-runner \
3+
// RUN: --shared-libs=%mlir_levelzero_runtime \
4+
// RUN: --shared-libs=%mlir_runner_utils \
5+
// RUN: --entry-point-result=void \
6+
// RUN: | FileCheck %s
7+
8+
#map = #xegpu.layout<sg_layout = [8, 4], sg_data = [32, 32], inst_data = [8, 16]>
9+
module @gemm attributes {gpu.container_module} {
10+
func.func @test_fast_math(%input: memref<256x256xf32>) -> (memref<256x256xf32>, memref<256x256xf32>) attributes {llvm.emit_c_interface} {
11+
%c1 = arith.constant 1 : index
12+
%c4 = arith.constant 4 : index
13+
%c8 = arith.constant 8 : index
14+
%c16 = arith.constant 16 : index
15+
%c32 = arith.constant 32 : index
16+
%c64 = arith.constant 64 : index
17+
%c128 = arith.constant 128 : index
18+
%c512 = arith.constant 512 : index
19+
%input_gpu = gpu.alloc () : memref<256x256xf32>
20+
gpu.memcpy %input_gpu, %input : memref<256x256xf32>, memref<256x256xf32>
21+
%result_gpu = gpu.alloc () : memref<256x256xf32>
22+
%result_gpu_with_fastmath = gpu.alloc () : memref<256x256xf32>
23+
// NOTE: Here we can't use [8, 64] wi threads following
24+
// the SG thread layout of [8, 4]. Because runtime will linearize
25+
// the x dimension first (we need y dimension to be linearized first).
26+
// So just use linearized thread layout of [512, 1] wi threads.
27+
gpu.launch_func @math_kernels::@gpu_exp blocks in (%c1, %c1, %c1) threads in (%c512, %c1, %c1) args(%input_gpu : memref<256x256xf32>, %result_gpu : memref<256x256xf32>)
28+
gpu.launch_func @math_kernels::@gpu_exp_with_fastmath blocks in (%c1, %c1, %c1) threads in (%c512, %c1, %c1) args(%input_gpu : memref<256x256xf32>, %result_gpu_with_fastmath : memref<256x256xf32>)
29+
30+
%result_host = memref.alloc() : memref<256x256xf32>
31+
%result_host_with_fastmath = memref.alloc() : memref<256x256xf32>
32+
gpu.memcpy %result_host, %result_gpu : memref<256x256xf32>, memref<256x256xf32>
33+
gpu.dealloc %input_gpu : memref<256x256xf32>
34+
gpu.dealloc %result_gpu : memref<256x256xf32>
35+
return %result_host, %result_host_with_fastmath : memref<256x256xf32>, memref<256x256xf32>
36+
}
37+
38+
gpu.module @math_kernels {
39+
gpu.func @gpu_exp(%input_gpu : memref<256x256xf32>, %result_gpu : memref<256x256xf32>) kernel {
40+
%c256 = arith.constant 256 : index
41+
%block_id_x = gpu.block_id x
42+
%block_id_y = gpu.block_id y
43+
%m = arith.muli %block_id_x, %c256 : index
44+
%n = arith.muli %block_id_y, %c256 : index
45+
%input_tdesc = xegpu.create_nd_tdesc %input_gpu : memref<256x256xf32> -> !xegpu.tensor_desc<256x256xf32, #map>
46+
%input_val = xegpu.load_nd %input_tdesc[%m, %n] : !xegpu.tensor_desc<256x256xf32, #map> -> vector<256x256xf32>
47+
%result_val = math.exp %input_val : vector<256x256xf32>
48+
%result_tdesc = xegpu.create_nd_tdesc %result_gpu : memref<256x256xf32> -> !xegpu.tensor_desc<256x256xf32, #map>
49+
xegpu.store_nd %result_val, %result_tdesc[%m, %n] : vector<256x256xf32>, !xegpu.tensor_desc<256x256xf32, #map>
50+
gpu.return
51+
}
52+
53+
// Kernel with fastmath attribute
54+
gpu.func @gpu_exp_with_fastmath(%input_gpu : memref<256x256xf32>, %result_gpu : memref<256x256xf32>) kernel {
55+
%c256 = arith.constant 256 : index
56+
%block_id_x = gpu.block_id x
57+
%block_id_y = gpu.block_id y
58+
%m = arith.muli %block_id_x, %c256 : index
59+
%n = arith.muli %block_id_y, %c256 : index
60+
%input_tdesc = xegpu.create_nd_tdesc %input_gpu : memref<256x256xf32> -> !xegpu.tensor_desc<256x256xf32, #map>
61+
%input_val = xegpu.load_nd %input_tdesc[%m, %n] : !xegpu.tensor_desc<256x256xf32, #map> -> vector<256x256xf32>
62+
%result_val = math.exp %input_val fastmath<fast> : vector<256x256xf32>
63+
%result_tdesc = xegpu.create_nd_tdesc %result_gpu : memref<256x256xf32> -> !xegpu.tensor_desc<256x256xf32, #map>
64+
xegpu.store_nd %result_val, %result_tdesc[%m, %n] : vector<256x256xf32>, !xegpu.tensor_desc<256x256xf32, #map>
65+
gpu.return
66+
}
67+
}
68+
69+
func.func @main() attributes {llvm.emit_c_interface} {
70+
%c0 = arith.constant 0 : index
71+
%c1 = arith.constant 1 : index
72+
%c2_f32 = arith.constant 2.2 : f32
73+
%c256 = arith.constant 256 : index
74+
%input = memref.alloc() : memref<256x256xf32>
75+
%input_ref = memref.alloc() : memref<256x256xf32>
76+
%cpu_ref_result = memref.alloc() : memref<256x256xf32>
77+
78+
scf.for %arg0 = %c0 to %c256 step %c1 {
79+
scf.for %arg1 = %c0 to %c256 step %c1 {
80+
memref.store %c2_f32, %input[%arg0, %arg1] : memref<256x256xf32>
81+
memref.store %c2_f32, %input_ref[%arg0, %arg1] : memref<256x256xf32>
82+
}
83+
}
84+
85+
// Run CPU version
86+
scf.for %arg0 = %c0 to %c256 step %c1 {
87+
scf.for %arg1 = %c0 to %c256 step %c1 {
88+
%val = memref.load %input_ref[%arg0, %arg1] : memref<256x256xf32>
89+
%res_val = math.exp %val : f32
90+
memref.store %res_val, %cpu_ref_result[%arg0, %arg1] : memref<256x256xf32>
91+
}
92+
}
93+
94+
// Run GPU version.
95+
%gpu_result, %gpu_result_fastmath = call @test_fast_math(%input) : (memref<256x256xf32>) -> (memref<256x256xf32>, memref<256x256xf32>)
96+
%gpu_result_cast = memref.cast %gpu_result : memref<256x256xf32> to memref<*xf32>
97+
// CHECK: Unranked Memref base@ = 0x{{[0-9a-f]+}}
98+
// CHECK-COUNT-256: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255]
99+
call @printMemrefF32(%gpu_result_cast) : (memref<*xf32>) -> ()
100+
101+
memref.dealloc %input : memref<256x256xf32>
102+
memref.dealloc %input_ref : memref<256x256xf32>
103+
memref.dealloc %cpu_ref_result : memref<256x256xf32>
104+
memref.dealloc %gpu_result : memref<256x256xf32>
105+
memref.dealloc %gpu_result_fastmath : memref<256x256xf32>
106+
return
107+
}
108+
func.func private @printMemrefF32(memref<*xf32>) attributes {llvm.emit_c_interface}
109+
}

0 commit comments

Comments
 (0)