-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert tt.reshape to tensor.collapse_shape or tensor.expand_shape wh…
…en possible (#195) The existing `ReshapeConverter` converts `tt.reshape` to `tensor.reshape`, which requires an explicit `shape` operand in its op semantics. However, almost all `tt.reshape` cases could be converted to `tensor.collapse_shape` or `tensor.expand_shape`, with proper `reassociation`: For instance ```mlir module { tt.func public @bcast_kernel_01(%arg0: !tt.ptr<f32>, %arg1: !tt.ptr<f32>) attributes {noinline = false} { %c32_i32 = arith.constant 32 : i32 %0 = tt.get_program_id x : i32 %1 = arith.muli %0, %c32_i32 : i32 %2 = tt.make_range {end = 32 : i32, start = 0 : i32} : tensor<32xi32> %3 = tt.splat %1 : i32 -> tensor<32xi32> %4 = arith.addi %3, %2 : tensor<32xi32> %5 = tt.make_range {end = 2048 : i32, start = 0 : i32} : tensor<2048xi32> %6 = tt.splat %1 : i32 -> tensor<2048xi32> %7 = arith.addi %6, %5 : tensor<2048xi32> %8 = tt.splat %arg0 : !tt.ptr<f32> -> tensor<32x!tt.ptr<f32>> %9 = tt.addptr %8, %4 : tensor<32x!tt.ptr<f32>>, tensor<32xi32> %10 = tt.load %9 : tensor<32x!tt.ptr<f32>> %11 = tt.reshape %10 {allow_reorder = false} : tensor<32xf32> -> tensor<1x32xf32> %12 = tt.broadcast %11 : tensor<1x32xf32> -> tensor<64x32xf32> %13 = tt.reshape %12 {allow_reorder = false} : tensor<64x32xf32> -> tensor<2048xf32> %14 = tt.splat %arg1 : !tt.ptr<f32> -> tensor<2048x!tt.ptr<f32>> %15 = tt.addptr %14, %7 : tensor<2048x!tt.ptr<f32>>, tensor<2048xi32> tt.store %15, %13 : tensor<2048x!tt.ptr<f32>> tt.return } } ``` to ```mlir #map = affine_map<(d0, d1) -> (0, d1)> #map1 = affine_map<(d0, d1) -> (d0, d1)> module { func.func @bcast_kernel_01(%arg0: memref<*xf32>, %arg1: memref<*xf32>, %arg2: i32, %arg3: i32, %arg4: i32, %arg5: i32, %arg6: i32, %arg7: i32) { %c32_i32 = arith.constant 32 : i32 %0 = arith.muli %arg5, %c32_i32 : i32 %1 = arith.index_cast %0 : i32 to index %reinterpret_cast = memref.reinterpret_cast %arg0 to offset: [%1], sizes: [32], strides: [1] : memref<*xf32> to memref<32xf32, strided<[1], offset: ?>> %alloc = memref.alloc() : memref<32xf32> memref.copy %reinterpret_cast, %alloc : memref<32xf32, strided<[1], offset: ?>> to memref<32xf32> %2 = bufferization.to_tensor %alloc restrict writable : memref<32xf32> %expanded = tensor.expand_shape %2 [[0, 1]] output_shape [1, 32] : tensor<32xf32> into tensor<1x32xf32> %3 = tensor.empty() : tensor<64x32xf32> %4 = linalg.generic {indexing_maps = [#map, #map1], iterator_types = ["parallel", "parallel"]} ins(%expanded : tensor<1x32xf32>) outs(%3 : tensor<64x32xf32>) attrs = {broadcastDims = array<i64: 0>} { ^bb0(%in: f32, %out: f32): linalg.yield %in : f32 } -> tensor<64x32xf32> %collapsed = tensor.collapse_shape %4 [[0, 1]] : tensor<64x32xf32> into tensor<2048xf32> %5 = arith.index_cast %0 : i32 to index %reinterpret_cast_0 = memref.reinterpret_cast %arg1 to offset: [%5], sizes: [2048], strides: [1] : memref<*xf32> to memref<2048xf32, strided<[1], offset: ?>> bufferization.materialize_in_destination %collapsed in writable %reinterpret_cast_0 : (tensor<2048xf32>, memref<2048xf32, strided<[1], offset: ?>>) -> () return } } ``` Co-authored-by: Xiaoran Weng <xiaoran.weng@verisilicon.com>
- Loading branch information
1 parent
6aa82f1
commit 5b17b80
Showing
4 changed files
with
33 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters