Skip to content

Commit 07fe2e7

Browse files
committed
Add runtime validation on CUDA compute cap for BF16
1 parent 66b6a7b commit 07fe2e7

3 files changed

Lines changed: 35 additions & 4 deletions

File tree

backends/candle/src/lib.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,40 @@ impl CandleBackend {
211211
}
212212
.map_err(|err| BackendError::Start(err.to_string()))?;
213213

214-
// Get candle dtype
215214
let dtype = if &dtype == "float32" {
216215
Ok(DType::F32)
217216
} else if &dtype == "float16" {
218217
Ok(DType::F16)
219218
} else if &dtype == "bfloat16" {
219+
match &device {
220+
Device::Cpu => {
221+
return Err(BackendError::Start(
222+
"BFloat16 is not supported on CPU. Use float16 or float32 instead."
223+
.to_string(),
224+
));
225+
}
226+
#[cfg(feature = "cuda")]
227+
Device::Cuda(_) => {
228+
let compute_cap = get_runtime_compute_cap().map_err(|e| {
229+
BackendError::Start(format!("Failed to get CUDA compute capability: {e:?}"))
230+
})?;
231+
if compute_cap < 80 {
232+
return Err(BackendError::Start(format!(
233+
"BFloat16 requires CUDA compute capability >= 8.0 (Ampere or newer), \
234+
but found {}.{}. Use float16 or float32 instead.",
235+
compute_cap / 10,
236+
compute_cap % 10
237+
)));
238+
}
239+
}
240+
#[cfg(not(feature = "cuda"))]
241+
Device::Cuda(_) => {
242+
return Err(BackendError::Start(
243+
"CUDA feature is not enabled".to_string(),
244+
));
245+
}
246+
Device::Metal(_) => (),
247+
}
220248
Ok(DType::BF16)
221249
} else {
222250
Err(BackendError::Start(format!(

backends/src/dtype.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ pub enum DType {
1313
Float16,
1414
#[cfg(any(feature = "python", feature = "candle", feature = "ort"))]
1515
Float32,
16+
// NOTE: For CUDA, BFloat16 requires Ampere (SM 80) or newer, which is validated at runtime, as
17+
// there are no specific features for the different CUDA compute capabilities to filter out
18+
// Turing and Volta from having `DType::Bfloat16`.
1619
#[cfg(any(
1720
feature = "python",
18-
// NOTE: It needs to exclude Turing (and Volta) i.e., anything before Ampere, as it won't
19-
// have support for BF16
2021
all(feature = "candle", any(feature = "metal", feature = "cuda"))
2122
))]
2223
Bfloat16,

router/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ struct Args {
3434
#[clap(long, env)]
3535
tokenization_workers: Option<usize>,
3636

37-
/// The dtype to be forced upon the model.
37+
/// The dtype to be forced upon the model, otherwise the value set in `dtype` (or `torch_dtype`
38+
/// as fallback) in the `config.json` file is used. Note that `bfloat16` is not supported on
39+
/// CPU, neither for Turing on CUDA, but only from Ampere onwards.
3840
#[clap(long, env, value_enum)]
3941
dtype: Option<DType>,
4042

0 commit comments

Comments
 (0)