tensor-sensor

Форк
0
/
test_jax.py 
90 строк · 2.9 Кб
1
"""
2
MIT License
3

4
Copyright (c) 2021 Terence Parr
5

6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
of this software and associated documentation files (the "Software"), to deal
8
in the Software without restriction, including without limitation the rights
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
copies of the Software, and to permit persons to whom the Software is
11
furnished to do so, subject to the following conditions:
12

13
The above copyright notice and this permission notice shall be included in all
14
copies or substantial portions of the Software.
15

16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
SOFTWARE.
23
"""
24
import jax.numpy as jnp
25
import numpy as np
26
import tsensor
27

28
def test_dot():
29
    size = 5000
30
    x = np.random.normal(size=(size, size)).astype(np.float32)
31
    y = np.random.normal(size=(5, 1)).astype(np.float32)
32

33
    msg = ""
34
    try:
35
        with tsensor.clarify():
36
            z = jnp.dot(x, y).block_until_ready()
37
    except TypeError as e:
38
        msg = e.args[0]
39

40
    expected = "Incompatible shapes for dot: got (5000, 5000) and (5, 1).\n"+\
41
               "Cause: jnp.dot(x, y) tensor arg x w/shape (5000, 5000), arg y w/shape (5, 1)"
42
    assert msg==expected
43

44

45
def test_scalar_arg():
46
    size = 5000
47
    x = np.random.normal(size=(size, size)).astype(np.float32)
48

49
    msg = ""
50
    try:
51
        with tsensor.clarify():
52
            z = jnp.dot(x, "foo")
53
    except TypeError as e:
54
        msg = e.args[0]
55

56
    expected = 'data type \'foo\' not understood\n'+\
57
               'Cause: jnp.dot(x, "foo") tensor arg x w/shape (5000, 5000)'
58
    assert msg==expected
59

60

61
def test_mmul():
62
    W = jnp.array([[1, 2], [3, 4]])
63
    b = jnp.array([9, 10, 11])
64

65
    msg = ""
66
    try:
67
        with tsensor.clarify():
68
            y = W @ b
69
    except TypeError as e:
70
        msg = e.args[0]
71

72
    expected = "dot_general requires contracting dimensions to have the same shape, got [2] and [3].\n"+\
73
               "Cause: @ on tensor operand W w/shape (2, 2) and operand b w/shape (3,)"
74
    assert msg==expected
75

76

77
def test_fft():
78
    "Test a library function that doesn't have a shape related message in the exception."
79
    x = np.exp(2j * np.pi * np.arange(8) / 8)
80
    msg = ""
81
    try:
82
        with tsensor.clarify():
83
            y = jnp.fft.fft(x, norm="something weird")
84
    except BaseException as e:
85
        msg = e.args[0]
86
        print(msg)
87

88
    expected = 'jax.numpy.fft.fft only supports norm=None, got something weird\n'+\
89
               'Cause: jnp.fft.fft(x, norm="something weird") tensor arg x w/shape (8,)'
90
    assert msg==expected
91

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.