Skip to content

Commit d3dcbd3

Browse files
committed
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into move_sgd_to_phi
2 parents 943dede + 852a872 commit d3dcbd3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1138
-506
lines changed

paddle/fluid/eager/api/utils/hook_utils.cc

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,49 +52,44 @@ void RegisterReduceHookForTensor(const paddle::experimental::Tensor& tensor,
5252
}
5353
}
5454

55-
static void RetainGradForRegularNode(
56-
const paddle::experimental::Tensor& tensor) {
57-
AutogradMeta* meta = EagerUtils::unsafe_autograd_meta(tensor);
58-
if (meta->RetainGrads()) {
55+
void RetainGradForTensor(const paddle::experimental::Tensor& tensor) {
56+
if (IsLeafTensor(tensor)) {
57+
// Leaf tensor's grad will always be retained
58+
// Refer to implementation of AccumulationNode for more details
5959
return;
6060
} else {
61-
meta->SetRetainGrads(true);
62-
}
61+
AutogradMeta* meta = EagerUtils::unsafe_autograd_meta(tensor);
62+
if (meta->RetainGrads()) {
63+
return;
64+
} else {
65+
meta->SetRetainGrads(true);
66+
}
6367

64-
std::weak_ptr<paddle::experimental::Tensor> weak_grad_tensor =
65-
meta->WeakGrad();
68+
std::weak_ptr<paddle::experimental::Tensor> weak_grad_tensor =
69+
meta->WeakGrad();
6670

67-
// Define Hook
68-
auto hook = [weak_grad_tensor](const paddle::experimental::Tensor& t) {
69-
if (!weak_grad_tensor.expired()) {
70-
auto grad_tensor = weak_grad_tensor.lock();
71-
if (t.defined()) {
72-
VLOG(7) << "Set impl for RetainGrad Hook for tensor: " << t.name();
73-
// Simply Copy impl() to grad_tensor
74-
grad_tensor->set_impl(t.impl());
75-
return *grad_tensor.get();
71+
// Define Hook
72+
auto hook = [weak_grad_tensor](const paddle::experimental::Tensor& t) {
73+
if (!weak_grad_tensor.expired()) {
74+
auto grad_tensor = weak_grad_tensor.lock();
75+
if (t.defined()) {
76+
VLOG(7) << "Set impl for RetainGrad Hook for tensor: " << t.name();
77+
// Simply Copy impl() to grad_tensor
78+
grad_tensor->set_impl(t.impl());
79+
return *grad_tensor.get();
80+
} else {
81+
VLOG(7) << "Retain NULL paddle::experimental::Tensor in Grad Hook";
82+
return paddle::experimental::Tensor();
83+
}
7684
} else {
7785
VLOG(7) << "Retain NULL paddle::experimental::Tensor in Grad Hook";
7886
return paddle::experimental::Tensor();
7987
}
80-
} else {
81-
VLOG(7) << "Retain NULL paddle::experimental::Tensor in Grad Hook";
82-
return paddle::experimental::Tensor();
83-
}
84-
};
88+
};
8589

86-
// Append to GradientHooks
87-
RegisterGradientHookForTensor(tensor,
88-
std::make_shared<egr::CppTensorHook>(hook));
89-
}
90-
91-
void RetainGradForTensor(const paddle::experimental::Tensor& tensor) {
92-
if (IsLeafTensor(tensor)) {
93-
// Leaf tensor's grad will always be retained
94-
// Refer to implementation of AccumulationNode for more details
95-
return;
96-
} else {
97-
RetainGradForRegularNode(tensor);
90+
// Append to GradientHooks
91+
RegisterGradientHookForTensor(tensor,
92+
std::make_shared<egr::CppTensorHook>(hook));
9893
}
9994
}
10095

paddle/fluid/eager/auto_code_generator/eager_generator.cc

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,11 +1156,13 @@ static std::string GenerateGradNodeCreationContent(
11561156
grad_node_creation_str += paddle::string::Sprintf(
11571157
SET_OUT_RANK_TEMPLATE, output_autograd_name, output_position);
11581158

1159-
const char* SET_HISTORY_TEMPLATE =
1160-
" egr::EagerUtils::SetHistory(&%s, grad_node);\n";
1161-
grad_node_creation_str +=
1162-
paddle::string::Sprintf(SET_HISTORY_TEMPLATE, output_autograd_name);
1163-
1159+
// Intermediate Tensor does not require SetHistory
1160+
if (!output.intermediate()) {
1161+
const char* SET_HISTORY_TEMPLATE =
1162+
" egr::EagerUtils::SetHistory(&%s, grad_node);\n";
1163+
grad_node_creation_str +=
1164+
paddle::string::Sprintf(SET_HISTORY_TEMPLATE, output_autograd_name);
1165+
}
11641166
const char* SET_GRAD_IN_META_TEMPLATE =
11651167
" grad_node->SetGradInMeta(&%s, %d);\n";
11661168
grad_node_creation_str += paddle::string::Sprintf(
@@ -1173,17 +1175,20 @@ static std::string GenerateGradNodeCreationContent(
11731175
grad_node_creation_str += paddle::string::Sprintf(
11741176
SET_OUT_RANK_TEMPLATE, output_autograd_name, output_position);
11751177

1176-
const char* SET_HISTORY_TEMPLATE =
1177-
" egr::EagerUtils::SetHistory(%s, grad_node);\n";
1178-
grad_node_creation_str +=
1179-
paddle::string::Sprintf(SET_HISTORY_TEMPLATE, output_autograd_name);
1180-
1178+
// Intermediate Tensor does not require SetHistory
1179+
if (!output.intermediate()) {
1180+
const char* SET_HISTORY_TEMPLATE =
1181+
" egr::EagerUtils::SetHistory(%s, grad_node);\n";
1182+
grad_node_creation_str +=
1183+
paddle::string::Sprintf(SET_HISTORY_TEMPLATE, output_autograd_name);
1184+
}
11811185
const char* SET_GRAD_IN_META_TEMPLATE =
11821186
" grad_node->SetGradInMeta(%s, %d);\n";
11831187
grad_node_creation_str += paddle::string::Sprintf(
11841188
SET_GRAD_IN_META_TEMPLATE, output_autograd_name, output_position);
11851189
}
11861190

1191+
// Intermediate Tensor does not require CheckAndRetainGrad
11871192
if (!output.intermediate()) {
11881193
VLOG(6) << "Generated Call RetainGradForTensor";
11891194
const char* RETAIN_GRAD_TEMPLATE =

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
core_ops_args_type_info = {}
2525

2626

27+
yaml_types_mapping = {
28+
'int' : 'int', 'int32_t' : 'int32_t', 'int64_t' : 'int64_t', 'size_t' : 'size_t', \
29+
'float' : 'float', 'double' : 'double', 'bool' : 'bool', \
30+
'Backend' : 'Backend', 'DataLayout' : 'DataLayout', 'DataType' : 'DataType', \
31+
'int64_t[]' : 'std::vector<int64_t>', 'int[]' : 'std::vector<int>',
32+
'Tensor' : 'Tensor',
33+
'Tensor[]' : 'std::vector<Tensor>',
34+
'Tensor[Tensor[]]' : 'std::vector<std::vector<Tensor>>'
35+
}
36+
37+
2738
def ParseArguments():
2839
parser = argparse.ArgumentParser(
2940
description='Eager Code Generator Args Parser')
@@ -59,7 +70,9 @@ def IsPlainTensorType(string):
5970

6071

6172
def IsVectorTensorType(string):
62-
vector_tensor_types = ['list(Tensor)']
73+
vector_tensor_types = [
74+
'std::vector<std::vector<Tensor>>', 'std::vector<Tensor>'
75+
]
6376
if string in vector_tensor_types:
6477
return True
6578
return False
@@ -180,6 +193,9 @@ def ParseYamlArgs(string):
180193
arg_name = m.group(3).split("=")[0].strip()
181194
default_value = m.group(3).split("=")[1].strip() if len(
182195
m.group(3).split("=")) > 1 else None
196+
197+
assert arg_type in yaml_types_mapping.keys()
198+
arg_type = yaml_types_mapping[arg_type]
183199
if "Tensor" in arg_type:
184200
assert default_value is None
185201
inputs_list.append([arg_name, arg_type, i])
@@ -219,6 +235,10 @@ def ParseYamlReturnsWithName(string):
219235
m = re.search(pattern, ret)
220236
ret_type = m.group(1)
221237
ret_name = m.group(2)
238+
239+
assert ret_type in yaml_types_mapping.keys()
240+
ret_type = yaml_types_mapping[ret_type]
241+
222242
assert "Tensor" in ret_type
223243
returns_list.append([ret_name, ret_type, i])
224244

paddle/fluid/eager/backward.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,11 @@ void RunBackward(const std::vector<paddle::experimental::Tensor>& tensors,
221221
<< " 's name is: " << grad_output_tensor.name();
222222

223223
auto* next_node = next_node_shared.get();
224-
225224
if (!node_input_buffers_dict.count(next_node)) {
226-
node_input_buffers_dict[next_node] =
227-
std::make_unique<GradTensorHolder>(next_node->InputMeta());
225+
const auto& input_meta = next_node->InputMeta();
226+
auto grad_tensor_holder =
227+
std::make_unique<GradTensorHolder>(input_meta);
228+
node_input_buffers_dict[next_node] = std::move(grad_tensor_holder);
228229
}
229230
VLOG(6) << "Sum grad inputs for edge slot: " << edge_rank.first
230231
<< ", rank: " << edge_rank.second;

paddle/fluid/eager/grad_node_info.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ GradNodeBase::ApplyGradientHooks(
244244
if (!out.defined() || !out.initialized()) {
245245
out = (*hook)(tensors[slot_id][rank]);
246246
} else {
247-
// If more than one hook is registered, the input to the next hook func
247+
// If more than one hook is registered, the input to the next hook func
248248
// should be the output of the previous hook
249249
out = (*hook)(out);
250250
}

paddle/fluid/eager/utils.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,21 @@ paddle::experimental::Tensor* EagerUtils::mutable_grad(
122122
void EagerUtils::SetHistory(std::vector<AutogradMeta*>* autograd_metas,
123123
const std::shared_ptr<GradNodeBase>& grad_node) {
124124
for (const auto& autograd_meta : *autograd_metas) {
125+
if (dynamic_cast<GradNodeAccumulation*>(autograd_meta->GradNode())) {
126+
VLOG(6) << "Warning: Reseting GradNodeAccumulation for leaf tensor is "
127+
"detected";
128+
}
125129
autograd_meta->SetGradNode(grad_node);
126130
}
127131
}
128132

129133
void EagerUtils::SetHistory(AutogradMeta* autograd_meta,
130134
const std::shared_ptr<GradNodeBase>& grad_node) {
135+
if (dynamic_cast<GradNodeAccumulation*>(autograd_meta->GradNode())) {
136+
VLOG(6)
137+
<< "Warning: Reseting GradNodeAccumulation for leaf tensor is detected";
138+
}
139+
131140
autograd_meta->SetGradNode(grad_node);
132141
}
133142

paddle/fluid/framework/infershape_utils.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class InferShapeArgumentMappingContext : public phi::ArgumentMappingContext {
8888
return var_types[0] == proto::VarType::SELECTED_ROWS;
8989
}
9090

91+
bool IsForInferShape() const override { return true; }
92+
9193
private:
9294
const InferShapeContext& ctx_;
9395
};
@@ -127,7 +129,9 @@ class CompatMetaTensor : public phi::MetaTensor {
127129
}
128130
} else {
129131
auto* var = BOOST_GET_CONST(VarDesc*, var_);
130-
return phi::make_ddim(var->GetShape());
132+
133+
return var->GetShape().empty() ? phi::make_ddim({0UL})
134+
: phi::make_ddim(var->GetShape());
131135
}
132136
}
133137

paddle/fluid/framework/operator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ class ExecutionArgumentMappingContext : public phi::ArgumentMappingContext {
489489
return ctx_.OutputVar(name)->IsType<phi::SelectedRows>();
490490
}
491491

492+
bool IsForInferShape() const override { return false; }
493+
492494
private:
493495
const ExecutionContext& ctx_;
494496
};

paddle/fluid/framework/phi_utils.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@ phi::KernelKey FallBackToCpu(const OpKernelType& expected_kernel_key,
125125
return phi::KernelKey(phi::Backend::CPU, kernel_key.layout(),
126126
kernel_key.dtype());
127127
}
128+
#endif
129+
#ifdef PADDLE_WITH_IPU
130+
if (platform::is_ipu_place(expected_kernel_key.place_)) {
131+
VLOG(3) << "pten missing IPU kernel: " << op.Type()
132+
<< ", expected_kernel_key:" << expected_kernel_key
133+
<< ", fallbacking to CPU one!";
134+
return phi::KernelKey(phi::Backend::CPU, kernel_key.layout(),
135+
kernel_key.dtype());
136+
}
128137
#endif
129138
return phi::KernelKey();
130139
}

paddle/fluid/inference/tests/api/CMakeLists.txt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,23 @@ if(WITH_MKLDNN)
453453
download_int8_data_without_verify(${INT8_GOOGLENET_MODEL_DIR} "GoogleNet_int8_model.tar.gz" )
454454
inference_analysis_api_int8_test_run_custom_warmup_batch_size(test_analyzer_int8_googlenet ${INT8_IMG_CLASS_TEST_APP} ${INT8_GOOGLENET_MODEL_DIR} ${IMAGENET_DATA_PATH} 10)
455455

456+
# mobilenetv3_large_x1_0 int8
457+
set(INT8_MOBILENETV3_LARGE_MODEL_DIR "${INT8_DATA_DIR}/mobilenetv3_large")
458+
set(INT8_MOBILENETV3_FILE_NAME "MobileNetV3_large_x1_0_infer.tar")
459+
if (NOT EXISTS ${INT8_MOBILENETV3_LARGE_MODEL_DIR}/${INT8_MOBILENETV3_FILE_NAME})
460+
inference_download_and_uncompress_without_verify(${INT8_MOBILENETV3_LARGE_MODEL_DIR} "https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/" ${INT8_MOBILENETV3_FILE_NAME})
461+
endif()
462+
inference_analysis_test_run(test_analyzer_int8_mobilenetv3_large
463+
COMMAND ${INT8_IMG_CLASS_TEST_APP}
464+
ARGS --infer_model=${INT8_MOBILENETV3_LARGE_MODEL_DIR}/MobileNetV3_large_x1_0_infer
465+
--infer_data=${IMAGENET_DATA_PATH}
466+
--warmup_batch_size=50
467+
--batch_size=1
468+
--enable_int8=true
469+
--cpu_num_threads=${CPU_NUM_THREADS_ON_CI}
470+
--iterations=100
471+
--with_accuracy_layer=false)
472+
456473
### BFLOAT16 tests
457474

458475
# build test binary to be used in subsequent tests
@@ -472,6 +489,17 @@ if(WITH_MKLDNN)
472489
# mobilenetv2 bfloat16
473490
inference_analysis_api_bfloat16_test_run(test_analyzer_bfloat16_mobilenetv2 ${BF16_IMG_CLASS_TEST_APP} ${INT8_MOBILENETV2_MODEL_DIR} ${IMAGENET_DATA_PATH})
474491

492+
# mobilenetv3_large
493+
inference_analysis_test_run(test_analyzer_bfloat16_mobilenetv3_large
494+
COMMAND ${BF16_IMG_CLASS_TEST_APP}
495+
ARGS --infer_model=${INT8_MOBILENETV3_LARGE_MODEL_DIR}/MobileNetV3_large_x1_0_infer
496+
--infer_data=${IMAGENET_DATA_PATH}
497+
--batch_size=1
498+
--enable_bf16=true
499+
--paddle_num_threads=${CPU_NUM_THREADS_ON_CI}
500+
--iterations=100
501+
--with_accuracy_layer=false)
502+
475503
### Object detection models
476504
set(PASCALVOC_DATA_PATH "${INT8_DATA_DIR}/pascalvoc_val_head_300.bin")
477505
set(INT8_OBJ_DETECT_TEST_APP "test_analyzer_int8_object_detection")
@@ -739,6 +767,7 @@ if(WITH_MKLDNN)
739767
set_tests_properties(test_analyzer_quant_performance_benchmark PROPERTIES TIMEOUT 120)
740768
set_tests_properties(test_analyzer_int8_mobilenetv2 PROPERTIES TIMEOUT 120)
741769
set_tests_properties(test_analyzer_int8_mobilenetv1 PROPERTIES TIMEOUT 120)
770+
set_tests_properties(test_analyzer_int8_mobilenetv3_large PROPERTIES TIMEOUT 120)
742771
endif()
743772

744773
set_tests_properties(lite_resnet50_test PROPERTIES TIMEOUT 120)

0 commit comments

Comments
 (0)