|
| 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 | +import unittest |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +from scipy.special import logit |
| 19 | + |
| 20 | +import paddle |
| 21 | +from paddle import base |
| 22 | + |
| 23 | + |
| 24 | +class TestSigmoidCrossEntropyWithLogitsOpGradWithAutoGrad(unittest.TestCase): |
| 25 | + def setUp(self): |
| 26 | + np.random.seed(2023) |
| 27 | + paddle.seed(2023) |
| 28 | + self.places = [base.CPUPlace()] |
| 29 | + if base.core.is_compiled_with_cuda(): |
| 30 | + self.places.append(base.CUDAPlace(0)) |
| 31 | + self.batch_size = 64 |
| 32 | + self.num_classes = 20 |
| 33 | + |
| 34 | + self.x = logit( |
| 35 | + np.random.uniform(0, 1, (self.batch_size, self.num_classes)).astype( |
| 36 | + "float32" |
| 37 | + ) |
| 38 | + ) |
| 39 | + |
| 40 | + self.lable = np.random.uniform( |
| 41 | + 0, 1, (self.batch_size, self.num_classes) |
| 42 | + ).astype("float32") |
| 43 | + |
| 44 | + self.pos_weight = np.random.uniform( |
| 45 | + 0, 1, (self.batch_size, self.num_classes) |
| 46 | + ).astype("float32") |
| 47 | + |
| 48 | + def test_check_grad_with_auto_grad(self): |
| 49 | + def fn_ref(x, label, weight): |
| 50 | + out = paddle._C_ops.sigmoid_cross_entropy_with_logits( |
| 51 | + x, label, weight, False, -100 |
| 52 | + ) |
| 53 | + loss = out.sum() |
| 54 | + loss.backward() |
| 55 | + return out, x.grad |
| 56 | + |
| 57 | + def fn_comp(x, label, weight): |
| 58 | + zeros = paddle.full((self.batch_size, self.num_classes), 0.0) |
| 59 | + t1 = paddle.where(x > 0, x, zeros) |
| 60 | + t2 = x * label |
| 61 | + t3 = paddle.log(1 + paddle.exp(-paddle.abs(x))) |
| 62 | + t4 = t1 - t2 + t3 * weight |
| 63 | + t5 = paddle.full((self.batch_size, self.num_classes), -100.0) |
| 64 | + out = paddle.where(label == t5, zeros, t4) |
| 65 | + loss = out.sum() |
| 66 | + loss.backward() |
| 67 | + return out, x.grad |
| 68 | + |
| 69 | + def cal(fn, place): |
| 70 | + x1 = paddle.to_tensor(self.x, stop_gradient=False, place=place) |
| 71 | + label1 = paddle.to_tensor(self.lable) |
| 72 | + pos_weight1 = paddle.to_tensor(self.pos_weight, place=place) |
| 73 | + res = fn(x1, label1, pos_weight1) |
| 74 | + return res |
| 75 | + |
| 76 | + for idx, p in enumerate(self.places): |
| 77 | + if idx == 0: |
| 78 | + paddle.set_device('cpu') |
| 79 | + else: |
| 80 | + paddle.set_device('gpu') |
| 81 | + |
| 82 | + ref = cal(fn_ref, p) |
| 83 | + actual = cal(fn_comp, p) |
| 84 | + |
| 85 | + for idx in range(len(ref)): |
| 86 | + np.testing.assert_allclose( |
| 87 | + ref[idx].numpy(), actual[idx].numpy(), atol=1e-6, rtol=1e-6 |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == '__main__': |
| 92 | + unittest.main() |
0 commit comments