Skip to content

Implement tiling method for linalg_ext.reverse ops. - #7159

Merged
hanhanW merged 3 commits into
iree-org:mainfrom
hanhanW:rev2
Sep 24, 2021
Merged

hanhanW merged 3 commits into
iree-org:mainfrom
hanhanW:rev2

Conversation

@hanhanW

@hanhanW hanhanW commented Sep 23, 2021

Copy link
Copy Markdown
Contributor

To tile a linalg_ext.reverse op, we have to store the tiled result to
mirror offsets. E.g.,

Before:

[T_0], [T_1], [T_2], [T_3]

After:

[Rev_t3], [Rev_t2], [Rev_t1], [Rev_t0]

The snippet after DispatchLinalgOnTensors, which shows that the op is
tiled and distributed:

func private @_foo(%arg0: tensor<?xi32>, %arg1: !shapex.ranked_shape<[?]>) -> (tensor<?xi32>, !shapex.ranked_shape<[?]>) {
  %c0 = constant 0 : index
  %c1 = constant 1 : index
  %0 = shapex.ranked_dim %arg1[0] : !shapex.ranked_shape<[?]> -> index
  %1 = shapex.tie_shape %arg0, %arg1 : tensor<?xi32>, !shapex.ranked_shape<[?]>
  %2 = linalg.init_tensor [%0] : tensor<?xi32>
  %3 = tensor.dim %1, %c0 : tensor<?xi32>
  %4 = tensor.dim %2, %c0 : tensor<?xi32>
  %5 = tensor.dim %1, %c0 : tensor<?xi32>
  %6 = flow.dispatch.workgroups[%3, %c1, %c1](%1, %0) : (tensor<?xi32>{%5}, index) -> tensor<?xi32>{%4} =
      (%arg2: !flow.dispatch.tensor<readonly:?xi32>, %arg3: index, %arg4: !flow.dispatch.tensor<writeonly:?xi32>) {
    %c0_0 = constant 0 : index
    %8 = flow.dispatch.tensor.load %arg2, offsets = [], sizes = [], strides = [] : !flow.dispatch.tensor<readonly:?xi32> -> tensor<?xi32>
    %9 = linalg.init_tensor [%arg3] : tensor<?xi32>
    %workgroup_size_0 = flow.dispatch.workgroup.size[0] : index
    %10 = tensor.dim %8, %c0_0 : tensor<?xi32>
    %workgroup_id_0 = flow.dispatch.workgroup.id[0] : index
    %workgroup_count_0 = flow.dispatch.workgroup.count[0] : index
    %11 = affine.apply affine_map<(d0)[s0] -> (d0 * s0)>(%workgroup_id_0)[%workgroup_size_0]
    %12 = affine.apply affine_map<(d0)[s0] -> (d0 * s0)>(%workgroup_count_0)[%workgroup_size_0]
    scf.for %arg5 = %11 to %10 step %12 {
      %13 = affine.min affine_map<(d0)[s0, s1] -> (s0, -d0 + s1)>(%arg5)[%workgroup_size_0, %10]
      %14 = flow.dispatch.tensor.load %arg2, offsets = [%arg5], sizes = [%13], strides = [1] : !flow.dispatch.tensor<readonly:?xi32> -> tensor<?xi32>
      %15 = tensor.dim %8, %c0_0 : tensor<?xi32>
      %16 = subi %15, %arg5 : index
      %17 = subi %16, %13 : index
      %18 = tensor.extract_slice %9[%arg5] [%13] [1] : tensor<?xi32> to tensor<?xi32>
      %19 = linalg_ext.reverse dimensions(dense<0> : tensor<1xi64>) {__internal_linalg_transform__ = "workgroup"} ins(%14 : tensor<?xi32>) outs(%18 : tensor<?xi32>) : tensor<?xi32>
      flow.dispatch.tensor.store %19, %arg4, offsets = [%17], sizes = [%13], strides = [1] : tensor<?xi32> -> !flow.dispatch.tensor<writeonly:?xi32>
    }
    flow.return
  }
  %7 = shapex.get_ranked_shape %6 : tensor<?xi32> -> !shapex.ranked_shape<[?]>
  return %6, %7 : tensor<?xi32>, !shapex.ranked_shape<[?]>
}

The print-ir-after-all log can be found at:
https://gist.githubusercontent.com/hanhanW/bba1d36149f37d948e8cea5fe95a6287/raw

This is a step toward #5045

@hanhanW

hanhanW commented Sep 23, 2021

Copy link
Copy Markdown
Contributor Author

Depends on #7155

To tile a linalg_ext.reverse op, we have to store the tiled result to
mirror offsets. E.g.,

Before:

```
[T_0], [T_1], [T_2], [T_3]
```

After:

```
[Rev_t3], [Rev_t2], [Rev_t1], [Rev_t0]
```

The snippet after DispatchLinalgOnTensors, which shows that the op is
tiled and distributed:

```mlir
func private @_foo(%arg0: tensor<?xi32>, %arg1: !shapex.ranked_shape<[?]>) -> (tensor<?xi32>, !shapex.ranked_shape<[?]>) {
  %c0 = constant 0 : index
  %c1 = constant 1 : index
  %0 = shapex.ranked_dim %arg1[0] : !shapex.ranked_shape<[?]> -> index
  %1 = shapex.tie_shape %arg0, %arg1 : tensor<?xi32>, !shapex.ranked_shape<[?]>
  %2 = linalg.init_tensor [%0] : tensor<?xi32>
  %3 = tensor.dim %1, %c0 : tensor<?xi32>
  %4 = tensor.dim %2, %c0 : tensor<?xi32>
  %5 = tensor.dim %1, %c0 : tensor<?xi32>
  %6 = flow.dispatch.workgroups[%3, %c1, %c1](%1, %0) : (tensor<?xi32>{%5}, index) -> tensor<?xi32>{%4} =
      (%arg2: !flow.dispatch.tensor<readonly:?xi32>, %arg3: index, %arg4: !flow.dispatch.tensor<writeonly:?xi32>) {
    %c0_0 = constant 0 : index
    %8 = flow.dispatch.tensor.load %arg2, offsets = [], sizes = [], strides = [] : !flow.dispatch.tensor<readonly:?xi32> -> tensor<?xi32>
    %9 = linalg.init_tensor [%arg3] : tensor<?xi32>
    %workgroup_size_0 = flow.dispatch.workgroup.size[0] : index
    %10 = tensor.dim %8, %c0_0 : tensor<?xi32>
    %workgroup_id_0 = flow.dispatch.workgroup.id[0] : index
    %workgroup_count_0 = flow.dispatch.workgroup.count[0] : index
    %11 = affine.apply affine_map<(d0)[s0] -> (d0 * s0)>(%workgroup_id_0)[%workgroup_size_0]
    %12 = affine.apply affine_map<(d0)[s0] -> (d0 * s0)>(%workgroup_count_0)[%workgroup_size_0]
    scf.for %arg5 = %11 to %10 step %12 {
      %13 = affine.min affine_map<(d0)[s0, s1] -> (s0, -d0 + s1)>(%arg5)[%workgroup_size_0, %10]
      %14 = flow.dispatch.tensor.load %arg2, offsets = [%arg5], sizes = [%13], strides = [1] : !flow.dispatch.tensor<readonly:?xi32> -> tensor<?xi32>
      %15 = tensor.dim %8, %c0_0 : tensor<?xi32>
      %16 = subi %15, %arg5 : index
      %17 = subi %16, %13 : index
      %18 = tensor.extract_slice %9[%arg5] [%13] [1] : tensor<?xi32> to tensor<?xi32>
      %19 = linalg_ext.reverse dimensions(dense<0> : tensor<1xi64>) {__internal_linalg_transform__ = "workgroup"} ins(%14 : tensor<?xi32>) outs(%18 : tensor<?xi32>) : tensor<?xi32>
      flow.dispatch.tensor.store %19, %arg4, offsets = [%17], sizes = [%13], strides = [1] : tensor<?xi32> -> !flow.dispatch.tensor<writeonly:?xi32>
    }
    flow.return
  }
  %7 = shapex.get_ranked_shape %6 : tensor<?xi32> -> !shapex.ranked_shape<[?]>
  return %6, %7 : tensor<?xi32>, !shapex.ranked_shape<[?]>
}
```

The `print-ir-after-all` log can be found at:
https://gist.githubusercontent.com/hanhanW/bba1d36149f37d948e8cea5fe95a6287/raw
@hanhanW
hanhanW changed the base branch from hanhan-rev1 to main September 23, 2021 22:37
@hanhanW
hanhanW merged commit 8128e96 into iree-org:main Sep 24, 2021
@hanhanW
hanhanW deleted the rev2 branch September 24, 2021 22:22
@NatashaKnk NatashaKnk mentioned this pull request Sep 27, 2021
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