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

Allow int data-type for Embedding indices. #6718

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions test/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,22 @@ def test_emb_bf16(self):
emb_out = emb(index)
assert emb_out.dtype == torch.bfloat16

def test_embedding_int_indices(self):
model = torch.nn.Embedding(1024, 10)

# 1 and 2-dimensional tensors.
# They have different execution paths.
for shape in ((5,), (2, 5)):

def test_on_device(device):
m = copy.deepcopy(model).to(device)
index = torch.ones(shape, dtype=torch.int, device=device)
return m(index)

out = test_on_device("cpu")
out_x = test_on_device(xm.xla_device())
self.assertEqual(out, out_x.cpu())

def test_transpose_1d(self):

def test_fn(t1):
Expand Down
2 changes: 1 addition & 1 deletion torch_xla/csrc/tensor_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ XLATensorPtr EmbeddingDenseBackward(const XLATensorPtr& grad_output,
XLATensorPtr Embedding(const XLATensorPtr& weight,
const XLATensorPtr& indices) {
XLA_CHECK_EQ(weight->shape().get().rank(), 2);
XLA_CHECK_EQ(indices->dtype(), at::ScalarType::Long);
XLA_CHECK(indices->dtype() == at::kLong || indices->dtype() == at::kInt);

if (indices->shape().get().rank() == 1) {
return tensor_methods::index_select(weight, 0, indices);
Expand Down
Loading