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

[BUG] Tensor reshape only compiles when inlined #3430

Open
hylkedonker opened this issue Aug 29, 2024 · 3 comments
Open

[BUG] Tensor reshape only compiles when inlined #3430

hylkedonker opened this issue Aug 29, 2024 · 3 comments
Labels
bug Something isn't working mojo Issues that are related to mojo mojo-repo Tag all issues with this label tensor

Comments

@hylkedonker
Copy link

hylkedonker commented Aug 29, 2024

Bug description

It looks like it is only possible to reshape a tensor immediately following it's definition.

Steps to reproduce

The following works as expected, when inlining all statements one after another:

from tensor import Tensor, TensorShape

def main():
    var a = Tensor[DType.float64](2, 1.0)
    var a_expanded_shape = TensorShape(1, 2)
    var a_reshaped = a.reshape(a_expanded_shape)

If the reshape is, however, placed inside a different function than the code no longer compiles:

from tensor import Tensor, TensorShape

fn my_reshape(a: Tensor[DType.float64]):
    var a_expanded_shape = TensorShape(1, 2)
    var a_reshaped = a.reshape(a_expanded_shape)

def main():
    var a = Tensor[DType.float64](2, 1.0)
    my_reshape(a)

Which gives the compilation error:

error: invalid call to 'reshape': invalid use of mutating method on rvalue of type 'Tensor[float64]'
var a_reshaped = a.reshape(a_expanded_shape)

Am I missing something obvious?

Reading the Tensor.reshape docs, I am expecting a.reshape to return a copy. So no mutations on the function's argument a, right?

Thanks in advance,

Hylke

System information

- Ubuntu 24.04
- `mojo --version`: mojo 2024.8.2805 (fd3bceba)
- `modular -v`: modular 0.9.2 (b3079bd5)
@hylkedonker hylkedonker added bug Something isn't working mojo-repo Tag all issues with this label labels Aug 29, 2024
@DimiM99
Copy link

DimiM99 commented Oct 18, 2024

I'm not sure but this might occur since the fn functions borrowed args (which is default) as immutable references. copy constructor and destructor are not invoked for a borrow hence you can't return a copy

@soraros
Copy link
Contributor

soraros commented Oct 18, 2024

from tensor import Tensor, TensorShape

fn my_reshape(inout a: Tensor[DType.float64]) raises:
    a_expanded_shape = TensorShape(1, 2)
    _ = a.reshape(a_expanded_shape)

def main():
    a = Tensor[DType.float64](2, 1.0)
    my_reshape(a)

@DimiM99
Copy link

DimiM99 commented Oct 18, 2024

from tensor import Tensor, TensorShape

fn my_reshape(inout a: Tensor[DType.float64]) raises:
    a_expanded_shape = TensorShape(1, 2)
    _ = a.reshape(a_expanded_shape)

def main():
    a = Tensor[DType.float64](2, 1.0)
    my_reshape(a)

you can also use a.ireshape(a_expanded_shape) instead of _ = a.reshape(a_expanded_shape) that way you'll be modifying the original value

@JoeLoser JoeLoser added the tensor label Nov 21, 2024 — with Linear
@ematejska ematejska added mojo-playground mojo Issues that are related to mojo labels Feb 26, 2025 — with Linear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mojo Issues that are related to mojo mojo-repo Tag all issues with this label tensor
Projects
None yet
Development

No branches or pull requests

5 participants