Skip to content

LLVMGPU] Move bufferization after vectorization for shared mem transpose - #10478

Merged
KoolJBlack merged 3 commits into
iree-org:mainfrom
KoolJBlack:transpose-vector-buffer
Sep 21, 2022
Merged

KoolJBlack merged 3 commits into
iree-org:mainfrom
KoolJBlack:transpose-vector-buffer

Conversation

@KoolJBlack

@KoolJBlack KoolJBlack commented Sep 20, 2022 •

Copy link
Copy Markdown
Contributor

Transition transpose shared mem pipeline to do vectorization before bufferization.
Based on #10217

Related to #10005

@KoolJBlack

Copy link
Copy Markdown
Contributor Author

@ThomasRaoux simple example of a single transpose before/after the bufferization swap:

w/ LLVMGPUTileAndDistribute:

      gpu.barrier
      %13 = affine.apply affine_map<()[s0, s1, s2] -> (s1 + s2 * 32 + s0 floordiv 8)>()[%0, %1, %2]
      %14 = affine.apply affine_map<()[s0] -> (s0 * 4 - (s0 floordiv 8) * 32)>()[%0]
      %15 = memref.subview %11[%13, %14] [1, 4] [1, 1] : memref<32x32xf32, strided<[768, 1], offset: ?>> to memref<1x4xf32, strided<[768, 1], offset: ?>>
      %16 = memref.subview %4[%13, %14] [1, 4] [1, 1] : memref<32x32xf32, strided<[33, 1]>, 3> to memref<1x4xf32, strided<[33, 1], offset: ?>, 3>
      %17 = vector.transfer_read %15[%c0, %c0], %cst {in_bounds = [true]} : memref<1x4xf32, strided<[768, 1], offset: ?>>, vector<4xf32>
      vector.transfer_write %17, %16[%c0, %c0] {in_bounds = [true]} : vector<4xf32>, memref<1x4xf32, strided<[33, 1], offset: ?>, 3>
      gpu.barrier
      %18 = affine.apply affine_map<()[s0] -> (s0 * 4)>()[%0]
      %19 = memref.subview %4[%18, %1] [4, 1] [1, 1] : memref<32x32xf32, strided<[33, 1]>, 3> to memref<4x1xf32, strided<[33, 1], offset: ?>, 3>
      %20 = memref.subview %12[%1, %18] [1, 4] [1, 1] : memref<32x32xf32, strided<[2048, 1], offset: ?>> to memref<1x4xf32, strided<[2048, 1], offset: ?>>
      %21 = memref.subview %10[%1, %18] [1, 4] [1, 1] : memref<32x32xf32, strided<[2048, 1], offset: ?>> to memref<1x4xf32, strided<[2048, 1], offset: ?>>
      %22 = vector.transfer_read %19[%c0, %c0], %cst {in_bounds = [true, true]} : memref<4x1xf32, strided<[33, 1], offset: ?>, 3>, vector<4x1xf32>
      %23 = vector.shape_cast %22 : vector<4x1xf32> to vector<1x4xf32>
      %24 = vector.transfer_read %20[%c0, %c0], %cst {in_bounds = [true]} : memref<1x4xf32, strided<[2048, 1], offset: ?>>, vector<4xf32>
      %25 = vector.extract %23[0] : vector<1x4xf32>
      %26 = arith.addf %25, %24 : vector<4xf32>
      vector.transfer_write %26, %21[%c0, %c0] {in_bounds = [true]} : vector<4xf32>, memref<1x4xf32, strided<[2048, 1], offset: ?>>

w/ LLVMGPUTensorAlloc:

      gpu.barrier
      %13 = affine.apply affine_map<()[s0, s1, s2] -> (s1 + s2 * 32 + s0 floordiv 8)>()[%0, %1, %2]
      %14 = affine.apply affine_map<()[s0] -> (s0 * 4 - (s0 floordiv 8) * 32)>()[%0]
      %15 = memref.subview %11[%13, %14] [1, 4] [1, 1] : memref<32x32xf32, strided<[768, 1], offset: ?>> to memref<1x4xf32, strided<[768, 1], offset: ?>>
      %16 = memref.subview %4[%13, %14] [1, 4] [1, 1] : memref<32x32xf32, strided<[33, 1]>, 3> to memref<1x4xf32, strided<[33, 1], offset: ?>, 3>
      %17 = vector.transfer_read %15[%c0, %c0], %cst {in_bounds = [true, true]} : memref<1x4xf32, strided<[768, 1], offset: ?>>, vector<1x4xf32>
      vector.transfer_write %17, %16[%c0, %c0] {in_bounds = [true, true]} : vector<1x4xf32>, memref<1x4xf32, strided<[33, 1], offset: ?>, 3>
      gpu.barrier
      %18 = affine.apply affine_map<()[s0] -> (s0 * 4)>()[%0]
      %19 = memref.subview %10[%1, %18] [1, 4] [1, 1] : memref<32x32xf32, strided<[2048, 1], offset: ?>> to memref<1x4xf32, strided<[2048, 1], offset: ?>>
      %20 = vector.transfer_read %4[%18, %1], %cst {in_bounds = [true, true]} : memref<32x32xf32, strided<[33, 1]>, 3>, vector<4x1xf32>
      %21 = vector.shape_cast %20 : vector<4x1xf32> to vector<1x4xf32>
      %22 = vector.transfer_read %12[%1, %18], %cst {in_bounds = [true]} : memref<32x32xf32, strided<[2048, 1], offset: ?>>, vector<4xf32>
      %23 = vector.extract %21[0] : vector<1x4xf32>
      %24 = arith.addf %23, %22 : vector<4xf32>
      vector.transfer_write %24, %19[%c0, %c0] {in_bounds = [true]} : vector<4xf32>, memref<1x4xf32, strided<[2048, 1], offset: ?>>

The key differences between the two IRs:

  • Between the GPU barriers, the vector.transfer_read is a 2D shape instead of 1D
  • The subviews for the input operands to the contents of the linalg generic add operation are omitted. Practically, the IR seems to have the same function, but I cannot tell if these subviews were particularly important.

Note that these differences all disappear after the ConvertAffineToStandard pass. So I'm pretty confident these are functionally the same, but I'll need to update all of the transpose_pipeline_test.mlir tests to reflect the changes in intermediate IR.

@ThomasRaoux

Copy link
Copy Markdown
Contributor

@ThomasRaoux simple example of a single transpose before/after the bufferization swap:

w/ LLVMGPUTileAndDistribute:

      gpu.barrier
      %13 = affine.apply affine_map<()[s0, s1, s2] -> (s1 + s2 * 32 + s0 floordiv 8)>()[%0, %1, %2]
      %14 = affine.apply affine_map<()[s0] -> (s0 * 4 - (s0 floordiv 8) * 32)>()[%0]
      %15 = memref.subview %11[%13, %14] [1, 4] [1, 1] : memref<32x32xf32, strided<[768, 1], offset: ?>> to memref<1x4xf32, strided<[768, 1], offset: ?>>
      %16 = memref.subview %4[%13, %14] [1, 4] [1, 1] : memref<32x32xf32, strided<[33, 1]>, 3> to memref<1x4xf32, strided<[33, 1], offset: ?>, 3>
      %17 = vector.transfer_read %15[%c0, %c0], %cst {in_bounds = [true]} : memref<1x4xf32, strided<[768, 1], offset: ?>>, vector<4xf32>
      vector.transfer_write %17, %16[%c0, %c0] {in_bounds = [true]} : vector<4xf32>, memref<1x4xf32, strided<[33, 1], offset: ?>, 3>
      gpu.barrier
      %18 = affine.apply affine_map<()[s0] -> (s0 * 4)>()[%0]
      %19 = memref.subview %4[%18, %1] [4, 1] [1, 1] : memref<32x32xf32, strided<[33, 1]>, 3> to memref<4x1xf32, strided<[33, 1], offset: ?>, 3>
      %20 = memref.subview %12[%1, %18] [1, 4] [1, 1] : memref<32x32xf32, strided<[2048, 1], offset: ?>> to memref<1x4xf32, strided<[2048, 1], offset: ?>>
      %21 = memref.subview %10[%1, %18] [1, 4] [1, 1] : memref<32x32xf32, strided<[2048, 1], offset: ?>> to memref<1x4xf32, strided<[2048, 1], offset: ?>>
      %22 = vector.transfer_read %19[%c0, %c0], %cst {in_bounds = [true, true]} : memref<4x1xf32, strided<[33, 1], offset: ?>, 3>, vector<4x1xf32>
      %23 = vector.shape_cast %22 : vector<4x1xf32> to vector<1x4xf32>
      %24 = vector.transfer_read %20[%c0, %c0], %cst {in_bounds = [true]} : memref<1x4xf32, strided<[2048, 1], offset: ?>>, vector<4xf32>
      %25 = vector.extract %23[0] : vector<1x4xf32>
      %26 = arith.addf %25, %24 : vector<4xf32>
      vector.transfer_write %26, %21[%c0, %c0] {in_bounds = [true]} : vector<4xf32>, memref<1x4xf32, strided<[2048, 1], offset: ?>>

w/ LLVMGPUTensorAlloc:

      gpu.barrier
      %13 = affine.apply affine_map<()[s0, s1, s2] -> (s1 + s2 * 32 + s0 floordiv 8)>()[%0, %1, %2]
      %14 = affine.apply affine_map<()[s0] -> (s0 * 4 - (s0 floordiv 8) * 32)>()[%0]
      %15 = memref.subview %11[%13, %14] [1, 4] [1, 1] : memref<32x32xf32, strided<[768, 1], offset: ?>> to memref<1x4xf32, strided<[768, 1], offset: ?>>
      %16 = memref.subview %4[%13, %14] [1, 4] [1, 1] : memref<32x32xf32, strided<[33, 1]>, 3> to memref<1x4xf32, strided<[33, 1], offset: ?>, 3>
      %17 = vector.transfer_read %15[%c0, %c0], %cst {in_bounds = [true, true]} : memref<1x4xf32, strided<[768, 1], offset: ?>>, vector<1x4xf32>
      vector.transfer_write %17, %16[%c0, %c0] {in_bounds = [true, true]} : vector<1x4xf32>, memref<1x4xf32, strided<[33, 1], offset: ?>, 3>
      gpu.barrier
      %18 = affine.apply affine_map<()[s0] -> (s0 * 4)>()[%0]
      %19 = memref.subview %10[%1, %18] [1, 4] [1, 1] : memref<32x32xf32, strided<[2048, 1], offset: ?>> to memref<1x4xf32, strided<[2048, 1], offset: ?>>
      %20 = vector.transfer_read %4[%18, %1], %cst {in_bounds = [true, true]} : memref<32x32xf32, strided<[33, 1]>, 3>, vector<4x1xf32>
      %21 = vector.shape_cast %20 : vector<4x1xf32> to vector<1x4xf32>
      %22 = vector.transfer_read %12[%1, %18], %cst {in_bounds = [true]} : memref<32x32xf32, strided<[2048, 1], offset: ?>>, vector<4xf32>
      %23 = vector.extract %21[0] : vector<1x4xf32>
      %24 = arith.addf %23, %22 : vector<4xf32>
      vector.transfer_write %24, %19[%c0, %c0] {in_bounds = [true]} : vector<4xf32>, memref<1x4xf32, strided<[2048, 1], offset: ?>>

The key differences between the two IRs:

  • Between the GPU barriers, the vector.transfer_read is a 2D shape instead of 1D
  • The subviews for the input operands to the contents of the linalg generic add operation are omitted. Practically, the IR seems to have the same function, but I cannot tell if these subviews were particularly important.

Note that these differences all disappear after the ConvertAffineToStandard pass. So I'm pretty confident these are functionally the same, but I'll need to update all of the transpose_pipeline_test.mlir tests to reflect the changes in intermediate IR.

Those difference are fine and as you notice go away. Since the IR differences seems minimal the best is probably to change the IR CHECKs, otherwise if it helps you can runs some extra passes in the test (but here it is probably not the solution)

@KoolJBlack
KoolJBlack force-pushed the transpose-vector-buffer branch from 581a539 to 7ac674c Compare September 20, 2022 17:50
@KoolJBlack
KoolJBlack marked this pull request as ready for review September 20, 2022 17:52
@KoolJBlack

Copy link
Copy Markdown
Contributor Author

@ThomasRaoux Note: some of the logic for transpose detection is duplicated between the TensorAlloc and KernelConfig. I'm consolidating these in a followup PR

@KoolJBlack
KoolJBlack force-pushed the transpose-vector-buffer branch from 7ac674c to 77e1d6a Compare September 20, 2022 23:10
@KoolJBlack
KoolJBlack merged commit 2829bcc into iree-org:main Sep 21, 2022
@KoolJBlack
KoolJBlack deleted the transpose-vector-buffer branch September 21, 2022 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants