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
mod graph_variable_op;
pub use graph_variable_op::GraphVariableOp;
use crate::{DataType, Graph, Operation, Shape, Tensor, ns_number_array_from_slice};
use objc2::{
extern_methods, msg_send,
rc::{Retained, autoreleasepool},
};
use objc2_foundation::{NSData, NSString};
use std::{mem::size_of_val, slice::from_raw_parts};
/// MemoryOps.
impl Graph {
extern_methods!();
}
impl Graph {
/// Creates a constant operation from raw bytes.
///
/// # Arguments
///
/// * `data` – Raw tensor bytes (`len` must equal `sizeof(data_type) * number_of_elements`).
/// * `shape` – Statically shaped output `[usize]`.
/// * `data_type` – [`DataType`] of the constant tensor.
///
/// # Returns
///
/// A new [`Tensor`] containing the constant values.
pub fn constant_with_ns_data(
&self,
data: &NSData,
shape: &[usize],
data_type: DataType,
) -> Retained<Tensor> {
unsafe {
msg_send![
self,
constantWithData: data,
shape: &*ns_number_array_from_slice(shape),
dataType: data_type
]
}
}
/// Creates a complex constant operation that fills the tensor with a single
/// complex scalar value.
///
/// # Arguments
///
/// * `real_part` – Real component of the scalar.
/// * `imaginary_part` – Imaginary component of the scalar.
/// * `shape` – Statically shaped output `[usize]`.
/// * `data_type` – [`DataType`] of the constant tensor.
///
/// # Returns
///
/// A new [`Tensor`] containing the constant complex values.
pub fn constant_with_real_imaginary_shape(
&self,
real_part: f64,
imaginary_part: f64,
shape: &[usize],
data_type: DataType,
) -> Retained<Tensor> {
unsafe {
msg_send![
self,
constantWithRealPart: real_part,
imaginaryPart: imaginary_part,
shape: &*ns_number_array_from_slice(shape),
dataType: data_type
]
}
}
/// Creates a placeholder tensor.
///
/// # Arguments
///
/// * `shape` – Optional shape of the tensor (`None` produces an unranked tensor).
/// * `data_type` – [`DataType`] of the placeholder.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A new placeholder [`Tensor`].
pub fn placeholder(
&self,
shape: Option<&[isize]>,
data_type: DataType,
name: Option<&str>,
) -> Retained<Tensor> {
let shape = shape.map(ns_number_array_from_slice);
autoreleasepool(|_| unsafe {
msg_send![
self,
placeholderWithShape: shape.as_deref(),
dataType: data_type,
name: name.map(NSString::from_str).as_deref()
]
})
}
/// Creates a constant operation from a slice of values.
///
/// # Arguments
///
/// * `data` – Slice containing tensor elements.
/// * `shape` – Statically shaped output `[usize]`.
/// * `data_type` – [`DataType`] of the constant tensor.
///
/// # Returns
///
/// A new [`Tensor`] containing the constant values.
pub fn constant_with_data<T: Copy>(
&self,
data: &[T],
shape: &[usize],
data_type: DataType,
) -> Retained<Tensor> {
autoreleasepool(|_| {
let ns_data = unsafe {
NSData::with_bytes(from_raw_parts(
data.as_ptr() as *const u8,
size_of_val(data),
))
};
unsafe {
msg_send![
self,
constantWithData: &*ns_data,
shape: &*ns_number_array_from_slice(shape),
dataType: data_type
]
}
})
}
/// Creates a scalar-filled constant operation.
///
/// # Arguments
///
/// * `scalar` – Scalar value used to fill the tensor.
/// * `shape_option` – Optional output shape; `None` produces a scalar tensor.
/// * `data_type` – [`DataType`] of the constant tensor.
///
/// # Returns
///
/// A new [`Tensor`] containing the scalar value.
pub fn constant_with_scalar(
&self,
scalar: f64,
shape_option: Option<&[usize]>,
data_type: DataType,
) -> Retained<Tensor> {
match shape_option {
Some(shape) => unsafe {
msg_send![
self,
constantWithScalar: scalar,
shape: &*ns_number_array_from_slice(shape),
dataType: data_type
]
},
None => unsafe {
msg_send![
self,
constantWithScalar: scalar,
dataType: data_type
]
},
}
}
/// Creates a complex scalar constant operation.
///
/// # Arguments
///
/// * `real_part` – Real component of the scalar.
/// * `imaginary_part` – Imaginary component of the scalar.
/// * `data_type` – Optional [`DataType`] (defaults to [`DataType::ComplexFloat32`]).
///
/// # Returns
///
/// A new [`Tensor`] containing the complex scalar.
pub fn constant_with_real_imaginary(
&self,
real_part: f64,
imaginary_part: f64,
data_type: Option<DataType>,
) -> Retained<Tensor> {
match data_type {
Some(data_type) => unsafe {
msg_send![
self,
constantWithRealPart: real_part,
imaginaryPart: imaginary_part,
dataType: data_type
]
},
None => unsafe {
msg_send![
self,
constantWithRealPart: real_part,
imaginaryPart: imaginary_part
]
},
}
}
/// Creates a variable operation from raw bytes.
///
/// # Arguments
///
/// * `data` – Raw tensor bytes.
/// * `shape` – Statically shaped output [`Shape`].
/// * `data_type` – [`DataType`] of the variable tensor.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A new variable [`Tensor`].
pub fn variable_with_ns_data(
&self,
data: &NSData,
shape: &[usize],
data_type: DataType,
name: Option<&str>,
) -> Retained<Tensor> {
unsafe {
msg_send![
self,
variableWithData: data,
shape: &*ns_number_array_from_slice(shape),
dataType: data_type,
name: name.map(NSString::from_str).as_deref()
]
}
}
/// Creates a variable operation from a slice of values.
///
/// # Arguments
///
/// * `data` – Slice containing tensor elements.
/// * `shape` – Statically shaped output [`Shape`].
/// * `data_type` – [`DataType`] of the variable tensor.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A new variable [`Tensor`].
pub fn variable_with_data<T: Copy>(
&self,
data: &[T],
shape: &[usize],
data_type: DataType,
name: Option<&str>,
) -> Retained<Tensor> {
autoreleasepool(|_| {
let data_size = size_of_val(data);
let ns_data = unsafe {
NSData::with_bytes(from_raw_parts(data.as_ptr() as *const u8, data_size))
};
Self::variable_with_ns_data(self, &ns_data, shape, data_type, name)
})
}
/// Creates a variable from another tensor.
///
/// # Arguments
///
/// * `tensor` – Source [`Tensor`].
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A new variable [`Tensor`].
pub fn variable_from_tensor(&self, tensor: &Tensor, name: Option<&str>) -> Retained<Tensor> {
unsafe {
msg_send![
self,
variableFromTensorWithTensor: tensor,
name: name.map(NSString::from_str).as_deref()
]
}
}
/// Reads the value of a variable tensor at this point in the graph.
///
/// # Arguments
///
/// * `variable` – Variable [`Tensor`] to read.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A [`Tensor`] representing the read result.
pub fn read_variable(&self, variable: &Tensor, name: Option<&str>) -> Retained<Tensor> {
unsafe {
msg_send![
self,
readVariable: variable,
name: name.map(NSString::from_str).as_deref()
]
}
}
/// Assigns `tensor` to `variable` at this point in the graph.
///
/// # Arguments
///
/// * `variable` – Variable [`Tensor`] to assign to.
/// * `tensor` – Value [`Tensor`] written into the variable.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// An [`Operation`] representing the assignment.
pub fn assign_variable(
&self,
variable: &Tensor,
tensor: &Tensor,
name: Option<&str>,
) -> Retained<Operation> {
unsafe {
msg_send![
self,
assignVariable: variable,
withValueOfTensor: tensor,
name: name.map(NSString::from_str).as_deref()
]
}
}
}