Skip to content

Commit

Permalink
[Zero-Dim] support 0D Tensor for reshape/create_parameters (PaddlePad…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhwesky2010 committed Oct 27, 2022
1 parent 2a6517d commit 34bbddf
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 40 deletions.
10 changes: 1 addition & 9 deletions paddle/phi/infermeta/unary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1460,11 +1460,6 @@ static phi::DDim ValidateShape(const std::vector<int64_t> shape,
void InferMetaFromVecValue(const MetaTensor& x,
const std::vector<int64_t>& shape,
MetaTensor* out) {
PADDLE_ENFORCE_EQ(!shape.empty(),
true,
phi::errors::InvalidArgument(
"The parameter 'shape' in ReshapeOp must be set. "
"But received 'shape' is empty."));
auto x_dims = x.dims();
auto out_dims = ValidateShape(shape, x_dims);
out->set_dims(out_dims);
Expand Down Expand Up @@ -2833,6 +2828,7 @@ void RepeatInterleaveInferMeta(const MetaTensor& x,
out->share_lod(x);
out->set_dtype(x.dtype());
}

void ReshapeInferMeta(const MetaTensor& x,
const IntArray& shape,
MetaTensor* out,
Expand All @@ -2846,10 +2842,6 @@ void ReshapeInferMeta(const MetaTensor& x,
out->share_lod(x);
return;
}
PADDLE_ENFORCE_GT(shape_data.size(),
0,
phi::errors::InvalidArgument(
"The shape's size in ReshapeOp can't be zero."));
InferMetaFromVecValue(x, shape_data, out);
}

Expand Down
10 changes: 5 additions & 5 deletions paddle/scripts/paddle_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3253,12 +3253,14 @@ function build_document_preview() {
# origin name: example
function exec_samplecode_test() {
if [ -d "${PADDLE_ROOT}/build/pr_whl" ];then
pip install ${PADDLE_ROOT}/build/pr_whl/*.whl
pip install ${PADDLE_ROOT}/build/pr_whl/*.whl --force-reinstall
else
pip install ${PADDLE_ROOT}/build/python/dist/*.whl
echo "WARNING: PR wheel is not found. Use develop wheel !!!"
pip install ${PADDLE_ROOT}/build/python/dist/*.whl --force-reinstall
fi

paddle version
python -c "import paddle;print(paddle.__version__);paddle.version.show()"

cd ${PADDLE_ROOT}/tools
if [ "$1" = "cpu" ] ; then
python sampcd_processor.py cpu; example_error=$?
Expand Down Expand Up @@ -3469,7 +3471,6 @@ function main() {
;;
build_and_check_gpu)
set +e
set +x
example_info_gpu=""
example_code_gpu=0
if [ "${WITH_GPU}" == "ON" ] ; then
Expand All @@ -3479,7 +3480,6 @@ function main() {
example_info=$(exec_samplecode_test cpu)
example_code=$?
summary_check_problems $[${example_code_gpu} + ${example_code}] "${example_info_gpu}\n${example_info}"
set -x
assert_api_spec_approvals
;;
check_whl_size)
Expand Down
12 changes: 0 additions & 12 deletions python/paddle/fluid/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -6583,10 +6583,6 @@ def __init__(self,
if dtype is None:
raise ValueError("The dtype of Parameter should not be None")

if len(shape) == 0:
raise ValueError(
"The dimensions of shape for Parameter must be greater than 0")

for each in shape:
if each < 0:
raise ValueError(
Expand Down Expand Up @@ -6687,10 +6683,6 @@ def __init__(self, shape, dtype, **kwargs):
if dtype is None:
raise ValueError("The dtype of Parameter should not be None")

if len(shape) == 0:
raise ValueError(
"The dimensions of shape for Parameter must be greater than 0")

for each in shape:
if each < 0:
raise ValueError(
Expand Down Expand Up @@ -6833,10 +6825,6 @@ def __init__(self, shape, dtype, **kwargs):
if dtype is None:
raise ValueError("The dtype of Parameter should not be None")

if len(shape) == 0:
raise ValueError(
"The dimensions of shape for Parameter must be greater than 0")

for each in shape:
if each < 0:
raise ValueError(
Expand Down
8 changes: 3 additions & 5 deletions python/paddle/fluid/tests/unittests/test_egr_python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ def constructor(self, place):
self.assertTrue(egr_tensor12.place._equals(paddle.fluid.CPUPlace()))
np.testing.assert_array_equal(egr_tensor12.numpy(), x)

zero_dim_param = EagerParamBase(shape=[], dtype="float32")
self.assertEqual(zero_dim_param.shape, [])

with self.assertRaisesRegexp(
ValueError, "The shape of Parameter should not be None"):
eager_param = EagerParamBase(shape=None, dtype="float32")
Expand All @@ -259,11 +262,6 @@ def constructor(self, place):
ValueError, "The dtype of Parameter should not be None"):
eager_param = EagerParamBase(shape=[1, 1], dtype=None)

with self.assertRaisesRegexp(
ValueError,
"The dimensions of shape for Parameter must be greater than 0"):
eager_param = EagerParamBase(shape=[], dtype="float32")

with self.assertRaisesRegexp(
ValueError,
"Each dimension of shape for Parameter must be greater than 0, but received /*"
Expand Down
8 changes: 7 additions & 1 deletion python/paddle/fluid/tests/unittests/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import copy
import paddle
from paddle.fluid.dygraph import guard
from paddle.fluid.framework import default_main_program, Variable, _test_eager_guard
from paddle.fluid.framework import default_main_program, Variable, _test_eager_guard, ParamBase
import paddle.fluid.core as core
from paddle.fluid.executor import Executor
import paddle.fluid.io as io
Expand Down Expand Up @@ -50,6 +50,9 @@ def test_parameter(self):
p = io.get_parameter_value_by_name('fc.w', exe, main_program)
np.testing.assert_array_equal(p, np.ones(shape) * val)

zero_dim_param = b.create_parameter(name='x', shape=[], dtype='float32')
self.assertEqual(zero_dim_param.shape, ())

def func_parambase(self):
with guard():
linear = paddle.nn.Linear(10, 10)
Expand All @@ -72,6 +75,9 @@ def func_parambase(self):
pram_copy2 = copy.deepcopy(param, memo)
self.assertEqual(id(param_copy), id(pram_copy2))

zero_dim_param = ParamBase(shape=[], dtype='float32')
self.assertEqual(zero_dim_param.shape, [])

def test_parambase(self):
with _test_eager_guard():
self.func_parambase()
Expand Down
24 changes: 16 additions & 8 deletions python/paddle/fluid/tests/unittests/test_reshape_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ def init_data(self):
class TestReshapeOp_ZeroDim2(OpTest):

def init_data(self):
self.ori_shape = (1)
self.new_shape = ()
self.infered_shape = ()
self.ori_shape = ()
self.new_shape = (-1)
self.infered_shape = (1)


class TestReshapeOp_ZeroDim3(OpTest):

def init_data(self):
self.ori_shape = ()
self.new_shape = (-1)
self.infered_shape = (1)
self.ori_shape = (1)
self.new_shape = ()
self.infered_shape = ()


class TestReshapeBF16Op(OpTest):
Expand Down Expand Up @@ -564,16 +564,24 @@ def test_dygraph(self):

out = paddle.reshape(x, [1])
out.backward()
self.assertEqual(out.shape, [1])
self.assertEqual(x.grad.shape, [])
self.assertEqual(out.shape, [1])
self.assertEqual(out.grad.shape, [1])

out = paddle.reshape(x, [-1, 1])
out.backward()
self.assertEqual(out.shape, [1, 1])
self.assertEqual(x.grad.shape, [])
self.assertEqual(out.shape, [1, 1])
self.assertEqual(out.grad.shape, [1, 1])

x = paddle.rand([1])
x.stop_gradient = False
out = paddle.reshape(x, [])
out.backward()
self.assertEqual(x.grad.shape, [1])
self.assertEqual(out.shape, [])
self.assertEqual(out.grad.shape, [])

paddle.enable_static()

def test_static(self):
Expand Down

0 comments on commit 34bbddf

Please sign in to comment.