未验证 提交 d1dd7302 编写于 作者: L LoneRanger 提交者: GitHub

[fp16] suppot fp16 in std (#50936)

上级 b0a604cb
...@@ -119,5 +119,30 @@ class TestStdError(unittest.TestCase): ...@@ -119,5 +119,30 @@ class TestStdError(unittest.TestCase):
self.assertRaises(TypeError, paddle.std, x) 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__': if __name__ == '__main__':
unittest.main() unittest.main()
...@@ -119,7 +119,7 @@ def var(x, axis=None, unbiased=True, keepdim=False, name=None): ...@@ -119,7 +119,7 @@ def var(x, axis=None, unbiased=True, keepdim=False, name=None):
Computes the variance of ``x`` along ``axis`` . Computes the variance of ``x`` along ``axis`` .
Args: 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). 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`` . - 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`` .
...@@ -145,7 +145,9 @@ def var(x, axis=None, unbiased=True, keepdim=False, name=None): ...@@ -145,7 +145,9 @@ def var(x, axis=None, unbiased=True, keepdim=False, name=None):
# [1. 4.33333333] # [1. 4.33333333]
""" """
if not in_dygraph_mode(): if not in_dygraph_mode():
check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'var') check_variable_and_dtype(
x, 'x', ['float16', 'float32', 'float64'], 'var'
)
u = mean(x, axis, True, name) u = mean(x, axis, True, name)
out = paddle.sum(paddle.pow((x - u), 2), axis, keepdim=keepdim, name=name) out = paddle.sum(paddle.pow((x - u), 2), axis, keepdim=keepdim, name=name)
...@@ -168,7 +170,7 @@ def std(x, axis=None, unbiased=True, keepdim=False, name=None): ...@@ -168,7 +170,7 @@ def std(x, axis=None, unbiased=True, keepdim=False, name=None):
Computes the standard-deviation of ``x`` along ``axis`` . Computes the standard-deviation of ``x`` along ``axis`` .
Args: 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 axis (int|list|tuple, optional): The axis along which to perform
standard-deviation calculations. ``axis`` should be int, list(int) standard-deviation calculations. ``axis`` should be int, list(int)
or tuple(int). If ``axis`` is a list/tuple of dimension(s), or tuple(int). If ``axis`` is a list/tuple of dimension(s),
...@@ -211,7 +213,9 @@ def std(x, axis=None, unbiased=True, keepdim=False, name=None): ...@@ -211,7 +213,9 @@ def std(x, axis=None, unbiased=True, keepdim=False, name=None):
""" """
if not in_dygraph_mode(): 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()) out = var(**locals())
return paddle.sqrt(out) return paddle.sqrt(out)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册