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
use crate::{AxesOrTensor, Graph, Tensor, ns_number_array_from_slice};
use objc2::{extern_methods, msg_send, rc::Retained};
use objc2_foundation::NSString;
impl Graph {
/// Removes all singleton dimensions from `tensor`.
///
/// # Arguments
///
/// * `tensor` – Input tensor.
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A [`Tensor`] with all size-1 dimensions removed.
pub fn squeeze(&self, tensor: &Tensor, name: Option<&str>) -> Retained<Tensor> {
unsafe {
msg_send![
self,
squeezeTensor: tensor,
name: name.map(NSString::from_str).as_deref(),
]
}
}
/// Removes the singleton dimension at `axis`.
///
/// # Arguments
///
/// * `tensor` – Input tensor.
/// * `axis` – Axis to remove (size must be 1).
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A [`Tensor`] with the specified axis removed.
pub fn squeeze_axis(&self, tensor: &Tensor, axis: i64, name: Option<&str>) -> Retained<Tensor> {
unsafe {
msg_send![self, squeezeTensor: tensor, axis: axis, name: name.map(NSString::from_str).as_deref()]
}
}
/// Removes singleton dimensions at multiple `axes`.
///
/// # Arguments
///
/// * `tensor` – Input tensor.
/// * `axes` – Axes to remove (slice or tensor).
/// * `name` – Optional debug label.
///
/// # Returns
///
/// A [`Tensor`] with the specified axes removed.
pub fn squeeze_axes<'a>(
&self,
tensor: &Tensor,
axes: AxesOrTensor<'a>,
name: Option<&str>,
) -> Retained<Tensor> {
match axes {
AxesOrTensor::Axes(axes) => unsafe {
msg_send![
self,
squeezeTensor: tensor,
axes: &*ns_number_array_from_slice(axes),
name: name.map(NSString::from_str).as_deref(),
]
},
AxesOrTensor::Tensor(axes) => unsafe {
msg_send![
self,
squeezeTensor: tensor,
axesTensor: axes,
name: name.map(NSString::from_str).as_deref(),
]
},
}
}
}