|
| 1 | +# Copyright (c) 2019 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 | +from __future__ import print_function |
| 16 | + |
| 17 | +import unittest |
| 18 | +import numpy as np |
| 19 | +from op_test import OpTest |
| 20 | +import paddle.fluid.core as core |
| 21 | +from paddle.fluid.op import Operator |
| 22 | + |
| 23 | + |
| 24 | +class TestWhereOp(OpTest): |
| 25 | + def setUp(self): |
| 26 | + self.op_type = "where" |
| 27 | + self.init_config() |
| 28 | + |
| 29 | + def test_check_output(self): |
| 30 | + self.check_output() |
| 31 | + |
| 32 | + def init_config(self): |
| 33 | + self.inputs = {'Condition': np.array([True, False, True]), } |
| 34 | + |
| 35 | + self.outputs = {'Out': np.array([[0], [2]], dtype='int64')} |
| 36 | + |
| 37 | + |
| 38 | +class TestAllFalse(unittest.TestCase): |
| 39 | + def setUp(self): |
| 40 | + self.op_type = "where" |
| 41 | + self.init_config() |
| 42 | + |
| 43 | + def check_with_place(self, place): |
| 44 | + scope = core.Scope() |
| 45 | + condition = scope.var('Condition').get_tensor() |
| 46 | + condition.set(self.cond_data, place) |
| 47 | + |
| 48 | + out = scope.var("Out").get_tensor() |
| 49 | + out.set(np.full(self.shape, 0).astype('int64'), place) |
| 50 | + |
| 51 | + op = Operator("where", Condition="Condition", Out="Out") |
| 52 | + op.run(scope, place) |
| 53 | + |
| 54 | + out_array = np.array(out) |
| 55 | + self.assertTrue((out_array == self.out_data).all()) |
| 56 | + |
| 57 | + def init_config(self): |
| 58 | + self.cond_data = np.array([False, False, False]) |
| 59 | + self.shape = (3, 1) |
| 60 | + self.out_data = np.array([], dtype='int64') |
| 61 | + |
| 62 | + def test_all_false(self): |
| 63 | + self.check_with_place(core.CPUPlace()) |
| 64 | + |
| 65 | + if core.is_compiled_with_cuda(): |
| 66 | + self.check_with_place(core.CUDAPlace(0)) |
| 67 | + |
| 68 | + |
| 69 | +class TestRank2(TestWhereOp): |
| 70 | + def init_config(self): |
| 71 | + self.inputs = {'Condition': np.array([[True, False], [False, True]]), } |
| 72 | + |
| 73 | + self.outputs = {'Out': np.array([[0, 0], [1, 1]], dtype='int64')} |
| 74 | + |
| 75 | + |
| 76 | +class TestRank3(TestWhereOp): |
| 77 | + def init_config(self): |
| 78 | + self.inputs = { |
| 79 | + 'Condition': np.array([[[True, False], [False, True]], |
| 80 | + [[False, True], [True, False]], |
| 81 | + [[False, False], [False, True]]]), |
| 82 | + } |
| 83 | + |
| 84 | + self.outputs = { |
| 85 | + 'Out': np.array( |
| 86 | + [[0, 0, 0], [0, 1, 1], [1, 0, 1], [1, 1, 0], [2, 1, 1]], |
| 87 | + dtype='int64') |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + unittest.main() |
0 commit comments