Skip to content

Make subtensor_insert either use in-place updates or use a lowering to loops directly #5042

Closed

Description

@MaheshRavishankar

Lowering of subtensor_insert to buffers requires two copies. For example

%c_tensor = subtensor_insert %a_tensor into %b_tensor[....]

when converted to buffers would be

linalg.copy(%b_buffer, %c_buffer)
%subview = subview %c_buffer[...]
linalg.copy(%a_buffer, %c_buffer)

The two copies have different workloads, and is not possible to implement these within a single dispatch. In the legacy path, the SplitDispatchFunction would split the two copies to enable GPU execution. Since this pass is on the deprecation path, a different approach is needed to lower to GPU.

Approach 1
If we know that %b_tensor and %c_tensor are mapped to the same underlying buffer, then the first copy becomes unnecessary. We can just do the second copy and lower to GPU

Approach 2
The subtensor_insert operation can be lowered to a single loop nest directly.

scf.for ...
   scf.for
       ...
       %val = scf.if <within the subview> {
          %a_val = load %a_buffer[...]
           scf.yield %a_val
       } else {
           %b_val = load %b_buffer[...]
           scf.yield %b_val
        }
        store %val, %c_buffer[...]

The ConvertToGPU pass contains utilities to distribute these loops.

Activity

  1. benvanik commented on Mar 10, 2021

    @benvanik
    Collaborator

    #5016 should make either approach possible and handles some of the trickier in-place conditions (like when the operand that is mutated in place is used by a subsequent op that is wanting to see the value prior to mutation) - all you'll need to do is either use flow.tensor.update for contiguous updates (it is now tied by default) or specify which result is tied back to which operand when you define the flow.dispatch.workgroups op (which could contain your scf loops).

    Here's an example dispatch workgroups op with a tied result (result 0 is tied to operand 0 and as such the workgroup is able to load/store to %arg0_capture):

      %0 = flow.dispatch.workgroups[%x, %y](%arg0, %arg1) : (tensor<?x4xf32>{%c128}, index) -> %arg0 =
      (%arg0_capture: !flow.dispatch.tensor<readwrite:?x4xf32>, %arg1_capture: index) {
        %t = flow.dispatch.tensor.load %arg0_capture : !flow.dispatch.tensor<readwrite:?x4xf32> -> tensor<?x4xf32>
        flow.dispatch.tensor.store %t, %arg0_capture : tensor<?x4xf32> -> !flow.dispatch.tensor<readwrite:?x4xf32>
        flow.return
      }

    and here's an example of how the immutability of tensors is preserved with the in-place operations (here the update is mutating %stream_target in place, but %stream_target is also used by the subsequent dispatch and so a snapshot is taken to preserve the contents first):

        //      CHECK: %[[TARGET_CLONE:.+]] = flow.tensor.clone %[[TARGET]] : tensor<2x4xi32>
        // CHECK-NEXT: %[[UPDATED:.+]] = flow.tensor.update %[[UPDATE]], %[[TARGET]]
        %t0 = flow.tensor.update %stream_update, %stream_target[%start0, %start1] : tensor<1x1xi32> -> tensor<2x4xi32>
        // CHECK-NEXT: %[[RETURN:.+]] = flow.dispatch @ex::@entry[%c8](%[[TARGET_CLONE]], %[[UPDATED]])
        %t1 = flow.dispatch @ex::@entry[%workload](%stream_target, %t0) : (tensor<2x4xi32>, tensor<2x4xi32>) -> tensor<2x4xi32>
  2. added
    codegen
    on Mar 10, 2021
  3. self-assigned this
    on Mar 10, 2021
  4. MaheshRavishankar commented on Mar 11, 2021

    @MaheshRavishankar
    CollaboratorAuthor

    @antiagainst the PR for in-place update landed. We should be able to use it for this now.

  5. antiagainst commented on Mar 11, 2021

    @antiagainst
    Member

    Fantastic. I'll be working on this tomorrow.

  6. antiagainst commented on Mar 15, 2021

    @antiagainst
    Member

    #5102 addressed this. Closing now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

codegen

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions