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

Make unary ops more efficient with non-contiguous inputs #192

Open
1 of 2 tasks
robertknight opened this issue May 20, 2024 · 0 comments
Open
1 of 2 tasks

Make unary ops more efficient with non-contiguous inputs #192

robertknight opened this issue May 20, 2024 · 0 comments
Labels
performance Issues that affect model inference or loading performance

Comments

@robertknight
Copy link
Owner

robertknight commented May 20, 2024

Unary operators (eg. sigmoid, tanh) are much less efficient with non-contiguous inputs. The problem is two-fold:

  • For SIMD-vectorized operators (eg. tanh), the fast path calls a SIMD function that applies the operator to the entire contiguous buffer at once. For non-contiguous inputs, it falls back to iterating over the input and applying the operator on one element at a time. Even worse, the fast path is parallel whereas the fallback is not
  • In the slow path for TensorBase::apply, it uses an iterator which is much less efficient than iterating over contiguous inputs. See also Replace all usage of TensorBase::broadcast_iter #189.

A better implementation would be something like:

  • Sort dimensions into maximally-contiguous order
  • If the size of the longest contiguous chunks is above a threshold, iterate over them and apply the operator
  • If the size is below the threshold, use nested loops instead of an iterator, ala. Replace all usage of TensorBase::broadcast_iter #189

Once this is done, copying activations in RNN operators (eg. GRU, LSTM) can be replaced with their in-place versions to reduce copying.

@robertknight robertknight added the performance Issues that affect model inference or loading performance label May 20, 2024
robertknight added a commit that referenced this issue May 20, 2024
This is a workaround needed because `tanh_in_place` is very slow with
non-contigous inputs. See #192.
robertknight added a commit that referenced this issue May 20, 2024
This is a workaround needed because `tanh_in_place` is very slow with
non-contigous inputs. See #192.
robertknight added a commit that referenced this issue May 20, 2024
This is a workaround until #192 is
solved more generally.
robertknight added a commit that referenced this issue May 20, 2024
This is a workaround until #192 is
solved more generally.
robertknight added a commit that referenced this issue May 31, 2024
Replace iterators with a pattern that uses a fixed number of nested loops.
The same approach was previously applied to binary and ternary ops.

Part of #192.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Issues that affect model inference or loading performance
Projects
None yet
Development

No branches or pull requests

1 participant