Skip to content

Make updated chainable to write along several axes - #179

Merged
marcelluethi merged 3 commits into
dimwit-dev:mainfrom
marcelluethi:updated-api
Oct 27, 2026
Merged

marcelluethi merged 3 commits into
dimwit-dev:mainfrom
marcelluethi:updated-api

Conversation

@marcelluethi

Copy link
Copy Markdown
Contributor

So far, updated writes along one axis and directly returns the updated tensor, so writing along several axes at once, e.g. a single entry, needs a manual read, modify and write back.

updated now returns a TensorUpdate. Applied to a value, it returns the updated tensor as before, so existing code is unchanged. Its updated method takes a further selector, which allows chaining updates like chained select:

val t : Tensor[A, B, Float32] = ???
t.updated(Axis[A].at(1)).updated(Axis[B].at(2))(3f)

updates the entry at (1, 2).

In the future, TensorUpdate could support other operations besides setting the values, such as adding them or taking minimums, like JAX's x.at[...].

So far, updated writes along one axis and directly returns the updated
tensor, so writing along several axes at once, e.g. a single entry,
needs a manual read, modify and write back.

updated now returns a TensorUpdate. Applied to a value, it returns the
updated tensor as before, so existing code is unchanged. Its updated
method takes a further selector, which allows chaining updates like
chained select:

  t.updated(Axis[A].at(1)).updated(Axis[B].at(2))(3f)

updates the entry at (1, 2). Each step reads the selection and writes
it back, reusing select and updated.

In the future, TensorUpdate could support other operations besides
setting the values, such as adding them or taking minimums, like
JAX's x.at[...].
@benikm91

Copy link
Copy Markdown
Collaborator

I don't like how it reads t.updated(Axis[A].at(1)).updated(Axis[B].at(2))(3f). Having two updated reads like we do set two values. If we need this now I suggest bringing the n axis method back (in correct form): t.updated(Axis[A].at(1), Axis[B].at(2))(3f). We had this syntax before but it was partly buggy so we removed it.

Another approach would be renaming, maybe t.view(Axis[A].at(1)).view(Axis[B].at(2)).updated(3f)...

@marcelluethi

Copy link
Copy Markdown
Contributor Author

I don't like how it reads t.updated(Axis[A].at(1)).updated(Axis[B].at(2))(3f). Having two updated reads like we do set two values. If we need this now I suggest bringing the n axis method back (in correct form): t.updated(Axis[A].at(1), Axis[B].at(2))(3f). We had this syntax before but it was partly buggy so we removed it.

Another approach would be renaming, maybe t.view(Axis[A].at(1)).view(Axis[B].at(2)).updated(3f)...

I agree that it is not optimal. view on the other hand is worse, as it suggests that it is a view, which it is not. But if we rename it to focus it would read ok:

t.focus(Axis[A].at(1)).focus(Axis[B].at(2)).updated(3f)

and have as a shorthand

t.updated(3f)

the idea could still work. It would then be a lens like structure.

@benikm91

Copy link
Copy Markdown
Collaborator

.focus feels okay to me, surely better than updated and view.

Instead of having chained update, this commit introduces the method focus, which, like a lens, focuses on some parts of the tensor and eventually makes it possible to update, read, modify, ... the parts on which the focus was set.
@marcelluethi

Copy link
Copy Markdown
Contributor Author

The latest commit renames update to focus and incorporates what has been discussed.

Changing the verb from updated to focus points to a more general mechanism than chained updated calls. It becomes natural to add methods like get, add, updatedWith on the focused parts of the tensors.
select(Axis[L]) and updated(Axis[L]) become special cases of the more general mechanism, for the cases where only one selected is used.

@benikm91 benikm91 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like it :)

@marcelluethi
marcelluethi merged commit 7c3c5ab into dimwit-dev:main Oct 27, 2026
1 check passed
benikm91 pushed a commit that referenced this pull request Oct 28, 2026
Make updated chainable to write along several axes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants