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

Type check accessors [], []= and add index_select for multiple values #653

Merged
merged 6 commits into from
May 28, 2024

Conversation

Vindaar
Copy link
Collaborator

@Vindaar Vindaar commented May 28, 2024

This finally makes the [], []= mostly "type safe". Just handing in any type and falling back to the compile time errors of the (mostly internal) procedures like index_select is a thing of the past now. If the argument is not allowed, we simply throw a (better) error message telling the user that the type is not valid.

On Nim devel we can also type check Tensor[T] (i.e. the inner type), because nowadays doing

  let validTensorTypes = [getTypeInst(Tensor[int])[1],
                          getTypeInst(Tensor[bool])[1]]
  for t in validTensorTypes:
    if sameType(typ, t): return true

works as expected. Nim issue #14021 nim-lang/Nim#14021 should in principle be fixed on devel (but not 2.0.4). Haven't check the Nim issue fully yet though.

This also adds support to not only index a tensor at multiple indices, but also assign multiple at the same time:

z2[[0, 2]] = [5.2, 1.2]
z2[[0, 2]] = @[5.2, 1.2]
z2[@[0, 2]] = @[5.2, 1.2]
z2[@[0, 2]] = toTensor [5.2, 1.2]
z2[toTensor [0, 2]] = [5.2, 1.2]
z2[toTensor [0, 2]] = @[5.2, 1.2]
z2[toTensor [0, 2]] = toTensor [5.2, 1.2]

are all valid now. Previously we could only read with multiple indices.

Note that multi dimensional slicing of arbitrary indices is still not supported.

To allow

```nim
t[[0, 2]] = [1, 2] # optionally seq or tensor
t[@[0, 2]] = [1, 2]
t[toTensor [0, 2]] = [1, 2]
```

filling multiple indices at same time from a array/seq/tensor.

Previously this only supported to set all indices given to the same value.
@AngelEzquerra
Copy link
Contributor

This is great, @Vindaar. Other than the weird git formatting on 3211c4e3211c4e, that we already discussed offline these changes (and this functionality!) look good to me!

@Vindaar Vindaar merged commit 549472f into mratsim:master May 28, 2024
6 checks passed
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