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

[Feature Request] Slice tensors using "standard" indexing #2638

Open
1 task done
ss32 opened this issue May 12, 2024 · 1 comment
Open
1 task done

[Feature Request] Slice tensors using "standard" indexing #2638

ss32 opened this issue May 12, 2024 · 1 comment
Labels
enhancement New feature or request mojo-python-interop mojo-repo Tag all issues with this label mojo-stdlib Tag for issues related to standard library

Comments

@ss32
Copy link

ss32 commented May 12, 2024

Review Mojo's priorities

What is your request?

Slicing tensors to assign new variables is not intuitive right now. Both Golang and Python use the same indexing and another modern language like Mojo should as well, especially if the aim is to lower the barrier to entry for new users.

package main
 
import (
 "fmt"
)
 
func main() {
 v := []int{1,2,3,4}
 x := v[0:2]
 y := v[2:4]
 fmt.Println("Vector v:", v)
 fmt.Println("Slice x:", x)
 fmt.Println("Slice y:", y)
}
import numpy as np
v = np.array([1,2,3,4])
x = v[0:2]
y = v[2:4]
print(f'Vector v: {v}')
print(f'Slice x: {x}')
print(f'Slice y: {y}')
 
## I think this is the easiest way? Even the official docs show a
## similar method to assign a numpy array from a Tensor
## calling numpy_array.itemset((col, row), tensor[col, row])
 
var t = Tensor[DType.float64](
    (3, 1),
    -1,
    0,
    1,
    0
)
var x: Tensor[DType.float64] = Tensor[DType.float64](2, 1)
var y: Tensor[DType.float64] = Tensor[DType.float64](2, 1)
for i in range(4):
    if i < 2:
        x[i] = t[i, 0]
    else:
        y[i] = t[i, 0]
print(x)
$ mojo run test.mojo
Tensor([[-1.0],
[0.0]], dtype=float64, shape=2x1)

What is your motivation for this change?

Following the examples and documentation I see syntax that is very nearly Python and I would expect the Tensor type to behave like tensors in Python as well.

Any other details?

A first cut could be a middle ground between what's possible now and how Golang and Python achieve this where the user still has to preallocate the memory but is able to assign using slice indices

var t = Tensor[DType.float64](
    (3, 1),
    -1,
    0,
    1,
    0
)
var x: Tensor[DType.float64] = Tensor[DType.float64](2, 1)
x = t[Index(0,0:2)]

A logical next iteration might relieve the user of having to call Index

var t = Tensor[DType.float64](
    (3, 1),
    -1,
    0,
    1,
    0
)
var x: Tensor[DType.float64] = Tensor[DType.float64](2, 1)
x = t[0,0:2]

The holy grail would be something akin to Python's default behavior, Golang's :=, and C++'s auto where the memory allocation is handled on the fly.

var t = Tensor[DType.float64](
    (3, 1),
    -1,
    0,
    1,
    0
)

x = t[0,0:2]
@ss32 ss32 added enhancement New feature or request mojo-repo Tag all issues with this label labels May 12, 2024
@JoeLoser JoeLoser added the mojo-stdlib Tag for issues related to standard library label May 17, 2024
@StandinKP
Copy link

Hey @JoeLoser any suggestion on how to proceed with this? I would like to start working on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request mojo-python-interop mojo-repo Tag all issues with this label mojo-stdlib Tag for issues related to standard library
Projects
None yet
Development

No branches or pull requests

4 participants