Skip to content

Commit

Permalink
Add at_mut template to enable masked_fill on slices (#613)
Browse files Browse the repository at this point in the history
* Add at_mut template to enable masked_fill on slices

This can be useful, for example, when assigning a value into a chain of slice operations which are usually considered immutable even if the original tensor is mutable. For example, the following code:

```nim
var x = arange(20).reshape([4, 3])
x[1..2, _][condition] = 100
```
will fail with a `a slice of an immutable tensor cannot be assigned to` error.
Using `at_mut` you can do:
```nim
x.at_mut(1..2, _)[condition] = 100
```
which works fine.

* Remove unsafe mut template
  • Loading branch information
AngelEzquerra authored Jan 14, 2024
1 parent 38ffb44 commit 297ce90
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/arraymancer/tensor/syntactic_sugar.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,29 @@ template at*[T](t: Tensor[T], args: varargs[untyped]): untyped =
## Usage:
## See the ``[]`` macro
t[args].squeeze

template at_mut*[T](t: var Tensor[T], args: varargs[untyped]): untyped =
## Slice a Tensor, collapse singleton dimension, returning a mutable slice of the input
##
## This can be useful, for example, when assigning a value into a chain
## of slice operations which are usually considered immutable even if
## the original tensor is mutable. For example, this lets you do:
##
## .. code:: nim
## var x = arange(12).reshape([4, 3])
## let condition = [[true, false, true], [true, false, true]].toTensor
## # The code `x[1..2, _][condition] = 1000` would fail with
## # a `a slice of an immutable tensor cannot be assigned to` error
## # Instead, using `at_mut` allows assignment to the slice
## x.at_mut(1..2, _)[condition] = 1000
##
## Input:
## - a Tensor
## - and:
## - specific coordinates (``varargs[int]``)
## - or a slice (cf. tutorial)
## Returns:
## - a mutable value or view of the Tensor corresponding to the slice
## Singleton dimension are collapsed
var mt = t[args].squeeze
mt
15 changes: 15 additions & 0 deletions tests/tensor/test_selectors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -378,5 +378,20 @@ proc main() =

check: checkered == expected

test "at_mut":
block:
var x = arange(12).reshape([4, 3])
# The code `x[1..2, _][condition] = 1000` would fail with
# a `a slice of an immutable tensor cannot be assigned to` error
# Instead, using `at_mut` allows assignment to the slice
let condition = [[true, false, true], [true, false, true]].toTensor
let expected = [[ 0, 1, 2],
[1000, 4, 1000],
[1000, 7, 1000],
[ 9, 10, 11]].toTensor
x.at_mut(1..2, _)[condition] = 1000
check: expected == x


main()
GC_fullCollect()

0 comments on commit 297ce90

Please sign in to comment.