You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Compiling for llvm-cpu without targeting a specific CPU is a bad experience #18561
I've seen multiple people falling down this hole: they run iree-compile on their model, targeting CPU. Then they get performance that is 10x-100x off of any reasonable expectation. Then they either go away silently or report back about poor experiences (not always reporting flags and such).
There are good reasons why a compiler like IREE shouldn't make assumptions about what the CPU target is, but on the other hand, it will almost always produce a a grossly subpar experience to not specify a target CPU, since the generic target (at least on X86) lacks so many features as to be basically useless for any high performance numerics.
I've even fallen down this hole recently and had to go remember the incantation to select a specific CPU. In the case I was working on (an f16 CPU LLM), performance was 100x different between not specifying a target CPU and specifying "host". We need to guide people better than this.
Proposal
As mentioned, there are good reasons for a compiler to not make too many assumptions without being told what to do. But I think we can/should actively warn, possibly with a link to the documentation site when the compiler is invoked without the user specifying a target CPU. Whatever the warning is should be very explicit that the user should pass --iree-llvmcpu-target-cpu=host to target the precise CPU they are running on. We should possibly also accept "generic" or something like that for if the user really wants to target the default and not get the warning. I basically want to guard against the case where the user has not specified anything and the compiler just silently generates 100x too slow of code. In almost all cases, it will be better for the user to say something and we should guide them on a proper choice.
The suggested proposal SGTM. I might even want to default to having LLVM use the current host for the target CPU and available features (if those are different), then have users explicitly pass "generic" for the lowest common denominator.
We could apply similar logic to the GPU backends - try to detect devices on the system (shell out to vulkaninfo / rocm-smi / nvidia-smi?) and default to what is available, but still support cross compilation with explicit device info and a "generic" target where possible.
Yuck - that is not a cheap thing to do and has a high risk of flakes - I am still not sure why proper documentation is insufficient? You must specify your target device (--iree-hal-target-device=) when compiling so also specifying a "use my hardware info" is fine. Just change the documentation to include both flags and then a user has a choice and knows what to do if they want to change things. Anything automatic is going to have issues. Users aren't coming in to iree-compile command line invocations blind - if all the docs specify the flag and they choose not to copy/paste it that's on them.
(if there's such a big concern about documentation not fixing this issue then I'd be ok with making compilation fail if the user doesn't specify an arch for a backend - whether a particular arg, generic, native, etc - but guessing is bad)
I'm seeing a proliferation of flags (mainly in rocm usage, but also cpu) and the documentation can't keep up. I want more of that to be captured somewhere - docs, samples, the compiler itself, etc.
That's insanity - besides the debug flags (dumping statistics/etc) if any of those are required that's a bug. I think Mahesh has said it before: a feature is not done until it's on by default and if all of those flags are needed to make the model compile or perform then the engineering was never completed. The only two flags required there should be --iree-hal-target-device=hip (that's using the old deprecated flag) and --iree-hip-target= (which could be native if we wanted to do what clang does and invoke amdgpu-arch if it's present).
I'm fine making --iree-llvmcpu-target-cpu=<something> required. Given the proliferation of things out there, I think that the way to get there may be to first do what I am suggesting: make it issue a warning if not specified and incorrectly/implicitly defaulting to a generic CPU (with a note that this flag will soon be required).
Agreed on all of the other points. Need to burn down all of the other flags. I'm just starting with this one.
That could work, yeah. When I say "test my changes" here, I'm still just referring to local development "testing", not automated CI testing - that would be a nice bonus.
Pushed an initial attempt at reworking how the target init is handled: #18587 . I could pass that off to someone else and context switch to other tasks 🤔
Sorry, I had not kept up with the discussion here, was heads down in GPU data tiling.
Here are the difficulties that I know of:
Different concepts are more or less relevant on different CPU architectures:
On x86, people want to talk in terms of "CPU" (meaning microarchitecture) such as znver4 or cascadelake. People do not typically want to talk in terms of CPU features on x86 because that is very cumbersome. For example, just enabling the baseline AVX-512 feature set on x86 is a combination of 5 features, each with long names; a typical compilation relies on > 10 CPU features.
On RISC-V, people want to talk in terms of CPU features, and there are many, but they are not too cumbersome thanks to very short names, e.g. +z,+a,+m. The "CPU" string is not much used on RISC-V, according to RISC-V folks I asked back then, due to the very modular nature of the architecture.
On Arm, people typically specify baseline Arm architecture version plus a few CPU features, e.g. armv8.2-a+i8mm. The CPU names are also not much used on Arm; when targeting Android, fragmentation makes that hard anyway.
To map a CPU name to CPU features, LLVM has a nice utility function doing that... on x86, but not on other architectures.
You know me, if that utility function had been available outside of x86, I would not have special-cased x86.
I tried implementing that on other architectures, but the ways I could see were either still architecture-specific in some way, or felt too heavy.
Here is what I would do:
When --iree-llvmcpu-target-triple is host (or unspecified, so defaults to host), default to --iree-llvmcpu-target-cpu=host.
This matches the behavior of hipcc, so I presume also nvcc.
When --iree-llvmcpu-target-triple is host (or unspecified, so defaults to host), default to --iree-llvmcpu-target-cpu=host.
This matches the behavior of hipcc, so I presume also nvcc.
Defaulting to host (and not requiring it be set explicitly) goes against @benvanik 's suggestions up in the issue: #18561 (comment) (unless that was specifically referring to autodetection for gpu targets?)
@ScottTodd , should we close this as completed by #18682 or leave this open until further changes are made, such as promoting that warning into an error? That we shouldn't do before a full release cycle of whichever distribution channel users are getting their IREE from, so I wonder if leaving this open means having an open, essentially unactionable issue for half a year.
Activity
ScottTodd commentedon Sep 20, 2024
Yes please! #15487
stellaraccident commentedon Sep 20, 2024
Let's stop over thinking this and do something simple like I suggest. Open to other options but would like to see this improved.
ScottTodd commentedon Sep 20, 2024
The suggested proposal SGTM. I might even want to default to having LLVM use the current host for the target CPU and available features (if those are different), then have users explicitly pass "generic" for the lowest common denominator.
We could apply similar logic to the GPU backends - try to detect devices on the system (shell out to vulkaninfo / rocm-smi / nvidia-smi?) and default to what is available, but still support cross compilation with explicit device info and a "generic" target where possible.
benvanik commentedon Sep 20, 2024
Yuck - that is not a cheap thing to do and has a high risk of flakes - I am still not sure why proper documentation is insufficient? You must specify your target device (
--iree-hal-target-device=) when compiling so also specifying a "use my hardware info" is fine. Just change the documentation to include both flags and then a user has a choice and knows what to do if they want to change things. Anything automatic is going to have issues. Users aren't coming in to iree-compile command line invocations blind - if all the docs specify the flag and they choose not to copy/paste it that's on them.benvanik commentedon Sep 20, 2024
Note that this is also what clang does - https://clang.llvm.org/docs/HIPSupport.html - you must pass
--offload-arch=to compile HIP code and if you want the native host target you must pass--offload-arch=native. nvcc does the same thing - you pass-arch=[some gpu]or-arch=native.benvanik commentedon Sep 20, 2024
(if there's such a big concern about documentation not fixing this issue then I'd be ok with making compilation fail if the user doesn't specify an arch for a backend - whether a particular arg, generic, native, etc - but guessing is bad)
ScottTodd commentedon Sep 20, 2024
We can certainly update the docs (https://iree.dev/guides/deployment-configurations/cpu/#compile-a-program) and start with a warning from the compiler if information is omitted and generic is used as the default.
I'm seeing a proliferation of flags (mainly in rocm usage, but also cpu) and the documentation can't keep up. I want more of that to be captured somewhere - docs, samples, the compiler itself, etc.
See one example here:
iree/experimental/regression_suite/shark-test-suite-models/sdxl/test_unet.py
Lines 90 to 110 in 914858f
benvanik commentedon Sep 20, 2024
That's insanity - besides the debug flags (dumping statistics/etc) if any of those are required that's a bug. I think Mahesh has said it before: a feature is not done until it's on by default and if all of those flags are needed to make the model compile or perform then the engineering was never completed. The only two flags required there should be
--iree-hal-target-device=hip(that's using the old deprecated flag) and--iree-hip-target=(which could benativeif we wanted to do what clang does and invokeamdgpu-archif it's present).stellaraccident commentedon Sep 20, 2024
I'm fine making
--iree-llvmcpu-target-cpu=<something>required. Given the proliferation of things out there, I think that the way to get there may be to first do what I am suggesting: make it issue a warning if not specified and incorrectly/implicitly defaulting to a generic CPU (with a note that this flag will soon be required).Agreed on all of the other points. Need to burn down all of the other flags. I'm just starting with this one.
ScottTodd commentedon Sep 20, 2024
I can take a pass at this, unless someone else wants to.
Plan:
--iree-llvmcpu-target-cpuis omittediree-llvmcpu-target-cpu-featuresis omitted--iree-hal-target-backends=llvm-cpuin-tree and set those flags explicitly--iree-hal-target-device?ScottTodd commentedon Sep 20, 2024
Can someone clarify why we have all three of these flags?
--iree-llvmcpu-target-triple--iree-llvmcpu-target-cpu--iree-llvmcpu-target-cpu-featuresIt seems like the triple could be a superset of the cpu? Is there some redundancy there? I see some riscv sample code setting both:
iree/runtime/src/iree/hal/local/elf/testdata/generate.sh
Lines 68 to 73 in d834aa7
but even our microkernels blog post (highlighting cpu performance work) only includes a few of the flags:
iree/docs/website/docs/community/blog/posts/microkernels.md
Lines 25 to 32 in d834aa7
Oh, the linalg tutorial from @bjacob explains that
target-cpuis for x86, buttarget-cpu-featuresis for other architectures?iree/docs/website/docs/community/blog/posts/linalg-tutorial.md
Lines 183 to 193 in d834aa7
There is some complicated logic and then a few calls into LLVM itself in https://github.com/iree-org/iree/blob/main/compiler/plugins/target/LLVMCPU/LLVMTargetOptions.cpp.
(This is why I filed #15487 - I've wanted someone directly familiar with LLVM CPU to be driving this)
ScottTodd commentedon Sep 20, 2024
More context on
cpuvscpu-features:iree-llvmcpu-target-cpuas an error outside of x86, and continue past errors #15477hostCPU togenericoutside of x86 #1548114 remaining items
ScottTodd commentedon Sep 23, 2024
That could work, yeah. When I say "test my changes" here, I'm still just referring to local development "testing", not automated CI testing - that would be a nice bonus.
stellaraccident commentedon Sep 23, 2024
Well, if you have the knobs to verify locally, then you're more than halfway to a lit test. That's how most of these things in llvm proper get tested.
ScottTodd commentedon Sep 23, 2024
Pushed an initial attempt at reworking how the target init is handled: #18587 . I could pass that off to someone else and context switch to other tasks 🤔
bjacob commentedon Sep 24, 2024
Sorry, I had not kept up with the discussion here, was heads down in GPU data tiling.
Here are the difficulties that I know of:
znver4orcascadelake. People do not typically want to talk in terms of CPU features on x86 because that is very cumbersome. For example, just enabling the baseline AVX-512 feature set onx86is a combination of 5 features, each with long names; a typical compilation relies on > 10 CPU features.+z,+a,+m. The "CPU" string is not much used on RISC-V, according to RISC-V folks I asked back then, due to the very modular nature of the architecture.armv8.2-a+i8mm. The CPU names are also not much used on Arm; when targeting Android, fragmentation makes that hard anyway.Here is what I would do:
--iree-llvmcpu-target-tripleishost(or unspecified, so defaults tohost), default to--iree-llvmcpu-target-cpu=host.hipcc, so I presume alsonvcc.--iree-llvmcpu-target-tripleis nothost, your Warn when--iree-llvmcpu-target-cpudefaults to "generic". #18587 sounds like a good way to go. Could dump a list of recognized CPU names for the specified target architecture.ScottTodd commentedon Sep 24, 2024
Thanks for the context!
Defaulting to host (and not requiring it be set explicitly) goes against @benvanik 's suggestions up in the issue: #18561 (comment) (unless that was specifically referring to autodetection for gpu targets?)
Oh, this sounds useful. Are there functions in LLVM that would help get such a list?
benvanik commentedon Sep 24, 2024
Yeah mostly about GPU targets (where we have to launch other tools that fully load/initialize drivers and such) - CPU detection in LLVM is free.
bjacob commentedon Oct 3, 2024
WDYT about #18682 ?
Warn when --iree-llvmcpu-target-cpu defaults to "generic". (#18682)
bjacob commentedon Oct 30, 2024
@ScottTodd , should we close this as completed by #18682 or leave this open until further changes are made, such as promoting that warning into an error? That we shouldn't do before a full release cycle of whichever distribution channel users are getting their IREE from, so I wonder if leaving this open means having an open, essentially unactionable issue for half a year.
ScottTodd commentedon Oct 30, 2024
Let's keep this open until we at least update our docs (https://iree.dev/guides/deployment-configurations/cpu/) to include the recommended best practices (e.g.
--iree-llvmcpu-target-cpu=host).We'll push a new stable release within a few weeks, and the next should then be 6-8 weeks later.
Refresh compile/run examples in deployment configuration guides. (#19920
Refresh compile/run examples in deployment configuration guides. (#19920
hanhanW commentedon May 8, 2026
Closing the issue because the docs already include the recommended flags.