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
//! Generated MLX-C wrappers for the array creation 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, raw_optional_float};
use crate::raw;
use crate::{MlxArray, MlxBindingsContext, MlxCError, MlxDtype};
impl MlxBindingsContext {
/// Applies MLX-C `mlx_bartlett` on the runtime GPU stream.
pub fn bartlett(&self, size: i32) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX bartlett", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_bartlett(output, size, stream) }
})
}
/// Applies MLX-C `mlx_blackman` on the runtime GPU stream.
pub fn blackman(&self, size: i32) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX blackman", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_blackman(output, size, stream) }
})
}
/// Applies MLX-C `mlx_eye` on the runtime GPU stream.
pub fn eye(&self, n: i32, m: i32, k: i32, dtype: MlxDtype) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX eye", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_eye(output, n, m, k, dtype.to_raw(), stream) }
})
}
/// Applies MLX-C `mlx_full_like` on the runtime GPU stream.
pub fn full_like(
&self,
a: &MlxArray,
vals: &MlxArray,
dtype: MlxDtype,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX full_like", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_full_like(output, a.raw(), vals.raw(), dtype.to_raw(), stream) }
})
}
/// Applies MLX-C `mlx_hadamard_transform` on the runtime GPU stream.
pub fn hadamard_transform(
&self,
a: &MlxArray,
scale: Option<f32>,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX hadamard_transform", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_hadamard_transform(output, a.raw(), raw_optional_float(scale), stream)
}
})
}
/// Applies MLX-C `mlx_hamming` on the runtime GPU stream.
pub fn hamming(&self, size: i32) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX hamming", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_hamming(output, size, stream) }
})
}
/// Applies MLX-C `mlx_hanning` on the runtime GPU stream.
pub fn hanning(&self, size: i32) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX hanning", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_hanning(output, size, stream) }
})
}
/// Applies MLX-C `mlx_linspace` on the runtime GPU stream.
pub fn linspace(
&self,
start: f64,
stop: f64,
num: i32,
dtype: MlxDtype,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linspace", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_linspace(output, start, stop, num, dtype.to_raw(), stream) }
})
}
/// Applies MLX-C `mlx_linspace_endpoint` on the runtime GPU stream.
pub fn linspace_endpoint(
&self,
start: f64,
stop: f64,
num: i32,
endpoint: bool,
dtype: MlxDtype,
) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX linspace_endpoint", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_linspace_endpoint(
output,
start,
stop,
num,
endpoint,
dtype.to_raw(),
stream,
)
}
})
}
/// Applies MLX-C `mlx_ones` on the runtime GPU stream.
pub fn ones(&self, shape: &[i32], dtype: MlxDtype) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX ones", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe {
raw::mlx_ones(
output,
optional_i32_slice(shape),
shape.len(),
dtype.to_raw(),
stream,
)
}
})
}
/// Applies MLX-C `mlx_ones_like` on the runtime GPU stream.
pub fn ones_like(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX ones_like", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_ones_like(output, a.raw(), stream) }
})
}
/// Applies MLX-C `mlx_ones_like_dtype` on the runtime GPU stream.
pub fn ones_like_dtype(&self, a: &MlxArray, dtype: MlxDtype) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX ones_like_dtype", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_ones_like_dtype(output, a.raw(), dtype.to_raw(), stream) }
})
}
/// Applies MLX-C `mlx_tri` on the runtime GPU stream.
pub fn tri(&self, n: i32, m: i32, k: i32, dtype: MlxDtype) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX tri", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_tri(output, n, m, k, dtype.to_raw(), stream) }
})
}
/// Applies MLX-C `mlx_tril` on the runtime GPU stream.
pub fn tril(&self, x: &MlxArray, k: i32) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX tril", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_tril(output, x.raw(), k, stream) }
})
}
/// Applies MLX-C `mlx_triu` on the runtime GPU stream.
pub fn triu(&self, x: &MlxArray, k: i32) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX triu", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_triu(output, x.raw(), k, stream) }
})
}
/// Applies MLX-C `mlx_zeros_like` on the runtime GPU stream.
pub fn zeros_like(&self, a: &MlxArray) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX zeros_like", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_zeros_like(output, a.raw(), stream) }
})
}
/// Applies MLX-C `mlx_zeros_like_dtype` on the runtime GPU stream.
pub fn zeros_like_dtype(&self, a: &MlxArray, dtype: MlxDtype) -> Result<MlxArray, MlxCError> {
self.output_array("apply MLX zeros_like_dtype", |output, stream| {
// SAFETY: Inputs and stream are live and output is uniquely writable.
unsafe { raw::mlx_zeros_like_dtype(output, a.raw(), dtype.to_raw(), stream) }
})
}
}