File: test_basic.py

package info (click to toggle)
libgpuarray 0.7.6-13
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,176 kB
  • sloc: ansic: 19,235; python: 4,591; makefile: 208; javascript: 71; sh: 15
file content (76 lines) | stat: -rw-r--r-- 2,495 bytes parent folder | download | duplicates (3)
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
import pygpu

from pygpu.basic import (tril, triu)
from unittest import TestCase
from .support import (gen_gpuarray, context)
import numpy


def test_tril():
    for shape in [(10, 5), (5, 10), (10, 10)]:
        for order in ['c', 'f']:
            for inplace in [True, False]:
                ac, ag = gen_gpuarray(shape, 'float32',
                                      order=order, ctx=context)
                result = tril(ag, inplace=inplace)
                assert numpy.all(numpy.tril(ac) == result)
                if inplace:
                    assert numpy.all(numpy.tril(ac) == ag)
                else:
                    assert numpy.all(ac == ag)


def test_triu():
    for shape in [(10, 5), (5, 10), (10, 10)]:
        for order in ['c', 'f']:
            for inplace in [True, False]:
                ac, ag = gen_gpuarray(shape, 'float32',
                                      order=order, ctx=context)
                result = triu(ag, inplace=inplace)
                assert numpy.all(numpy.triu(ac) == result)
                if inplace:
                    assert numpy.all(numpy.triu(ac) == ag)
                else:
                    assert numpy.all(ac == ag)


class test_errors(TestCase):

    def runTest(self):
        self.assertRaises(ValueError, self.run_1d_triu)
        self.assertRaises(ValueError, self.run_3d_triu)
        self.assertRaises(ValueError, self.run_1d_tril)
        self.assertRaises(ValueError, self.run_3d_tril)

        self.assertRaises(ValueError, self.run_noncontiguous_tril)
        self.assertRaises(ValueError, self.run_noncontiguous_triu)

    def run_1d_triu(self):
        ac, ag = gen_gpuarray((10, ), 'float32', ctx=context)
        triu(ag)

    def run_3d_triu(self):
        ac, ag = gen_gpuarray((10, 10, 10), 'float32', ctx=context)
        triu(ag)

    def run_1d_tril(self):
        ac, ag = gen_gpuarray((10, ), 'float32', ctx=context)
        tril(ag)

    def run_3d_tril(self):
        ac, ag = gen_gpuarray((10, 10, 10), 'float32', ctx=context)
        tril(ag)

    def run_noncontiguous_tril(self):
        a = numpy.random.rand(5, 5)
        b = pygpu.array(a, context=context)
        b = b[::-1]
        assert b.flags.c_contiguous is b.flags.f_contiguous is False
        tril(b)

    def run_noncontiguous_triu(self):
        a = numpy.random.rand(5, 5)
        b = pygpu.array(a, context=context)
        b = b[::-1]
        assert b.flags.c_contiguous is b.flags.f_contiguous is False
        triu(b)