Pad to next power of 2 for hl.specialize'ed shape value used in device tensor creation #804
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.
We want to support usage pattern like:
Particularly
grad_w_m += x[mb, :].to(torch.float32).sum(0)
is difficult to support, because the LHSgrad_w_m
is of shapex.size(1)
which can be a non-power-of-2 value (e.g. 56), while the RHSx[mb, :].to(torch.float32).sum(0)
is a the next power-of-2 value ofx.size(1)
(i.e. 64), resulting in a shape mismatch.I explored many solutions, and the cleanest / simplest way is to bump
n
to next power-of-2 when it's used as device tensor shape value (e.g. when used intorch.zeros
in device loop).Alternative is we ask users to explicitly wrap
weight_shape
withhelion.next_power_of_2(...)
, likegrad_w_m = x.new_zeros(helion.next_power_of_2(weight_shape), dtype=torch.float32)
, but I feel that the UX friction is quite high if we do that.Fixes #737.
Fixes #741.
cc. @v0i0 @mengluy0125