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)