diff --git a/paddle/fluid/pybind/eager_properties.cc b/paddle/fluid/pybind/eager_properties.cc index 7af221b9ac82ecb3c7c11c6d791d5a2b41cfcba2..0473c29a3342bec49570190144bfeae6fb6b64a9 100644 --- a/paddle/fluid/pybind/eager_properties.cc +++ b/paddle/fluid/pybind/eager_properties.cc @@ -37,6 +37,13 @@ extern PyTypeObject* p_tensor_type; PyObject* tensor_properties_get_name(TensorObject* self, void* closure) { EAGER_TRY + // NOTE(dev): [why not use egr::Controller::Instance::GernerateUniqueName()?] + // Beacause Controller must holder a tracer, but 'tensor.name' maybe called + // everywhere such as static mode in @to_static, which means tracer is None. + static egr::UniqueNameGenerator name_generator; + if (self->tensor.name().empty()) { + self->tensor.set_name(name_generator.Generate()); + } return ToPyObject(self->tensor.name()); EAGER_CATCH_AND_THROW_RETURN_NULL } diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_spec_names.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_spec_names.py index bafc4707c4ad996885c0321a22bb44943018577b..361fcbf9c73f5b056389e45c58b83e5e6308d000 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_spec_names.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_spec_names.py @@ -16,8 +16,6 @@ import paddle from paddle.nn import Layer import numpy as np import unittest -from paddle.fluid.framework import _enable_legacy_dygraph -_enable_legacy_dygraph() class Net(Layer):