[Relax] Allow softmax to work on a large tensor when dimension is not the last one #17720
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For large tensors with non-last dimension softmax, we transpose to move the softmax dimension to the end, apply softmax, and then transpose back to the original shape.
Since the issue here that using softmax on non-last dimension could cause python/tvm/dlight/gpu/general_reduction.py to create arrays that are too big for the GPU shared memory, I tried to address this TODO by making changes to general_reduction.py, without success. However, as I was experimenting, I added a suggested handling for the case where num_leading_s = 0 in general_reduction.py. I thought I might as well leave that in the PR.
cc: @MasterJH5574
Edit: we may not merge this at all because it's better to fix the reduction directly, and the fix in this PR may simply be extra overhead