Skip to content

Commit 851d920

Browse files
authored
Remove the default feature from cubecl-hip-sys Cargo.toml (#13)
* Remove the default feature from cubecl-hip-sys Cargo.toml * Check for rocm__ conflicts as well * Set a ROCm version in CI * Set default features when possible in build.rs * Fix cargo audit weird bug * Fix format * Checkout repo in CI
1 parent 5666999 commit 851d920

File tree

5 files changed

+102
-17
lines changed

5 files changed

+102
-17
lines changed

.github/workflows/ci.yml

+18-11
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@ jobs:
1515
checks:
1616
runs-on: amd-rx7600
1717
steps:
18-
- name: Setup Rust
19-
uses: tracel-ai/github-actions/[email protected]
18+
# --------------------------------------------------------------------------------
19+
# We don't use our github-action because it seems that the cache does not work well
20+
# with our AMD runner.
21+
# cargo-audit is not found for example whereas it is correctly installed.
22+
- name: checkout
23+
uses: actions/checkout@v4
24+
- name: install rust
25+
uses: dtolnay/rust-toolchain@master
2026
with:
21-
rust-toolchain: stable
22-
cache-key: stable-linux
23-
linux-pre-cleanup: false
24-
# --------------------------------------------------------------------------------
27+
components: rustfmt, clippy
28+
toolchain: stable
29+
# --------------------------------------------------------------------------------
2530
- name: Audit
2631
run: cargo xtask check audit
2732
# --------------------------------------------------------------------------------
@@ -39,12 +44,14 @@ jobs:
3944
tests:
4045
runs-on: amd-rx7600
4146
steps:
42-
- name: Setup Rust
43-
uses: tracel-ai/github-actions/[email protected]
47+
# --------------------------------------------------------------------------------
48+
- name: checkout
49+
uses: actions/checkout@v4
50+
- name: install rust
51+
uses: dtolnay/rust-toolchain@master
4452
with:
45-
rust-toolchain: stable
46-
cache-key: stable-linux
47-
linux-pre-cleanup: false
53+
components: rustfmt, clippy
54+
toolchain: stable
4855
# --------------------------------------------------------------------------------
4956
- name: Lint
5057
run: cargo xtask check lint

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ If a fix is required and the default ROCm version remains `6.2.4`, the `cubecl-h
4343

4444
Add the crate [cubecl-hip-sys][2] to the `Cargo.toml` of your project and enable the feature
4545
corresponding to the version of ROCm you have installed.
46-
If you no feature corresponds to your ROCm installation then read the next section to learn
46+
47+
```toml
48+
cubecl-hip-sys = { version = "6.3.1000", features = ["rocm__6_3_1"] }
49+
```
50+
51+
If no feature corresponds to your ROCm installation then read the next section to learn
4752
how to generate and submit new bindings for your version.
4853

4954
Next you need to point out where you installed ROCm so that `rustc` can link to your ROCM libraries. To do so set the variable `ROCM_PATH`, or `HIP_PATH` or the more specific `CUBECL_ROCM_PATH` to its

crates/cubecl-hip-sys/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ version.workspace = true
1212
rust-version = "1.81"
1313

1414
[features]
15-
default = ["rocm__6_3_1"]
15+
default = []
1616

1717
# ROCm versions
1818
rocm__6_2_2 = [ "hip_41134" ]

crates/cubecl-hip-sys/build.rs

+76-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,53 @@ const ROCM_HIP_FEATURE_PREFIX: &str = "CARGO_FEATURE_HIP_";
55

66
include!("src/build_script.rs");
77

8+
/// Return true if at least one rocm_x_x_x feature is set
9+
fn is_rocm_feature_set() -> bool {
10+
let mut enabled_features = Vec::new();
11+
12+
for (key, value) in env::vars() {
13+
if key.starts_with(ROCM_FEATURE_PREFIX) && value == "1" {
14+
enabled_features.push(format!(
15+
"rocm__{}",
16+
key.strip_prefix(ROCM_FEATURE_PREFIX).unwrap()
17+
));
18+
}
19+
}
20+
21+
!enabled_features.is_empty()
22+
}
23+
24+
/// Make sure that at least one and only one rocm version feature is set
25+
fn ensure_single_rocm_version_feature_set() {
26+
let mut enabled_features = Vec::new();
27+
28+
for (key, value) in env::vars() {
29+
if key.starts_with(ROCM_FEATURE_PREFIX) && value == "1" {
30+
enabled_features.push(format!(
31+
"rocm__{}",
32+
key.strip_prefix(ROCM_FEATURE_PREFIX).unwrap()
33+
));
34+
}
35+
}
36+
37+
match enabled_features.len() {
38+
1 => {}
39+
0 => panic!("No ROCm version feature is enabled. One ROCm version feature must be set."),
40+
_ => panic!(
41+
"Multiple ROCm version features are enabled: {:?}. Only one can be set.",
42+
enabled_features
43+
),
44+
}
45+
}
46+
847
/// Make sure that at least one and only one hip feature is set
9-
fn ensure_single_rocm_hip_feature_set() {
48+
fn ensure_single_hip_feature_set() {
1049
let mut enabled_features = Vec::new();
1150

1251
for (key, value) in env::vars() {
1352
if key.starts_with(ROCM_HIP_FEATURE_PREFIX) && value == "1" {
1453
enabled_features.push(format!(
15-
"rocm__{}",
54+
"hip_{}",
1655
key.strip_prefix(ROCM_HIP_FEATURE_PREFIX).unwrap()
1756
));
1857
}
@@ -31,6 +70,10 @@ fn ensure_single_rocm_hip_feature_set() {
3170
/// Checks if the version inside `rocm_path` matches crate version
3271
fn check_rocm_version(rocm_path: impl AsRef<Path>) -> std::io::Result<bool> {
3372
let rocm_system_version = get_rocm_system_version(rocm_path)?;
73+
if !is_rocm_feature_set() {
74+
// If there is no feature set but we found a system version we continue
75+
return Ok(true);
76+
}
3477
let rocm_feature_version = get_rocm_feature_version();
3578

3679
if rocm_system_version.major == rocm_feature_version.major {
@@ -50,6 +93,34 @@ fn check_rocm_version(rocm_path: impl AsRef<Path>) -> std::io::Result<bool> {
5093
}
5194
}
5295

96+
/// If no rocm_x_x_x feature is set then we set the feature corresponding
97+
/// to the passed ROCm path.
98+
fn set_default_rocm_version(rocm_path: impl AsRef<Path>) -> std::io::Result<()> {
99+
if is_rocm_feature_set() {
100+
// a feature has been prodived to set the ROCm version
101+
return Ok(());
102+
}
103+
println!("cargo::warning=No `rocm__x_x_x` feature set. Using the version of a default installation of ROCm if found on the system. Consider setting a `rocm__x_x_x` feature in the Cargo.toml file of your crate.");
104+
105+
// Set default feature with the version found on the system
106+
let rocm_system_version = get_rocm_system_version(&rocm_path)?;
107+
let hip_system_patch = get_hip_system_version(&rocm_path)?;
108+
println!("cargo::warning=Found default version of ROCm on system: {rocm_system_version}. Associated HIP patch version is: {}", hip_system_patch.patch);
109+
let default_rocm_feature = format!("rocm__{}", rocm_system_version).replace(".", "_");
110+
let default_hip_feature = format!("hip_{}", hip_system_patch.patch);
111+
println!("cargo:rustc-cfg=feature=\"{}\"", default_rocm_feature);
112+
println!("cargo:rustc-cfg=feature=\"{}\"", default_hip_feature);
113+
env::set_var(
114+
format!("{ROCM_FEATURE_PREFIX}{}", rocm_system_version).replace(".", "_"),
115+
"1",
116+
);
117+
env::set_var(
118+
format!("{ROCM_HIP_FEATURE_PREFIX}{}", hip_system_patch.patch),
119+
"1",
120+
);
121+
Ok(())
122+
}
123+
53124
/// Return the ROCm version corresponding to the enabled rocm__<version> feature
54125
fn get_rocm_feature_version() -> Version {
55126
for (key, value) in env::vars() {
@@ -115,7 +186,9 @@ fn main() {
115186
let rocm_path = rocm_path_candidates.find(|path| check_rocm_version(path).unwrap_or_default());
116187

117188
if let Some(valid_rocm_path) = rocm_path {
118-
ensure_single_rocm_hip_feature_set();
189+
set_default_rocm_version(valid_rocm_path).unwrap();
190+
ensure_single_rocm_version_feature_set();
191+
ensure_single_hip_feature_set();
119192
// verify HIP compatibility
120193
let Version {
121194
patch: hip_system_patch_version,

0 commit comments

Comments
 (0)