Skip to content

Set vectorSize back to 4 for non-divisible by 4 dimensions - #11210

Merged
grypp merged 1 commit into
iree-org:mainfrom
grypp:vectorize_not_divisiblez
Nov 21, 2022
Merged

grypp merged 1 commit into
iree-org:mainfrom
grypp:vectorize_not_divisiblez

Conversation

@grypp

@grypp grypp commented Nov 18, 2022

Copy link
Copy Markdown
Contributor

For issue #10003, the WorkgroupSpecializationPass nicely implements combination codegen of fastpath and the slowpath. But the tile size is still set incorrectly, this causes load stores on fastpath are still not vectorized.

The example given in the PR has a tensor of size 102401. TileAndDistributeToWorkgroups sets the tile size to <tile_sizes = [[64]]>. However, it should be 256, because the inner loop is unrolled with 4, and the 4 loads/stores are vectorized. See the generated ir below after this pass.

scf.for %arg0 = blockIdx.x to 102401 step blockDim.x  {
...
	linalg.generic {indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>], iterator_types = ["parallel"]} ins(%6, %7 : tensor<?xf32>, tensor<?xf32>) outs(%8 : tensor<?xf32>) 
	attrs =  {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[64]]>} ...
}

This leds load/store are fastpath do not get vectorized, see vector<1xf32>.

if(fastpath) {
    %7 = affine.apply #map3(%threadIdx.x)[%blockIdx]
    %8 = vector.transfer_read %0[%7], %cst {in_bounds = [true]} : memref<102401xf32>, vector<1xf32>
    ...
} else {
	//slowpath
}

This assumes that fastpath+slowpath is implemented. Even if loop trip count is not divisable by 4, it still sets vectorSize to 4. This helps generating correct tile size, see tile_sizes = [[256]] in code below.

scf.for %arg0 = blockIdx.x to 102401 step blockDim.x  {
        %9 = linalg.generic {indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>], iterator_types = ["parallel"]} ins(%6, %7 : tensor<?xf32>, tensor<?xf32>) outs(%8 : tensor<?xf32>) 
	attrs =  {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[256]]>} ...
}

Setting tile size correctly helps to vectorize load/store, see vector<4xf32>.

 if(fastpath) {
    %7 = affine.apply #map3(%threadIdx.x)[%blockIdx]
    %8 = vector.transfer_read %0[%7], %cst {in_bounds = [true]} : memref<102401xf32>, vector<4xf32>
    ...
} else {
	//slowpath
}

For issue iree-org#10003, the `WorkgroupSpecializationPass` nicely implements combination codegen of fastpath and the slowpath. But the tile size is still set incorrectly, this causes load stores on fastpath are still not vectorized.

The example given in the PR has a tensor of size 102401. `TileAndDistributeToWorkgroups` sets the tile size to "<tile_sizes = [[64]]>". However, it should be 256, because the inner loop is unrolled with 4, and the 4 loads/stores are vectorized. See the generated ir below after this pass.

```
scf.for %arg0 = blockIdx.x to 102401 step blockDim.x  {
...
	linalg.generic {indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>], iterator_types = ["parallel"]} ins(%6, %7 : tensor<?xf32>, tensor<?xf32>) outs(%8 : tensor<?xf32>) attrs =  {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[64]]>} ...
}
```
This leds load/store are fastpath do not get vectorized, see `vector<1xf32>`.

```
if(fastpath) {
    %7 = affine.apply #map3(%threadIdx.x)[%blockIdx]
    %8 = vector.transfer_read %0[%7], %cst {in_bounds = [true]} : memref<102401xf32>, vector<1xf32>
    ...
} else {
	//slowpath
}
```

This assumes that fastpath+slowpath is implemented. Even if loop trip count is not divisable by 4, it still sets vectorSize to 4. This helps generating correct tile size, see `tile_sizes = [[256]]` in code below.

```
scf.for %arg0 = blockIdx.x to 102401 step blockDim.x  {
        %9 = linalg.generic {indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>], iterator_types = ["parallel"]} ins(%6, %7 : tensor<?xf32>, tensor<?xf32>) outs(%8 : tensor<?xf32>) attrs =  {lowering_config = #iree_codegen.lowering_config<tile_sizes = [[256]]>} ...
}
```
Setting tile size correctly helps to vectorize load/store, see `vector<4xf32>`.

```
 if(fastpath) {
    %7 = affine.apply #map3(%threadIdx.x)[%blockIdx]
    %8 = vector.transfer_read %0[%7], %cst {in_bounds = [true]} : memref<102401xf32>, vector<4xf32>
    ...
} else {
	//slowpath
}
```
@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

Just to clarify the PR message, TileAndDistributeToWorkgroups does not set the tile sizes. The tile sizes are set in KernelConfig.cpp. It might look that way if you do dump-ir-after-all but if you do dump-ir-before-all you can see that these are decided before TileAndDistributeToWorkgroups.

@grypp

grypp commented Nov 18, 2022

Copy link
Copy Markdown
Contributor Author

I fall victim to after-all :) I can see now, LLVMGPULowerExecutableTargetPass calls KernelConfig.cpp, and it's set in there. Thanks for pointing that out!
My fix is also in KernelConfig.cpp I am wondering what do you think about it.

@ThomasRaoux ThomasRaoux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @grypp!

@iree-github-actions-bot

Copy link
Copy Markdown
Contributor

Abbreviated Linux Benchmark Summary

@ commit 3e2c4f31477b8ac6b7181007251f34021f234449 (vs. base ed20752b8a46061e8df0396bed6ab6064c0040be)

No improved or regressed benchmarks 🏖️

Regressed Compilation Times 🚩

Benchmark Name Compilation Time (ms)
BertForMaskedLM [fp32,seqlen512] (TF) CPU-x86\_64-CascadeLake 8-thread,full-inference,default-flags 11433 (vs. 10834, 5.53%↑)
BertForMaskedLM [fp32,seqlen512] (TF) CPU-x86\_64-CascadeLake full-inference,default-flags 11433 (vs. 10834, 5.53%↑)
BertForMaskedLM [fp32,seqlen512] (TF) CPU-x86\_64-CascadeLake 4-thread,full-inference,default-flags 11433 (vs. 10834, 5.53%↑)

[Top 3 out of 4 results showed]

Improved Compilation Times 🎉

Benchmark Name Compilation Time (ms)
BertForMaskedLM [fp32,seqlen512] (TF) GPU-CUDA-SM\_80 full-inference,default-flags 3735 (vs. 5681, 34.25%↓)
PoseNet [fp32] (TFLite) CPU-x86\_64-CascadeLake 8-thread,full-inference,default-flags 11069 (vs. 13205, 16.18%↓)
PoseNet [fp32] (TFLite) CPU-x86\_64-CascadeLake full-inference,default-flags 11069 (vs. 13205, 16.18%↓)

[Top 3 out of 80 results showed]

For more information:

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

I fall victim to after-all :) I can see now, LLVMGPULowerExecutableTargetPass calls KernelConfig.cpp, and it's set in there. Thanks for pointing that out! My fix is also in KernelConfig.cpp I am wondering what do you think about it.

I have no opinions on it. It seems like the right place to me...

@ThomasRaoux

Copy link
Copy Markdown
Contributor

I fall victim to after-all :) I can see now, LLVMGPULowerExecutableTargetPass calls KernelConfig.cpp, and it's set in there. Thanks for pointing that out! My fix is also in KernelConfig.cpp I am wondering what do you think about it.

I have no opinions on it. It seems like the right place to me...

+1 this is the right solution.

@grypp
grypp merged commit 078d1de into iree-org:main Nov 21, 2022
@grypp
grypp deleted the vectorize_not_divisiblez branch November 21, 2022 08:10
@grypp

grypp commented Nov 21, 2022

Copy link
Copy Markdown
Contributor Author

No improved or regressed benchmarks

Isn't it interesting that enabling fastpath did not give any speedup? Maybe if the all of the ld/st queues are sufficiently full and coalesced properly, generating vector ld/st doesn't have much advantage and or the advantage is negligible.

But generating a vector load/store is really painful. For example:

  1. Increases the number of registers used. This might cause slowdown if there is no space in the register file.
  2. Coarse the work for a thread, iree might launch fewer thread blocks. This can cause workload imbalance among thread blocks, it is kind of "tail effect" [1].
  3. Data must be aligned. I don't know how iree always knows this and don't know what it does if it's not aligned.

Maybe we don't always really need the combination of fastpath + slowpath for a kernels that have enough load/store.
(my 2 cents )

[1] https://developer.nvidia.com/blog/cuda-pro-tip-minimize-the-tail-effect/

@ThomasRaoux

Copy link
Copy Markdown
Contributor

No improved or regressed benchmarks

Isn't it interesting that enabling fastpath did not give any speedup? Maybe if the all of the ld/st queues are sufficiently full and coalesced properly, generating vector ld/st doesn't have much advantage and or the advantage is negligible.

But generating a vector load/store is really painful. For example:

  1. Increases the number of registers used. This might cause slowdown if there is no space in the register file.
  2. Coarse the work for a thread, iree might launch fewer thread blocks. This can cause workload imbalance among thread blocks, it is kind of "tail effect" [1].
  3. Data must be aligned. I don't know how iree always knows this and don't know what it does if it's not aligned.

Maybe we don't always really need the combination of fastpath + slowpath for a kernels that have enough load/store. (my 2 cents )

[1] https://developer.nvidia.com/blog/cuda-pro-tip-minimize-the-tail-effect/

I wasn’t expecting any speedup as we have only fee benchmarks and unaligned elementwise are fairly rare. The goal here is really the generalize the compiler and avoid performance cliff. Not being able to vectorize is usually bad and generates bad code. If we really want to prove that this improved performance we would need to try on some micro-benchmarks. This could he an interesting exercise but may not be super high priority for the moment.

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.

4 participants