Skip to content

Commit

Permalink
Fix _numel func logic and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
veyron95 committed Dec 3, 2021
1 parent c1fd1b1 commit dfa1247
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 0 additions & 4 deletions paddle/fluid/pybind/imperative.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1963,10 +1963,6 @@ void BindImperative(py::module *m_ptr) {
.def("_numel",
[](std::shared_ptr<imperative::VarBase> &self) {
auto *t = self->MutableVar()->GetMutable<framework::LoDTensor>();
PADDLE_ENFORCE_EQ(
t->IsInitialized(), true,
platform::errors::InvalidArgument(
"Tensor %s has not been initialized!", self->Name()));
return t->numel();
})
.def_property("name", &imperative::VarBase::Name,
Expand Down
8 changes: 7 additions & 1 deletion python/paddle/fluid/tests/unittests/test_var_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,14 +1279,20 @@ def test_varbase_init(self):


class TestVarBaseNumel(unittest.TestCase):
def test_numel(self):
def test_numel_normal(self):
paddle.disable_static()
np_x = np.random.random((3, 8, 8))
x = paddle.to_tensor(np_x, dtype="float64")
x_actual_numel = x._numel()
x_expected_numel = np.product((3, 8, 8))
self.assertEqual(x_actual_numel, x_expected_numel)

def test_numel_without_holder(self):
paddle.disable_static()
x_without_holder = core.VarBase()
x_actual_numel = x_without_holder._numel()
self.assertEqual(x_actual_numel, 0)


class TestVarBaseCopyGradientFrom(unittest.TestCase):
def test_copy_gradient_from(self):
Expand Down

1 comment on commit dfa1247

@paddle-bot-old
Copy link

Choose a reason for hiding this comment

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

Congratulation! Your pull request passed all required CI. You could ask reviewer(s) to approve and merge. 🎉

Please sign in to comment.