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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! Generated MLX-C wrappers for the random sampling family.
//!
//! Each wrapper maps one MLX-C operation onto the runtime GPU stream
//! with the crate's owned-handle and captured-error semantics. The
//! coverage contract keeps this family in lockstep with the pinned
//! upstream headers.
use crate::bindings::support::optional_i32_slice;
use crate::raw;
use crate::{MlxArray, MlxBindingsContext, MlxCError, MlxDtype};
impl MlxBindingsContext {
/// Applies MLX-C `mlx_random_bernoulli` on the runtime GPU stream.
pub fn random_bernoulli(
&self,
p: &MlxArray,
shape: &[i32],
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_bernoulli", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_bernoulli(
output,
p.raw(),
optional_i32_slice(shape),
shape.len(),
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
})
}
/// Applies MLX-C `mlx_random_bits` on the runtime GPU stream.
pub fn random_bits(
&self,
shape: &[i32],
width: i32,
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_bits", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_bits(
output,
optional_i32_slice(shape),
shape.len(),
width,
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
})
}
/// Applies MLX-C `mlx_random_categorical_num_samples` on the runtime GPU stream.
pub fn random_categorical_num_samples(
&self,
logits_: &MlxArray,
axis: i32,
num_samples: i32,
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array(
"apply MLX random_categorical_num_samples",
|output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_categorical_num_samples(
output,
logits_.raw(),
axis,
num_samples,
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
},
)
}
/// Applies MLX-C `mlx_random_categorical_shape` on the runtime GPU stream.
pub fn random_categorical_shape(
&self,
logits: &MlxArray,
axis: i32,
shape: &[i32],
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_categorical_shape", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_categorical_shape(
output,
logits.raw(),
axis,
optional_i32_slice(shape),
shape.len(),
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
})
}
/// Applies MLX-C `mlx_random_gumbel` on the runtime GPU stream.
pub fn random_gumbel(
&self,
shape: &[i32],
dtype: MlxDtype,
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_gumbel", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_gumbel(
output,
optional_i32_slice(shape),
shape.len(),
dtype.to_raw(),
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
})
}
/// Applies MLX-C `mlx_random_laplace` on the runtime GPU stream.
pub fn random_laplace(
&self,
shape: &[i32],
dtype: MlxDtype,
loc: f32,
scale: f32,
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_laplace", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_laplace(
output,
optional_i32_slice(shape),
shape.len(),
dtype.to_raw(),
loc,
scale,
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
})
}
/// Applies MLX-C `mlx_random_multivariate_normal` on the runtime GPU stream.
pub fn random_multivariate_normal(
&self,
mean: &MlxArray,
cov: &MlxArray,
shape: &[i32],
dtype: MlxDtype,
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_multivariate_normal", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_multivariate_normal(
output,
mean.raw(),
cov.raw(),
optional_i32_slice(shape),
shape.len(),
dtype.to_raw(),
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
})
}
/// Applies MLX-C `mlx_random_normal_broadcast` on the runtime GPU stream.
pub fn random_normal_broadcast(
&self,
shape: &[i32],
dtype: MlxDtype,
loc: Option<&MlxArray>,
scale: Option<&MlxArray>,
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_normal_broadcast", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_normal_broadcast(
output,
optional_i32_slice(shape),
shape.len(),
dtype.to_raw(),
loc.map_or(MlxArray::empty_raw(), MlxArray::raw),
scale.map_or(MlxArray::empty_raw(), MlxArray::raw),
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
})
}
/// Applies MLX-C `mlx_random_permutation` on the runtime GPU stream.
pub fn random_permutation(
&self,
x: &MlxArray,
axis: i32,
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_permutation", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_permutation(
output,
x.raw(),
axis,
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
})
}
/// Applies MLX-C `mlx_random_permutation_arange` on the runtime GPU stream.
pub fn random_permutation_arange(
&self,
x: i32,
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_permutation_arange", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_permutation_arange(
output,
x,
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
})
}
/// Applies MLX-C `mlx_random_randint` on the runtime GPU stream.
pub fn random_randint(
&self,
low: &MlxArray,
high: &MlxArray,
shape: &[i32],
dtype: MlxDtype,
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_randint", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_randint(
output,
low.raw(),
high.raw(),
optional_i32_slice(shape),
shape.len(),
dtype.to_raw(),
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
})
}
/// Applies MLX-C `mlx_random_split_num` on the runtime GPU stream.
pub fn random_split_num(&self, key: &MlxArray, num: i32) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_split_num", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_random_split_num(output, key.raw(), num, stream) }
})
}
/// Applies MLX-C `mlx_random_truncated_normal` on the runtime GPU stream.
pub fn random_truncated_normal(
&self,
lower: &MlxArray,
upper: &MlxArray,
shape: &[i32],
dtype: MlxDtype,
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_truncated_normal", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_truncated_normal(
output,
lower.raw(),
upper.raw(),
optional_i32_slice(shape),
shape.len(),
dtype.to_raw(),
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
})
}
/// Applies MLX-C `mlx_random_uniform` on the runtime GPU stream.
pub fn random_uniform(
&self,
low: &MlxArray,
high: &MlxArray,
shape: &[i32],
dtype: MlxDtype,
key: Option<&MlxArray>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX random_uniform", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_random_uniform(
output,
low.raw(),
high.raw(),
optional_i32_slice(shape),
shape.len(),
dtype.to_raw(),
key.map_or(MlxArray::empty_raw(), MlxArray::raw),
stream,
)
}
})
}
/// Seeds MLX's default random key sequence through MLX-C `mlx_random_seed`.
pub fn seed_random_generation(&self, seed: u64) -> Result<(), MlxCError> {
// SAFETY: Seeding takes a plain integer and reports status through the
// captured-error machinery.
let status = unsafe { raw::mlx_random_seed(seed) };
crate::error::check_status(status, "seed MLX random generation")
}
}