Skip to content

Commit 8e7f5e6

Browse files
authored
[Dy2St] Fix missing Tensor name when trans to contiguous (#62896)
1 parent 65126fa commit 8e7f5e6

6 files changed

Lines changed: 16 additions & 9 deletions

File tree

paddle/fluid/eager/auto_code_generator/generator/eager_gen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1154,7 +1154,7 @@ def GenerateNodeCreationCodes(self, for_backward=False, is_inplaced=False):
11541154
for name, (ttype, pos) in forward_inputs_position_map.items():
11551155
if name in need_pre_contiguous_set:
11561156
pre_contiguous_list.append(
1157-
f"{indent}const auto& {name}_tmp = (require_any_grad && {name}.is_dense_tensor() && !std::dynamic_pointer_cast<phi::DenseTensor>({name}.impl())->meta().is_contiguous()) ? paddle::Tensor(std::make_shared<phi::DenseTensor>(paddle::experimental::Trans2Contiguous(*(std::dynamic_pointer_cast<phi::DenseTensor>({name}.impl())))), {name}.mutable_autograd_meta()) : {name};"
1157+
f"{indent}const auto& {name}_tmp = (require_any_grad && {name}.is_dense_tensor() && !std::dynamic_pointer_cast<phi::DenseTensor>({name}.impl())->meta().is_contiguous()) ? paddle::Tensor(std::make_shared<phi::DenseTensor>(paddle::experimental::Trans2Contiguous(*(std::dynamic_pointer_cast<phi::DenseTensor>({name}.impl())))), {name}.mutable_autograd_meta(), {name}.name()) : {name};"
11581158
)
11591159
self.inputs_call_list_tmp[pos] = (
11601160
self.inputs_call_list_tmp[pos] + '_tmp'

paddle/fluid/eager/to_static/run_program_op_func.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ static std::vector<paddle::Tensor> Trans2ContiguousTensors(
124124
std::make_shared<phi::DenseTensor>(
125125
paddle::experimental::Trans2Contiguous(
126126
*(std::dynamic_pointer_cast<phi::DenseTensor>(t.impl())))),
127-
t.mutable_autograd_meta());
127+
t.mutable_autograd_meta(),
128+
t.name());
128129
} else {
129130
res.emplace_back(t);
130131
}

paddle/fluid/eager/to_static/run_program_op_node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ static void ShareTensorsIntoScopeWithName(
201201
const std::vector<std::string> &tensor_names,
202202
paddle::framework::Scope *scope) {
203203
for (size_t i = 0; i < tensors.size(); ++i) {
204-
VLOG(4) << "Share Tensor Into Scope: " << i;
205204
auto name = tensor_names[i];
205+
VLOG(4) << "Share Tensor Into Scope: " << name;
206206
if (name == paddle::framework::kFakeVarName ||
207207
name == paddle::framework::kEmptyVarName) {
208208
continue;

paddle/fluid/pybind/eager_method.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1831,7 +1831,8 @@ static PyObject* tensor__setitem_dygraph(TensorObject* self,
18311831
paddle::experimental::Trans2Contiguous(
18321832
*(std::dynamic_pointer_cast<phi::DenseTensor>(
18331833
transback_sub_tensor.impl())))),
1834-
transback_sub_tensor.mutable_autograd_meta())
1834+
transback_sub_tensor.mutable_autograd_meta(),
1835+
transback_sub_tensor.name())
18351836
: transback_sub_tensor;
18361837

18371838
grad_node = std::shared_ptr<SetValueWithTensorGradNode>(

paddle/phi/api/include/tensor.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,16 @@ class PADDLE_API Tensor final {
142142
explicit Tensor(const std::string& name) : name_(name) {}
143143

144144
/**
145-
* @brief Construct a new Tensor object by a TensorBase pointer and
146-
* autograd_meta
145+
* @brief Construct a new Tensor object by a TensorBase pointer, autograd meta
146+
* and name
147147
*
148148
* @param tensor_impl
149149
* @param autograd_meta
150+
* @param name
150151
*/
151152
Tensor(std::shared_ptr<phi::TensorBase> tensor_impl,
152-
std::shared_ptr<AbstractAutogradMeta> autograd_meta);
153+
std::shared_ptr<AbstractAutogradMeta> autograd_meta,
154+
const std::string& name);
153155

154156
/* Part 2: Dimension, DataType and DataLayout methods */
155157

paddle/phi/api/lib/tensor.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ Tensor::Tensor(std::shared_ptr<phi::TensorBase> tensor_impl)
5353
}
5454

5555
Tensor::Tensor(std::shared_ptr<phi::TensorBase> tensor_impl,
56-
std::shared_ptr<AbstractAutogradMeta> autograd_meta)
57-
: impl_(std::move(tensor_impl)), autograd_meta_(std::move(autograd_meta)) {
56+
std::shared_ptr<AbstractAutogradMeta> autograd_meta,
57+
const std::string &name)
58+
: impl_(std::move(tensor_impl)),
59+
autograd_meta_(std::move(autograd_meta)),
60+
name_(name) {
5861
PADDLE_ENFORCE_NOT_NULL(
5962
impl_,
6063
phi::errors::InvalidArgument("TensorImpl with nullptr is not supported"));

0 commit comments

Comments
 (0)