|
|
@ -224,6 +224,11 @@ def cast(x, dtype):
|
|
|
|
x = paddle.to_tensor([2, 3, 4], 'float64')
|
|
|
|
x = paddle.to_tensor([2, 3, 4], 'float64')
|
|
|
|
y = paddle.cast(x, 'uint8')
|
|
|
|
y = paddle.cast(x, 'uint8')
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
|
|
|
|
if in_dygraph_mode():
|
|
|
|
|
|
|
|
if not isinstance(dtype, core.VarDesc.VarType):
|
|
|
|
|
|
|
|
dtype = convert_np_dtype_to_dtype_(dtype)
|
|
|
|
|
|
|
|
out = core.ops.cast(x, 'in_dtype', x.dtype, 'out_dtype', dtype)
|
|
|
|
|
|
|
|
|
|
|
|
check_variable_and_dtype(
|
|
|
|
check_variable_and_dtype(
|
|
|
|
x, 'x',
|
|
|
|
x, 'x',
|
|
|
|
['bool', 'float16', 'float32', 'float64', 'int32', 'int64', 'uint8'],
|
|
|
|
['bool', 'float16', 'float32', 'float64', 'int32', 'int64', 'uint8'],
|
|
|
@ -234,7 +239,8 @@ def cast(x, dtype):
|
|
|
|
], 'cast')
|
|
|
|
], 'cast')
|
|
|
|
|
|
|
|
|
|
|
|
helper = LayerHelper('cast', **locals())
|
|
|
|
helper = LayerHelper('cast', **locals())
|
|
|
|
out = helper.create_variable_for_type_inference(dtype=dtype)
|
|
|
|
out = helper.create_variable_for_type_inference(
|
|
|
|
|
|
|
|
dtype=dtype, stop_gradient=x.stop_gradient)
|
|
|
|
helper.append_op(
|
|
|
|
helper.append_op(
|
|
|
|
type='cast',
|
|
|
|
type='cast',
|
|
|
|
inputs={'X': [x]},
|
|
|
|
inputs={'X': [x]},
|
|
|
|