Skip to content

Commit

Permalink
Refactor: Remove TensorTrait (#1819)
Browse files Browse the repository at this point in the history
* Refactor: Remove `TensorTrait`

We don't need it anymore
  • Loading branch information
emilk authored Apr 11, 2023
1 parent 7d4514e commit c254e2f
Show file tree
Hide file tree
Showing 18 changed files with 25 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
with:
mode: minimum
count: 1
labels: "📊 analytics, 🪳 bug, 🧑‍💻 dev experience, 📖 documentation, 💬 discussion, examples, 📉 performance, 🐍 python API, ⛃ re_datastore, 📺 re_viewer, 🔺 re_renderer, ⛴ release, 🦀 rust SDK, 🔨 testing, ui, 🕸️ web"
labels: "📊 analytics, 🪳 bug, 🧑‍💻 dev experience, 📖 documentation, 💬 discussion, examples, 📉 performance, 🐍 python API, ⛃ re_datastore, 📺 re_viewer, 🔺 re_renderer, 🚜 refactor, ⛴ release, 🦀 rust SDK, 🔨 testing, ui, 🕸️ web"
2 changes: 1 addition & 1 deletion crates/re_log_types/src/component_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub use size::Size3D;
#[cfg(feature = "image")]
pub use tensor::TensorImageError;
pub use tensor::{
Tensor, TensorCastError, TensorData, TensorDataMeaning, TensorDimension, TensorId, TensorTrait,
Tensor, TensorCastError, TensorData, TensorDataMeaning, TensorDimension, TensorId,
};
pub use text_entry::TextEntry;
pub use transform::{Pinhole, Rigid3, Transform};
Expand Down
32 changes: 10 additions & 22 deletions crates/re_log_types/src/component_types/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ use crate::{TensorDataType, TensorElement};

use super::arrow_convert_shims::BinaryBuffer;

pub trait TensorTrait {
fn id(&self) -> TensorId;
fn shape(&self) -> &[TensorDimension];
fn num_dim(&self) -> usize;
fn is_shaped_like_an_image(&self) -> bool;
fn is_vector(&self) -> bool;
fn meaning(&self) -> TensorDataMeaning;
fn get(&self, index: &[u64]) -> Option<TensorElement>;
fn dtype(&self) -> TensorDataType;
fn size_in_bytes(&self) -> usize;
}

// ----------------------------------------------------------------------------

/// A unique id per [`Tensor`].
Expand Down Expand Up @@ -365,23 +353,23 @@ pub struct Tensor {
pub meter: Option<f32>,
}

impl TensorTrait for Tensor {
impl Tensor {
#[inline]
fn id(&self) -> TensorId {
pub fn id(&self) -> TensorId {
self.tensor_id
}

#[inline]
fn shape(&self) -> &[TensorDimension] {
pub fn shape(&self) -> &[TensorDimension] {
self.shape.as_slice()
}

#[inline]
fn num_dim(&self) -> usize {
pub fn num_dim(&self) -> usize {
self.shape.len()
}

fn is_shaped_like_an_image(&self) -> bool {
pub fn is_shaped_like_an_image(&self) -> bool {
self.num_dim() == 2
|| self.num_dim() == 3 && {
matches!(
Expand All @@ -393,17 +381,17 @@ impl TensorTrait for Tensor {
}

#[inline]
fn is_vector(&self) -> bool {
pub fn is_vector(&self) -> bool {
let shape = &self.shape;
shape.len() == 1 || { shape.len() == 2 && (shape[0].size == 1 || shape[1].size == 1) }
}

#[inline]
fn meaning(&self) -> TensorDataMeaning {
pub fn meaning(&self) -> TensorDataMeaning {
self.meaning
}

fn get(&self, index: &[u64]) -> Option<TensorElement> {
pub fn get(&self, index: &[u64]) -> Option<TensorElement> {
let mut stride: usize = 1;
let mut offset: usize = 0;
for (TensorDimension { size, .. }, index) in self.shape.iter().zip(index).rev() {
Expand All @@ -429,11 +417,11 @@ impl TensorTrait for Tensor {
}
}

fn dtype(&self) -> TensorDataType {
pub fn dtype(&self) -> TensorDataType {
self.data.dtype()
}

fn size_in_bytes(&self) -> usize {
pub fn size_in_bytes(&self) -> usize {
self.data.size_in_bytes()
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/re_sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ pub mod components {
EncodedMesh3D, InstanceKey, KeypointId, Label, LineStrip2D, LineStrip3D, Mat3x3, Mesh3D,
MeshFormat, MeshId, Pinhole, Point2D, Point3D, Quaternion, Radius, RawMesh3D, Rect2D,
Rigid3, Scalar, ScalarPlotProps, Size3D, Tensor, TensorData, TensorDataMeaning,
TensorDimension, TensorId, TensorTrait, TextEntry, Transform, Vec2D, Vec3D, Vec4D,
ViewCoordinates,
TensorDimension, TensorId, TextEntry, Transform, Vec2D, Vec3D, Vec4D, ViewCoordinates,
};
}

Expand Down
2 changes: 1 addition & 1 deletion crates/re_tensor_ops/tests/tensor_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use re_log_types::component_types::{
Tensor, TensorCastError, TensorData, TensorDataMeaning, TensorDimension, TensorId, TensorTrait,
Tensor, TensorCastError, TensorData, TensorDataMeaning, TensorDimension, TensorId,
};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer/src/misc/caches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod mesh_cache;
mod tensor_decode_cache;
mod tensor_image_cache;

use re_log_types::component_types::{self, TensorTrait};
use re_log_types::component_types::{self};
pub use tensor_image_cache::ColoredTensorView;

/// Does memoization of different things for the immediate mode UI.
Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer/src/misc/caches/tensor_decode_cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use re_log_types::component_types::{Tensor, TensorDimension, TensorId, TensorTrait};
use re_log_types::component_types::{Tensor, TensorDimension, TensorId};

#[derive(thiserror::Error, Clone, Debug)]
pub enum TensorDecodeError {
Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer/src/misc/caches/tensor_image_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use egui::{Color32, ColorImage};
use egui_extras::RetainedImage;
use image::DynamicImage;
use re_log_types::{
component_types::{self, ClassId, Tensor, TensorData, TensorDataMeaning, TensorTrait},
component_types::{self, ClassId, Tensor, TensorData, TensorDataMeaning},
MsgId,
};
use re_renderer::{
Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer/src/ui/data_ui/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use egui::{ColorImage, Vec2};
use itertools::Itertools as _;

use re_log_types::{
component_types::{ClassId, Tensor, TensorDataMeaning, TensorTrait},
component_types::{ClassId, Tensor, TensorDataMeaning},
TensorElement,
};

Expand Down
5 changes: 1 addition & 4 deletions crates/re_viewer/src/ui/space_view_heuristics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use itertools::Itertools;
use nohash_hasher::IntSet;
use re_arrow_store::{DataStore, LatestAtQuery, Timeline};
use re_data_store::{log_db::EntityDb, query_latest_single, ComponentName, EntityPath};
use re_log_types::{
component_types::{Tensor, TensorTrait},
Component,
};
use re_log_types::{component_types::Tensor, Component};

use crate::{
misc::{space_info::SpaceInfoCollection, ViewerContext},
Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer/src/ui/view_bar_chart/scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::BTreeMap;
use re_arrow_store::LatestAtQuery;
use re_data_store::EntityPath;
use re_log::warn_once;
use re_log_types::component_types::{self, InstanceKey, Tensor, TensorTrait as _};
use re_log_types::component_types::{self, InstanceKey, Tensor};
use re_query::query_entity_with_primary;

use crate::{misc::ViewerContext, ui::scene::SceneQuery};
Expand Down
3 changes: 1 addition & 2 deletions crates/re_viewer/src/ui/view_category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use re_arrow_store::{LatestAtQuery, TimeInt};
use re_data_store::{EntityPath, LogDb, Timeline};
use re_log_types::{
component_types::{
Box3D, LineStrip2D, LineStrip3D, Point2D, Point3D, Rect2D, Scalar, Tensor, TensorTrait,
TextEntry,
Box3D, LineStrip2D, LineStrip3D, Point2D, Point3D, Rect2D, Scalar, Tensor, TextEntry,
},
Arrow3D, Component, Mesh3D, Transform,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use itertools::Itertools;

use re_data_store::{query_latest_single, EntityPath, EntityProperties, InstancePathHash};
use re_log_types::{
component_types::{ColorRGBA, InstanceKey, Tensor, TensorData, TensorDataMeaning, TensorTrait},
component_types::{ColorRGBA, InstanceKey, Tensor, TensorData, TensorDataMeaning},
Component, Transform,
};
use re_query::{query_primary_with_history, EntityView, QueryError};
Expand Down
1 change: 0 additions & 1 deletion crates/re_viewer/src/ui/view_spatial/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ impl ViewSpatialState {
if tensor.meaning == TensorDataMeaning::Depth {
if properties.depth_from_world_scale.is_auto() {
let auto = tensor.meter.unwrap_or_else(|| {
use re_log_types::component_types::TensorTrait as _;
if tensor.dtype().is_integer() {
1000.0
} else {
Expand Down
1 change: 0 additions & 1 deletion crates/re_viewer/src/ui/view_spatial/ui_2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use egui::{
};
use macaw::IsoTransform;
use re_data_store::EntityPath;
use re_log_types::component_types::TensorTrait;
use re_renderer::view_builder::{TargetConfiguration, ViewBuilder};

use super::{
Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer/src/ui/view_tensor/scene.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use re_arrow_store::LatestAtQuery;
use re_data_store::{EntityPath, EntityProperties, InstancePath};
use re_log_types::component_types::{InstanceKey, Tensor, TensorTrait};
use re_log_types::component_types::{InstanceKey, Tensor};
use re_query::{query_entity_with_primary, EntityView, QueryError};

use crate::{misc::ViewerContext, ui::SceneQuery};
Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer/src/ui/view_tensor/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use egui::{epaint::TextShape, Color32, ColorImage, NumExt as _, Vec2};
use ndarray::{Axis, Ix2};

use re_log_types::{
component_types::{self, Tensor, TensorTrait},
component_types::{self, Tensor},
TensorDataType,
};
use re_tensor_ops::dimension_mapping::{DimensionMapping, DimensionSelector};
Expand Down
2 changes: 1 addition & 1 deletion rerun_py/src/python_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub use rerun::{
EncodedMesh3D, InstanceKey, KeypointId, Label, LineStrip2D, LineStrip3D, Mat3x3, Mesh3D,
MeshFormat, MeshId, Pinhole, Point2D, Point3D, Quaternion, Radius, RawMesh3D, Rect2D,
Rigid3, Scalar, ScalarPlotProps, Size3D, Tensor, TensorData, TensorDimension, TensorId,
TensorTrait, TextEntry, Transform, Vec2D, Vec3D, Vec4D, ViewCoordinates,
TextEntry, Transform, Vec2D, Vec3D, Vec4D, ViewCoordinates,
},
coordinates::{Axis3, Handedness, Sign, SignedAxis3},
};
Expand Down

1 comment on commit c254e2f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rust Benchmark

Benchmark suite Current: c254e2f Previous: 7d4514e Ratio
datastore/num_rows=1000/num_instances=1000/packed=false/insert/default 10256134 ns/iter (± 434056) 10655704 ns/iter (± 500613) 0.96
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at/default 1817 ns/iter (± 20) 1839 ns/iter (± 21) 0.99
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at_missing/primary/default 279 ns/iter (± 0) 280 ns/iter (± 0) 1.00
datastore/num_rows=1000/num_instances=1000/packed=false/latest_at_missing/secondaries/default 436 ns/iter (± 0) 434 ns/iter (± 0) 1.00
datastore/num_rows=1000/num_instances=1000/packed=false/range/default 10381084 ns/iter (± 297466) 10819118 ns/iter (± 1050608) 0.96
mono_points_arrow/generate_message_bundles 43572009 ns/iter (± 1120680) 46382388 ns/iter (± 1940474) 0.94
mono_points_arrow/generate_messages 149214024 ns/iter (± 1115181) 169558340 ns/iter (± 1538629) 0.88
mono_points_arrow/encode_log_msg 180387034 ns/iter (± 693931) 193458262 ns/iter (± 1130953) 0.93
mono_points_arrow/encode_total 379472133 ns/iter (± 2300136) 409625227 ns/iter (± 2931999) 0.93
mono_points_arrow/decode_log_msg 232193823 ns/iter (± 742826) 248309845 ns/iter (± 1431760) 0.94
mono_points_arrow/decode_message_bundles 69529504 ns/iter (± 900827) 83077512 ns/iter (± 937432) 0.84
mono_points_arrow/decode_total 306361429 ns/iter (± 1434216) 337306060 ns/iter (± 2775485) 0.91
mono_points_arrow_batched/generate_message_bundles 36630803 ns/iter (± 986396) 40333025 ns/iter (± 2101631) 0.91
mono_points_arrow_batched/generate_messages 7102180 ns/iter (± 304370) 8420011 ns/iter (± 833650) 0.84
mono_points_arrow_batched/encode_log_msg 1469316 ns/iter (± 3969) 1459604 ns/iter (± 7542) 1.01
mono_points_arrow_batched/encode_total 45984798 ns/iter (± 1237785) 52606002 ns/iter (± 1953837) 0.87
mono_points_arrow_batched/decode_log_msg 856740 ns/iter (± 2370) 858989 ns/iter (± 2897) 1.00
mono_points_arrow_batched/decode_message_bundles 11429057 ns/iter (± 179088) 12068697 ns/iter (± 689960) 0.95
mono_points_arrow_batched/decode_total 12510562 ns/iter (± 309773) 13643801 ns/iter (± 612416) 0.92
batch_points_arrow/generate_message_bundles 331320 ns/iter (± 601) 333570 ns/iter (± 838) 0.99
batch_points_arrow/generate_messages 6362 ns/iter (± 16) 6352 ns/iter (± 15) 1.00
batch_points_arrow/encode_log_msg 393486 ns/iter (± 879) 384658 ns/iter (± 3508) 1.02
batch_points_arrow/encode_total 762488 ns/iter (± 1337) 736022 ns/iter (± 3775) 1.04
batch_points_arrow/decode_log_msg 333884 ns/iter (± 2112) 337204 ns/iter (± 1221) 0.99
batch_points_arrow/decode_message_bundles 2328 ns/iter (± 6) 2295 ns/iter (± 17) 1.01
batch_points_arrow/decode_total 337767 ns/iter (± 639) 350135 ns/iter (± 1114) 0.96
arrow_mono_points/insert 6417918245 ns/iter (± 18029498) 7350272140 ns/iter (± 25118734) 0.87
arrow_mono_points/query 1754581 ns/iter (± 14456) 1780072 ns/iter (± 7323) 0.99
arrow_batch_points/insert 3186548 ns/iter (± 8533) 3214273 ns/iter (± 8280) 0.99
arrow_batch_points/query 16415 ns/iter (± 34) 16367 ns/iter (± 58) 1.00
arrow_batch_vecs/insert 44602 ns/iter (± 82) 44355 ns/iter (± 99) 1.01
arrow_batch_vecs/query 388468 ns/iter (± 1506) 388792 ns/iter (± 478) 1.00
tuid/Tuid::random 34 ns/iter (± 0) 34 ns/iter (± 0) 1

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.