“1a4a51db2b8834d893cc011439e9f5367ec4f125”上不存在“python/paddle/quantization/imperative/__init__.py”
test_nan_inf.py 2.4 KB
Newer Older
W
WangXi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import subprocess
17 18 19
import sys
import unittest

P
pangyoki 已提交
20 21 22
import paddle

paddle.enable_static()
W
WangXi 已提交
23 24 25 26 27 28 29 30 31 32


class TestNanInf(unittest.TestCase):
    def setUp(self):
        self._python_interp = sys.executable
        if os.getenv('WITH_COVERAGE', 'OFF') == 'ON':
            self._python_interp += " -m coverage run --branch -p"

        self.env = os.environ.copy()

33
    def check_nan_inf(self):
W
WangXi 已提交
34 35
        cmd = self._python_interp

36 37 38 39 40 41
        proc = subprocess.Popen(
            cmd.split(" "),
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            env=self.env,
        )
W
WangXi 已提交
42 43 44 45 46 47 48 49

        out, err = proc.communicate()
        returncode = proc.returncode

        print(out)
        print(err)

        # in python3, type(out+err) is 'bytes', need use encode
50
        if paddle.fluid.core.is_compiled_with_cuda():
51
            assert (out + err).find('find_nan=1, find_inf=1'.encode()) != -1
52
        else:
53
            assert (out + err).find(
54 55
                'There are `nan` or `inf` in tensor'.encode()
            ) != -1
W
WangXi 已提交
56

57 58 59 60 61 62 63 64
    def test_nan_inf_in_static_mode(self):
        self._python_interp += " check_nan_inf_base.py"
        self.check_nan_inf()

    def test_nan_inf_in_dynamic_mode(self):
        self._python_interp += " check_nan_inf_base_dygraph.py"
        self.check_nan_inf()

W
WangXi 已提交
65 66 67

class TestNanInfEnv(TestNanInf):
    def setUp(self):
68
        super().setUp()
W
WangXi 已提交
69 70 71 72 73
        # windows python have some bug with env, so need use str to pass ci
        # otherwise, "TypeError: environment can only contain strings"
        self.env[str("PADDLE_INF_NAN_SKIP_OP")] = str("mul")
        self.env[str("PADDLE_INF_NAN_SKIP_ROLE")] = str("loss")
        self.env[str("PADDLE_INF_NAN_SKIP_VAR")] = str(
74 75
            "elementwise_add:fc_0.tmp_1"
        )
W
WangXi 已提交
76 77 78 79


if __name__ == '__main__':
    unittest.main()