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
// DO NOT EDIT! This file was auto-generated by crates/re_types_builder/src/codegen/rust/api.rs
// Based on "crates/re_types/definitions/rerun/datatypes/tensor_data.fbs".
#![allow(trivial_numeric_casts)]
#![allow(unused_parens)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::iter_on_single_items)]
#![allow(clippy::map_flatten)]
#![allow(clippy::match_wildcard_for_single_variants)]
#![allow(clippy::needless_question_mark)]
#![allow(clippy::new_without_default)]
#![allow(clippy::redundant_closure)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::too_many_lines)]
#![allow(clippy::unnecessary_cast)]
/// **Datatype**: A multi-dimensional `Tensor` of data.
///
/// The number of dimensions and their respective lengths is specified by the `shape` field.
/// The dimensions are ordered from outermost to innermost. For example, in the common case of
/// a 2D RGB Image, the shape would be `[height, width, channel]`.
///
/// These dimensions are combined with an index to look up values from the `buffer` field,
/// which stores a contiguous array of typed values.
#[derive(Clone, Debug, PartialEq)]
pub struct TensorData {
pub shape: Vec<crate::datatypes::TensorDimension>,
pub buffer: crate::datatypes::TensorBuffer,
}
impl<'a> From<TensorData> for ::std::borrow::Cow<'a, TensorData> {
#[inline]
fn from(value: TensorData) -> Self {
std::borrow::Cow::Owned(value)
}
}
impl<'a> From<&'a TensorData> for ::std::borrow::Cow<'a, TensorData> {
#[inline]
fn from(value: &'a TensorData) -> Self {
std::borrow::Cow::Borrowed(value)
}
}
impl crate::Loggable for TensorData {
type Name = crate::DatatypeName;
#[inline]
fn name() -> Self::Name {
"rerun.datatypes.TensorData".into()
}
#[allow(unused_imports, clippy::wildcard_imports)]
#[inline]
fn arrow_datatype() -> arrow2::datatypes::DataType {
use ::arrow2::datatypes::*;
DataType::Struct(vec![
Field {
name: "shape".to_owned(),
data_type: DataType::List(Box::new(Field {
name: "item".to_owned(),
data_type: <crate::datatypes::TensorDimension>::arrow_datatype(),
is_nullable: false,
metadata: [].into(),
})),
is_nullable: false,
metadata: [].into(),
},
Field {
name: "buffer".to_owned(),
data_type: <crate::datatypes::TensorBuffer>::arrow_datatype(),
is_nullable: false,
metadata: [].into(),
},
])
}
#[allow(unused_imports, clippy::wildcard_imports)]
fn to_arrow_opt<'a>(
data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
) -> crate::SerializationResult<Box<dyn ::arrow2::array::Array>>
where
Self: Clone + 'a,
{
re_tracing::profile_function!();
use crate::{Loggable as _, ResultExt as _};
use ::arrow2::{array::*, datatypes::*};
Ok({
let (somes, data): (Vec<_>, Vec<_>) = data
.into_iter()
.map(|datum| {
let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
(datum.is_some(), datum)
})
.unzip();
let bitmap: Option<::arrow2::bitmap::Bitmap> = {
let any_nones = somes.iter().any(|some| !*some);
any_nones.then(|| somes.into())
};
StructArray::new(
<crate::datatypes::TensorData>::arrow_datatype(),
vec![
{
let (somes, shape): (Vec<_>, Vec<_>) = data
.iter()
.map(|datum| {
let datum = datum.as_ref().map(|datum| {
let Self { shape, .. } = &**datum;
shape.clone()
});
(datum.is_some(), datum)
})
.unzip();
let shape_bitmap: Option<::arrow2::bitmap::Bitmap> = {
let any_nones = somes.iter().any(|some| !*some);
any_nones.then(|| somes.into())
};
{
use arrow2::{buffer::Buffer, offset::OffsetsBuffer};
let shape_inner_data: Vec<_> = shape
.iter()
.flatten()
.flatten()
.cloned()
.map(Some)
.collect();
let shape_inner_bitmap: Option<::arrow2::bitmap::Bitmap> = None;
let offsets = ::arrow2::offset::Offsets::<i32>::try_from_lengths(
shape.iter().map(|opt| {
opt.as_ref().map(|datum| datum.len()).unwrap_or_default()
}),
)
.unwrap()
.into();
ListArray::new(
DataType::List(Box::new(Field {
name: "item".to_owned(),
data_type: <crate::datatypes::TensorDimension>::arrow_datatype(
),
is_nullable: false,
metadata: [].into(),
})),
offsets,
{
_ = shape_inner_bitmap;
crate::datatypes::TensorDimension::to_arrow_opt(
shape_inner_data,
)?
},
shape_bitmap,
)
.boxed()
}
},
{
let (somes, buffer): (Vec<_>, Vec<_>) = data
.iter()
.map(|datum| {
let datum = datum.as_ref().map(|datum| {
let Self { buffer, .. } = &**datum;
buffer.clone()
});
(datum.is_some(), datum)
})
.unzip();
let buffer_bitmap: Option<::arrow2::bitmap::Bitmap> = {
let any_nones = somes.iter().any(|some| !*some);
any_nones.then(|| somes.into())
};
{
_ = buffer_bitmap;
crate::datatypes::TensorBuffer::to_arrow_opt(buffer)?
}
},
],
bitmap,
)
.boxed()
})
}
#[allow(unused_imports, clippy::wildcard_imports)]
fn from_arrow_opt(
arrow_data: &dyn ::arrow2::array::Array,
) -> crate::DeserializationResult<Vec<Option<Self>>>
where
Self: Sized,
{
re_tracing::profile_function!();
use crate::{Loggable as _, ResultExt as _};
use ::arrow2::{array::*, buffer::*, datatypes::*};
Ok({
let arrow_data = arrow_data
.as_any()
.downcast_ref::<::arrow2::array::StructArray>()
.ok_or_else(|| {
crate::DeserializationError::datatype_mismatch(
DataType::Struct(vec![
Field {
name: "shape".to_owned(),
data_type: DataType::List(Box::new(Field {
name: "item".to_owned(),
data_type: <crate::datatypes::TensorDimension>::arrow_datatype(
),
is_nullable: false,
metadata: [].into(),
})),
is_nullable: false,
metadata: [].into(),
},
Field {
name: "buffer".to_owned(),
data_type: <crate::datatypes::TensorBuffer>::arrow_datatype(),
is_nullable: false,
metadata: [].into(),
},
]),
arrow_data.data_type().clone(),
)
})
.with_context("rerun.datatypes.TensorData")?;
if arrow_data.is_empty() {
Vec::new()
} else {
let (arrow_data_fields, arrow_data_arrays) =
(arrow_data.fields(), arrow_data.values());
let arrays_by_name: ::std::collections::HashMap<_, _> = arrow_data_fields
.iter()
.map(|field| field.name.as_str())
.zip(arrow_data_arrays)
.collect();
let shape = {
if !arrays_by_name.contains_key("shape") {
return Err(crate::DeserializationError::missing_struct_field(
Self::arrow_datatype(),
"shape",
))
.with_context("rerun.datatypes.TensorData");
}
let arrow_data = &**arrays_by_name["shape"];
{
let arrow_data = arrow_data
.as_any()
.downcast_ref::<::arrow2::array::ListArray<i32>>()
.ok_or_else(|| {
crate::DeserializationError::datatype_mismatch(
DataType::List(Box::new(Field {
name: "item".to_owned(),
data_type:
<crate::datatypes::TensorDimension>::arrow_datatype(),
is_nullable: false,
metadata: [].into(),
})),
arrow_data.data_type().clone(),
)
})
.with_context("rerun.datatypes.TensorData#shape")?;
if arrow_data.is_empty() {
Vec::new()
} else {
let arrow_data_inner = {
let arrow_data_inner = &**arrow_data.values();
crate::datatypes::TensorDimension::from_arrow_opt(arrow_data_inner)
.with_context("rerun.datatypes.TensorData#shape")?
.into_iter()
.collect::<Vec<_>>()
};
let offsets = arrow_data.offsets();
arrow2::bitmap::utils::ZipValidity::new_with_validity(
offsets.iter().zip(offsets.lengths()),
arrow_data.validity(),
)
.map(|elem| {
elem.map(|(start, len)| {
let start = *start as usize;
let end = start + len;
if end as usize > arrow_data_inner.len() {
return Err(crate::DeserializationError::offset_slice_oob(
(start, end),
arrow_data_inner.len(),
));
}
#[allow(unsafe_code, clippy::undocumented_unsafe_blocks)]
let data = unsafe {
arrow_data_inner.get_unchecked(start as usize..end as usize)
};
let data = data
.iter()
.cloned()
.map(Option::unwrap_or_default)
.collect();
Ok(data)
})
.transpose()
})
.collect::<crate::DeserializationResult<Vec<Option<_>>>>()?
}
.into_iter()
}
};
let buffer = {
if !arrays_by_name.contains_key("buffer") {
return Err(crate::DeserializationError::missing_struct_field(
Self::arrow_datatype(),
"buffer",
))
.with_context("rerun.datatypes.TensorData");
}
let arrow_data = &**arrays_by_name["buffer"];
crate::datatypes::TensorBuffer::from_arrow_opt(arrow_data)
.with_context("rerun.datatypes.TensorData#buffer")?
.into_iter()
};
arrow2::bitmap::utils::ZipValidity::new_with_validity(
::itertools::izip!(shape, buffer),
arrow_data.validity(),
)
.map(|opt| {
opt.map(|(shape, buffer)| {
Ok(Self {
shape: shape
.ok_or_else(crate::DeserializationError::missing_data)
.with_context("rerun.datatypes.TensorData#shape")?,
buffer: buffer
.ok_or_else(crate::DeserializationError::missing_data)
.with_context("rerun.datatypes.TensorData#buffer")?,
})
})
.transpose()
})
.collect::<crate::DeserializationResult<Vec<_>>>()
.with_context("rerun.datatypes.TensorData")?
}
})
}
}