def test_static(self):
        for use_cuda in ([False, True] if
                         paddle.device.is_compiled_with_cuda() else [False]):
            place = paddle.CUDAPlace(0) if use_cuda else paddle.CPUPlace()

            paddle.enable_static()
            prog = paddle.static.Program()
            with paddle.static.program_guard(prog):
                x = paddle.static.data(name=f"x_{self.dtype}",
                                       shape=[13, 17],
                                       dtype=self.dtype)
                y = paddle.static.data(name=f"y_{self.dtype}",
                                       shape=[13, 17],
                                       dtype=self.dtype)
                out = paddle.heaviside(x, y)

            exe = paddle.static.Executor(place=place)
            res = exe.run(prog,
                          feed={
                              f"x_{self.dtype}": self.x_np,
                              f"y_{self.dtype}": self.y_np
                          },
                          fetch_list=out,
                          use_prune=True)

            self.assertTrue(np.allclose(res, self.out_np))
    def test_dygraph(self):
        for use_cuda in ([False, True] if
                         paddle.device.is_compiled_with_cuda() else [False]):
            place = paddle.CUDAPlace(0) if use_cuda else paddle.CPUPlace()
            paddle.disable_static(place=place)
            result = paddle.heaviside(paddle.to_tensor(self.x_np),
                                      paddle.to_tensor(self.y_np))

            self.assertTrue(np.allclose(result.numpy(), self.out_np))
    def test_broadcast(self):
        paddle.disable_static()
        self.tensor_1 = paddle.to_tensor(self.input_1)
        self.tensor_2 = paddle.to_tensor(self.input_2)
        self.tensor_3 = paddle.to_tensor(self.input_3)
        self.tensor_4 = paddle.to_tensor(self.input_4)
        self.tensor_5 = paddle.to_tensor(self.input_5)

        res = paddle.heaviside(self.tensor_1, self.tensor_2)
        res = res.numpy()
        self.assertTrue(np.allclose(res, self.np_expected1))

        res = paddle.heaviside(self.tensor_2, self.tensor_3)
        res = res.numpy()
        self.assertTrue(np.allclose(res, self.np_expected2))

        res = paddle.heaviside(self.tensor_2, self.tensor_4)
        res = res.numpy()
        self.assertTrue(np.allclose(res, self.np_expected3))

        res = paddle.heaviside(self.tensor_4, self.tensor_5)
        res = res.numpy()
        self.assertTrue(np.allclose(res, self.np_expected4))
 def test_input_xy():
     paddle.heaviside(paddle.randn([100], 'float32'),
                      paddle.randn([100], 'float64'))
 def test_input_y():
     paddle.heaviside(paddle.randn([100]), 1)
 def test_input_x():
     paddle.heaviside(1, paddle.randn([100]))