File: qatembedding_ops_test.py

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (61 lines) | stat: -rw-r--r-- 2,614 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import operator_benchmark as op_bench
import torch
import torch.ao.nn.qat as nnqat
import numpy
from pt import configs
from torch.ao.quantization import default_embedding_qat_qconfig
"""
Microbenchmarks for QAT Embedding + EmbeddingBag operators.
"""

class QATEmbeddingBagBenchmark(op_bench.TorchBenchmarkBase):
    def init(self, embeddingbags, dim, mode, input_size, offset, sparse, include_last_offset, device):
        qconfig = default_embedding_qat_qconfig
        self.embedding = nnqat.EmbeddingBag(
            num_embeddings=embeddingbags,
            embedding_dim=dim,
            mode=mode,
            include_last_offset=include_last_offset,
            sparse=sparse, device=device, qconfig=qconfig)
        numpy.random.seed((1 << 32) - 1)
        offsets = torch.LongTensor([offset], device=device)
        input = torch.tensor(numpy.random.randint(0, embeddingbags, input_size), device=device).long()
        self.inputs = {
            "input": input,
            "offset": torch.cat((offsets, torch.tensor([input.size(0)], dtype=torch.long)), 0)
        }
        self.set_module_name('qatEmbeddingBag')

    def forward(self, input, offset):
        return self.embedding(input, offset)

# Currently, EmbeddingBag QAT does not support sparse embeddings.
embeddingbag_short_dense_configs = [config for config in configs.embeddingbag_short_configs
                                    if {'sparse': True} not in config]

op_bench.generate_pt_test(embeddingbag_short_dense_configs, QATEmbeddingBagBenchmark)
op_bench.generate_pt_gradient_test(embeddingbag_short_dense_configs, QATEmbeddingBagBenchmark)

class QATEmbeddingBenchmark(op_bench.TorchBenchmarkBase):
    def init(self, num_embeddings, embedding_dim, input_size, device):
        qconfig = default_embedding_qat_qconfig
        self.embedding = nnqat.Embedding(
            num_embeddings=num_embeddings,
            embedding_dim=embedding_dim,
            qconfig=qconfig, device=device)
        self.embedding.qconfig = default_embedding_qat_qconfig
        numpy.random.seed((1 << 32) - 1)
        self.input = torch.tensor(numpy.random.randint(0, num_embeddings, input_size),
                                  device=device).long()
        self.inputs = {"input": self.input}
        self.set_module_name('qatEmbedding')

    def forward(self, input):
        return self.embedding(input)


op_bench.generate_pt_test(configs.embedding_short_configs, QATEmbeddingBenchmark)
op_bench.generate_pt_gradient_test(configs.embedding_short_configs, QATEmbeddingBenchmark)

if __name__ == "__main__":
    op_bench.benchmark_runner.main()