|
| 1 | +# Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +import unittest |
| 17 | + |
| 18 | +import numpy as np |
| 19 | +from get_test_cover_info import ( |
| 20 | + XPUOpTestWrapper, |
| 21 | + create_test_class, |
| 22 | + get_xpu_op_support_types, |
| 23 | +) |
| 24 | +from op_test_xpu import XPUOpTest |
| 25 | + |
| 26 | +import paddle |
| 27 | + |
| 28 | +paddle.enable_static() |
| 29 | + |
| 30 | + |
| 31 | +def sample_output_one_dimension(out, dim): |
| 32 | + # count numbers of different categories |
| 33 | + sample_prob = np.zeros(dim).astype("float32") |
| 34 | + sample_index_prob = np.unique(out, return_counts=True) |
| 35 | + sample_prob[sample_index_prob[0]] = sample_index_prob[1] |
| 36 | + sample_prob /= sample_prob.sum() |
| 37 | + return sample_prob |
| 38 | + |
| 39 | + |
| 40 | +def sample_output_two_dimension(out, shape): |
| 41 | + num_dist = shape[0] |
| 42 | + out_list = np.split(out, num_dist, axis=0) |
| 43 | + sample_prob = np.zeros(shape).astype("float32") |
| 44 | + for i in range(num_dist): |
| 45 | + sample_index_prob = np.unique(out_list[i], return_counts=True) |
| 46 | + sample_prob[i][sample_index_prob[0]] = sample_index_prob[1] |
| 47 | + sample_prob /= sample_prob.sum(axis=-1, keepdims=True) |
| 48 | + return sample_prob |
| 49 | + |
| 50 | + |
| 51 | +class XPUTestMultinomialOp(XPUOpTestWrapper): |
| 52 | + def __init__(self): |
| 53 | + self.op_name = 'multinomial' |
| 54 | + self.use_dynamic_create_class = False |
| 55 | + |
| 56 | + class TestMultinomialOp(XPUOpTest): |
| 57 | + def setUp(self): |
| 58 | + self.dtype = self.in_type |
| 59 | + self.place = paddle.XPUPlace(0) |
| 60 | + paddle.enable_static() |
| 61 | + self.op_type = "multinomial" |
| 62 | + self.python_api = paddle.multinomial |
| 63 | + self.init_data() |
| 64 | + self.inputs = {"X": self.input_np} |
| 65 | + |
| 66 | + def init_data(self): |
| 67 | + # input probability is a vector, and replacement is True |
| 68 | + self.input_np = np.random.rand(4).astype(self.dtype) |
| 69 | + self.outputs = {"Out": np.zeros(100000).astype("int64")} |
| 70 | + self.attrs = {"num_samples": 100000, "replacement": True} |
| 71 | + |
| 72 | + def test_check_output(self): |
| 73 | + self.check_output_with_place_customized( |
| 74 | + self.verify_output, self.place |
| 75 | + ) |
| 76 | + |
| 77 | + def sample_output(self, out): |
| 78 | + return sample_output_one_dimension(out, 4) |
| 79 | + |
| 80 | + def verify_output(self, outs): |
| 81 | + # normalize the input to get the probability |
| 82 | + prob = self.input_np / self.input_np.sum(axis=-1, keepdims=True) |
| 83 | + sample_prob = self.sample_output(np.array(outs[0])) |
| 84 | + np.testing.assert_allclose( |
| 85 | + sample_prob, |
| 86 | + prob, |
| 87 | + rtol=0, |
| 88 | + atol=0.01, |
| 89 | + err_msg='sample_prob: ' |
| 90 | + + str(sample_prob) |
| 91 | + + '\nprob: ' |
| 92 | + + str(prob), |
| 93 | + ) |
| 94 | + |
| 95 | + class TestMultinomialOp2(TestMultinomialOp): |
| 96 | + def init_data(self): |
| 97 | + # input probability is a matrix |
| 98 | + self.input_np = np.random.rand(3, 4).astype(self.dtype) |
| 99 | + self.outputs = {"Out": np.zeros((3, 100000)).astype("int64")} |
| 100 | + self.attrs = {"num_samples": 100000, "replacement": True} |
| 101 | + |
| 102 | + def sample_output(self, out): |
| 103 | + return sample_output_two_dimension(out, [3, 4]) |
| 104 | + |
| 105 | + class TestMultinomialOp3(TestMultinomialOp): |
| 106 | + def init_data(self): |
| 107 | + # replacement is False. number of samples must be less than number of categories. |
| 108 | + self.input_np = np.random.rand(1000).astype(self.dtype) |
| 109 | + self.outputs = {"Out": np.zeros(100).astype("int64")} |
| 110 | + self.attrs = {"num_samples": 100, "replacement": False} |
| 111 | + |
| 112 | + def verify_output(self, outs): |
| 113 | + out = np.array(outs[0]) |
| 114 | + unique_out = np.unique(out) |
| 115 | + self.assertEqual( |
| 116 | + len(unique_out), |
| 117 | + 100, |
| 118 | + "replacement is False. categories can't be sampled repeatedly", |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +support_types = get_xpu_op_support_types('multinomial') |
| 123 | +for stype in support_types: |
| 124 | + create_test_class(globals(), XPUTestMultinomialOp, stype) |
| 125 | + |
| 126 | +if __name__ == "__main__": |
| 127 | + unittest.main() |
0 commit comments