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

fix embedding_backward_dense decomp with broadcasting #95499

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions test/dynamo/test_repros.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,25 @@ def f(x):

f(torch.ones(2, device="cuda", dtype=torch.float64))

def test_embedding_backward_broadcasting_decomp(self):
def f(grad_output, indices):
num_weights = 10
padding_idx = 1
scale_grad_by_freq = True
return torch.ops.aten.embedding_dense_backward(
grad_output, indices, num_weights, padding_idx, scale_grad_by_freq
)

f_compiled = torch.compile(f, backend="aot_eager")

grad_output = torch.ones(2, 4, 3, dtype=torch.float16)
indices = torch.ones(2, 4, dtype=torch.int64)

out_ref = f(grad_output, indices)
out_test = f_compiled(grad_output, indices)

self.assertEqual(out_ref, out_test)

def test_reformer_eval(self):
with torch.no_grad():
cnt = self._reformer(nopython=True)
Expand Down
2 changes: 1 addition & 1 deletion torch/_decomp/decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1071,7 +1071,7 @@ def embedding_dense_backward(
ones = torch.ones_like(indices)
counts = counts.index_put([indices], ones, accumulate=True)
grad_weights_scale = counts[indices]
grad_output = grad_output / grad_weights_scale.unsqueeze(1)
grad_output = grad_output / grad_weights_scale.unsqueeze(-1)

mask = _unsqueeze_to_dim(indices == padding_idx, grad_output.ndim)
grad = grad_output.masked_fill(mask, 0)
Expand Down