Repository navigation
[Codegen] Add an option to enable fp8/fp4 software emulation for all GPU targets. - #23238
Conversation
878704e to
187e541
Compare
|
Can we make sure there's a big fat warning when this happens? Also, how do we handle MMA over unsupported types? |
They should already become intrinsics when you reach here. This is a very end step before LLVM conversion. Currently the pass is run right before the conversion. iree/compiler/src/iree/compiler/Codegen/LLVMGPU/Passes.cpp Lines 1060 to 1069 in 5aa6453 |
|
So we will still error out on any non-trivial inputs? Can we have a test that tries to run matmul with unsupported element types and use |
I have to try, but I don't expect error out on GPU side as long as the codegen path is generic enough. You never choose MMA attribute for such matmul, so it is just a linalg generic op. No matter it is vectorized or not, it becomes a seq of arith ops; the emulation handles both scalar and vector code. We do have CPU working for scaled matmul e2e; CPU does not support the type natively. It is already enabled on main branch since yesterday. Sorry, I misread the first comment. I'll try and report back tomorrow. But above is what I'd expect. |
If we can't find a matching intrinsic, the performance with scalar math is going to be an order of magnitude slower than expected (or more) -- we should emit a warning somewhere, and have a test that makes sure we get it on a matmul/conv dispatch through If someone uses the wrong fp types, it's almost always a user error, not something intentional. |
|
The main exception to "it'll be slow if we can't find an intrinsic" is f32 on RDNA, where there's no intrinsic because, paraphrasing, "it wouldn't be faster than just doing the FMAs and we don't want to waste an opcode" |
I think it is already happening on main branch I think. E.g., if you compile f32f32f32 matmul for gfx1100, you use vector<4xf32> in the lowering. There are no intrinsics: https://gist.github.com/hanhanW/2d893b240c5e9cf1aeed3a0b21d6d258 I think small float types are not different in this context. It is easy to provide the bad shape/types that the hardware does not have native support. |
|
I think there's a meaningful difference between "no intrrinsic" (happens sometimes) and "we're falling back to expensive software emulation between because we don't have this datatype" (heck, there's probably a parallel warning about f64 being lossy) |
|
The practical difference is that many people can get confused and use the wrong mlir without realizing they are using the unsupported set of fp8/fp4 types. We've had a few situations like this and should warn about it. As for f32 -> f32, or f64, if they are using wide types like this, most likely they don't know what they are doing anyway, so I wouldn't warn. |
I think this kind of things have been happening for a long time. We have been seeing it on CPU side as well, e.g., people don't specify the target cpu and generic is slow. E.g., people don't use data-tiling flag and complain about the performance. Users usually copy the commands either from docs or codebase. The best I can think of is guarding the warning under a |
@kuhar I'm going to emit performance warnings like #23259. I'll introduce a GPU version and emit warnings when the flag is on. We can discuss in a call if we need bandwidth. For now, I'm planning to enable it for O3, most of people would see it because they usually only care performance and use O3. CI does not widely use O3, so it is still clean for our standard build. |
@kuhar @krzysz00 may I prefetch if this sounds okay to you? I'd like to sort out the issues in advance. Otherwise, I'm working on something not related. Thanks for your time. |
|
We talked about this during the codegen sync. I like @bjacob's suggestion the most: have it off by default and add a verbose flag to enable emulation of unsupported types. Then in the hard error on unsupported types print out the flag name, so that users who want to opt into it can discover it easily. This also solves the issue of our own tests emitting too many warnings. |
Thanks for the summary. I think flipping between Side noteLater on, I can move the flag to GPUCodegenOptions after I complete the support: #23290 I discussed with Mahesh about CPU one and the overall direction looks good. We'll have the GPU support soon. It is independent to this PR, so this is mostly a note. |
0e281a3 to
1b5e5a9
Compare
Previously, using fp8 types on chips without hardware support would produce a compile error (e.g., f8E5M2 on gfx942 or f8E5M2FNUZ on gfx950). This change enables software emulation as a fallback, allowing any small float type to be used on any chip. Also marks arith.scaling_extf/truncf as legal since they have their own expansion patterns that run in later passes. AMDGPU support matrix: | Type | gfx908/90a | gfx942 | gfx950 | gfx11xx | gfx12xx | |--------------|------------|--------|--------|---------|---------| | f8E4M3FNUZ | emu | hw | emu | emu | emu | | f8E5M2FNUZ | emu | hw | emu | emu | emu | | f8E4M3FN | emu | emu | hw | emu | hw | | f8E5M2 | emu | emu | hw | emu | hw | | f4E2M1FN | emu | emu | hw | emu | hw | (hw = hardware instructions, emu = software emulation) The revision also fixes a bug that gfx950 does not have FNUZ hardware support. So `<=` is replaced with `<`. The GPU specific tests are refreshed; all the executable target attributes are removed from existing tests. Three of existing GPU tests are removed because we already have the same tests. Signed-off-by: hanhanW <[email protected]>
Signed-off-by: hanhanW <[email protected]>
1b5e5a9 to
a21c987
Compare
…GPU targets. (#23238) Previously, using fp8 types on chips without hardware support would produce a compile error (e.g., f8E5M2 on gfx942 or f8E5M2FNUZ on gfx950). This change enables software emulation as a fallback, allowing any small float type to be used on any chip. Also marks arith.scaling_extf/truncf as legal since they have their own expansion patterns that run in later passes. AMDGPU support matrix: | Type | gfx908/90a | gfx942 | gfx950 | gfx11xx | gfx12xx | |--------------|------------|--------|--------|---------|---------| | f8E4M3FNUZ | emu | hw | emu | emu | emu | | f8E5M2FNUZ | emu | hw | emu | emu | emu | | f8E4M3FN | emu | emu | hw | emu | hw | | f8E5M2 | emu | emu | hw | emu | hw | | f4E2M1FN | emu | emu | hw | emu | hw | (hw = hardware instructions, emu = software emulation) The revision also fixes a bug that gfx950 does not have FNUZ hardware support. So <= is replaced with <. The GPU specific tests are refreshed; all the executable target attributes are removed from existing tests. Three of existing GPU tests are removed because we already have the same tests. Also adds a flag to control software emulation. Signed-off-by: hanhanW <[email protected]>
krzysz00
left a comment
There was a problem hiding this comment.
Overall setup seems reasonable - can we make sure that scaling_extf/scaling_truncf get emulated too? I'm wanting to make sure there isn't phase ordering issues between your code and the upstream ArithExpandOps 's handling of those ops {and of fp4)
Signed-off-by: hanhanW <[email protected]>
Signed-off-by: hanhanW <[email protected]>
func.func @f4E2M1FN_scaled_to_f32() {
// [0, 1.0, 2.0, 0.5] : f4E2M1FN
%input = util.unfoldable_constant dense<[0, 2, 4, 1]> : tensor<4xi8>
%scale = util.unfoldable_constant dense<2.0> : tensor<4xf8E8M0FNU>
%init0 = tensor.empty() : tensor<4xf32>
%res = linalg.generic {indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>], iterator_types = ["parallel"]}
ins(%input, %scale : tensor<4xi8>, tensor<4xf8E8M0FNU>) outs(%init0 : tensor<4xf32>) {
^bb0(%in: i8, %s: f8E8M0FNU, %out: f32):
%0 = arith.trunci %in : i8 to i4
%1 = arith.bitcast %0 : i4 to f4E2M1FN
%2 = arith.scaling_extf %1, %s : f4E2M1FN, f8E8M0FNU to f32
linalg.yield %2 : f32
} -> tensor<4xf32>
check.expect_eq_const(%res, dense<[0.0, 2.0, 4.0, 1.0]> : tensor<4xf32>) : tensor<4xf32>
return
}
func.func @f32_to_f4E2M1FN_scaled() {
%input = util.unfoldable_constant dense<[0.0, 2.0, 4.0, 1.0]> : tensor<4xf32>
%scale = util.unfoldable_constant dense<2.0> : tensor<4xf8E8M0FNU>
%init0 = tensor.empty() : tensor<4xi8>
%res = linalg.generic {indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>], iterator_types = ["parallel"]}
ins(%input, %scale : tensor<4xf32>, tensor<4xf8E8M0FNU>) outs(%init0 : tensor<4xi8>) {
^bb0(%in: f32, %s: f8E8M0FNU, %out: i8):
%0 = arith.scaling_truncf %in, %s : f32, f8E8M0FNU to f4E2M1FN
%1 = arith.bitcast %0 : f4E2M1FN to i4
%2 = arith.extui %1 : i4 to i8
linalg.yield %2 : i8
} -> tensor<4xi8>
// [0.0, 1.0, 2.0, 0.5] : tensor<4xf4E2M1FN>
check.expect_eq_const(%res, dense<[0, 2, 4, 1]> : tensor<4xi8>) : tensor<4xi8>
return
}It is handled in |
|
@hanhanW The code you linked shows the exact phase ordering problem I'm worried about! That code populates the upstream emulation patterns for fp4 and f8E8M0FNU, not the ones you added, and can introduce extf and truncf operations. Therefore, those scaled ext/scaled trunc expansions need to be run before (or alongside) your patterns, and the two populate calls afterwards need to be removed to ensure consistency. |
|
(To give a concrete example, if I were passing f8E5M2 to those expansion tests and not an fp4, they wouldn't work, as far as I can tell) |
|
It seems to be pre-existing issue as it was added to late phase. I noticed something simliar when I made changes in CPU, and maybe we follow it: 6202dcd |
…GPU targets. (#23238) Previously, using fp8 types on chips without hardware support would produce a compile error (e.g., f8E5M2 on gfx942 or f8E5M2FNUZ on gfx950). This change adds a flag to enable software emulation as a fallback, allowing any small float type to be used on any chip. If the flag is not enabled, it emits the error message with the hint of adding the flag. On ConvertUnsupportedFloatArith pass side, it marks arith.scaling_extf/truncf as legal since they have their own expansion patterns that run in later passes. The revision also fixes a bug that gfx950 does not have FNUZ hardware support. So `<=` is replaced with `<`. The GPU specific tests are refreshed; all the executable target attributes are removed from existing tests. Three of existing GPU tests are removed because we already have the same tests. AMDGPU support matrix: | Type | gfx908/90a | gfx942 | gfx950 | gfx11xx | gfx12xx | |--------------|------------|--------|--------|---------|---------| | f8E4M3FNUZ | emu | hw | emu | emu | emu | | f8E5M2FNUZ | emu | hw | emu | emu | emu | | f8E4M3FN | emu | emu | hw | emu | hw | | f8E5M2 | emu | emu | hw | emu | hw | | f4E2M1FN | emu | emu | hw | emu | hw | (hw = hardware instructions, emu = software emulation) --------- Signed-off-by: hanhanW <[email protected]>
…GPU targets. (iree-org#23238) Previously, using fp8 types on chips without hardware support would produce a compile error (e.g., f8E5M2 on gfx942 or f8E5M2FNUZ on gfx950). This change adds a flag to enable software emulation as a fallback, allowing any small float type to be used on any chip. If the flag is not enabled, it emits the error message with the hint of adding the flag. On ConvertUnsupportedFloatArith pass side, it marks arith.scaling_extf/truncf as legal since they have their own expansion patterns that run in later passes. The revision also fixes a bug that gfx950 does not have FNUZ hardware support. So `<=` is replaced with `<`. The GPU specific tests are refreshed; all the executable target attributes are removed from existing tests. Three of existing GPU tests are removed because we already have the same tests. AMDGPU support matrix: | Type | gfx908/90a | gfx942 | gfx950 | gfx11xx | gfx12xx | |--------------|------------|--------|--------|---------|---------| | f8E4M3FNUZ | emu | hw | emu | emu | emu | | f8E5M2FNUZ | emu | hw | emu | emu | emu | | f8E4M3FN | emu | emu | hw | emu | hw | | f8E5M2 | emu | emu | hw | emu | hw | | f4E2M1FN | emu | emu | hw | emu | hw | (hw = hardware instructions, emu = software emulation) --------- Signed-off-by: hanhanW <[email protected]>
Previously, using fp8 types on chips without hardware support would produce a compile error (e.g., f8E5M2 on gfx942 or f8E5M2FNUZ on gfx950).
This change adds a flag to enable software emulation as a fallback, allowing any small float type to be used on any chip. If the flag is not enabled, it emits the error message with the hint of adding the flag.
On ConvertUnsupportedFloatArith pass side, it marks arith.scaling_extf/truncf as legal since they have their own expansion patterns that run in later passes. The revision also fixes a bug that gfx950 does not have FNUZ hardware support. So
<=is replaced with<.The GPU specific tests are refreshed; all the executable target attributes are removed from existing tests. Three of existing GPU tests are removed because we already have the same tests.
AMDGPU support matrix:
(hw = hardware instructions, emu = software emulation)