|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | + |
| 4 | +from . import performance_utils as utils |
| 5 | + |
| 6 | + |
| 7 | +class EmbeddingBenchmark(utils.GenericBenchmark2DOnly): |
| 8 | + def set_more_shapes(self): |
| 9 | + # TODO: add more shapes |
| 10 | + return None |
| 11 | + |
| 12 | + |
| 13 | +def embedding_input_fn(shape, dtype, device): |
| 14 | + num_embeddings, embedding_dim = shape |
| 15 | + indices = torch.randint(0, num_embeddings, (num_embeddings,), device=device) |
| 16 | + weight = torch.randn((num_embeddings, embedding_dim), device=device, dtype=dtype) |
| 17 | + yield {"input": indices, "weight": weight}, |
| 18 | + |
| 19 | + if utils.Config.bench_level == utils.BenchLevel.COMPREHENSIVE: |
| 20 | + indices_2d = torch.randint( |
| 21 | + 0, |
| 22 | + num_embeddings, |
| 23 | + (num_embeddings, num_embeddings), |
| 24 | + device=device, |
| 25 | + ) |
| 26 | + |
| 27 | + yield {"input": indices_2d, "weight": weight}, |
| 28 | + |
| 29 | + |
| 30 | +def embedding_backward_input_fn(shape, dtype, device): |
| 31 | + for forward_args in embedding_input_fn(shape, dtype, device): |
| 32 | + input = forward_args[0]["input"] |
| 33 | + weight = forward_args[0]["weight"] |
| 34 | + |
| 35 | + weight.requires_grad_(True) |
| 36 | + # import pudb; pudb.set_trace() |
| 37 | + # output = torch.nn.functional.embedding(input, weight) |
| 38 | + # grad_output = torch.randn_like(output) |
| 39 | + yield input, weight |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.embedding |
| 43 | +def test_embedding(): |
| 44 | + # Note(Zhengzekang): triton do not support bfloat16 atomic add which is used in embedding grad. |
| 45 | + bench = EmbeddingBenchmark( |
| 46 | + input_fn=embedding_input_fn, |
| 47 | + op_name="embedding", |
| 48 | + torch_op=torch.nn.functional.embedding, |
| 49 | + dtypes=[ |
| 50 | + torch.float32, |
| 51 | + torch.float16, |
| 52 | + ], |
| 53 | + ) |
| 54 | + bench.run() |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.embedding_backward |
| 58 | +def test_embedding_backward(): |
| 59 | + # Note(Zhengzekang): triton do not support bfloat16 atomic add which is used in embedding grad. |
| 60 | + bench = EmbeddingBenchmark( |
| 61 | + input_fn=embedding_backward_input_fn, |
| 62 | + op_name="embedding_backward", |
| 63 | + torch_op=torch.nn.functional.embedding, |
| 64 | + dtypes=[ |
| 65 | + torch.float32, |
| 66 | + torch.float16, |
| 67 | + ], |
| 68 | + is_backward=True, |
| 69 | + ) |
| 70 | + bench.run() |
0 commit comments