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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! Utility operations for CUDA runtime
use crate::dtype::DType;
use crate::error::Result;
use crate::ops::{BinaryOps, UtilityOps};
use crate::runtime::cuda::kernels::{
launch_arange, launch_eye, launch_fill_with_f64, launch_linspace,
};
use crate::runtime::cuda::{CudaClient, CudaRuntime};
use crate::runtime::{validate_arange, validate_eye};
use crate::tensor::Tensor;
impl UtilityOps<CudaRuntime> for CudaClient {
fn clamp(
&self,
a: &Tensor<CudaRuntime>,
min_val: f64,
max_val: f64,
) -> Result<Tensor<CudaRuntime>> {
// Use native CUDA implementation via composition of maximum and minimum
// clamp(x, min, max) = min(max(x, min), max)
// This approach uses existing optimized kernels
// Create scalar tensors for min and max
let min_scalar = self.fill(&[], min_val, a.dtype())?;
let max_scalar = self.fill(&[], max_val, a.dtype())?;
// First: max(x, min_val)
let clamped_low = self.maximum(a, &min_scalar)?;
// Then: min(result, max_val)
self.minimum(&clamped_low, &max_scalar)
}
fn fill(&self, shape: &[usize], value: f64, dtype: DType) -> Result<Tensor<CudaRuntime>> {
let numel: usize = shape.iter().product();
if numel == 0 {
// Empty tensor - just allocate
return Tensor::<CudaRuntime>::empty(shape, dtype, &self.device);
}
// `FillValue::from_f64` stores Bool as a raw `value as u8`, which is
// wrong for any fill value other than 0.0/1.0 (e.g. `fill(_, 2.0,
// Bool)` would write byte 2, not the boolean collapse). Route through
// the dtype-generic constructor instead, which applies the
// crate-wide Bool convention (any nonzero, NaN included, is true)
// and matches CPU/WebGPU `fill` above/below.
if dtype == DType::Bool {
return Tensor::<CudaRuntime>::full_scalar(shape, dtype, value, &self.device);
}
// Allocate output tensor
let out = Tensor::<CudaRuntime>::empty(shape, dtype, &self.device)?;
// Launch native CUDA fill kernel
unsafe {
launch_fill_with_f64(
&self.context,
&self.stream,
self.device.index,
dtype,
value,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn arange(
&self,
start: f64,
stop: f64,
step: f64,
dtype: DType,
) -> Result<Tensor<CudaRuntime>> {
// Use shared validation
let numel = validate_arange(start, stop, step)?;
// Handle empty tensor case
if numel == 0 {
return Tensor::<CudaRuntime>::empty(&[0], dtype, &self.device);
}
let out = Tensor::<CudaRuntime>::empty(&[numel], dtype, &self.device)?;
unsafe {
launch_arange(
&self.context,
&self.stream,
self.device.index,
dtype,
start,
step,
out.ptr(),
numel,
)?;
}
Ok(out)
}
fn linspace(
&self,
start: f64,
stop: f64,
steps: usize,
dtype: DType,
) -> Result<Tensor<CudaRuntime>> {
// linspace supports all numeric dtypes - computation is done in higher precision,
// then converted to the output dtype. This matches NumPy behavior.
// Handle edge cases
if steps == 0 {
return Tensor::<CudaRuntime>::empty(&[0], dtype, &self.device);
}
if steps == 1 {
return self.fill(&[1], start, dtype);
}
let out = Tensor::<CudaRuntime>::empty(&[steps], dtype, &self.device)?;
unsafe {
launch_linspace(
&self.context,
&self.stream,
self.device.index,
dtype,
start,
stop,
out.ptr(),
steps,
)?;
}
Ok(out)
}
fn one_hot(
&self,
indices: &Tensor<CudaRuntime>,
num_classes: usize,
) -> Result<Tensor<CudaRuntime>> {
crate::ops::impl_generic::one_hot_impl(self, indices, num_classes)
}
fn meshgrid(
&self,
tensors: &[&Tensor<CudaRuntime>],
indexing: crate::ops::MeshgridIndexing,
) -> Result<Vec<Tensor<CudaRuntime>>> {
crate::ops::impl_generic::meshgrid_impl(tensors, indexing)
}
fn eye(&self, n: usize, m: Option<usize>, dtype: DType) -> Result<Tensor<CudaRuntime>> {
// Use shared validation
let (rows, cols) = validate_eye(n, m);
// Handle edge cases
if rows == 0 || cols == 0 {
return Tensor::<CudaRuntime>::empty(&[rows, cols], dtype, &self.device);
}
let out = Tensor::<CudaRuntime>::empty(&[rows, cols], dtype, &self.device)?;
unsafe {
launch_eye(
&self.context,
&self.stream,
self.device.index,
dtype,
rows,
cols,
out.ptr(),
)?;
}
Ok(out)
}
}