Skip to content

Immutable tensors #6960

@pygy

Description

@pygy

As proposed in this post.

Having immutable tensors would elegantly solve the temporary allocation problem that currently plagues vectorized code in Julia.

Evaluation would occur lazily in a devectorized fashion when mandatory, and the result would the be cached in the object, an immutable type whose "realized" field is a one slot array. That way you get the best of both worlds: immutability for operations and a writable field for the result.

I know that Stefan doesn't like memoization, but I think it could make sense in this case.

Beside transposition (#6837), addition, subtraction, scalar operations and maybe other ones could be trivially implemented without any temporary allocation.

Not sure if there's a way to make it fast, though. It may be possible to analyze the code flow (along with type analysis?) and prepare specialized code inlined at the evaluation point.

Proposed syntax:

matrix = [| 1, 2 |
          | 3, 4 |]

inline = [|1, 2| |3, 4|]

alternatively [|1, 2; 3, 4|]

threedee = [|| 1, 2 | | 5, 6 ||
            || 3, 4 | | 7, 8 ||]

xyplane = [|| 1, 2 ||
           || 3, 4 ||]

xzplane = [|| 1, 2 | | 5, 6||]

yzplane = [|| 1 | | 5 ||
           || 3 | | 7 ||]

alt3d = [|| 1, 2 ; 5, 6 ;; 3, 4 ; 7, 8 ||]

And let's go wild:

fourdee = [||| 1, 2 | | 5, 6 ||
            || 3, 4 | | 7, 8 |||

           ||| a, b | | e, f ||
            || c, d | | g, h |||]

Parsing that may not beautiful ;-)

Is there a language out there that allows to cleanly represent 4d data?


Edit:

I know that there's an inconsistency between the 2d representation and the other two:

For matrices, , is the x separator and | the y one.

In 3d and 4d, it's x => ,, y => ||, z => | and w => |||.

It allows to get a nice 2d view along these lines:

+----------z-------->
|  +--x-->
|  |
|  y
|  |
|  V
|
w
|
V

... rather than this which you would get with symbolically consistent separators.

+----------y-------->
|  +--x-->
|  |
|  z
|  |
|  V
|
w
|
V

It may take some time to get past the cognitive dissonance, but I think it's worth it for the visual result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    speculativeWhether the change will be implemented is speculative

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions