Skip to content

Commit 2f4c619

Browse files
author
Ben Liepert
committed
First pass at updating TensorData macros
1 parent 339b4c5 commit 2f4c619

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

crates/store/re_types/src/datatypes/tensor_data_ext.rs

+32-18
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ use crate::archetypes::EncodedImage;
88

99
use super::{TensorBuffer, TensorData, TensorDimension};
1010

11-
// Much of the following duplicates code from: `crates/re_components/src/tensor.rs`, which
12-
// will eventually go away as the Tensor migration is completed.
13-
1411
// ----------------------------------------------------------------------------
1512

1613
impl TensorData {
@@ -169,7 +166,6 @@ macro_rules! tensor_from_ndarray {
169166
type Error = TensorCastError;
170167

171168
fn try_from(value: ndarray::Array<$type, D>) -> Result<Self, Self::Error> {
172-
let value = value.as_standard_layout();
173169
let shape = value
174170
.shape()
175171
.iter()
@@ -178,15 +174,27 @@ macro_rules! tensor_from_ndarray {
178174
name: None,
179175
})
180176
.collect();
181-
value
182-
.is_standard_layout()
183-
.then(|| TensorData {
184-
shape,
185-
buffer: TensorBuffer::$variant(
186-
value.to_owned().into_raw_vec_and_offset().0.into(),
187-
),
188-
})
189-
.ok_or(TensorCastError::NotContiguousStdOrder)
177+
178+
let vec = if value.is_standard_layout() {
179+
let (mut vec, offset) = value.into_raw_vec_and_offset();
180+
// into_raw_vec_and_offset() guarantees that the logical element order (.iter()) matches the internal
181+
// storage order in the returned vector. Therefore, since it's in standard layout, it's contiguous in
182+
// memory and safe to assume all our data is stored starting at the offset returned
183+
if let Some(offset) = offset {
184+
vec.drain(..offset);
185+
vec
186+
} else {
187+
debug_assert!(vec.is_empty());
188+
vec
189+
}
190+
} else {
191+
value.into_iter().collect::<Vec<_>>()
192+
};
193+
194+
Ok(Self {
195+
shape,
196+
buffer: TensorBuffer::$variant(vec.into()),
197+
})
190198
}
191199
}
192200

@@ -313,13 +321,19 @@ impl<D: ::ndarray::Dimension> TryFrom<::ndarray::Array<half::f16, D>> for Tensor
313321
})
314322
.collect();
315323
if value.is_standard_layout() {
324+
let (vec, offset) = value.into_raw_vec_and_offset();
325+
// into_raw_vec_and_offset() guarantees that the logical element order (.iter()) matches the internal
326+
// storage order in the returned vector. Therefore, since it's in standard layout, it's contiguous in
327+
// memory and it's safe to assume all our data is stored starting at the offset returned
328+
let vec_slice = if let Some(offset) = offset {
329+
&vec[offset..]
330+
} else {
331+
debug_assert!(vec.is_empty());
332+
&vec
333+
};
316334
Ok(Self {
317335
shape,
318-
buffer: TensorBuffer::F16(
319-
bytemuck::cast_slice(value.into_raw_vec_and_offset().0.as_slice())
320-
.to_vec()
321-
.into(),
322-
),
336+
buffer: TensorBuffer::F16(Vec::from(bytemuck::cast_slice(vec_slice)).into()),
323337
})
324338
} else {
325339
Ok(Self {

0 commit comments

Comments
 (0)