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

Change _numel func logic and add test #37810

Merged
merged 1 commit into from
Dec 3, 2021
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
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