【UnitTestFix No.6】test_gather_op 单测 优化及新增特性 by aztice · Pull Request #75621 · PaddlePaddle/Paddle (original) (raw)
将所有 paddle.disable_static() 以及 paddle.enable_static() 以 with paddle.base.dygraph.guard() 代替,同时建议在后续可能存在的新feature采用自动的 disable + enable,避免意外。
@unittest.skipIf(
not (core.is_compiled_with_cuda() or is_custom_device()),
"only support compiled with CUDA.",
)
class TestGatherGPUCPUConsistency(unittest.TestCase):
def test_gpu_cpu_consistency(self):
with paddle.base.dygraph.guard():
np.random.seed(42)
x = np.random.rand(1000, 128).astype("float32")
index = np.random.randint(0, 1000, size=(100,))
cpu_out = paddle.gather(
paddle.to_tensor(x, place=paddle.CPUPlace()),
paddle.to_tensor(index),
)
gpu_out = paddle.gather(
paddle.to_tensor(x, place=paddle.CUDAPlace(0)),
paddle.to_tensor(index),
)
np.testing.assert_allclose(
cpu_out.numpy(), gpu_out.numpy(), rtol=1e-6
)
/opt/conda/envs/python35-paddle120-env/lib/python3.10/site-packages/paddle/base/framework.py:722: VisibleDeprecationWarning:
Warning:
API "paddle.base.dygraph.tensor_patch_methods.gradient" is deprecated since 2.1.0, and will be removed in future versions.
Reason: Please use tensor.grad, which returns the tensor value of the gradient.
return func(*args, **kwargs)
...........
----------------------------------------------------------------------
Ran 129 tests in 84.951s
OK