Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indices Operator #1735

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

McArthur-Alford
Copy link

Indices Operator for Int Tensors

Checklist

  • [ x ] Confirmed that run-checks all script has been executed.
  • [ x ] Made sure the book is up to date with changes in this PR.

Related Issues/PRs

None

Changes

Added a indices function for int tensors. This is similar to pytorches meshgrid, or numpys indices functions though with slightly different arrangement. For example, the output of Tensor::<B, 2, Int>::indices::<3>(Shape { dims: [2, 3] }, &device); would be:

[[[0, 0],  [0, 1],  [0, 2]],
 [[1, 0],  [1, 1],  [1, 2]]],

Testing

Added a super basic but functional test to make sure indices produces some expected typical outputs.

Copy link

codecov bot commented May 6, 2024

Codecov Report

Attention: Patch coverage is 96.87500% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 86.54%. Comparing base (7f94f4c) to head (2a9920a).
Report is 2 commits behind head on main.

Files Patch % Lines
crates/burn-tensor/src/tensor/api/int.rs 95.23% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1735   +/-   ##
=======================================
  Coverage   86.54%   86.54%           
=======================================
  Files         699      700    +1     
  Lines       83223    83255   +32     
=======================================
+ Hits        72025    72056   +31     
- Misses      11198    11199    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@laggui laggui left a comment

Choose a reason for hiding this comment

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

Thanks for working on this 🙂

Implementation looks good, some minor comments on form.

@@ -285,6 +285,7 @@ Those operations are only available for `Int` tensors.
| ------------------------------------------------ | ------------------------------------------------------- |
| `tensor.arange(5..10, device) ` | `tensor.arange(start=5, end=10, device=device)` |
| `tensor.arange_step(5..10, 2, device)` | `tensor.arange(start=5, end=10, step=2, device=device)` |
| `tensor.indices(shape, device)` | `torch.meshgrid(tensors)` |
Copy link
Member

Choose a reason for hiding this comment

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

Two notes:

  1. Usage should be Tensor::indices(shape, device) like Tensor::cat or Tensor::empty.
  2. Given the implementation here, there is no 1-to-1 equivalent for torch since meshgrid takes tensors as input and not the shape of the desired grid. In this case this is much closer to numpy.indices but the indexing is in cartesian space. The actual torch equivalent for the 2D example in your unit tests would be:
yv, xv = torch.meshgrid([torch.arange(2), torch.arange(2)], indexing='xy')
grid = torch.stack((xv, yv), 2)

So in this case, I'm not sure we would have to provide the comparison in the table.

Comment on lines +74 to +97
/// Produces an indices tensor for the given shape and device.
/// The resulting tensor contains coordinates corresponding to each element in the shape at dimension D.
///
/// # Arguments
///
/// * `shape` - The shape specifying the dimensions of the tensor.
/// * `device` - The device to create the tensor on.
///
/// # Panics
///
/// Panics if `D2` is not equal to `D+1`.
///
/// # Examples
///
/// ```rust
/// use burn_tensor::Int;
/// use burn_tensor::{backend::Backend, Shape, Tensor};
/// fn example<B: Backend>() {
/// let device = Default::default();
/// let result = Tensor::<B, 2, Int>::indices::<3>(Shape { dims: [2, 3] }, &device);
/// println!("{}", result);
/// }
/// ```
pub fn indices<const D2: usize>(shape: Shape<D>, device: &B::Device) -> Tensor<B, D2, Int> {
Copy link
Member

Choose a reason for hiding this comment

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

Usually indices will be produced to use with a matrix/tensor (i.e., indexing in i, j like np.indices) but I think the intention with this method is to produce a grid in cartesian space. With that in mind, I think I would make it a bit more explicit in the method's doc and rename the method to something like nd_grid, meshgrid or even more explicit cartesian_grid (also opened to suggestions).

Copy link
Member

Choose a reason for hiding this comment

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

cartesian_grid seems to be a good name!

Comment on lines +74 to +97
/// Produces an indices tensor for the given shape and device.
/// The resulting tensor contains coordinates corresponding to each element in the shape at dimension D.
///
/// # Arguments
///
/// * `shape` - The shape specifying the dimensions of the tensor.
/// * `device` - The device to create the tensor on.
///
/// # Panics
///
/// Panics if `D2` is not equal to `D+1`.
///
/// # Examples
///
/// ```rust
/// use burn_tensor::Int;
/// use burn_tensor::{backend::Backend, Shape, Tensor};
/// fn example<B: Backend>() {
/// let device = Default::default();
/// let result = Tensor::<B, 2, Int>::indices::<3>(Shape { dims: [2, 3] }, &device);
/// println!("{}", result);
/// }
/// ```
pub fn indices<const D2: usize>(shape: Shape<D>, device: &B::Device) -> Tensor<B, D2, Int> {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of forcing users to pass a Shape, we could have the api like this:

pub fn indices<S: Into<Shape<D>>, const D2: usize,>(shape: S, device: &B::Device) -> Tensor<B, D2, Int>

So an array could be provided by the user and it would work.

@laggui
Copy link
Member

laggui commented May 7, 2024

@nathanielsimard what do you think about the naming for this method? (see my comments for possible suggestions)

Comment on lines +74 to +97
/// Produces an indices tensor for the given shape and device.
/// The resulting tensor contains coordinates corresponding to each element in the shape at dimension D.
///
/// # Arguments
///
/// * `shape` - The shape specifying the dimensions of the tensor.
/// * `device` - The device to create the tensor on.
///
/// # Panics
///
/// Panics if `D2` is not equal to `D+1`.
///
/// # Examples
///
/// ```rust
/// use burn_tensor::Int;
/// use burn_tensor::{backend::Backend, Shape, Tensor};
/// fn example<B: Backend>() {
/// let device = Default::default();
/// let result = Tensor::<B, 2, Int>::indices::<3>(Shape { dims: [2, 3] }, &device);
/// println!("{}", result);
/// }
/// ```
pub fn indices<const D2: usize>(shape: Shape<D>, device: &B::Device) -> Tensor<B, D2, Int> {
Copy link
Member

Choose a reason for hiding this comment

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

cartesian_grid seems to be a good name!

/// println!("{}", result);
/// }
/// ```
pub fn indices<const D2: usize>(shape: Shape<D>, device: &B::Device) -> Tensor<B, D2, Int> {
Copy link
Member

Choose a reason for hiding this comment

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

I think we should move this method to the backend API so that backends can optimize it, since calling a lot of arange and repeat can be very expansive for big matrices. We should keep default implementation in the backend definition.

Copy link
Member

Choose a reason for hiding this comment

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

@McArthur-Alford if you're not sure what that means, see for example the narrow op. It is defined by a default implementation but also overridden by some backends.

Copy link
Author

Choose a reason for hiding this comment

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

cartesian_grid sounds good to me. Ill get started on moving it to a backend op.

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.

None yet

3 participants