From 7adecf4050ad4627974da5e9bd654f961f843a0f Mon Sep 17 00:00:00 2001 From: Ghost Screaming Date: Sun, 23 Apr 2023 12:04:40 +0800 Subject: [PATCH] Fix bug of block desc. (#53163) (#53176) * Fix bug of reduce_sum op. When input.numel() > INT32_MAX, its result is wrong. * Remove climits. * Fix bug of BlockDesc::MoveFrom(). It's used to rebuild main_program_desc from ProgramDesc modified by Fusion Pass. As some fused operators need to create new Variables in modified ProgramDesc, MoveFrom function uses std::move() function to move these VarDesc to main_program_desc. As a result, their pointers holded by modified ProgramDesc become nullptr. When call block()->Program()->proto() function, it will call ProgramDesc::Flush() function at first, which may cause a segmentation fault. --- paddle/fluid/framework/block_desc.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/framework/block_desc.cc b/paddle/fluid/framework/block_desc.cc index f8fe099255d..a972f69b152 100644 --- a/paddle/fluid/framework/block_desc.cc +++ b/paddle/fluid/framework/block_desc.cc @@ -313,7 +313,13 @@ void BlockDesc::MoveFrom(BlockDesc *block) { } else if (attr_type == proto::AttrType::BLOCK) { ProgramDesc *program = block->Program(); std::vector old_block_desc; - for (int i = 0; i < program->Proto()->blocks_size(); ++i) { + // NOTE(GhostScreaming): don't use program->proto()->blocks_size(), + // previous assignment of new Variable in vars_ use std::move, + // which makes 'var_ptr' which holded by 'block' a nullptr. + // block->Program()->proto() will calls Flush() at firtst, + // a null var_ptr will cause segmentation fault. + int block_size = static_cast(program->Size()); + for (int i = 0; i < block_size; ++i) { // record all block desc's ptr from origin block's program old_block_desc.emplace_back(program->MutableBlock(i)); } -- GitLab