Skip to content

Commit

Permalink
finally
Browse files Browse the repository at this point in the history
  • Loading branch information
a162837 committed Nov 2, 2024
1 parent 33efec7 commit 9ac124d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
24 changes: 20 additions & 4 deletions python/paddle/tensor/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -3814,8 +3814,16 @@ def clip(
min_n = check_clip_tensor(x, min, min_, value_dtype, 'min')
max_n = check_clip_tensor(x, max, max_, value_dtype, 'max')

min_n = paddle.broadcast_to(min_n, x.shape) if min_n.shape != x.shape else min_n
max_n = paddle.broadcast_to(max_n, x.shape) if max_n.shape != x.shape else max_n
min_n = (
paddle.broadcast_to(min_n, x.shape)
if min_n.shape != x.shape
else min_n
)
max_n = (
paddle.broadcast_to(max_n, x.shape)
if max_n.shape != x.shape
else max_n
)

output_min = paddle.where(x < min_n, min_n, x)
output = paddle.where(output_min > max_n, max_n, output_min)
Expand Down Expand Up @@ -3921,8 +3929,16 @@ def clip_(
max = check_clip_tensor(x, max, fmin, x.dtype, 'max')
min = check_clip_tensor(x, min, fmin, x.dtype, 'min')

max_expand = paddle.broadcast_to(max, x.shape) if max.shape != x.shape else max
min_expand = paddle.broadcast_to(min, x.shape) if min.shape != x.shape else min
max_expand = (
paddle.broadcast_to(max, x.shape)
if max.shape != x.shape
else max
)
min_expand = (
paddle.broadcast_to(min, x.shape)
if min.shape != x.shape
else min
)

paddle.where_(x > min_expand, x, min_expand)
return paddle.where_(x < max_expand, x, max_expand)
Expand Down
63 changes: 55 additions & 8 deletions test/legacy_test/test_clip_tensor_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,13 @@ def test_static_clip(self):
)
data = np.random.random(data_shape).astype('float32')
min_data = np.random.random([1]).astype('float32')
max_data = min_data + 10.
max_data = min_data + 10.0
paddle.enable_static()
with paddle.static.program_guard(paddle.static.Program()):
x = paddle.static.data(name='x', shape=data_shape, dtype='float32')
min = paddle.static.data(
name='min', shape=[1], dtype='float32'
)
max = paddle.static.data(
name='max', shape=[1], dtype='float32'
)
out = paddle.clip(x, min)
min = paddle.static.data(name='min', shape=[1], dtype='float32')
max = paddle.static.data(name='max', shape=[1], dtype='float32')
out = paddle.clip(x, min, max)
exe = base.Executor(self.place)
res = exe.run(
feed={"x": data, 'min': min_data, 'max': max_data},
Expand All @@ -137,6 +133,34 @@ def test_static_clip(self):
np.testing.assert_allclose(res_np, res[0], rtol=1e-05)
paddle.disable_static()

if base.core.is_compiled_with_cuda():
paddle.enable_static()
data_shape = [1, 9, 9, 4]
data = np.random.random(data_shape).astype('float16')

with paddle.static.program_guard(paddle.static.Program()):
images = paddle.static.data(
name='image1', shape=data_shape, dtype='float16'
)
min = paddle.static.data(
name='min1', shape=[1], dtype='float16'
)
max = paddle.static.data(
name='max1', shape=[1], dtype='float16'
)
out = paddle.clip(images, min, max)
place = paddle.CUDAPlace(0)
exe = paddle.static.Executor(place)
res1 = exe.run(
feed={
"image1": data,
"min1": np.array([0.2]).astype('float16'),
"max1": np.array([0.8]).astype('float16'),
},
fetch_list=[out],
)
paddle.disable_static()

def test_dygraph_clip(self):
self.place = (
base.CUDAPlace(0)
Expand Down Expand Up @@ -197,6 +221,29 @@ def test_dygraph_clip(self):
out = paddle.clip(data, min_data, max_data)
np.testing.assert_allclose(out.numpy(), out_np, rtol=1e-05)

if base.core.is_compiled_with_cuda():
data_shape = [1, 2, 3, 4]
data = np.random.random(data_shape).astype('float16')
min_data = np.random.random(data_shape[-2:]).astype('float16')
max_data = np.random.random(data_shape[-1:]).astype('float16')
out_np = np.clip(data, min_data, max_data)
data = paddle.to_tensor(data)
min_data = paddle.to_tensor(min_data)
max_data = paddle.to_tensor(max_data)
out = paddle.clip(data, min_data, max_data)
np.testing.assert_allclose(out.numpy(), out_np, rtol=1e-05)

data_shape = [1, 2, 3, 4]
data = np.random.random(data_shape).astype('float16')
min_data = -10.0
max_data = 10.0
out_np = np.clip(data, min_data, max_data)
data = paddle.to_tensor(data)
min_data = paddle.to_tensor(min_data)
max_data = paddle.to_tensor(max_data)
out = paddle.clip(data, min_data, max_data)
np.testing.assert_allclose(out.numpy(), out_np, rtol=1e-05)

paddle.enable_static()

def test_shapeerror_clip(self):
Expand Down

0 comments on commit 9ac124d

Please sign in to comment.