|
|
|
|
@ -16,9 +16,9 @@ from __future__ import print_function
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
import numpy as np
|
|
|
|
|
import paddle
|
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
|
import paddle.fluid.layers as layers
|
|
|
|
|
import paddle.tensor as tensor
|
|
|
|
|
import paddle.fluid.core as core
|
|
|
|
|
from op_test import OpTest
|
|
|
|
|
from paddle.fluid import compiler, Program, program_guard
|
|
|
|
|
@ -60,61 +60,64 @@ class TestWhereOp3(TestWhereOp):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestWhereAPI(unittest.TestCase):
|
|
|
|
|
def test_api(self, use_cuda=False):
|
|
|
|
|
main_program = Program()
|
|
|
|
|
with fluid.program_guard(main_program):
|
|
|
|
|
x = fluid.layers.data(name='x', shape=[4], dtype='float32')
|
|
|
|
|
y = fluid.layers.data(name='y', shape=[4], dtype='float32')
|
|
|
|
|
x_i = np.array([0.9383, 0.1983, 3.2, 1.2]).astype("float32")
|
|
|
|
|
y_i = np.array([1.0, 1.0, 1.0, 1.0]).astype("float32")
|
|
|
|
|
cond_i = np.array([False, False, True, True]).astype("bool")
|
|
|
|
|
result = tensor.where(x > 1, x=x, y=y)
|
|
|
|
|
def setUp(self):
|
|
|
|
|
self.init_data()
|
|
|
|
|
|
|
|
|
|
for use_cuda in [False, True]:
|
|
|
|
|
if use_cuda and not fluid.core.is_compiled_with_cuda():
|
|
|
|
|
return
|
|
|
|
|
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
|
|
|
|
|
exe = fluid.Executor(place)
|
|
|
|
|
out = exe.run(fluid.default_main_program(),
|
|
|
|
|
feed={'x': x_i,
|
|
|
|
|
'y': y_i},
|
|
|
|
|
fetch_list=[result])
|
|
|
|
|
assert np.array_equal(out[0], np.where(cond_i, x_i, y_i))
|
|
|
|
|
def init_data(self):
|
|
|
|
|
self.shape = [10, 15]
|
|
|
|
|
self.cond = np.array(np.random.randint(2, size=self.shape), dtype=bool)
|
|
|
|
|
self.x = np.random.uniform(-2, 3, self.shape).astype(np.float32)
|
|
|
|
|
self.y = np.random.uniform(-2, 3, self.shape).astype(np.float32)
|
|
|
|
|
self.out = np.where(self.cond, self.x, self.y)
|
|
|
|
|
|
|
|
|
|
def test_grad(self, use_cuda=False):
|
|
|
|
|
main_program = Program()
|
|
|
|
|
with fluid.program_guard(main_program):
|
|
|
|
|
x = fluid.layers.data(name='x', shape=[4], dtype='float32')
|
|
|
|
|
y = fluid.layers.data(name='y', shape=[4], dtype='float32')
|
|
|
|
|
for x_stop_gradient, y_stop_gradient in [[False, False],
|
|
|
|
|
[True, False],
|
|
|
|
|
[False, True]]:
|
|
|
|
|
x.stop_gradient = x_stop_gradient
|
|
|
|
|
y.stop_gradient = y_stop_gradient
|
|
|
|
|
x_i = np.array([0.9383, 0.1983, 3.2, 1.2]).astype("float32")
|
|
|
|
|
y_i = np.array([1.0, 1.0, 1.0, 1.0]).astype("float32")
|
|
|
|
|
cond_i = np.array([False, False, True, True]).astype("bool")
|
|
|
|
|
result = tensor.where(x > 1, x=x, y=y)
|
|
|
|
|
x_mean = layers.mean(x)
|
|
|
|
|
append_backward(x_mean)
|
|
|
|
|
y_mean = layers.mean(y)
|
|
|
|
|
append_backward(y_mean)
|
|
|
|
|
|
|
|
|
|
for use_cuda in [False, True]:
|
|
|
|
|
if use_cuda and not fluid.core.is_compiled_with_cuda():
|
|
|
|
|
return
|
|
|
|
|
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
|
|
|
|
|
exe = fluid.Executor(place)
|
|
|
|
|
out = exe.run(
|
|
|
|
|
fluid.default_main_program(),
|
|
|
|
|
feed={'x': x_i,
|
|
|
|
|
'y': y_i},
|
|
|
|
|
fetch_list=[result, x.grad_name, y.grad_name])
|
|
|
|
|
x_grad = [0.25] * 4
|
|
|
|
|
y_grad = [0.25] * 4
|
|
|
|
|
assert np.array_equal(out[0], np.where(cond_i, x_i, y_i))
|
|
|
|
|
assert np.array_equal(out[1], x_grad)
|
|
|
|
|
assert np.array_equal(out[2], y_grad)
|
|
|
|
|
def ref_x_backward(self, dout):
|
|
|
|
|
return np.where(self.cond == True, dout, 0)
|
|
|
|
|
|
|
|
|
|
def ref_y_backward(self, dout):
|
|
|
|
|
return np.where(self.cond == False, dout, 0)
|
|
|
|
|
|
|
|
|
|
def test_api(self, use_cuda=False):
|
|
|
|
|
for x_stop_gradient in [False, True]:
|
|
|
|
|
for y_stop_gradient in [False, True]:
|
|
|
|
|
with fluid.program_guard(Program(), Program()):
|
|
|
|
|
cond = fluid.layers.data(
|
|
|
|
|
name='cond', shape=self.shape, dtype='bool')
|
|
|
|
|
x = fluid.layers.data(
|
|
|
|
|
name='x', shape=self.shape, dtype='float32')
|
|
|
|
|
y = fluid.layers.data(
|
|
|
|
|
name='y', shape=self.shape, dtype='float32')
|
|
|
|
|
x.stop_gradient = x_stop_gradient
|
|
|
|
|
y.stop_gradient = y_stop_gradient
|
|
|
|
|
result = paddle.where(cond, x, y)
|
|
|
|
|
append_backward(layers.mean(result))
|
|
|
|
|
|
|
|
|
|
for use_cuda in [False, True]:
|
|
|
|
|
if use_cuda and not fluid.core.is_compiled_with_cuda():
|
|
|
|
|
break
|
|
|
|
|
place = fluid.CUDAPlace(
|
|
|
|
|
0) if use_cuda else fluid.CPUPlace()
|
|
|
|
|
exe = fluid.Executor(place)
|
|
|
|
|
fetch_list = [result, result.grad_name]
|
|
|
|
|
if x_stop_gradient is False:
|
|
|
|
|
fetch_list.append(x.grad_name)
|
|
|
|
|
if y_stop_gradient is False:
|
|
|
|
|
fetch_list.append(y.grad_name)
|
|
|
|
|
out = exe.run(
|
|
|
|
|
fluid.default_main_program(),
|
|
|
|
|
feed={'cond': self.cond,
|
|
|
|
|
'x': self.x,
|
|
|
|
|
'y': self.y},
|
|
|
|
|
fetch_list=fetch_list)
|
|
|
|
|
assert np.array_equal(out[0], self.out)
|
|
|
|
|
if x_stop_gradient is False:
|
|
|
|
|
assert np.array_equal(out[2],
|
|
|
|
|
self.ref_x_backward(out[1]))
|
|
|
|
|
if y.stop_gradient is False:
|
|
|
|
|
assert np.array_equal(
|
|
|
|
|
out[3], self.ref_y_backward(out[1]))
|
|
|
|
|
elif y.stop_gradient is False:
|
|
|
|
|
assert np.array_equal(out[2],
|
|
|
|
|
self.ref_y_backward(out[1]))
|
|
|
|
|
|
|
|
|
|
def test_api_broadcast(self, use_cuda=False):
|
|
|
|
|
main_program = Program()
|
|
|
|
|
@ -124,9 +127,7 @@ class TestWhereAPI(unittest.TestCase):
|
|
|
|
|
x_i = np.array([[0.9383, 0.1983, 3.2, 1.2]]).astype("float32")
|
|
|
|
|
y_i = np.array([[1.0, 1.0, 1.0, 1.0],
|
|
|
|
|
[1.0, 1.0, 1.0, 1.0]]).astype("float32")
|
|
|
|
|
cond_i = np.array([[False, False, True, True],
|
|
|
|
|
[False, False, True, True]]).astype("bool")
|
|
|
|
|
result = tensor.where(x > 1, x=x, y=y)
|
|
|
|
|
result = paddle.where(x > 1, x=x, y=y)
|
|
|
|
|
|
|
|
|
|
for use_cuda in [False, True]:
|
|
|
|
|
if use_cuda and not fluid.core.is_compiled_with_cuda():
|
|
|
|
|
@ -137,7 +138,7 @@ class TestWhereAPI(unittest.TestCase):
|
|
|
|
|
feed={'x': x_i,
|
|
|
|
|
'y': y_i},
|
|
|
|
|
fetch_list=[result])
|
|
|
|
|
assert np.array_equal(out[0], np.where(cond_i, x_i, y_i))
|
|
|
|
|
assert np.array_equal(out[0], np.where(x_i > 1, x_i, y_i))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestWhereDygraphAPI(unittest.TestCase):
|
|
|
|
|
@ -149,7 +150,7 @@ class TestWhereDygraphAPI(unittest.TestCase):
|
|
|
|
|
x = fluid.dygraph.to_variable(x_i)
|
|
|
|
|
y = fluid.dygraph.to_variable(y_i)
|
|
|
|
|
cond = fluid.dygraph.to_variable(cond_i)
|
|
|
|
|
out = tensor.where(cond, x, y)
|
|
|
|
|
out = paddle.where(cond, x, y)
|
|
|
|
|
assert np.array_equal(out.numpy(), np.where(cond_i, x_i, y_i))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -161,7 +162,7 @@ class TestWhereOpError(unittest.TestCase):
|
|
|
|
|
cond_i = np.array([False, False, True, True]).astype("bool")
|
|
|
|
|
|
|
|
|
|
def test_Variable():
|
|
|
|
|
tensor.where(cond_i, x_i, y_i)
|
|
|
|
|
paddle.where(cond_i, x_i, y_i)
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, test_Variable)
|
|
|
|
|
|
|
|
|
|
@ -169,7 +170,7 @@ class TestWhereOpError(unittest.TestCase):
|
|
|
|
|
x = fluid.layers.data(name='x', shape=[4], dtype='bool')
|
|
|
|
|
y = fluid.layers.data(name='y', shape=[4], dtype='float16')
|
|
|
|
|
cond = fluid.layers.data(name='cond', shape=[4], dtype='int32')
|
|
|
|
|
tensor.where(cond, x, y)
|
|
|
|
|
paddle.where(cond, x, y)
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, test_type)
|
|
|
|
|
|
|
|
|
|
|