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 std #50936

Merged
merged 5 commits into from
Mar 2, 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
25 changes: 25 additions & 0 deletions python/paddle/fluid/tests/unittests/test_std_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,30 @@ def test_error(self):
self.assertRaises(TypeError, paddle.std, x)


class Testfp16Std(unittest.TestCase):
def test_fp16_with_gpu(self):
paddle.enable_static()
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"
)

y = paddle.std(x)

exe = paddle.static.Executor(place)
res = exe.run(
paddle.static.default_main_program(),
feed={
"x": input,
},
fetch_list=[y],
)


if __name__ == '__main__':
unittest.main()
12 changes: 8 additions & 4 deletions python/paddle/tensor/stat.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def var(x, axis=None, unbiased=True, keepdim=False, name=None):
Computes the variance of ``x`` along ``axis`` .

Args:
x (Tensor): The input Tensor with data type float32, float64.
x (Tensor): The input Tensor with data type float16, float32, float64.
axis (int|list|tuple, optional): The axis along which to perform variance calculations. ``axis`` should be int, list(int) or tuple(int).

- If ``axis`` is a list/tuple of dimension(s), variance is calculated along all element(s) of ``axis`` . ``axis`` or element(s) of ``axis`` should be in range [-D, D), where D is the dimensions of ``x`` .
Expand All @@ -145,7 +145,9 @@ def var(x, axis=None, unbiased=True, keepdim=False, name=None):
# [1. 4.33333333]
"""
if not in_dygraph_mode():
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'var')
check_variable_and_dtype(
x, 'x', ['float16', 'float32', 'float64'], 'var'
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var的文档也要记得修改哈。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的哈👌

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还是遗漏了上面var的文档修改


u = mean(x, axis, True, name)
out = paddle.sum(paddle.pow((x - u), 2), axis, keepdim=keepdim, name=name)
Expand All @@ -168,7 +170,7 @@ def std(x, axis=None, unbiased=True, keepdim=False, name=None):
Computes the standard-deviation of ``x`` along ``axis`` .

Args:
x (Tensor): The input Tensor with data type float32, float64.
x (Tensor): The input Tensor with data type float16, float32, float64.
axis (int|list|tuple, optional): The axis along which to perform
standard-deviation calculations. ``axis`` should be int, list(int)
or tuple(int). If ``axis`` is a list/tuple of dimension(s),
Expand Down Expand Up @@ -211,7 +213,9 @@ def std(x, axis=None, unbiased=True, keepdim=False, name=None):

"""
if not in_dygraph_mode():
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'std')
check_variable_and_dtype(
x, 'x', ['float16', 'float32', 'float64'], 'std'
)
out = var(**locals())
return paddle.sqrt(out)

Expand Down