test_funcs.py 10.8 KB
Newer Older
1 2
import pytest
import torch
3
from treevalue import TreeValue
4

5
import treetensor.tensor as ttorch
6 7 8 9 10 11


# noinspection DuplicatedCode
@pytest.mark.unittest
class TestTensorFuncs:
    def test_zeros(self):
12 13
        assert ttorch.all(ttorch.zeros((2, 3)) == torch.zeros(2, 3))
        assert ttorch.all(ttorch.zeros(TreeValue({
14 15 16 17 18
            'a': (2, 3),
            'b': (5, 6),
            'x': {
                'c': (2, 3, 4),
            }
19
        })) == ttorch.TreeTensor({
20 21 22 23 24
            'a': torch.zeros(2, 3),
            'b': torch.zeros(5, 6),
            'x': {
                'c': torch.zeros(2, 3, 4),
            }
25
        }))
26 27

    def test_zeros_like(self):
28 29
        assert ttorch.all(
            ttorch.zeros_like(torch.tensor([[1, 2, 3], [4, 5, 6]])) ==
HansBug's avatar
HansBug 已提交
30 31
            torch.tensor([[0, 0, 0], [0, 0, 0]]),
        )
32 33
        assert ttorch.all(
            ttorch.zeros_like(ttorch.TreeTensor({
34 35 36 37 38 39
                'a': torch.tensor([[1, 2, 3], [4, 5, 6]]),
                'b': torch.tensor([1, 2, 3, 4]),
                'x': {
                    'c': torch.tensor([5, 6, 7]),
                    'd': torch.tensor([[[8, 9]]]),
                }
40
            })) == ttorch.TreeTensor({
41 42 43 44 45 46 47
                'a': torch.tensor([[0, 0, 0], [0, 0, 0]]),
                'b': torch.tensor([0, 0, 0, 0]),
                'x': {
                    'c': torch.tensor([0, 0, 0]),
                    'd': torch.tensor([[[0, 0]]]),
                }
            })
48
        )
49 50

    def test_ones(self):
51 52
        assert ttorch.all(ttorch.ones((2, 3)) == torch.ones(2, 3))
        assert ttorch.all(ttorch.ones(TreeValue({
53 54 55 56 57
            'a': (2, 3),
            'b': (5, 6),
            'x': {
                'c': (2, 3, 4),
            }
58
        })) == ttorch.TreeTensor({
59 60 61 62 63
            'a': torch.ones(2, 3),
            'b': torch.ones(5, 6),
            'x': {
                'c': torch.ones(2, 3, 4),
            }
64
        }))
65 66

    def test_ones_like(self):
67 68
        assert ttorch.all(
            ttorch.ones_like(torch.tensor([[1, 2, 3], [4, 5, 6]])) ==
HansBug's avatar
HansBug 已提交
69 70
            torch.tensor([[1, 1, 1], [1, 1, 1]])
        )
71 72
        assert ttorch.all(
            ttorch.ones_like(ttorch.TreeTensor({
73 74 75 76 77 78
                'a': torch.tensor([[1, 2, 3], [4, 5, 6]]),
                'b': torch.tensor([1, 2, 3, 4]),
                'x': {
                    'c': torch.tensor([5, 6, 7]),
                    'd': torch.tensor([[[8, 9]]]),
                }
79
            })) == ttorch.TreeTensor({
80 81 82 83 84 85 86
                'a': torch.tensor([[1, 1, 1], [1, 1, 1]]),
                'b': torch.tensor([1, 1, 1, 1]),
                'x': {
                    'c': torch.tensor([1, 1, 1]),
                    'd': torch.tensor([[[1, 1]]]),
                }
            })
87
        )
HansBug's avatar
HansBug 已提交
88 89

    def test_randn(self):
90
        _target = ttorch.randn((200, 300))
HansBug's avatar
HansBug 已提交
91 92 93 94
        assert -0.02 <= _target.view(60000).mean().tolist() <= 0.02
        assert 0.98 <= _target.view(60000).std().tolist() <= 1.02
        assert _target.shape == torch.Size([200, 300])

95
        _target = ttorch.randn(TreeValue({
HansBug's avatar
HansBug 已提交
96 97 98 99 100
            'a': (2, 3),
            'b': (5, 6),
            'x': {
                'c': (2, 3, 4),
            }
101
        }))
102
        assert _target.shape == ttorch.TreeSize({
HansBug's avatar
HansBug 已提交
103 104 105 106 107 108 109 110
            'a': torch.Size([2, 3]),
            'b': torch.Size([5, 6]),
            'x': {
                'c': torch.Size([2, 3, 4]),
            }
        })

    def test_randn_like(self):
111
        _target = ttorch.randn_like(torch.ones(200, 300))
HansBug's avatar
HansBug 已提交
112 113 114 115
        assert -0.02 <= _target.view(60000).mean().tolist() <= 0.02
        assert 0.98 <= _target.view(60000).std().tolist() <= 1.02
        assert _target.shape == torch.Size([200, 300])

116
        _target = ttorch.randn_like(ttorch.TreeTensor({
HansBug's avatar
HansBug 已提交
117 118 119 120 121 122 123
            'a': torch.tensor([[1, 2, 3], [4, 5, 6]], dtype=torch.float32),
            'b': torch.tensor([1, 2, 3, 4], dtype=torch.float32),
            'x': {
                'c': torch.tensor([5, 6, 7], dtype=torch.float32),
                'd': torch.tensor([[[8, 9]]], dtype=torch.float32),
            }
        }))
124
        assert _target.shape == ttorch.TreeSize({
HansBug's avatar
HansBug 已提交
125 126 127 128 129 130 131 132 133
            'a': torch.Size([2, 3]),
            'b': torch.Size([4]),
            'x': {
                'c': torch.Size([3]),
                'd': torch.Size([1, 1, 2]),
            }
        })

    def test_randint(self):
134
        _target = ttorch.randint(TreeValue({
HansBug's avatar
HansBug 已提交
135 136 137 138 139
            'a': (2, 3),
            'b': (5, 6),
            'x': {
                'c': (2, 3, 4),
            }
140
        }), -10, 10)
141 142 143
        assert ttorch.all(_target < 10)
        assert ttorch.all(-10 <= _target)
        assert _target.shape == ttorch.TreeSize({
HansBug's avatar
HansBug 已提交
144 145 146 147 148 149 150
            'a': torch.Size([2, 3]),
            'b': torch.Size([5, 6]),
            'x': {
                'c': torch.Size([2, 3, 4]),
            }
        })

151
        _target = ttorch.randint(TreeValue({
HansBug's avatar
HansBug 已提交
152 153 154 155 156
            'a': (2, 3),
            'b': (5, 6),
            'x': {
                'c': (2, 3, 4),
            }
157
        }), 10)
158 159 160
        assert ttorch.all(_target < 10)
        assert ttorch.all(0 <= _target)
        assert _target.shape == ttorch.TreeSize({
HansBug's avatar
HansBug 已提交
161 162 163 164 165 166 167 168
            'a': torch.Size([2, 3]),
            'b': torch.Size([5, 6]),
            'x': {
                'c': torch.Size([2, 3, 4]),
            }
        })

    def test_randint_like(self):
169
        _target = ttorch.randint_like(ttorch.TreeTensor({
HansBug's avatar
HansBug 已提交
170 171 172 173 174 175 176
            'a': torch.tensor([[1, 2, 3], [4, 5, 6]]),
            'b': torch.tensor([1, 2, 3, 4]),
            'x': {
                'c': torch.tensor([5, 6, 7]),
                'd': torch.tensor([[[8, 9]]]),
            }
        }), -10, 10)
177 178 179
        assert ttorch.all(_target < 10)
        assert ttorch.all(-10 <= _target)
        assert _target.shape == ttorch.TreeSize({
HansBug's avatar
HansBug 已提交
180 181 182 183 184 185 186 187
            'a': torch.Size([2, 3]),
            'b': torch.Size([4]),
            'x': {
                'c': torch.Size([3]),
                'd': torch.Size([1, 1, 2]),
            }
        })

188
        _target = ttorch.randint_like(ttorch.TreeTensor({
HansBug's avatar
HansBug 已提交
189 190 191 192 193 194 195
            'a': torch.tensor([[1, 2, 3], [4, 5, 6]]),
            'b': torch.tensor([1, 2, 3, 4]),
            'x': {
                'c': torch.tensor([5, 6, 7]),
                'd': torch.tensor([[[8, 9]]]),
            }
        }), 10)
196 197 198
        assert ttorch.all(_target < 10)
        assert ttorch.all(0 <= _target)
        assert _target.shape == ttorch.TreeSize({
HansBug's avatar
HansBug 已提交
199 200 201 202 203 204 205 206 207
            'a': torch.Size([2, 3]),
            'b': torch.Size([4]),
            'x': {
                'c': torch.Size([3]),
                'd': torch.Size([1, 1, 2]),
            }
        })

    def test_full(self):
208
        _target = ttorch.full(TreeValue({
HansBug's avatar
HansBug 已提交
209 210 211 212 213
            'a': (2, 3),
            'b': (5, 6),
            'x': {
                'c': (2, 3, 4),
            }
214
        }), 233)
215 216
        assert ttorch.all(_target == 233)
        assert _target.shape == ttorch.TreeSize({
HansBug's avatar
HansBug 已提交
217 218 219 220 221 222 223 224
            'a': torch.Size([2, 3]),
            'b': torch.Size([5, 6]),
            'x': {
                'c': torch.Size([2, 3, 4]),
            }
        })

    def test_full_like(self):
225
        _target = ttorch.full_like(ttorch.TreeTensor({
HansBug's avatar
HansBug 已提交
226 227 228 229 230 231 232
            'a': torch.tensor([[1, 2, 3], [4, 5, 6]]),
            'b': torch.tensor([1, 2, 3, 4]),
            'x': {
                'c': torch.tensor([5, 6, 7]),
                'd': torch.tensor([[[8, 9]]]),
            }
        }), 233)
233 234
        assert ttorch.all(_target == 233)
        assert _target.shape == ttorch.TreeSize({
HansBug's avatar
HansBug 已提交
235 236 237 238 239 240 241
            'a': torch.Size([2, 3]),
            'b': torch.Size([4]),
            'x': {
                'c': torch.Size([3]),
                'd': torch.Size([1, 1, 2]),
            }
        })
242 243

    def test_all(self):
244
        r1 = ttorch.all(torch.tensor([True, True, True]))
245 246
        assert torch.is_tensor(r1)
        assert r1 == torch.tensor(True)
247
        assert r1
248

249
        r2 = ttorch.all(torch.tensor([True, True, False]))
250 251
        assert torch.is_tensor(r2)
        assert r2 == torch.tensor(False)
252
        assert not r2
253

254
        r3 = ttorch.all(torch.tensor([False, False, False]))
255
        assert torch.is_tensor(r3)
256 257
        assert r3 == torch.tensor(False)
        assert not r3
258

259 260 261 262
        r4 = ttorch.all(ttorch.TreeTensor({
            'a': torch.tensor([True, True, True]),
            'b': torch.tensor([True, True, True]),
        })).all()
263
        assert torch.is_tensor(r4)
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
        assert r4 == torch.tensor(True)
        assert r4

        r5 = ttorch.all(ttorch.TreeTensor({
            'a': torch.tensor([True, True, True]),
            'b': torch.tensor([True, True, False]),
        })).all()
        assert torch.is_tensor(r5)
        assert r5 == torch.tensor(False)
        assert not r5

        r6 = ttorch.all(ttorch.TreeTensor({
            'a': torch.tensor([False, False, False]),
            'b': torch.tensor([False, False, False]),
        })).all()
        assert torch.is_tensor(r6)
        assert r6 == torch.tensor(False)
        assert not r6

    def test_any(self):
        r1 = ttorch.any(torch.tensor([True, True, True]))
        assert torch.is_tensor(r1)
        assert r1 == torch.tensor(True)
        assert r1

        r2 = ttorch.any(torch.tensor([True, True, False]))
        assert torch.is_tensor(r2)
        assert r2 == torch.tensor(True)
        assert r2

        r3 = ttorch.any(torch.tensor([False, False, False]))
        assert torch.is_tensor(r3)
        assert r3 == torch.tensor(False)
        assert not r3

        r4 = ttorch.any(ttorch.TreeTensor({
            'a': torch.tensor([True, True, True]),
            'b': torch.tensor([True, True, True]),
        })).all()
        assert torch.is_tensor(r4)
        assert r4 == torch.tensor(True)
        assert r4

        r5 = ttorch.any(ttorch.TreeTensor({
            'a': torch.tensor([True, True, True]),
            'b': torch.tensor([True, True, False]),
        })).all()
        assert torch.is_tensor(r5)
        assert r5 == torch.tensor(True)
        assert r5

        r6 = ttorch.any(ttorch.TreeTensor({
            'a': torch.tensor([False, False, False]),
            'b': torch.tensor([False, False, False]),
        })).all()
        assert torch.is_tensor(r6)
        assert r6 == torch.tensor(False)
        assert not r6

    def test_eq(self):
        assert ttorch.eq(torch.tensor([1, 2, 3]), torch.tensor([1, 2, 3])).all()
        assert not ttorch.eq(torch.tensor([1, 2, 3]), torch.tensor([1, 2, 2])).all()
        assert ttorch.eq(torch.tensor([1, 1, 1]), 1).all()
        assert not ttorch.eq(torch.tensor([1, 1, 2]), 1).all()

        assert ttorch.eq(ttorch.TreeTensor({
            'a': torch.tensor([1, 2, 3]),
            'b': torch.tensor([4, 5, 6]),
        }), ttorch.TreeTensor({
            'a': torch.tensor([1, 2, 3]),
            'b': torch.tensor([4, 5, 6]),
        })).all()
        assert not ttorch.eq(ttorch.TreeTensor({
            'a': torch.tensor([1, 2, 3]),
            'b': torch.tensor([4, 5, 6]),
        }), ttorch.TreeTensor({
            'a': torch.tensor([1, 2, 3]),
            'b': torch.tensor([4, 5, 5]),
        })).all()