Skip to content

Reduce control overhead in generated kernels #2134

Closed

Description

@MaheshRavishankar

Current kernel generation introduces lot of control overhead. There are two reasons for this

  1. The generated kernel is accounting for when the number of workgroups is not enough to execute the entire iteration space
  2. All operations are tiled to partition to workgroups, and then partitioned to workitems.

The result is the current kernel has the following structure (using CUDA parlance)

for (bz = lb_z + blockIdx.z * blockDim.z ; bz < ub_z; bz+= gridDim.z * blockDim.z)
  for (by = lb_y + blockIdx.y * blockDim.y ; by < ub_y; by+= gridDim.y * blockDim.y)
    for (bx = lb_x + blockIdx.x * blockDim.x ; bx < ub_x; bx+= gridDim.x * blockDim.x)  {
      ... 
      for (tz = bz + threadIdx.z ; bz < ub_z; tz+= blockDim.z)
        for (ty = by + threadIdx.y ; ty < ub_y; ty+= blockDim.y)
          for (tx = lb_x + threadIdx.x ; tx < ub_x; tx+= blockDim.x) {
            ....
          }
}

Ideally for most operations, you dont require tiling. Tiling is only needed when you want to utilize workgroup local memory for staging intermediate computations (or for efficient data access for the tile being computed). For now these would be for matmul, conv, pooling. For elementwise operations, you can

  1. linearize the iteration space
  2. Use a single if statement to make threads "outside" of the iteration space inactive.
niters = (ub_z - lb_z) * (ub_y - lb_y) * (ub_x - lb_x)
if (threadIdx.x + blockIdx.x * blockDim.x < niters) {
   .....
}

Further improvements could be done for reducing control overhead even for matmul, conv, etc. that do require tiling.

bz = lb_z + blockIdx.z * blockDim.z;
by = lb_y + blockIdx.y * blockDim.y;
bx = lb_x + blockIdx.x * blockDim.x;
tz = bz + threadIdx.z
ty = by + threadIdx.y
tx = bx + threadIdx.x
if (tz < ub_z && ty < ub_y && tx < ub_x) {

}

This will require making sure that the number of blocks launched is enough to execute the entire iteration space.

Activity

  1. MaheshRavishankar commented on Jun 8, 2020

    @MaheshRavishankar
    CollaboratorAuthor

    @antiagainst @hanhanW for visibility.

    I am looking at not making tiling required for all operations now.

  2. added
    codegen
    compiler/dialects
    and removed
    compiler/dialects
    on Jun 8, 2020
  3. MaheshRavishankar commented on Jun 29, 2020

    @MaheshRavishankar
    CollaboratorAuthor

    Commit 6073d91 resubmits PR #2134. Some outstanding issues using the reduced control flow for distributing loops within a workgourp seems to have a correctness issue in convolution/pooling and it only shows up in Resnet. Thinking it might be because of loop bound computation when lowering a tiled convolution op
    FYI : @nicolasvasilache

  4. added
    codegen/spirv
    hal/vulkan
    on Jul 20, 2020
  5. MaheshRavishankar commented on Oct 8, 2020

    @MaheshRavishankar
    CollaboratorAuthor

    This is blocked by some correctness issue that manifests on Resnet models. @hanhanW was able to create a smaller repro

    func @predict_ex_dispatch_4(%arg0: tensor<1x18x18x64xf32>) -> tensor<1x8x8x64xf32> {
      %cst = constant dense<0xFF800000> : tensor<f32>
      %0 = "mhlo.reduce_window"(%arg0, %cst) ( {
      ^bb0(%arg1: tensor<f32>, %arg2: tensor<f32>):  // no predecessors
        %1 = mhlo.maximum %arg1, %arg2 : tensor<f32>
        "mhlo.return"(%1) : (tensor<f32>) -> ()
      }) {window_dimensions = dense<[1, 3, 3, 1]> : tensor<4xi64>, window_strides = dense<[1, 2, 2, 1]> : tensor<4xi64>} : (tensor<1x18x18x64xf32>, tensor<f32>) -> tensor<1x8x8x64xf32>
      return %0 : tensor<1x8x8x64xf32>
    }
    
    $ iree-run-mlir -iree-hal-target-backends=vulkan-spirv -input-value="1x18x18x64xf32=0" ~/x.mlir
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

codegencodegen/spirvhal/vulkan

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions