fuse_onehot_matmul_to_gather: normalize one_hot's axis against its output rank - #2789
Open
LeSingh1 wants to merge 1 commit into
Open
fuse_onehot_matmul_to_gather: normalize one_hot's axis against its output rank#2789LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
…tput rank
one_hot's axis is relative to its *output* rank, which is rank(indices) + 1
(one_hot.type_inference accepts axis in [-rank-1, rank]). The pass normalizes a
non-negative axis by subtracting rank(indices), which is one too few. As a
result an axis that is one before the last normalizes to -1 and the pattern
matches, even though matmul does not contract over the one-hot dimension there:
@mb.program(input_specs=[mb.TensorSpec(shape=(4,), dtype=types.int32)])
def prog(indices):
oh = mb.one_hot(indices=indices, one_hot_vector_size=5, axis=0,
on_value=1.0, off_value=0.0) # (5, 4)
return mb.matmul(x=oh, y=np.arange(12).reshape(4, 3).astype(np.float32))
['one_hot', 'matmul'] -> ['gather'], output shape (5, 3) -> (4, 3)
The original computes out[d][m] = sum over the i with indices[i] == d of
W[i][m]; the rewritten program computes out[i][m] = W[indices[i]][m]. Also
reproduces with rank-2 indices and axis=1.
The same off-by-one makes the pass miss the case it is meant to handle when the
last axis is spelled non-negatively (axis == rank(indices)), which now fuses.
common::fuse_onehot_matmul_to_gather is in the default pipeline.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
one_hot'saxisis relative to its output rank, which isrank(indices) + 1—one_hot.type_inferenceacceptsaxisin[-rank-1, rank].fuse_onehot_matmul_to_gathernormalizes a non-negative axis by subtractingrank(indices), which is one too few:So
axis == rank(indices) - 1— a dimension that is not last — normalizes to-1and the pattern matches.matmuldoes not contract over the one-hot dimension in that case, so rewriting togather(W, indices, axis=0)is not valid:The original computes
out[d][m] = sum over the i with indices[i] == d of W[i][m]; the rewritten program computesout[i][m] = W[indices[i]][m]— a different function, and a different output shape. It also reproduces with rank-2 indices andaxis=1((2,4,5)becomes(2,3,5)).The same off-by-one makes the pass miss the case it is meant to handle whenever the last axis is written non-negatively (
axis == rank(indices)), which is a legal spelling ofaxis=-1.common::fuse_onehot_matmul_to_gatheris in the default pipeline (pass_pipeline.py).Fix
Normalize against
rank(indices) + 1, the rankaxisactually refers to.Tests
In
TestFuseOnehotMatmulToGather:test_no_fusion_when_onehot_axis_is_not_last[rank=1,2,3]— the one-hot dimension is second to last; the pass must leave the program alone. Fails onmain.test_fuse_onehot_matmul_to_gather_non_negative_axis[rank=1,2,3]— the last axis written asaxis == rank(indices); the pass must fuse. Fails onmain(the fusion is skipped).The existing
test_fuse_onehot_matmul_to_gatheronly usesaxis=-1and is untouched. I ran the whole class before and after the change; the pre-existing failures are identical (this environment cannot load CoreML.framework, so theassert_model_is_validpredictions fail there either way — the graph-structure assertions preceding them all run and pass).