Repository navigation
Add TosaToStandard to cpu/gpu target pipeline from ApplyScale lowering - #5288
Conversation
Lowering tosa.apply_scale is performed at the end of compilation to support custom cpu lowerings. The default implementation perform all arithmetic with 64-bit operators. Rescale tests that depend on this behavior are included.
| // TOSA rescale will be lowered to Standard right before LLVM codegen. Future | ||
| // lowerings should using specific CPU operations to avoid requiring i64 | ||
| // values. | ||
| tosa::populateTosaRescaleToStandardConversionPatterns(&patterns); |
There was a problem hiding this comment.
Do we have a conversion pass that runs before both "LinalgToLLVM" and "LinalgToSPIRV"? Neither of these files looks like the right place for a "TosaToStandard" conversion to me. This would also run during our conversion from different frontend dialects (e.g. HLO)... wouldn't we rather push frontend conversions up a level?
There was a problem hiding this comment.
So this is specifically for a TOSA operation that occurs within Linalg bodies. We need to perform this at the per-backend in level as future versions should support specific CPU intrinsics. Initially I performed this part of the lowering during the TosaToLinalg conversion, however that wouldn't handle separate lowerings per-backend.
There was a problem hiding this comment.
Can you post some IR snippets around this conversion flow?
There was a problem hiding this comment.
Added more thorough comment with explanation why it works.
There was a problem hiding this comment.
That's not quite what I had in mind...
I want to discuss why these patterns exist here, on this PR, with context for why TOSA ops appear inside linalg bodies. Comments in the code are nice, but that comment is too specific (and would probably need to be mirrored between both codegen files).
For example, I'd like to see the -print-ir-after-all before/after adding these patterns during conversion, possibly edited to just include the relevant ops. It's still not clear to me what is happening with these dialects. I'm less concerned with the specific implementation of a particular bundle of arithmetic and more with how the IR is being modified.
There was a problem hiding this comment.
The most concise explanation I can provide is the ApplyScale, unlike other TOSA Ops, is a utility operation for supporting lowering. TOSA multiply, round, shift behavior is not expressible in StandardOps without bit-extending to i64 types.
Specifically this Op is constructed during the TOSA to Linalg lowerings and is placed without linalg.generic bodies. It is not expanded to StandardOps at that point so that different targets can support custom intrinsics that are not expressible our StandardOps/LLVM dialects.
As for specifying the ir-after-all, the difference is that a single tosa.apply_scale(...) op is expanded to the pseudo-IR shown.
There was a problem hiding this comment.
I'm still not quite following, but this is helping. Thanks for your patience. So LinalgToLLVM/ConvertToLLVM.cpp and LinalgToSPIRV/ConvertToSPIRVPass.cpp will diverge? I think the comments should speak to that more directly.
I don't think the "This can be performed using i64 types as" comment in LinalgToLLVM/ConvertToLLVM.cpp is useful as-is. It's too specific when surrounded by other lowerings that are just "populateVectorToSCFConversionPatterns" (which itself probably does stuff at least as complicated as this :P) and could be replaced with something like
// Use the default lowering for TOSA's ApplyScale operator.
// <_brief_ 1-2 sentence description of how bit extending factors into this?>
// TODO(name): Add lowering that uses custom intrinsics specific to LLVM/SPIRV.
There was a problem hiding this comment.
This looks weird to me too. TOSA dialect should be higher level IR to me. If something is missing in Standard/Math/SomeQuant dialect, we should add such support. Leaving a TOSA op until the latest stage looks weird to me, and I'm afraid if it will cause more maintenance.
There was a problem hiding this comment.
@ScottTodd updated following your preferences mapping to silvas and antiagainst as each advised on their respective specializations.
@hanhanW Silvas and Antiagainst specifically advised in favor of lowering to target specific behavior for ApplyScale, noting that long term a quantization specific dialect should exists. However with only a single use-case, we cannot demonstrate a sufficient need for a new dialect in MLIR. We expect this to change in the future, however it is not expected to happen anytime soon.
| // Use the default 64-bit lowering for TOSA's ApplyScale operator: | ||
| // This lowering widens integer types to 64-bit an performs the non-fused | ||
| // operations, specifically multiply, add, and shift. Bit-widening | ||
| // is used to guarantee higher-order bits are not truncated during the | ||
| // multiply or add. | ||
| // | ||
| // TODO(antiagainst): Use a lowering that uses specific SPIRV intrinsics. | ||
| tosa::populateTosaRescaleToStandardConversionPatterns(&patterns); |
There was a problem hiding this comment.
Does this lower to 64 bit floats in spirv then? shaderFloat64 is optional (https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/VkPhysicalDeviceFeatures.html) so additional handling is/will be needed.
There was a problem hiding this comment.
This depends on target_enviroment. If the triple and target_enviroment is set correctly, and shaderFloat64 is available. I think it will work. If not, it will use other types (maybe f32) instead.
There was a problem hiding this comment.
The LLVM compilations for SPIRV can perform simulated i64 operations by decomposing to lower bitwidths. Mul.mlir is dependent on this behavior working.
| // is used to guarantee higher-order bits are not truncated during the | ||
| // multiply or add. | ||
| // | ||
| // TODO(silvasean): Use a lowering that uses specific ARM/X86 intrinsics. |
There was a problem hiding this comment.
Probably best to TODO(bjacob) :)
* 0132538 Merge google -> main #5404 * 53e7279 Limit fusion of linalg.generic/indexed_generic ops to avoid redundant computat.. * 516d1ac Merge branch 'main' into google-to-main * 9630450 Make PrePartitioningConversionPass not recurse into Linalg (#5358) * 29610ce Merge google -> main (#5388) * 692c586 Merge branch 'main' into google-to-main * 9b159c1 [CUDA Codegen] Add support for promotion to workgroup memory. (#5386) * 11b3bfe MaterializeCPULaunch configurations before bufferization (#5362) * ee0ddf4 Legalize ui64 to i32 in Flow type converter (#5384) * 87346a2 Handle tensor.cast bufferization in LinalgBufferizePass (#5369) * 2922e11 Removes tied transient buffer assignment propagation. (#5365) * 5b952e4 Making flow.tensor.reshape a tied-op. (#5368) * 7048463 Fixing ASAN-instrumented dylib library loading. (#5382) * 2f3ad70 Adding some basic flow.tensor.reshape folders. (#5366) * 357fa58 Switch simple_embeddding to test on DyLib + Vulkan (#5374) * cb7d144 Turn on operand fusion flag for benchmarks of Linalg on tensors (#5380) * f2d74fd Disable fusion of named ops with elementwise operations by default. (#5363) * 3fc84c3 Add support for returning Python tuples (#5347) * 8fb65cb Remove handling of shape dialect in flow->hal stream conversion. (#5360) * 68c27a0 Add logging for Android (#5232) * 56307e1 Merge pull request #5328 from google/benvanik-constant-io * d3eb8e4 Merge pull request #5357 from google/benvanik-host-visible * 4f86152 Materialize interfaces based on usage for flow.dispatch.workgroups. * f41b25d Splitting -iree-hal-materialize-interfaces into old/new paths. This will insul.. * 62a3dfd Move constant pool ops/hal.constant.subspan into streams. This dramatically re.. * 79dc138 [spirv] NFC: use upstream push constant utility functions (#5352) * 38daefb Revert "Disable running BERT on Vulkan (see Issue #5268) (#5269)" * 0995956 Revert "[vulkan] Disable ResNet50 test failing on NVIDIA GPUs (#5332)" * 0eeb3f8 Removing hack that forced all Vulkan buffers to be host-visible. After removin.. * 8abc33b Tracy fixes for GPU name allocation and allocation callstack mode. (#5356) * 55f76d3 Fix submodule check failure (#5355) * 830b547 [CodeGen] Copy constant at the proper place during bufferization (#5343) * 59ad168 Drop absl flat_hash_set and some unused includes. (#5346) * de2aabc Control TF MLIR print options via flags (#5336) * 2e2dfdd Merge google -> main (#5345) * ef9b787 Merge branch 'main' into google-to-main * 8b70733 Remove extra comma from context_wrapper.cc * 8cc6256 Fix linalg-on-tensors smoketests (#5341) * a578d6a Add TosaToStandard to cpu/gpu target pipeline from ApplyScale lowering (#5288) * 0c92b88 Add mixed static-dynamic arguments interface for flow.dispatch.tensor.{load|st.. * 9890722 Update Java Bindings To Build In Experimental (#5226) * 734ec24 Add smoketest_linalg_on_tensors to LLVM and Vulkan-SPIRV targets. (#5324) * d8bcb78 Drop absl logging and flat_hash_map. (#5334) * 5c63668 [vulkan] Disable ResNet50 test failing on NVIDIA GPUs (#5332) * 55d3715 Fix vulkan_device struct manipulation issue with std::vector. (#5333) * 47d00c6 Fix benchmarking MobileNetV2 on S20 GPU in Linalg on tensor path. (#5331) * ccae793 Fix bugs in ExtractReduceWindowOpPaddingAttributes. (#5329) * 652e328 Don't inline block arguments that can be written (#5247) * 86e7d29 [CodeGen] Enable fusion with consumer elementwise ops (#5282) * 44ad774 Integrate MLIR-EmitC at iml130/mlir-emitc@3c265bf (#5326) * 0141857 Added pad test, update LegalizeInputTypes to allow linalg.yield (#5305) * 5a3653f Add mobile-bert f16 version to Mako benchmark. (#5204) * f596cdb Add a target_env_conversion test for Linalg on tensors. (#5309) * f1aa6f4 Print out the status when iree-check-module fails (#5323) * 40d7589 Check for VMLA and Vulkan components for simple_embedding_test. (#5318) COPYBARA_INTEGRATE_REVIEW=#5407 from rsuderman:main-to-google c9c3ea7 PiperOrigin-RevId: 368121520
* 0132538 Merge google -> main #5404 * 53e7279 Limit fusion of linalg.generic/indexed_generic ops to avoid redundant computat.. * 516d1ac Merge branch 'main' into google-to-main * 9630450 Make PrePartitioningConversionPass not recurse into Linalg (#5358) * 29610ce Merge google -> main (#5388) * 692c586 Merge branch 'main' into google-to-main * 9b159c1 [CUDA Codegen] Add support for promotion to workgroup memory. (#5386) * 11b3bfe MaterializeCPULaunch configurations before bufferization (#5362) * ee0ddf4 Legalize ui64 to i32 in Flow type converter (#5384) * 87346a2 Handle tensor.cast bufferization in LinalgBufferizePass (#5369) * 2922e11 Removes tied transient buffer assignment propagation. (#5365) * 5b952e4 Making flow.tensor.reshape a tied-op. (#5368) * 7048463 Fixing ASAN-instrumented dylib library loading. (#5382) * 2f3ad70 Adding some basic flow.tensor.reshape folders. (#5366) * 357fa58 Switch simple_embeddding to test on DyLib + Vulkan (#5374) * cb7d144 Turn on operand fusion flag for benchmarks of Linalg on tensors (#5380) * f2d74fd Disable fusion of named ops with elementwise operations by default. (#5363) * 3fc84c3 Add support for returning Python tuples (#5347) * 8fb65cb Remove handling of shape dialect in flow->hal stream conversion. (#5360) * 68c27a0 Add logging for Android (#5232) * 56307e1 Merge pull request #5328 from google/benvanik-constant-io * d3eb8e4 Merge pull request #5357 from google/benvanik-host-visible * 4f86152 Materialize interfaces based on usage for flow.dispatch.workgroups. * f41b25d Splitting -iree-hal-materialize-interfaces into old/new paths. This will insul.. * 62a3dfd Move constant pool ops/hal.constant.subspan into streams. This dramatically re.. * 79dc138 [spirv] NFC: use upstream push constant utility functions (#5352) * 38daefb Revert "Disable running BERT on Vulkan (see Issue #5268) (#5269)" * 0995956 Revert "[vulkan] Disable ResNet50 test failing on NVIDIA GPUs (#5332)" * 0eeb3f8 Removing hack that forced all Vulkan buffers to be host-visible. After removin.. * 8abc33b Tracy fixes for GPU name allocation and allocation callstack mode. (#5356) * 55f76d3 Fix submodule check failure (#5355) * 830b547 [CodeGen] Copy constant at the proper place during bufferization (#5343) * 59ad168 Drop absl flat_hash_set and some unused includes. (#5346) * de2aabc Control TF MLIR print options via flags (#5336) * 2e2dfdd Merge google -> main (#5345) * ef9b787 Merge branch 'main' into google-to-main * 8b70733 Remove extra comma from context_wrapper.cc * 8cc6256 Fix linalg-on-tensors smoketests (#5341) * a578d6a Add TosaToStandard to cpu/gpu target pipeline from ApplyScale lowering (#5288) * 0c92b88 Add mixed static-dynamic arguments interface for flow.dispatch.tensor.{load|st.. * 9890722 Update Java Bindings To Build In Experimental (#5226) * 734ec24 Add smoketest_linalg_on_tensors to LLVM and Vulkan-SPIRV targets. (#5324) * d8bcb78 Drop absl logging and flat_hash_map. (#5334) * 5c63668 [vulkan] Disable ResNet50 test failing on NVIDIA GPUs (#5332) * 55d3715 Fix vulkan_device struct manipulation issue with std::vector. (#5333) * 47d00c6 Fix benchmarking MobileNetV2 on S20 GPU in Linalg on tensor path. (#5331) * ccae793 Fix bugs in ExtractReduceWindowOpPaddingAttributes. (#5329) * 652e328 Don't inline block arguments that can be written (#5247) * 86e7d29 [CodeGen] Enable fusion with consumer elementwise ops (#5282) * 44ad774 Integrate MLIR-EmitC at iml130/mlir-emitc@3c265bf (#5326) * 0141857 Added pad test, update LegalizeInputTypes to allow linalg.yield (#5305) * 5a3653f Add mobile-bert f16 version to Mako benchmark. (#5204) * f596cdb Add a target_env_conversion test for Linalg on tensors. (#5309) * f1aa6f4 Print out the status when iree-check-module fails (#5323) * 40d7589 Check for VMLA and Vulkan components for simple_embedding_test. (#5318) PiperOrigin-RevId: 368121520
Lowering tosa.apply_scale is performed at the end of compilation to
support custom cpu lowerings. The default implementation perform all
arithmetic with 64-bit operators.
Rescale tests that depend on this behavior are included and Mul was
updated for its shift behavior.