|
| 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 paddle.fluid.tests.unittests.op_test import OpTest, convert_float_to_uint16 |
| 20 | +import paddle |
| 21 | +import paddle.fluid as fluid |
| 22 | +import paddle.fluid.core as core |
| 23 | + |
| 24 | + |
| 25 | +@unittest.skipIf(not core.supports_bfloat16(), |
| 26 | + "place does not support BF16 evaluation") |
| 27 | +@unittest.skipIf(core.is_compiled_with_cuda(), |
| 28 | + "core is compiled with CUDA which has no BF implementation") |
| 29 | +class TestScaleOpBF16(OpTest): |
| 30 | + def setUp(self): |
| 31 | + self.op_type = "scale" |
| 32 | + self.x_fp32 = np.random.random((10, 10)).astype(np.float32) |
| 33 | + self.x_bf16 = convert_float_to_uint16(self.x_fp32) |
| 34 | + self.scale = -2.3 |
| 35 | + self.inputs = {'X': self.x_bf16} |
| 36 | + self.attrs = {'scale': self.scale, 'use_mkldnn': True, 'bias': 0.4} |
| 37 | + self.use_mkldnn = True |
| 38 | + self.outputs = { |
| 39 | + 'Out': (self.x_fp32 * self.attrs['scale']) + self.attrs['bias'] |
| 40 | + } |
| 41 | + |
| 42 | + def calculate_grads(self): |
| 43 | + bias = 0 |
| 44 | + if 'bias' in self.attrs: |
| 45 | + bias = self.attrs['bias'] |
| 46 | + |
| 47 | + scale = self.scale |
| 48 | + if 'ScaleTensor' in self.attrs: |
| 49 | + scale = self.attrs['ScaleTensor'] |
| 50 | + |
| 51 | + self.out = (self.x_fp32 * scale) + bias |
| 52 | + self.dx = (self.out * scale) |
| 53 | + |
| 54 | + def test_check_output(self): |
| 55 | + self.check_output(check_dygraph=False) |
| 56 | + |
| 57 | + def test_check_grad(self): |
| 58 | + self.calculate_grads() |
| 59 | + self.check_grad_with_place( |
| 60 | + core.CPUPlace(), ["X"], |
| 61 | + "Out", |
| 62 | + check_dygraph=False, |
| 63 | + user_defined_grads=[self.dx], |
| 64 | + user_defined_grad_outputs=[convert_float_to_uint16(self.out)]) |
| 65 | + |
| 66 | + |
| 67 | +class TestScaleOpBF16BiasNotAfterScale(TestScaleOpBF16): |
| 68 | + def setUp(self): |
| 69 | + self.op_type = "scale" |
| 70 | + self.x_fp32 = np.random.random((10, 10)).astype(np.float32) |
| 71 | + self.x_bf16 = convert_float_to_uint16(self.x_fp32) |
| 72 | + self.scale = 1.5 |
| 73 | + self.inputs = {'X': self.x_bf16} |
| 74 | + self.attrs = { |
| 75 | + 'scale': self.scale, |
| 76 | + 'use_mkldnn': True, |
| 77 | + 'bias': 0.0, |
| 78 | + 'bias_after_scale': False |
| 79 | + } |
| 80 | + self.use_mkldnn = True |
| 81 | + self.outputs = { |
| 82 | + 'Out': (self.x_fp32 + self.attrs['bias']) * self.attrs['scale'] |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | +class TestScaleOpBF16ScaleTensor(TestScaleOpBF16): |
| 87 | + def setUp(self): |
| 88 | + self.op_type = "scale" |
| 89 | + self.scale = -2.3 |
| 90 | + self.x_fp32 = np.random.random((10, 10)).astype(np.float32) |
| 91 | + self.x_bf16 = convert_float_to_uint16(self.x_fp32) |
| 92 | + self.scale_tensor = np.array([self.scale]).astype(np.float32) |
| 93 | + self.inputs = { |
| 94 | + 'X': self.x_bf16, |
| 95 | + 'ScaleTensor': convert_float_to_uint16(self.scale_tensor) |
| 96 | + } |
| 97 | + self.attrs = {'use_mkldnn': True} |
| 98 | + self.outputs = {'Out': self.x_fp32 * self.scale} |
| 99 | + |
| 100 | + |
| 101 | +class TestScaleOpBF16ScaleTensorNotBiasAfterScale(TestScaleOpBF16): |
| 102 | + def setUp(self): |
| 103 | + self.op_type = "scale" |
| 104 | + self.scale = 1.2 |
| 105 | + self.x_fp32 = np.random.random((9, 13)).astype(np.float32) |
| 106 | + self.x_bf16 = convert_float_to_uint16(self.x_fp32) |
| 107 | + self.scale_tensor = np.array([self.scale]).astype(np.float32) |
| 108 | + self.inputs = { |
| 109 | + 'X': self.x_bf16, |
| 110 | + 'ScaleTensor': convert_float_to_uint16(self.scale_tensor) |
| 111 | + } |
| 112 | + self.attrs = { |
| 113 | + 'bias': -1.1, |
| 114 | + 'bias_after_scale': False, |
| 115 | + 'use_mkldnn': True |
| 116 | + } |
| 117 | + self.outputs = {'Out': (self.x_fp32 + self.attrs['bias']) * self.scale} |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + paddle.enable_static() |
| 122 | + unittest.main() |
0 commit comments