Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
#
# Test reference implementation and model for ONNX Runtime conrtib op torch_embedding
 
import unittest
 
import numpy as np
import onnx
from onnx_contrib_ops_helper import expect
 
 
def torch_embedding_reference_implementation(weight, indices, padding_idx=None, scale=False):
    return np.take(weight, indices, axis=0)
 
 
class ONNXReferenceImplementationTest(unittest.TestCase):
    def test_torch_embedding(self):
        node = onnx.helper.make_node(
            "TorchEmbedding",
            inputs=["w", "x"],
            outputs=["y"],
            domain="com.microsoft",
        )
 
        x = np.random.randn(2, 4).astype(np.int64)
        w = np.random.randn(10, 3).astype(np.float32)
        y = torch_embedding_reference_implementation(w, x)
        expect(node, inputs=[w, x], outputs=[y], name="test_torch_embedding")
 
    def test_torch_embedding_long(self):
        node = onnx.helper.make_node(
            "TorchEmbedding",
            inputs=["w", "x"],
            outputs=["y"],
            domain="com.microsoft",
        )
 
        x = np.random.randn(2, 4).astype(np.int64)
        w = np.random.randn(10, 3).astype(np.int64)
        y = torch_embedding_reference_implementation(w, x)
        expect(node, inputs=[w, x], outputs=[y], name="test_torch_embedding_long")
 
    def test_torch_embedding_zero_dim(self):
        node = onnx.helper.make_node(
            "TorchEmbedding",
            inputs=["w", "x"],
            outputs=["y"],
            domain="com.microsoft",
        )
 
        x = np.random.randn(0, 4).astype(np.int64)
        w = np.random.randn(10, 3).astype(np.float32)
        y = torch_embedding_reference_implementation(w, x)
        expect(node, inputs=[w, x], outputs=[y], name="test_torch_embedding_zero_dim")
 
    def test_torch_embedding_padding_idx(self):
        node = onnx.helper.make_node(
            "TorchEmbedding",
            inputs=["w", "x", "padding_idx"],
            outputs=["y"],
            domain="com.microsoft",
        )
 
        x = np.random.randn(3, 4).astype(np.int64)
        w = np.random.randn(10, 3).astype(np.float32)
        padding_idx = np.random.randint(3, size=1).astype(np.int64)
        y = torch_embedding_reference_implementation(w, x, padding_idx)
        expect(
            node,
            inputs=[w, x, padding_idx],
            outputs=[y],
            name="test_torch_embedding_padding_idx",
        )
 
    def test_torch_embedding_scale_grad_by_freq(self):
        node = onnx.helper.make_node(
            "TorchEmbedding",
            inputs=["w", "x", "padding_idx", "scale"],
            outputs=["y"],
            domain="com.microsoft",
        )
 
        x = np.random.randn(3, 4).astype(np.int64)
        w = np.random.randn(10, 3).astype(np.float32)
        padding_idx = np.random.randint(3, size=1).astype(np.int64)
        scale = np.array([1]).astype(bool)
        y = torch_embedding_reference_implementation(w, x, padding_idx, scale)
        expect(
            node,
            inputs=[w, x, padding_idx, scale],
            outputs=[y],
            name="test_torch_embedding_scale_grad_by_freq",
        )
 
 
if __name__ == "__main__":
    unittest.main(module=__name__, buffer=True)