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

unify inplace_version checking log in new and old dygraph framework #41209

Merged
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
20 changes: 10 additions & 10 deletions paddle/fluid/eager/tensor_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,25 @@ class TensorWrapper {
static_cast<phi::DenseTensor*>(intermidiate_tensor_.impl().get());
auto& inplace_version_counter = dense_tensor->InplaceVersionCounter();

uint32_t current_inplace_version =
inplace_version_counter.CurrentVersion();
uint32_t wrapper_version_snapshot = inplace_version_snapshot_;
uint32_t tensor_version = inplace_version_counter.CurrentVersion();
PADDLE_ENFORCE_EQ(
current_inplace_version, inplace_version_snapshot_,
tensor_version, wrapper_version_snapshot,
paddle::platform::errors::PermissionDenied(
"Tensor '%s' used in gradient computation has been "
"modified by an inplace operation. "
"Its version is %d but the expected version is %d. "
"Please fix your code to void calling an inplace operator "
"after using the Tensor which will used in gradient "
"computation.",
intermidiate_tensor_.name(), current_inplace_version,
inplace_version_snapshot_));
VLOG(6) << " The inplace_version_snapshot_ of Tensor '"
intermidiate_tensor_.name(), tensor_version,
wrapper_version_snapshot));
VLOG(6) << " The wrapper_version_snapshot of Tensor '"
<< intermidiate_tensor_.name() << "' is [ "
<< inplace_version_snapshot_ << " ]";
VLOG(6) << " The current_inplace_version of Tensor '"
<< intermidiate_tensor_.name() << "' is [ "
<< current_inplace_version << " ]";
<< wrapper_version_snapshot << " ]";
VLOG(6) << " The tensor_version of Tensor '"
<< intermidiate_tensor_.name() << "' is [ " << tensor_version
<< " ]";
}
}

Expand Down
34 changes: 10 additions & 24 deletions python/paddle/fluid/tests/unittests/test_inplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,11 @@ def func_test_backward_error(self):
var_d = var_b**2

loss = paddle.nn.functional.relu(var_c + var_d)
if in_dygraph_mode():
with self.assertRaisesRegexp(
RuntimeError,
"received current_inplace_version:{} != inplace_version_snapshot_:{}".
format(1, 0)):
loss.backward()
else:
with self.assertRaisesRegexp(
RuntimeError,
"received tensor_version:{} != wrapper_version_snapshot:{}".
format(1, 0)):
loss.backward()
with self.assertRaisesRegexp(
RuntimeError,
"received tensor_version:{} != wrapper_version_snapshot:{}".
format(1, 0)):
loss.backward()

def test_backward_error(self):
with _test_eager_guard():
Expand Down Expand Up @@ -207,18 +200,11 @@ def func_test_backward_error(self):
self.inplace_api_processing(var_b)

loss = paddle.nn.functional.relu(var_c)
if in_dygraph_mode():
with self.assertRaisesRegexp(
RuntimeError,
"received current_inplace_version:{} != inplace_version_snapshot_:{}".
format(1, 0)):
loss.backward()
else:
with self.assertRaisesRegexp(
RuntimeError,
"received tensor_version:{} != wrapper_version_snapshot:{}".
format(1, 0)):
loss.backward()
with self.assertRaisesRegexp(
RuntimeError,
"received tensor_version:{} != wrapper_version_snapshot:{}".
format(1, 0)):
loss.backward()

def test_backward_error(self):
with _test_eager_guard():
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/fluid/tests/unittests/test_pylayer_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def forward(self, data):
z = layer(data)
with self.assertRaisesRegexp(
RuntimeError,
"received current_inplace_version:{} != inplace_version_snapshot_:{}".
"received tensor_version:{} != wrapper_version_snapshot:{}".
format(1, 0)):
z.backward()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,11 @@ def func_test_backward_error(self):
view_var_b[0] = 2. # var_b is modified inplace

loss = paddle.nn.functional.relu(var_c)
if in_dygraph_mode():
with self.assertRaisesRegexp(
RuntimeError,
"received current_inplace_version:{} != inplace_version_snapshot_:{}".
format(1, 0)):
loss.backward()
else:
with self.assertRaisesRegexp(
RuntimeError,
"received tensor_version:{} != wrapper_version_snapshot:{}".
format(1, 0)):
loss.backward()
with self.assertRaisesRegexp(
RuntimeError,
"received tensor_version:{} != wrapper_version_snapshot:{}".
format(1, 0)):
loss.backward()

def test_backward_error(self):
with _test_eager_guard():
Expand Down