|
| 1 | +# Copyright (c) 2023 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 | +import unittest |
| 16 | + |
| 17 | +import numpy as np |
| 18 | + |
| 19 | +import paddle |
| 20 | +from paddle import _C_ops |
| 21 | +from npu_utils import check_soc_version |
| 22 | + |
| 23 | + |
| 24 | +def promote_dtype(x): |
| 25 | + if x.dtype in [paddle.float16, paddle.bfloat16]: |
| 26 | + return x.astype(paddle.float32) |
| 27 | + else: |
| 28 | + return x |
| 29 | + |
| 30 | + |
| 31 | +def recreate(x, multi_precision): |
| 32 | + if isinstance(x, (list, tuple)): |
| 33 | + return [recreate(item, multi_precision) for item in x] |
| 34 | + |
| 35 | + if x is None: |
| 36 | + return None |
| 37 | + |
| 38 | + if multi_precision: |
| 39 | + x = promote_dtype(x) |
| 40 | + |
| 41 | + return paddle.to_tensor(x.numpy()) |
| 42 | + |
| 43 | + |
| 44 | +def run_ground_truth(x, dy, dweight, dbias, multi_precision, has_bias): |
| 45 | + x, dy, dweight, dbias = recreate([x, dy, dweight, dbias], multi_precision) |
| 46 | + |
| 47 | + dweight_tmp = paddle.matmul( |
| 48 | + x.reshape([-1, x.shape[-1]]), |
| 49 | + dy.reshape([-1, dy.shape[-1]]), |
| 50 | + transpose_x=True, |
| 51 | + ) |
| 52 | + if dweight is None: |
| 53 | + dweight = dweight_tmp |
| 54 | + else: |
| 55 | + assert dweight.shape == dweight_tmp.shape |
| 56 | + assert dweight.dtype == dweight.dtype |
| 57 | + dweight += dweight_tmp |
| 58 | + |
| 59 | + if has_bias: |
| 60 | + dbias_tmp = dy.reshape([-1, dy.shape[-1]]).sum(axis=0) |
| 61 | + if dbias is None: |
| 62 | + dbias = dbias_tmp |
| 63 | + else: |
| 64 | + assert dbias.shape == dbias_tmp.shape |
| 65 | + assert dbias.dtype == dbias_tmp.dtype |
| 66 | + dbias += dbias_tmp |
| 67 | + |
| 68 | + return promote_dtype(dweight).numpy(), promote_dtype(dbias).numpy() |
| 69 | + else: |
| 70 | + return promote_dtype(dweight).numpy() |
| 71 | + |
| 72 | + |
| 73 | +def run_fused_linear_param_grad_add(x, dy, dweight, dbias, multi_precision, has_bias): |
| 74 | + dweight_new, dbias_new = _C_ops.fused_linear_param_grad_add( |
| 75 | + x, dy, dweight, dbias, multi_precision, has_bias |
| 76 | + ) |
| 77 | + if dweight is not None: |
| 78 | + assert dweight_new.data_ptr() == dweight.data_ptr() |
| 79 | + if has_bias: |
| 80 | + return ( |
| 81 | + promote_dtype(dweight_new).numpy(), |
| 82 | + promote_dtype(dbias_new).numpy(), |
| 83 | + ) |
| 84 | + else: |
| 85 | + return promote_dtype(dweight_new).numpy() |
| 86 | + |
| 87 | + |
| 88 | +class TestMainClassBase(unittest.TestCase): |
| 89 | + def setUp(self): |
| 90 | + self.shape = [3, 4, 32] |
| 91 | + self.output_size = 128 |
| 92 | + self.dtype = paddle.float16 |
| 93 | + |
| 94 | + def config(self): |
| 95 | + pass |
| 96 | + |
| 97 | + def rand(self, shape, dtype=None): |
| 98 | + x = np.random.randint(low=-5, high=5, size=shape) |
| 99 | + x = paddle.to_tensor(x) |
| 100 | + return x.astype(dtype or self.dtype) |
| 101 | + |
| 102 | + def generate_rand_inputs(self, has_dweight, has_dbias, multi_precision, has_bias): |
| 103 | + x_shape = self.shape |
| 104 | + dy_shape = self.shape[:-1] + [self.output_size] |
| 105 | + dweight_shape = [self.shape[-1], self.output_size] |
| 106 | + dbias_shape = [self.output_size] |
| 107 | + |
| 108 | + x = self.rand(x_shape) |
| 109 | + dy = self.rand(dy_shape) |
| 110 | + if has_dweight: |
| 111 | + dweight = self.rand(dweight_shape) |
| 112 | + if multi_precision: |
| 113 | + dweight = promote_dtype(dweight) |
| 114 | + else: |
| 115 | + dweight = None |
| 116 | + |
| 117 | + if has_bias and has_dbias: |
| 118 | + dbias = self.rand(dbias_shape) |
| 119 | + if multi_precision: |
| 120 | + dbias = promote_dtype(dbias) |
| 121 | + else: |
| 122 | + dbias = None |
| 123 | + return x, dy, dweight, dbias |
| 124 | + |
| 125 | + def check_main(self, has_dweight, has_dbias, multi_precision, has_bias): |
| 126 | + x, dy, dweight, dbias = self.generate_rand_inputs( |
| 127 | + has_dweight, has_dbias, multi_precision, has_bias |
| 128 | + ) |
| 129 | + res1 = run_ground_truth(x, dy, dweight, dbias, multi_precision, has_bias) |
| 130 | + res2 = run_fused_linear_param_grad_add( |
| 131 | + x, dy, dweight, dbias, multi_precision, has_bias |
| 132 | + ) |
| 133 | + self.assertEqual(len(res1), len(res2)) |
| 134 | + for r1, r2 in zip(res1, res2): |
| 135 | + max_diff = np.max(np.abs(r1 - r2)) |
| 136 | + self.assertLess(max_diff, 1e-10) |
| 137 | + |
| 138 | + @check_soc_version |
| 139 | + def test_main(self): |
| 140 | + for has_dweight in [False, True]: |
| 141 | + for has_bias in [False, True]: |
| 142 | + for has_dbias in [False, True]: |
| 143 | + for multi_precision in [False, True]: |
| 144 | + self.check_main( |
| 145 | + has_dweight, has_dbias, multi_precision, has_bias |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +class TestMainClassBF16(TestMainClassBase): |
| 150 | + def config(self): |
| 151 | + self.dtype = paddle.bfloat16 |
| 152 | + |
| 153 | + |
| 154 | +class TestMainClassFP32(TestMainClassBase): |
| 155 | + def config(self): |
| 156 | + self.dtype = paddle.float32 |
| 157 | + |
| 158 | + |
| 159 | +class TestMainClassFP64(TestMainClassBase): |
| 160 | + def config(self): |
| 161 | + self.dtype = paddle.float64 |
| 162 | + |
| 163 | + |
| 164 | +if __name__ == "__main__": |
| 165 | + unittest.main() |
0 commit comments