|
15 | 15 | import unittest
|
16 | 16 |
|
17 | 17 | import numpy as np
|
18 |
| -from tests.op_test import OpTest |
19 | 18 |
|
20 | 19 | import paddle
|
21 | 20 | from paddle import base
|
| 21 | +from paddle.pir_utils import test_with_pir_api |
22 | 22 |
|
23 | 23 |
|
24 |
| -class TestHistogramOpError(unittest.TestCase): |
25 |
| - """Test histogram op error.""" |
| 24 | +class TestHistogram(unittest.TestCase): |
| 25 | + """Test histogram api.""" |
26 | 26 |
|
27 |
| - def run_network(self, net_func): |
28 |
| - main_program = base.Program() |
29 |
| - startup_program = base.Program() |
30 |
| - with base.program_guard(main_program, startup_program): |
31 |
| - net_func() |
32 |
| - exe = base.Executor() |
33 |
| - exe.run(main_program) |
34 |
| - |
35 |
| - def test_bins_error(self): |
36 |
| - """Test bins should be greater than or equal to 1.""" |
37 |
| - |
38 |
| - def net_func(): |
39 |
| - input_value = paddle.tensor.fill_constant( |
40 |
| - shape=[3, 4], dtype="float32", value=3.0 |
41 |
| - ) |
42 |
| - paddle.histogram(input=input_value, bins=-1, min=1, max=5) |
43 |
| - |
44 |
| - with self.assertRaises(ValueError): |
45 |
| - self.run_network(net_func) |
46 |
| - |
47 |
| - def test_min_max_error(self): |
48 |
| - """Test max must be larger or equal to min.""" |
49 |
| - |
50 |
| - def net_func(): |
51 |
| - input_value = paddle.tensor.fill_constant( |
52 |
| - shape=[3, 4], dtype="float32", value=3.0 |
53 |
| - ) |
54 |
| - paddle.histogram(input=input_value, bins=1, min=5, max=1) |
55 |
| - |
56 |
| - with self.assertRaises(ValueError): |
57 |
| - self.run_network(net_func) |
58 |
| - |
59 |
| - |
60 |
| -class TestHistogramOp(OpTest): |
61 | 27 | def setUp(self):
|
62 |
| - self.op_type = "histogram" |
63 | 28 | self.init_test_case()
|
64 |
| - np_input = np.random.uniform(low=0.0, high=20.0, size=self.in_shape) |
65 |
| - self.python_api = paddle.histogram |
66 |
| - self.inputs = {"X": np_input} |
67 |
| - self.init_attrs() |
68 |
| - Out, _ = np.histogram(np_input, bins=self.bins, range=(self.min, self.max)) |
69 |
| - self.outputs = {"Out": Out.astype(np.int64)} |
| 29 | + self.input_np = np.random.uniform( |
| 30 | + low=0.0, high=20.0, size=self.in_shape |
| 31 | + ).astype(np.float32) |
| 32 | + self.weight_np = np.random.uniform( |
| 33 | + low=0.0, high=1.0, size=self.in_shape |
| 34 | + ).astype(np.float32) |
70 | 35 |
|
71 | 36 | def init_test_case(self):
|
72 | 37 | self.in_shape = (10, 12)
|
73 | 38 | self.bins = 5
|
74 | 39 | self.min = 1
|
75 | 40 | self.max = 5
|
| 41 | + self.density = False |
| 42 | + self.is_weight = False |
| 43 | + |
| 44 | + @test_with_pir_api |
| 45 | + def test_static_graph(self): |
| 46 | + startup_program = paddle.static.Program() |
| 47 | + train_program = paddle.static.Program() |
| 48 | + with paddle.static.program_guard(train_program, startup_program): |
| 49 | + inputs = paddle.static.data( |
| 50 | + name="input", dtype="float32", shape=self.in_shape |
| 51 | + ) |
| 52 | + if self.is_weight: |
| 53 | + weight = paddle.static.data( |
| 54 | + name="weight", dtype="float32", shape=self.in_shape |
| 55 | + ) |
| 56 | + output = paddle.histogram( |
| 57 | + inputs, |
| 58 | + bins=self.bins, |
| 59 | + min=self.min, |
| 60 | + max=self.max, |
| 61 | + weight=weight, |
| 62 | + density=self.density, |
| 63 | + ) |
| 64 | + else: |
| 65 | + output = paddle.histogram( |
| 66 | + inputs, |
| 67 | + bins=self.bins, |
| 68 | + min=self.min, |
| 69 | + max=self.max, |
| 70 | + density=self.density, |
| 71 | + ) |
| 72 | + place = base.CPUPlace() |
| 73 | + if base.core.is_compiled_with_cuda(): |
| 74 | + place = base.CUDAPlace(0) |
| 75 | + exe = base.Executor(place) |
| 76 | + if self.is_weight: |
| 77 | + res = exe.run( |
| 78 | + feed={ |
| 79 | + "input": self.input_np, |
| 80 | + "weight": self.weight_np, |
| 81 | + }, |
| 82 | + fetch_list=[output], |
| 83 | + ) |
| 84 | + else: |
| 85 | + res = exe.run(feed={"input": self.input_np}, fetch_list=[output]) |
| 86 | + |
| 87 | + actual = np.array(res[0]) |
| 88 | + Out, _ = np.histogram( |
| 89 | + self.input_np, |
| 90 | + bins=self.bins, |
| 91 | + range=(self.min, self.max), |
| 92 | + density=self.density, |
| 93 | + weights=self.weight_np if self.is_weight else None, |
| 94 | + ) |
| 95 | + np.testing.assert_allclose(actual, Out, rtol=1e-58, atol=1e-5) |
| 96 | + |
| 97 | + def test_dygraph(self): |
| 98 | + with base.dygraph.guard(): |
| 99 | + inputs_np = np.random.uniform( |
| 100 | + low=0.0, high=20.0, size=self.in_shape |
| 101 | + ).astype(np.float32) |
| 102 | + |
| 103 | + self.inputs = paddle.to_tensor(inputs_np) |
76 | 104 |
|
77 |
| - def init_attrs(self): |
78 |
| - self.attrs = {"bins": self.bins, "min": self.min, "max": self.max} |
| 105 | + weight_np = np.random.uniform(low=0.0, high=1.0, size=self.in_shape).astype( |
| 106 | + np.float32 |
| 107 | + ) |
| 108 | + weight = paddle.to_tensor(weight_np) |
| 109 | + |
| 110 | + actual = paddle.histogram( |
| 111 | + self.inputs, |
| 112 | + bins=5, |
| 113 | + min=1, |
| 114 | + max=5, |
| 115 | + weight=weight if self.is_weight else None, |
| 116 | + density=self.density, |
| 117 | + ) |
| 118 | + |
| 119 | + Out, _ = np.histogram( |
| 120 | + inputs_np, |
| 121 | + bins=5, |
| 122 | + range=(1, 5), |
| 123 | + weights=weight_np if self.is_weight else None, |
| 124 | + density=self.density, |
| 125 | + ) |
79 | 126 |
|
80 |
| - def test_check_output(self): |
81 |
| - self.check_output() |
| 127 | + np.testing.assert_allclose(actual.numpy(), Out, rtol=1e-58, atol=1e-5) |
82 | 128 |
|
83 | 129 |
|
84 |
| -class TestHistogramOp_ZeroDim(TestHistogramOp): |
| 130 | +class TestHistogramOp_ZeroDim(TestHistogram): |
85 | 131 | def init_test_case(self):
|
86 | 132 | self.in_shape = []
|
87 | 133 | self.bins = 5
|
88 | 134 | self.min = 1
|
89 | 135 | self.max = 5
|
| 136 | + self.density = False |
| 137 | + self.is_weight = False |
90 | 138 |
|
91 | 139 |
|
92 | 140 | if __name__ == "__main__":
|
|
0 commit comments