iree: [ROCM][gfx942] shared memory limit exceeded on elemwise broadcast (bf16) (flux-dev)
What happened?
Compiling a new SD3-like model for gfx942 is introducing a few new things we may need to massage through; the shapes don’t seem too out of the ordinary but the graph is in bf16. Given bf16, I’m not sure what to expect w.r.t support in codegen – I am running into a few compilation failures, which I have tracked down to a few dispatches:
On an elementwise broadcast (24x4608x64x2xbf16) dispatch we run into a shared memory issue:
iree-compile configured_compiled_flux_sampler_run_forward_async_dispatch_36.mlir --iree-input-type=torch --iree-vm-bytecode-module-output-format=flatbuffer-binary --iree-hal-target-backends=rocm --mlir-print-debuginfo --mlir-print-op-on-diagnostic=false --iree-hal-target-backends=rocm --iree-rocm-target-chip=gfx942 --iree-vm-bytecode-module-output-format=flatbuffer-binary --iree-flow-enable-aggressive-fusion --iree-opt-aggressively-propagate-transposes=true --iree-codegen-llvmgpu-use-vector-distribution=true --iree-opt-outer-dim-concat=true --iree-opt-data-tiling=false --iree-codegen-gpu-native-math-precision=true --iree-vm-target-truncate-unsupported-floats --iree-codegen-llvmgpu-enable-transform-dialect-jit=false --iree-global-opt-propagate-transposes=true --iree-opt-const-eval=false --iree-llvmgpu-enable-prefetch=true --iree-execution-model=async-external --iree-preprocessing-pass-pipeline='builtin.module(iree-preprocessing-transpose-convolution-pipeline, util.func(iree-preprocessing-pad-to-intrinsics))' --iree-codegen-transform-dialect-library=attention_and_matmul_spec_mfma.mlir --mlir-print-debuginfo ./sampler_dps/compiled_flux_sampler_run_forward$async_dispatch_36.mlir:9:6: error: 'func.func' op uses 56623104 bytes of shared memory; exceeded the limit of 65536 bytes
func.func @run_forward$async_dispatch_36_elementwise_broadcast_24x4608x64x2_f32xf32xf32xf32xbf16() {
^
./flux_sampler_dps/compiled_flux_sampler_run_forward$async_dispatch_36.mlir:2:2: error: failed to run translation of source executable to target executable for backend #hal.executable.target<"rocm", "rocm-hsaco-fb", {iree.gpu.target = #iree_gpu.target<arch = "gfx942", features = "", wgp = <compute = fp64|fp32|fp16|int64|int32|int16|int8, storage = b64|b32|b16|b8, subgroup = shuffle|arithmetic, dot = dp4xi8toi32, mma = [<MFMA_F16_16x16x16_F32>, <MFMA_F16_32x32x8_F32>, <MFMA_I8_16x16x32_I32>, <MFMA_I8_32x32x16_I32>], subgroup_size_choices = [64], max_workgroup_sizes = [1024, 1024, 1024], max_thread_count_per_workgroup = 1024, max_workgroup_memory_bytes = 65536>>, ukernels = "none"}>
hal.executable.variant public @rocm_hsaco_fb target(<"rocm", "rocm-hsaco-fb", {iree.gpu.target = #iree_gpu.target<arch = "gfx942", features = "", wgp = <compute = fp64|fp32|fp16|int64|int32|int16|int8, storage = b64|b32|b16|b8, subgroup = shuffle|arithmetic, dot = dp4xi8toi32, mma = [<MFMA_F16_16x16x16_F32>, <MFMA_F16_32x32x8_F32>, <MFMA_I8_16x16x32_I32>, <MFMA_I8_32x32x16_I32>], subgroup_size_choices = [64], max_workgroup_sizes = [1024, 1024, 1024], max_thread_count_per_workgroup = 1024, max_workgroup_memory_bytes = 65536>>, ukernels = "none"}>) {
^
failed to translate executables
And the soon following attention dispatch fails without useful diagnostics:
iree-compile configured_compiled_flux_sampler_run_forward_async_dispatch_38.mlir --iree-input-type=torch --iree-vm-bytecode-module-output-format=flatbuffer-binary --iree-hal-target-backends=rocm --mlir-print-debuginfo --mlir-print-op-on-diagnostic=false --iree-hal-target-backends=rocm --iree-rocm-target-chip=gfx942 --iree-vm-bytecode-module-output-format=flatbuffer-binary --iree-flow-enable-aggressive-fusion --iree-opt-aggressively-propagate-transposes=true --iree-codegen-llvmgpu-use-vector-distribution=true --iree-opt-outer-dim-concat=true --iree-opt-data-tiling=false --iree-codegen-gpu-native-math-precision=true --iree-vm-target-truncate-unsupported-floats --iree-codegen-llvmgpu-enable-transform-dialect-jit=false --iree-global-opt-propagate-transposes=true --iree-opt-const-eval=false --iree-llvmgpu-enable-prefetch=true --iree-execution-model=async-external --iree-preprocessing-pass-pipeline='builtin.module(iree-preprocessing-transpose-convolution-pipeline, util.func(iree-preprocessing-pad-to-intrinsics))' --iree-codegen-transform-dialect-library=attention_and_matmul_spec_mfma.mlir --mlir-print-debuginfo
./flux_sampler_dps/compiled_flux_sampler_run_forward$async_dispatch_38.mlir:2:2: error: failed to run translation of source executable to target executable for backend #hal.executable.target<"rocm", "rocm-hsaco-fb", {iree.gpu.target = #iree_gpu.target<arch = "gfx942", features = "", wgp = <compute = fp64|fp32|fp16|int64|int32|int16|int8, storage = b64|b32|b16|b8, subgroup = shuffle|arithmetic, dot = dp4xi8toi32, mma = [<MFMA_F16_16x16x16_F32>, <MFMA_F16_32x32x8_F32>, <MFMA_I8_16x16x32_I32>, <MFMA_I8_32x32x16_I32>], subgroup_size_choices = [64], max_workgroup_sizes = [1024, 1024, 1024], max_thread_count_per_workgroup = 1024, max_workgroup_memory_bytes = 65536>>, ukernels = "none"}>
hal.executable.variant public @rocm_hsaco_fb target(<"rocm", "rocm-hsaco-fb", {iree.gpu.target = #iree_gpu.target<arch = "gfx942", features = "", wgp = <compute = fp64|fp32|fp16|int64|int32|int16|int8, storage = b64|b32|b16|b8, subgroup = shuffle|arithmetic, dot = dp4xi8toi32, mma = [<MFMA_F16_16x16x16_F32>, <MFMA_F16_32x32x8_F32>, <MFMA_I8_16x16x32_I32>, <MFMA_I8_32x32x16_I32>], subgroup_size_choices = [64], max_workgroup_sizes = [1024, 1024, 1024], max_thread_count_per_workgroup = 1024, max_workgroup_memory_bytes = 65536>>, ukernels = "none"}>) {
^
failed to translate executables
Steps to reproduce your issue
Download files: attention_and_matmul_spec_mfma.mlir (shared/sdxl_quantized) attention_and_matmul_spec.mlir (main) configured_compiled_flux_sampler_run_forward_async_dispatch_36.mlir configured_compiled_flux_sampler_run_forward_async_dispatch_38.mlir full model if interested – flux_dev_bs1_512_1024x1024_bf16_sampler.mlir
compile command (shared/sdxl_quantized):
iree-compile --iree-input-type=torch --iree-vm-bytecode-module-output-format=flatbuffer-binary --iree-hal-target-backends=rocm --mlir-print-debuginfo --mlir-print-op-on-diagnostic=false --iree-hal-target-backends=rocm --iree-rocm-target-chip=gfx942 --iree-vm-bytecode-module-output-format=flatbuffer-binary --iree-flow-enable-aggressive-fusion --iree-opt-aggressively-propagate-transposes=true --iree-codegen-llvmgpu-use-vector-distribution=true --iree-opt-outer-dim-concat=true --iree-opt-data-tiling=false --iree-codegen-gpu-native-math-precision=true --iree-vm-target-truncate-unsupported-floats --iree-codegen-llvmgpu-enable-transform-dialect-jit=false --iree-global-opt-propagate-transposes=true --iree-opt-const-eval=false --iree-llvmgpu-enable-prefetch=true --iree-execution-model=async-external --iree-preprocessing-pass-pipeline='builtin.module(iree-preprocessing-transpose-convolution-pipeline, util.func(iree-preprocessing-pad-to-intrinsics))' --iree-codegen-transform-dialect-library=attention_and_matmul_spec_mfma.mlir <input_mlir> -o out.vmfb
compile command (main):
iree-compile --iree-input-type=torch --iree-vm-bytecode-module-output-format=flatbuffer-binary --iree-hal-target-backends=rocm --mlir-print-debuginfo --mlir-print-op-on-diagnostic=false --iree-hal-target-backends=rocm --iree-rocm-target-chip=gfx942 --iree-vm-bytecode-module-output-format=flatbuffer-binary --iree-flow-enable-aggressive-fusion --iree-opt-aggressively-propagate-transposes=true --iree-codegen-llvmgpu-use-vector-distribution=true --iree-opt-outer-dim-concat=true --iree-opt-data-tiling=false --iree-codegen-gpu-native-math-precision=true --iree-vm-target-truncate-unsupported-floats --iree-codegen-llvmgpu-enable-transform-dialect-jit=false --iree-global-opt-propagate-transposes=true --iree-opt-const-eval=false --iree-llvmgpu-enable-prefetch=true --iree-execution-model=async-external --iree-preprocessing-pass-pipeline='builtin.module(iree-preprocessing-transpose-convolution-pipeline, iree-preprocessing-pad-to-intrinsics)' --iree-codegen-transform-dialect-library=attention_and_matmul_spec.mlir <input_mlir> -o out.vmfb
What component(s) does this issue relate to?
Compiler
Version information
Tested on the following commits: tip of shared/sdxl_quantized tip of main
Additional context
shared memory issue seems unrelated to any changes on shared/sdxl_quantized or TD spec compatibility, as both configurations (iree/main vs. iree/shared/sdxl_quantized and their respective td specs) give the same error.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 25 (9 by maintainers)
https://github.com/iree-org/iree/pull/18332 should fix this. I tried the small repro from here and I get
which seems right
In the meantime, I have found a workaround in the torch model code – simply removing the cast to float in these lines: https://github.com/black-forest-labs/flux/blob/main/src/flux/math.py#L26-27 gives identical results in pytorch and prevents us having to deal with this troublesome pattern. That at least makes this less blocking, but naturally supporting cases like this would be better in the long run.
@IanWood1 here is what I believe is responsible for this: https://github.com/black-forest-labs/flux/blob/main/src/flux/math.py#L6
As suspected, I was able to minimize further by restricting to the
apply_ropefunction in the linkedflux/math.pyfile: https://gist.github.com/monorimet/d39a0cfa7d9307e91a61da209452e2f6/raw/dc1b12ece62b75cd01db7b99615a95cc2019b6a0/flux_apply_rope_torch.mlir@monorimet adding tracing changes the way fusion works (at least today, maybe we can make that not be the case). Sorry I wasnt online over the weekend and looks like you tried a bunch of different things. Reading through, the most important point is
kind of IR (not even thinking of dispatches, but just lowering form IR) for the backend is complicated to generate code for. (Side note: We should fix dispatch region formation to not create such dispatches). but instead of the above we should lower to
The slicing of the inner most dimension that are made into separate tensors to feed into another operation is a bad lowering sequence. The
iree-opt-outer-dim-concatis bit of a red-herring cause that is meant for cases where concatenation is happening on the inner most dim, but this is not what is happening here (it has some similar artifacts, but its not a good match).Sorry you tried a whole bunch of different things and ran into various issues, but the easiest fix for me is to fix the lowering to Linalg. I think recovering from the lowering in the compiler is much harder.
Gotcha. Without trace tensors, the failing dispatch index moves to 18 – so here’s that IR: https://sharkpublic.blob.core.windows.net/sharkpublic/ean/flux-dev/compiled_attn_main_async_dispatch_18.mlir
My compile command:
and here’s the IR dump:
https://sharkpublic.blob.core.windows.net/sharkpublic/ean/flux-dev/dispatch_18_ir_dump_fail.txt
Cc @IanWood1 . I think he has seen and fixed a similar issue previously. I don’t know how he did it though. But extracting a long the innermost dimension this way is an anitpattern IMO. Maybe it is something like a complex number being handled as two separate floats?