-
Notifications
You must be signed in to change notification settings - Fork 562
Add Experimental limited sparse embedding bag #8905
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
Open
amjames
wants to merge
1
commit into
master
Choose a base branch
from
amjames/sparse_embedding_bag
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -456,3 +456,5 @@ autograd: | |
| - max_pool3d | ||
| - native_layer_norm | ||
| - native_group_norm | ||
| - sparse_mask | ||
| - coalesce | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import itertools | ||
| import random | ||
| import unittest | ||
| import sys | ||
| from itertools import product | ||
|
|
||
| import torch | ||
| import torch_xla | ||
| import torch_xla.core.xla_model as xm | ||
| import torch.nn as nn | ||
| import torch.nn.functional as F | ||
| from torch.optim.sparse_adam import SparseAdam | ||
| from torch.testing._internal.common_utils import TestCase | ||
|
|
||
| from absl.testing import parameterized | ||
| from torch_xla.experimental.sparse import Embedding, EmbeddingBag, embedding as xla_embedding, embedding_bag as xla_embedding_bag, SparseCOOTensor | ||
|
|
||
| index_dtypes = {torch.int32, torch.int64} | ||
| float_dtypes = {torch.float32, torch.float16, torch.bfloat16} | ||
|
|
||
| xla_device = torch_xla.device() | ||
|
|
||
|
|
||
| class TestEmbedding(TestCase): | ||
|
|
||
| def test_embedding_sparse_basic(self): | ||
| embedding = Embedding(10, 20, sparse=True, device=xla_device) | ||
| input = torch.tensor([[0, 2, 4, 5], [4, 3, 0, 9]], | ||
| dtype=torch.long, | ||
| device=xla_device) | ||
| output = embedding(input) | ||
| output.max().backward() | ||
| self.assertTrue(embedding.weight.grad.is_sparse) | ||
| self.assertEqual(embedding.weight.grad.shape, embedding.weight.shape) | ||
|
|
||
| def test_embedding_functional(self): | ||
| torch_input = torch.tensor([[0, 2, 4, 5], [4, 3, 0, 9]], dtype=torch.long) | ||
| torch_weights = torch.randn(10, 20, requires_grad=True) | ||
| xla_input = torch_input.to(xla_device) | ||
| xla_weights = torch_weights.detach().to(xla_device).requires_grad_(True) | ||
| torch_out = F.embedding(torch_input, torch_weights, sparse=True) | ||
| xla_out = xla_embedding(xla_input, xla_weights, sparse=True) | ||
| self.assertEqual(xla_out, torch_out) | ||
| torch_out.max().backward() | ||
| xla_out.max().backward() | ||
| self.assertTrue(xla_weights.grad.is_sparse) | ||
| self.assertEqual(xla_weights.grad, torch_weights.grad) | ||
|
|
||
| def test_embedding_optimizer(self): | ||
| context_size = 5 | ||
| # We will use Shakespeare Sonnet 2, first few lines | ||
| test_sentence = """When forty winters shall besiege thy brow, | ||
| And dig deep trenches in thy beauty's field, | ||
| Thy youth's proud livery so gazed on now, | ||
| Will be a totter'd weed of small worth held:""".split() | ||
| ngrams = [([test_sentence[i - j - 1] | ||
| for j in range(context_size)], test_sentence[i]) | ||
| for i in range(context_size, len(test_sentence))] | ||
| vocab = set(test_sentence) | ||
| word_to_ix = {word: i for i, word in enumerate(vocab)} | ||
| vocab_size = len(vocab) | ||
|
|
||
| model = Embedding(vocab_size, context_size, sparse=True).to(xla_device) | ||
| cloned_weight = model.weight.clone().detach() | ||
|
|
||
| optimizer = SparseAdam(model.parameters(), lr=0.9) | ||
| optimizer.state | ||
| total_loss = 0 | ||
| for _ in range(5): | ||
| for context, _ in ngrams: | ||
| context_idxs = torch.tensor([word_to_ix[w] for w in context], | ||
| dtype=torch.long).to(xla_device) | ||
| model.zero_grad() | ||
| log_probs = model(context_idxs) | ||
| loss = log_probs.sum() | ||
| loss.backward() | ||
| optimizer.step() | ||
| total_loss += loss.detach().item() | ||
| xm.mark_step() | ||
| self.assertNotEqual(total_loss, 0) | ||
| self.assertNotEqual(cloned_weight, model.weight) | ||
|
|
||
|
|
||
| class TestEmbeddingBag(TestCase): | ||
|
|
||
| def test_embedding_bag_sparse_basic(self): | ||
| embedding = EmbeddingBag(10, 20, sparse=True, device=xla_device) | ||
| input = torch.tensor([[0, 2, 4, 5], [4, 3, 0, 9]], | ||
| dtype=torch.long, | ||
| device=xla_device) | ||
| output = embedding(input) | ||
| output.max().backward() | ||
| self.assertTrue(embedding.weight.grad.is_sparse) | ||
| self.assertEqual(embedding.weight.grad.shape, embedding.weight.shape) | ||
|
|
||
| def test_embedding_bag_functional(self): | ||
| torch_input = torch.tensor([[0, 2, 4, 5], [4, 3, 0, 9]], dtype=torch.long) | ||
| torch_weights = torch.randn(10, 20, requires_grad=True) | ||
| xla_input = torch_input.to(xla_device) | ||
| xla_weights = torch_weights.detach().to(xla_device).requires_grad_(True) | ||
| torch_out = F.embedding_bag(torch_input, torch_weights, sparse=True) | ||
| xla_out = xla_embedding_bag(xla_input, xla_weights, sparse=True) | ||
| self.assertEqual(xla_out, torch_out) | ||
| torch_out.max().backward() | ||
| xla_out.max().backward() | ||
| self.assertTrue(xla_weights.grad.is_sparse) | ||
| self.assertEqual(xla_weights.grad, torch_weights.grad) | ||
|
|
||
| def test_embedding_bag_optimizer(self): | ||
| context_size = 10 | ||
| embedding_dim = 5 | ||
| # We will use Shakespeare Sonnet 2, first few lines | ||
| test_sentence = """When forty winters shall besiege thy brow, | ||
| And dig deep trenches in thy beauty's field, | ||
| Thy youth's proud livery so gazed on now, | ||
| Will be a totter'd weed of small worth held:""".split() | ||
|
|
||
| ngrams = [([test_sentence[i - j - 1] | ||
| for j in range(context_size)], test_sentence[i]) | ||
| for i in range(context_size, len(test_sentence))] | ||
| vocab = set(test_sentence) | ||
| word_to_ix = {word: i for i, word in enumerate(vocab)} | ||
| vocab_size = len(vocab) | ||
|
|
||
| model = EmbeddingBag(vocab_size, embedding_dim, sparse=True).to(xla_device) | ||
| cloned_weight = model.weight.clone().detach() | ||
|
|
||
| optimizer = SparseAdam(model.parameters(), lr=0.9) | ||
| total_loss = 0 | ||
| offsets = torch.tensor([0, 5], dtype=torch.int64, device=xla_device) | ||
| for _ in range(5): | ||
| for context, _ in ngrams: | ||
| context_idxs = torch.tensor([word_to_ix[w] for w in context], | ||
| dtype=torch.long).to(xla_device) | ||
| model.zero_grad() | ||
| log_probs = model(context_idxs, offsets) | ||
| loss = log_probs.sum() | ||
| loss.backward() | ||
| optimizer.step() | ||
| total_loss += loss.detach().item() | ||
| xm.mark_step() | ||
| self.assertNotEqual(total_loss, 0) | ||
| self.assertNotEqual(cloned_weight, model.weight) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| test = unittest.main() | ||
| sys.exit(0 if test.result.wasSuccessful() else 1) |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from .coo import SparseCOOTensor | ||
| from .embedding_bag import EmbeddingBag, embedding_bag, embedding, Embedding | ||
|
|
||
| __all__ = [ | ||
| "SparseCOOTensor", "EmbeddingBag", "embedding_bag", "Embedding", "embedding" | ||
| ] |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.