Skip to content

Commit

Permalink
fix bug of inplace fill_ and zero_ API (#41229)
Browse files Browse the repository at this point in the history
* fix inplace fill_ and zero_ API

* add eager unittest
  • Loading branch information
pangyoki committed Apr 1, 2022
1 parent 93cb235 commit f1c5815
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion paddle/fluid/eager/auto_code_generator/eager_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1824,7 +1824,7 @@ static std::pair<std::string, std::string> GenerateForwardFunctionContents(
// Bump inplace version of inplace tensor.
auto inplace_input_name = inplace_map[output_name];
const char* FWD_OUT_TENSOR_TEMPLATE =
" egr::EagerUtils::ModifyInplaceInput(outs[\"%s\"][0], &%s);\n"
" egr::EagerUtils::GetOutput(outs[\"%s\"][0], &%s);\n"
" %s.bump_inplace_version();\n"
" VLOG(3) << \"Tensor(\" << %s.name() << \") uses Inplace "
"Strategy.\";\n";
Expand Down
21 changes: 0 additions & 21 deletions paddle/fluid/eager/utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -271,27 +271,6 @@ void EagerUtils::HandleViewBetweenInputAndOutput(
}
}

void EagerUtils::ModifyInplaceInput(
const std::shared_ptr<EagerVariable>& inplace_variable,
paddle::experimental::Tensor* inplace_tensor) {
// Only modify the meta information of the inplace tensor, because
// EagerVariable cannot modify Tensor's meta information after inplace
// op (such as ``reshape``) is executed.
PADDLE_ENFORCE_NOT_NULL(inplace_tensor,
paddle::platform::errors::Fatal(
"Inplace Tensor is null and cannot be modified. "
"We are tring to Modify Inplace Input from its "
"shared_ptr, this error may indicate the inplace "
" input is nullptr"));
if (phi::DenseTensor::classof(inplace_variable->GetTensorBase().get())) {
phi::DenseTensor* variable_dense_tensor =
static_cast<phi::DenseTensor*>(inplace_variable->GetTensorBase().get());
phi::DenseTensor* tensor_dense_tensor =
static_cast<phi::DenseTensor*>(inplace_tensor->impl().get());
tensor_dense_tensor->set_meta(variable_dense_tensor->meta());
}
}

std::vector<paddle::experimental::Tensor> EagerUtils::GetOutputs(
const std::vector<std::shared_ptr<EagerVariable>>& outs) {
std::vector<paddle::experimental::Tensor> res;
Expand Down
3 changes: 0 additions & 3 deletions paddle/fluid/eager/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,6 @@ class EagerUtils {
static std::vector<std::shared_ptr<EagerVariable>> CreateVars(
const size_t num);
// Construct Tensor From var
static void ModifyInplaceInput(
const std::shared_ptr<EagerVariable>& inplace_variable,
paddle::experimental::Tensor* inplace_tensor);
static std::vector<paddle::experimental::Tensor> GetOutputs(
const std::vector<std::shared_ptr<EagerVariable>>& outs);
static paddle::experimental::Tensor GetOutput(
Expand Down
6 changes: 6 additions & 0 deletions python/paddle/fluid/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ def _test_eager_guard(place=None):
if not _already_patch_eager_tensor:
monkey_patch_varbase()
monkey_patch_math_varbase()

# Ugly setting
from paddle.tensor.manipulation import fill_, zero_
setattr(core.eager.Tensor, 'fill_', fill_)
setattr(core.eager.Tensor, 'zero_', zero_)

_already_patch_eager_tensor = True
try:
yield
Expand Down
22 changes: 19 additions & 3 deletions python/paddle/fluid/tests/unittests/test_tensor_fill_.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import numpy as np
import six
import paddle
from paddle.fluid.framework import _test_eager_guard


class TensorFill_Test(unittest.TestCase):
def setUp(self):
self.shape = [32, 32]

def test_tensor_fill_true(self):
def func_test_tensor_fill_true(self):
typelist = ['float32', 'float64', 'int32', 'int64', 'float16']
places = [fluid.CPUPlace()]
if fluid.core.is_compiled_with_cuda():
Expand All @@ -46,7 +47,12 @@ def test_tensor_fill_true(self):
tensor.fill_(var) #var type is basic type in typelist
self.assertEqual((tensor.numpy() == target).all(), True)

def test_tensor_fill_backward(self):
def test_tensor_fill_true(self):
with _test_eager_guard():
self.func_test_tensor_fill_true()
self.func_test_tensor_fill_true()

def func_test_tensor_fill_backward(self):
typelist = ['float32']
places = [fluid.CPUPlace()]
if fluid.core.is_compiled_with_cuda():
Expand All @@ -71,13 +77,23 @@ def test_tensor_fill_backward(self):

self.assertEqual((y.grad.numpy() == 0).all().item(), True)

def test_errors(self):
def test_tensor_fill_backward(self):
with _test_eager_guard():
self.func_test_tensor_fill_backward()
self.func_test_tensor_fill_backward()

def func_test_errors(self):
def test_list():
x = paddle.to_tensor([2, 3, 4])
x.fill_([1])

self.assertRaises(TypeError, test_list)

def test_errors(self):
with _test_eager_guard():
self.func_test_errors()
self.func_test_errors()


if __name__ == '__main__':
unittest.main()
8 changes: 7 additions & 1 deletion python/paddle/fluid/tests/unittests/test_tensor_zero_.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import numpy as np
import six
import paddle
from paddle.fluid.framework import _test_eager_guard


class TensorFill_Test(unittest.TestCase):
def setUp(self):
self.shape = [32, 32]

def test_tensor_fill_true(self):
def func_test_tensor_fill_true(self):
typelist = ['float32', 'float64', 'int32', 'int64', 'float16']
places = [fluid.CPUPlace()]
if fluid.core.is_compiled_with_cuda():
Expand All @@ -41,6 +42,11 @@ def test_tensor_fill_true(self):
tensor.zero_()
self.assertEqual((tensor.numpy() == target).all().item(), True)

def test_tensor_fill_true(self):
with _test_eager_guard():
self.func_test_tensor_fill_true()
self.func_test_tensor_fill_true()


if __name__ == '__main__':
unittest.main()

0 comments on commit f1c5815

Please sign in to comment.