Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fp16] suppot fp16 in broadcast #50905

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions python/paddle/fluid/tests/unittests/test_broadcast_to_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,41 @@ def test_api(self):
assert np.array_equal(res_2, np.tile(input, (1, 1)))
assert np.array_equal(res_3, np.tile(input, (1, 1)))

def test_api_fp16_gpu(self):
if paddle.fluid.core.is_compiled_with_cuda():
place = paddle.CUDAPlace(0)
with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
input = np.random.random([12, 14]).astype("float16")
x = paddle.static.data(
name="x", shape=[12, 14], dtype="float16"
)

positive_2 = paddle.fluid.layers.fill_constant([1], "int32", 12)
expand_shape = paddle.static.data(
name="expand_shape",
shape=[2],
dtype="int32",
)

out_1 = paddle.broadcast_to(x, shape=[12, 14])
out_2 = paddle.broadcast_to(x, shape=[positive_2, 14])
out_3 = paddle.broadcast_to(x, shape=expand_shape)

exe = paddle.static.Executor(place)
res_1, res_2, res_3 = exe.run(
paddle.static.default_main_program(),
feed={
"x": input,
"expand_shape": np.array([12, 14]).astype("int32"),
},
fetch_list=[out_1, out_2, out_3],
)
assert np.array_equal(res_1, np.tile(input, (1, 1)))
assert np.array_equal(res_2, np.tile(input, (1, 1)))
assert np.array_equal(res_3, np.tile(input, (1, 1)))


if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions python/paddle/tensor/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3247,7 +3247,7 @@ def broadcast_to(x, shape, name=None):


Args:
x (Tensor): The input tensor, its data type is bool, float32, float64, int32 or int64.
x (Tensor): The input tensor, its data type is bool, float16, float32, float64, int32 or int64.
shape (list|tuple|Tensor): The result shape after broadcasting. The data type is int32. If shape is a list or tuple, all its elements
should be integers or 1-D Tensors with the data type int32. If shape is a Tensor, it should be an 1-D Tensor with the data type int32.
The value -1 in shape means keeping the corresponding dimension unchanged.
Expand Down Expand Up @@ -3285,7 +3285,7 @@ def broadcast_to(x, shape, name=None):
check_variable_and_dtype(
x,
'x',
['bool', 'float32', 'float64', 'int32', 'int64'],
['bool', 'float16', 'float32', 'float64', 'int32', 'int64'],
'broadcast_to',
)
check_type(shape, 'shape', (list, tuple, Variable), 'broadcast_to')
Expand Down