|
| 1 | +# Copyright (c) 2021 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 |
| 21 | +import paddle.fluid as fluid |
| 22 | +from paddle.framework import core |
| 23 | + |
| 24 | +SEED = 2021 |
| 25 | +np.random.seed(SEED) |
| 26 | + |
| 27 | + |
| 28 | +def get_c_embedding(start, end, table, ids): |
| 29 | + index = ids.flatten() |
| 30 | + input_mask = (index < start) | (index >= end) |
| 31 | + masked_input = index - start |
| 32 | + masked_input[input_mask] = 0 |
| 33 | + output = table[masked_input] |
| 34 | + output[input_mask] = 0.0 |
| 35 | + return output |
| 36 | + |
| 37 | + |
| 38 | +class TestCEmbeddingCPU(OpTest): |
| 39 | + def setUp(self): |
| 40 | + self.init_dtype() |
| 41 | + self.initcase() |
| 42 | + if core.is_compiled_with_npu(): |
| 43 | + self.__class__.use_npu = True |
| 44 | + elif core.is_compiled_with_cuda(): |
| 45 | + self.__class__.exist_fp64_check_grad = True |
| 46 | + |
| 47 | + def initcase(self): |
| 48 | + self.op_type = "c_embedding" |
| 49 | + table = np.random.random((17, 64)).astype(self.dtype) |
| 50 | + ids = np.random.randint( |
| 51 | + low=0, high=17 * 2, size=(2, 4)).astype(self.ids_dtype) |
| 52 | + self.start_index = 10 |
| 53 | + self.end_index = self.start_index + 17 |
| 54 | + |
| 55 | + self.inputs = {'W': table, 'Ids': ids} |
| 56 | + np_out = get_c_embedding(self.start_index, self.end_index, table, ids) |
| 57 | + self.outputs = {'Out': np_out.reshape((2, 4, 64))} |
| 58 | + self.attrs = {'start_index': self.start_index} |
| 59 | + if core.is_compiled_with_npu(): |
| 60 | + self.__class__.use_npu = True |
| 61 | + |
| 62 | + def test_check_cpu(self): |
| 63 | + self.check_output_with_place(core.CPUPlace()) |
| 64 | + |
| 65 | + def test_check_cpu_grad(self): |
| 66 | + self.check_grad_with_place(core.CPUPlace(), ['W'], 'Out') |
| 67 | + |
| 68 | + def init_dtype(self): |
| 69 | + self.dtype = "float32" |
| 70 | + self.ids_dtype = "int64" |
| 71 | + |
| 72 | + |
| 73 | +class TestCEmbeddingOpBase(TestCEmbeddingCPU): |
| 74 | + def setUp(self): |
| 75 | + self.init_dtype() |
| 76 | + self.initcase() |
| 77 | + |
| 78 | + def test_check_output(self): |
| 79 | + if core.is_compiled_with_cuda(): |
| 80 | + self.check_output_with_place(core.CUDAPlace(0)) |
| 81 | + elif core.is_compiled_with_npu(): |
| 82 | + self.check_output_with_place(core.NPUPlace(0)) |
| 83 | + |
| 84 | + def test_check_grad(self): |
| 85 | + if core.is_compiled_with_cuda(): |
| 86 | + self.check_grad_with_place(core.CUDAPlace(0), ['W'], 'Out') |
| 87 | + elif core.is_compiled_with_npu(): |
| 88 | + self.check_grad_with_place(core.NPUPlace(0), ['W'], 'Out') |
| 89 | + |
| 90 | + def init_dtype(self): |
| 91 | + if core.is_compiled_with_cuda(): |
| 92 | + self.dtype = "float64" |
| 93 | + self.ids_dtype = "int64" |
| 94 | + elif core.is_compiled_with_npu(): |
| 95 | + self.dtype = "float32" |
| 96 | + self.ids_dtype = "int32" |
| 97 | + |
| 98 | + |
| 99 | +class TestCEmbeddingOpFP32(TestCEmbeddingOpBase): |
| 100 | + def setUp(self): |
| 101 | + self.init_dtype() |
| 102 | + self.initcase() |
| 103 | + |
| 104 | + def initcase(self): |
| 105 | + self.op_type = "c_embedding" |
| 106 | + table = np.random.random((17, 64)).astype(self.dtype) |
| 107 | + ids = np.random.randint( |
| 108 | + low=0, high=17 * 2, size=(2, 4)).astype(self.ids_dtype) |
| 109 | + self.start_index = 10 |
| 110 | + ids[0][1] = 12 |
| 111 | + ids[0][2] = 12 |
| 112 | + ids[1][2] = 12 |
| 113 | + ids[1][3] = 12 |
| 114 | + self.end_index = self.start_index + 17 |
| 115 | + |
| 116 | + self.inputs = {'W': table, 'Ids': ids} |
| 117 | + np_out = get_c_embedding(self.start_index, self.end_index, table, ids) |
| 118 | + self.outputs = {'Out': np_out.reshape((2, 4, 64))} |
| 119 | + self.attrs = {'start_index': self.start_index} |
| 120 | + |
| 121 | + if core.is_compiled_with_npu(): |
| 122 | + self.__class__.use_npu = True |
| 123 | + elif core.is_compiled_with_cuda(): |
| 124 | + self.__class__.exist_fp64_check_grad = True |
| 125 | + |
| 126 | + def init_dtype(self): |
| 127 | + self.dtype = "float32" |
| 128 | + self.ids_dtype = "int32" |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + unittest.main() |
0 commit comments