Skip to content

[Codegen] Add an option to enable fp8/fp4 software emulation for all GPU targets. - #23238

Merged
hanhanW merged 4 commits into
iree-org:mainfrom
hanhanW:users/hanhanW/amdgpu-fp-types
Feb 4, 2026
Merged

hanhanW merged 4 commits into
iree-org:mainfrom
hanhanW:users/hanhanW/amdgpu-fp-types

Conversation

@hanhanW

@hanhanW hanhanW commented Jan 22, 2026 •

Copy link
Copy Markdown
Contributor

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)

@kuhar

kuhar commented Jan 22, 2026

Copy link
Copy Markdown
Member

Can we make sure there's a big fat warning when this happens? Also, how do we handle MMA over unsupported types?

@hanhanW

hanhanW commented Jan 22, 2026

Copy link
Copy Markdown
Contributor Author

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.

if (forROCDL) {
// convert to ROCDL.
funcPassManager.addPass(createConvertUnsupportedFloatArithPass);
modulePassManager.addPass(createConvertToROCDLPass());
modulePassManager.addNestedPass<LLVM::LLVMFuncOp>(
createROCDLAnnotateKernelForTranslationPass());
} else {
// convert to NVVM.
modulePassManager.addPass(createConvertToNVVMPass());
}

@kuhar

kuhar commented Jan 22, 2026

Copy link
Copy Markdown
Member

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 --verify-diagnostics to document the behavior?

@hanhanW

hanhanW commented Jan 22, 2026 •

Copy link
Copy Markdown
Contributor Author

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 --verify-diagnostics to document the behavior?

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.

@kuhar

kuhar commented Jan 22, 2026 •

Copy link
Copy Markdown
Member

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.

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 // expected-warning@below.

If someone uses the wrong fp types, it's almost always a user error, not something intentional.

@krzysz00

Copy link
Copy Markdown
Contributor

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"

@hanhanW

hanhanW commented Jan 22, 2026

Copy link
Copy Markdown
Contributor Author

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.

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 // expected-warning@below.

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.

@kuhar

kuhar commented Jan 22, 2026

Copy link
Copy Markdown
Member

Maybe the solution is to keep your emulation as-is and warn around the kernel configuration time, @krzysz00 @hanhanW ?

@krzysz00

Copy link
Copy Markdown
Contributor

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)

@kuhar

kuhar commented Jan 22, 2026

Copy link
Copy Markdown
Member

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.

@hanhanW

hanhanW commented Jan 22, 2026 •

Copy link
Copy Markdown
Contributor Author

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 aggressively-warning flag. It emits warning only when the flag is on; we expect users to use the flag when they copy the commands. Thus, we still don't have warnings on default path; users can spot the warning because they just copy the commands from somewhere.

@hanhanW

hanhanW commented Jan 23, 2026

Copy link
Copy Markdown
Contributor Author

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 aggressively-warning flag. It emits warning only when the flag is on; we expect users to use the flag when they copy the commands. Thus, we still don't have warnings on default path; users can spot the warning because they just copy the commands from somewhere.

@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.

@hanhanW

hanhanW commented Feb 2, 2026

Copy link
Copy Markdown
Contributor Author

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 aggressively-warning flag. It emits warning only when the flag is on; we expect users to use the flag when they copy the commands. Thus, we still don't have warnings on default path; users can spot the warning because they just copy the commands from somewhere.

@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.

@kuhar

kuhar commented Feb 3, 2026

Copy link
Copy Markdown
Member

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.

@hanhanW

hanhanW commented Feb 3, 2026

Copy link
Copy Markdown
Contributor Author

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 crash + verbose message to enable it with a flag and inefficient performance + warning is an easy switch, so I don't have strong opinion on which one should be default. We can easily flip them based on our needs. I'll go ahead to create a flag to unblock this PR.


Side note

Later 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.

@hanhanW
hanhanW force-pushed the users/hanhanW/amdgpu-fp-types branch from 0e281a3 to 1b5e5a9 Compare February 4, 2026 00:19
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]>
@hanhanW
hanhanW force-pushed the users/hanhanW/amdgpu-fp-types branch from 1b5e5a9 to a21c987 Compare February 4, 2026 00:29
@hanhanW
hanhanW requested a review from krzysz00 February 4, 2026 00:31
@hanhanW hanhanW changed the title [Codegen] Enable fp8/fp4 software emulation for all GPU targets. [Codegen] Add an option to enable fp8/fp4 software emulation for all GPU targets. Feb 4, 2026
@hanhanW

hanhanW commented Feb 4, 2026

Copy link
Copy Markdown
Contributor Author

Okay, this is ready for review. @kuhar @krzysz00 can you take a look? Thanks.

hanhanW added a commit that referenced this pull request Feb 4, 2026
…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 krzysz00 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.

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)

@hanhanW
hanhanW enabled auto-merge (squash) February 4, 2026 18:30
@hanhanW

hanhanW commented Feb 4, 2026

Copy link
Copy Markdown
Contributor Author

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)

scaling_extf/scaling_truncf must be emulated. Otherwise, you won't pass fp4_f32_conversion.mlir tests.

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 ConvertToROCDLPass in GPU backends.

arith::populateExpandScalingExtTruncPatterns(fallbackSmallFloatPatterns);

@hanhanW
hanhanW merged commit f83732d into iree-org:main Feb 4, 2026
52 of 56 checks passed
@hanhanW
hanhanW deleted the users/hanhanW/amdgpu-fp-types branch February 4, 2026 19:13
@krzysz00

krzysz00 commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

@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.

@krzysz00

krzysz00 commented Feb 4, 2026

Copy link
Copy Markdown
Contributor

(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)

@hanhanW

hanhanW commented Feb 5, 2026

Copy link
Copy Markdown
Contributor Author

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

hanhanW added a commit that referenced this pull request Feb 6, 2026
…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]>
MaheshRavishankar pushed a commit to MaheshRavishankar/iree that referenced this pull request Feb 24, 2026
…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]>
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.

3 participants