Slicing support for multi-dimensional collections #7935
Replies: 4 comments 45 replies
-
This likely requires expansion to the |
Beta Was this translation helpful? Give feedback.
-
Python also has a value denoted '...' which is a wildcard over dimensions and allows you to write code that is generic over the number of dimensions. It may be most useful for variable-rank types, but could also be used for something like: tensor[...] = Tensor<int>.Ones(3); where it would overwrite the contents of 'tensor', rather than modify the variable 'tensor' |
Beta Was this translation helpful? Give feedback.
-
Question. Why is tensor[:, 2:, :3, 5:10] # gets a view of the sub-tensor scoped to the specified dimensions that much better than: tensor[.., 2.., ..3, 5..10]; Is it just to ape python? That's not hugely compelling to me. With the C# 13 work to support params collections, we should be able to have: public Tensor<T> this(params ReadOnlySpan<Range> ranges); so you would literally be able to write the latter and have it work. |
Beta Was this translation helpful? Give feedback.
-
Note that this statement in Eirik's initial post: tensor[0, ..] = Tensor<int>.Ones(3); would require an implicit conversion from integer to System.Range, so that |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In #7913 (comment) I proposed adding support for multi-dimensional collection literals. This would be an important addition facilitating usage of the new tensor library that we'll be working on for .NET 9.
One feature request that dovetails with the above is support for multi-dimensional slicing. In pytorch, it is possible to create slices using the following syntax:
Slices can also be assigned to as the following example illustrates:
Transcribing the above to C# slicing syntax might look as follows
Sketch Design
A strawman implementation of multi-dimensional slicing on collection types of fixed rank might look as follows
Whereas for variable rank types such as tensors it might look as follows:
Given the above, the previous expressions should desugar into
Thoughts? Is this something we could pursue for the next C#?
Bonus: The ellipsis operator
Python defines
...
as a special constant that among other things can be passed as a parameter in indexer methods. The codecan be used as a synonym to writing
It would be nice if we could achieve something similar in C# slicers, however it should be noted that this cannot be implemented as syntactic sugar for the case of tensors -- the rank of the tensor cannot be known at compile time. Most likely it would require runtime representation (potentially as an augmentation of the
System.Range
type?) which the slicer can interpret accordingly.Beta Was this translation helpful? Give feedback.
All reactions