torch_npu.npu.utils.npu_check_overflow(grad) -> bool
检测梯度是否溢出,INF_NAN模式下检测输入Tensor是否溢出;饱和模式通过检查硬件溢出标志位判断是否溢出。
输入为torch.Tensor或float,在INF_NAN模式下判断输入中是否有inf或nan;饱和模式忽略输入检查硬件溢出标志位。
True溢出,False未溢出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import torch import torch_npu.npu.utils as utils from torch_npu.testing.testcase import TestCase, run_tests class TestCheckOverFlow(TestCase): def test_check_over_flow(self): a = torch.Tensor([65535]).npu().half() a = a + a ret = utils.npu_check_overflow(a) self.assertTrue(ret) if __name__ == "__main__": run_tests() |