|
| 1 | +// RUN: %python_executable %imex_runner --requires=l0-runtime -i %s --pass-pipeline-file=%p/xegpu-to-llvm.pp \ |
| 2 | +// RUN: --runner imex-cpu-runner -e main \ |
| 3 | +// RUN: --entry-point-result=void \ |
| 4 | +// RUN: --shared-libs=%irunner_utils,%mlir_runner_utils,%mlir_c_runner_utils,%levelzero_runtime --filecheck |
| 5 | +// RUN: %python_executable %imex_runner --requires=sycl-runtime -i %s --pass-pipeline-file=%p/xegpu-to-llvm.pp \ |
| 6 | +// RUN: --runner imex-cpu-runner -e main \ |
| 7 | +// RUN: --entry-point-result=void \ |
| 8 | +// RUN: --shared-libs=%irunner_utils,%mlir_runner_utils,%mlir_c_runner_utils,%sycl_runtime --filecheck |
| 9 | +module @gemm attributes {gpu.container_module} { |
| 10 | + func.func @test(%A: memref<8x16xf16>, %B: memref<16x16xf16> ) -> memref<8x16xf32> attributes {llvm.emit_c_interface} { |
| 11 | + %c1 = arith.constant 1 : index |
| 12 | + %memref = gpu.alloc host_shared () : memref<8x16xf16> |
| 13 | + %memref_1 = gpu.alloc host_shared () : memref<16x16xf16> |
| 14 | + memref.copy %A, %memref : memref<8x16xf16> to memref<8x16xf16> |
| 15 | + memref.copy %B, %memref_1 : memref<16x16xf16> to memref<16x16xf16> |
| 16 | + %memref_2 = gpu.alloc host_shared () : memref<8x16xf32> |
| 17 | + gpu.launch_func @module0::@test_kernel blocks in (%c1, %c1, %c1) threads in (%c1, %c1, %c1) args(%memref : memref<8x16xf16>, %memref_1 : memref<16x16xf16>, %memref_2 : memref<8x16xf32>) |
| 18 | + gpu.dealloc %memref : memref<8x16xf16> |
| 19 | + gpu.dealloc %memref_1 : memref<16x16xf16> |
| 20 | + return %memref_2 : memref<8x16xf32> |
| 21 | + } |
| 22 | + |
| 23 | + gpu.module @module0 attributes {spirv.target_env = #spirv.target_env<#spirv.vce<v1.4, [Addresses, Float16Buffer, Int64, Int16, Int8, Kernel, Linkage, Vector16, GenericPointer, Groups, Float16, Float64, AtomicFloat32AddEXT, ExpectAssumeKHR, SubgroupDispatch, VectorComputeINTEL, VectorAnyINTEL], [SPV_EXT_shader_atomic_float_add, SPV_KHR_expect_assume, SPV_INTEL_vector_compute]>, api=OpenCL, #spirv.resource_limits<>>} { |
| 24 | + gpu.func @test_kernel(%A: memref<8x16xf16>, %B: memref<16x16xf16>, %Out: memref<8x16xf32>) kernel attributes {VectorComputeFunctionINTEL, spirv.entry_point_abi = #spirv.entry_point_abi<>} { |
| 25 | + %c0 = arith.constant 0 : index |
| 26 | + %c16 = arith.constant 16 : index |
| 27 | + // load A tile |
| 28 | + %a_tile0 = xegpu.create_nd_tdesc %A [%c0, %c0] { mode = vc } : memref<8x16xf16> -> !xegpu.tensor_desc<8x16xf16> |
| 29 | + %val0 = xegpu.load_nd %a_tile0 { mode = vc, vnni_axis = 1} : !xegpu.tensor_desc<8x16xf16> -> vector<8x8x2xf16> |
| 30 | + // load B tile |
| 31 | + %b_tile0 = xegpu.create_nd_tdesc %B [%c0, %c0] { mode = vc } : memref<16x16xf16> -> !xegpu.tensor_desc<16x16xf16> |
| 32 | + %val2 = xegpu.load_nd %b_tile0 { mode = vc, vnni_axis = 0} : !xegpu.tensor_desc<16x16xf16> -> vector<8x16x2xf16> |
| 33 | + // do DPAS |
| 34 | + %val4 = xegpu.dpas %val0, %val2 : vector<8x8x2xf16>, vector<8x16x2xf16> -> vector<8x16xf32> |
| 35 | + // extract second 8x8 |
| 36 | + %val5 = vector.extract_strided_slice %val4 {sizes = [8, 8], strides = [1, 1], offsets = [0, 8]} : vector<8x16xf32> to vector<8x8xf32> |
| 37 | + %cst_8x8_flat = arith.constant dense<1.0> : vector<64xf32> |
| 38 | + %cst_8x8 = vector.shape_cast %cst_8x8_flat : vector<64xf32> to vector<8x8xf32> |
| 39 | + // shift the first half to left and use %cst_8x8 as the second half |
| 40 | + %val6 = vector.shuffle %val5, %cst_8x8 [0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15] : vector<8x8xf32>, vector<8x8xf32> |
| 41 | + %val7 = vector.shape_cast %val6 : vector<16x8xf32> to vector<8x16xf32> |
| 42 | + // store |
| 43 | + %out_tile = xegpu.create_nd_tdesc %Out [%c0, %c0] { mode = vc } : memref<8x16xf32> -> !xegpu.tensor_desc<8x16xf32> |
| 44 | + xegpu.store_nd %val7, %out_tile { mode = vc} : vector<8x16xf32>, !xegpu.tensor_desc<8x16xf32> |
| 45 | + gpu.return |
| 46 | + } |
| 47 | + } |
| 48 | + func.func @main() attributes {llvm.emit_c_interface} { |
| 49 | + // init constants |
| 50 | + %c0 = arith.constant 0 : index |
| 51 | + %c1 = arith.constant 1 : index |
| 52 | + %c8 = arith.constant 8 : index |
| 53 | + %c16 = arith.constant 16 : index |
| 54 | + %c1_f32 = arith.constant 1.0 : f32 |
| 55 | + // random init |
| 56 | + %lower = arith.constant -1.0 : f32 |
| 57 | + %upper = arith.constant 1.0 : f32 |
| 58 | + %false = arith.constant 0 : i1 |
| 59 | + %A = memref.alloc() : memref<8x16xf16> |
| 60 | + %B = memref.alloc() : memref<16x16xf16> |
| 61 | + %Out_cpu = memref.alloc() : memref<8x16xf32> |
| 62 | + %A_random = memref.cast %A : memref<8x16xf16> to memref<*xf16> |
| 63 | + %B_random = memref.cast %B : memref<16x16xf16> to memref<*xf16> |
| 64 | + call @fillResource1DRandomF16(%A_random, %lower, %upper, %false) : (memref<*xf16>, f32, f32, i1) -> () |
| 65 | + call @fillResource1DRandomF16(%B_random, %lower, %upper, %false) : (memref<*xf16>, f32, f32, i1) -> () |
| 66 | + // run GPU version |
| 67 | + %Out_gpu = call @test(%A, %B) : (memref<8x16xf16>, memref<16x16xf16>) -> memref<8x16xf32> |
| 68 | + %Out_gpu_cast = memref.cast %Out_gpu : memref<8x16xf32> to memref<*xf32> |
| 69 | + // run CPU version |
| 70 | + scf.for %i = %c0 to %c8 step %c1 { |
| 71 | + scf.for %j = %c8 to %c16 step %c1 { |
| 72 | + %v0_init = arith.constant 0.0 : f32 |
| 73 | + %result:1 = scf.for %k = %c0 to %c16 step %c1 iter_args(%v0 = %v0_init) -> f32 { |
| 74 | + %a0 = memref.load %A[%i, %k] : memref<8x16xf16> |
| 75 | + %b0 = memref.load %B[%k, %j] : memref<16x16xf16> |
| 76 | + %a0_f32 = arith.extf %a0 : f16 to f32 |
| 77 | + %b0_f32 = arith.extf %b0 : f16 to f32 |
| 78 | + %t0 = arith.mulf %a0_f32, %b0_f32 : f32 |
| 79 | + %v0_new = arith.addf %v0, %t0 : f32 |
| 80 | + scf.yield %v0_new : f32 |
| 81 | + } |
| 82 | + // only update the first 8x8 of the result, next 8x8 is value 1 |
| 83 | + %shifted_j = arith.subi %j, %c8 : index |
| 84 | + memref.store %result#0, %Out_cpu[%i, %shifted_j] : memref<8x16xf32> |
| 85 | + memref.store %c1_f32, %Out_cpu[%i, %j] : memref<8x16xf32> |
| 86 | + } |
| 87 | + } |
| 88 | + %Out_cpu_cast = memref.cast %Out_cpu : memref<8x16xf32> to memref<*xf32> |
| 89 | + // print GPU and CPU outs |
| 90 | + // call @printMemrefF32(%Out_cpu_cast) : (memref<*xf32>) -> () |
| 91 | + // call @printMemrefF32(%Out_gpu_cast) : (memref<*xf32>) -> () |
| 92 | + // CHECK: [ALLCLOSE: TRUE] |
| 93 | + call @printAllcloseF32(%Out_gpu_cast, %Out_cpu_cast) : (memref<*xf32>, memref<*xf32>) -> () |
| 94 | + // dealloc |
| 95 | + memref.dealloc %A : memref<8x16xf16> |
| 96 | + memref.dealloc %B : memref<16x16xf16> |
| 97 | + memref.dealloc %Out_cpu : memref<8x16xf32> |
| 98 | + // gpu dealloc |
| 99 | + gpu.dealloc %Out_gpu : memref<8x16xf32> |
| 100 | + return |
| 101 | + } |
| 102 | + func.func private @printMemrefF32(memref<*xf32>) attributes {llvm.emit_c_interface} |
| 103 | + func.func private @fillResource1DRandomF16(memref<*xf16>, f32, f32, i1) attributes {llvm.emit_c_interface} |
| 104 | + func.func private @printAllcloseF32(memref<*xf32>, memref<*xf32>) attributes {llvm.emit_c_interface} |
| 105 | +} |
0 commit comments