1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! Type casting CUDA kernel launchers
//!
//! Provides launchers for casting tensors between different dtypes.
use cudarc::driver::PushKernelArg;
use cudarc::driver::safe::CudaContext;
use std::sync::Arc;
use super::loader::{
BLOCK_SIZE, dtype_suffix, elementwise_launch_config, get_kernel_function, get_or_load_module,
kernel_names, launch_config,
};
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::cuda::GuardedStream;
/// DTypes the `cast.cu` instantiation matrix covers.
///
/// Every ordered pair of these dtypes has a `cast_{src}_{dst}` kernel, so this
/// is the single source of truth for what `launch_cast` accepts. Keep it in
/// step with the row list at the bottom of `cast.cu`.
const CAST_DTYPES: &[DType] = &[
DType::F32,
DType::F64,
DType::F16,
DType::BF16,
DType::FP8E4M3,
DType::FP8E5M2,
DType::I64,
DType::I32,
DType::I16,
DType::I8,
DType::U64,
DType::U32,
DType::U16,
DType::U8,
DType::Bool,
];
/// Launch a cast operation kernel.
///
/// Converts tensor elements from `src_dtype` to `dst_dtype`.
///
/// # Supported Conversions
///
/// All ordered pairs of [`CAST_DTYPES`]. Complex dtypes are not supported.
///
/// # Numerical Semantics
///
/// Conversions go through f64 and saturate on the way to an integer, matching
/// the CPU reference in `src/runtime/cpu/kernels/memory.rs`: NaN becomes 0 and
/// out-of-range values clamp to the nearest bound. Integer to integer saturates
/// too, because the CPU path also routes it through f64.
///
/// # Safety
///
/// - All pointers must be valid device memory
/// - Input tensor must have at least `numel` elements of `src_dtype`
/// - Output tensor must have at least `numel` elements of `dst_dtype`
///
/// # Arguments
///
/// * `context` - CUDA context
/// * `stream` - CUDA stream for async execution
/// * `device_index` - Device index for module caching
/// * `src_dtype` - Source data type
/// * `dst_dtype` - Destination data type
/// * `input_ptr` - Device pointer to input tensor
/// * `output_ptr` - Device pointer to output tensor
/// * `numel` - Number of elements
pub unsafe fn launch_cast(
context: &Arc<CudaContext>,
stream: &GuardedStream,
device_index: usize,
src_dtype: DType,
dst_dtype: DType,
input_ptr: u64,
output_ptr: u64,
numel: usize,
) -> Result<()> {
// Same dtype is a no-op (should be handled by caller)
if src_dtype == dst_dtype {
return Ok(());
}
// Validate supported types
let is_supported = |d: DType| CAST_DTYPES.contains(&d);
if !is_supported(src_dtype) {
return Err(Error::UnsupportedDType {
dtype: src_dtype,
op: "cast",
});
}
if !is_supported(dst_dtype) {
return Err(Error::UnsupportedDType {
dtype: dst_dtype,
op: "cast",
});
}
unsafe {
let module = get_or_load_module(context, device_index, kernel_names::CAST_MODULE)?;
let func_name = format!(
"cast_{}_{}",
dtype_suffix(src_dtype),
dtype_suffix(dst_dtype)
);
let func = get_kernel_function(&module, &func_name)?;
let grid = elementwise_launch_config(numel)?;
let block = (BLOCK_SIZE, 1, 1);
let n = numel as u32;
let cfg = launch_config(grid, block, 0);
let mut builder = stream.launch_builder(&func);
builder.arg(&input_ptr);
builder.arg(&output_ptr);
builder.arg(&n);
builder.launch(cfg).map_err(|e| {
Error::Internal(format!(
"CUDA cast kernel '{}' launch failed: {:?}",
func_name, e
))
})?;
Ok(())
}
}