Skip to content

Add Generic Tensor Type#12

Merged
Edwardius merged 8 commits intomainfrom
eddy/add_tensor_type
Sep 20, 2025
Merged

Add Generic Tensor Type#12
Edwardius merged 8 commits intomainfrom
eddy/add_tensor_type

Conversation

@Edwardius
Copy link
Contributor

@Edwardius Edwardius commented Sep 19, 2025

Adds a generic tensor type to interface between ros messages and the backend plugins.

Talks about a generic tensor type for ROS have begun https://discourse.openrobotics.org/t/native-rcl-tensor-type/49497 but the timeline for something like that is not set in stone.

For now, we'll use a generic, and relatively simple tensor type to just pass data around in Deep ROS. For the sake of replacing such a type with future types, deep_tensor should not be built on (or at least limited in expansion)

Copy link
Contributor

@thomasonzhou thomasonzhou left a comment

Choose a reason for hiding this comment

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

Added some comments

Copy link
Contributor

@thomasonzhou thomasonzhou left a comment

Choose a reason for hiding this comment

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

Added some comments on is_view_

Comment on lines +1 to +2
// Copyright (c) 2025-present WATonomous. All rights reserved.
//
Copy link
Contributor

Choose a reason for hiding this comment

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

TODO: Add test for view to ensure the invalid memory access is handled. either use a shared_ptr to manage the memory or throw an exception stating the original memory was freed

Copy link
Contributor

Choose a reason for hiding this comment

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

+ other view operations if you want

Comment on lines +66 to +89
Tensor::Tensor(const std::vector<size_t> & shape, DataType dtype)
: shape_(shape)
, dtype_(dtype)
, is_view_(true)
{
calculate_strides();

size_t total_elements = std::accumulate(shape_.begin(), shape_.end(), 1UL, std::multiplies<size_t>());
byte_size_ = total_elements * get_dtype_size(dtype_);

allocate_memory();
}

Tensor::Tensor(void * data, const std::vector<size_t> & shape, DataType dtype)
: shape_(shape)
, dtype_(dtype)
, data_(data)
, is_view_(false)
{
calculate_strides();

size_t total_elements = std::accumulate(shape_.begin(), shape_.end(), 1UL, std::multiplies<size_t>());
byte_size_ = total_elements * get_dtype_size(dtype_);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe I'm misunderstanding but shouldn't this be swapped?
There is no memory ownership with the pointer so it would be a view (potentially switch to a shared_ptr to prevent access after free?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the tensor class actually doesn't store data inside it. It stores a pointer to the data. So what that means is that this tensor class is always a view no matter what, assuming you mean view in the literal sense that it only knows of the data's address and nothing else.

I think of view in a more high-level sense. As in if is_view_ == true then this tensor datastructure does not have the permissions to mutate the data. It can mutate the data if it isn't a view and thus the original assigned owner... does owner sound better then?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm thinking tensor_view in PyTorch which shares data with the base tensor

https://docs.pytorch.org/docs/stable/tensor_view.html

Here view still allows for data mutation (which I think we should allow too), but it is not the "owner" of the data in the sense that it refers to a chunk of memory allocated when creating the original tensor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

two things that im bending my mind around because of this:

  • to what extent do we need to support various tensor operations? the intent is not to reinvent pytorch, or numpy at that. rather it should do only what we need it to do which is to be a generic type to pass tensors to downstream backends
  • making this tensor class actually hold the tensor data is problematic, especially when it comes to different devices. Im currently looking into how we could "pluginize" the memory allocation so that different backends can figure out how they want to allocate the memory of the tensor

Copy link
Contributor

Choose a reason for hiding this comment

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

  • If we don't need to support tensor operations then it might make sense to try to create an Ort Tensor/Value directly
  • I don't think it needs to hold the data, a pointer to the data is fine. In that sense maybe all of our tensors are technically views then

Copy link
Contributor

@thomasonzhou thomasonzhou left a comment

Choose a reason for hiding this comment

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

+1

Can prune in the future if needed

@Edwardius Edwardius merged commit b14c6c1 into main Sep 20, 2025
2 checks passed
Edwardius added a commit that referenced this pull request Sep 20, 2025
* adding generic tensor type

* indentation of docstring

* no inline, and readme

* stuff

* updating package.xml

* addressing batching with images

* added 16 bit yuv

* is view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants