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

[CINN+PIR]Fix Fetch XShape Variable logic #60722

Merged
merged 1 commit into from
Jan 11, 2024
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
7 changes: 6 additions & 1 deletion paddle/fluid/framework/new_executor/feed_fetch_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ void FetchTensors(const std::vector<std::string>& job_fetch_names,
auto& src = var->Get<phi::DenseTensor>();
auto* dst =
&(PADDLE_GET(phi::DenseTensor, fetch_list->at(micro_batch_id)[col]));
TensorCopy(src, platform::CPUPlace(), dst);
if (src.IsInitialized()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主要是这一行。之前是默认都要有数据区的,若不满足就会报错。这里是做了判断,即不会报错了,并额外输出了日志。

TensorCopy(src, platform::CPUPlace(), dst);
} else {
VLOG(6) << "Found " << var_name
<< " is not initialized and skip TensorCopy.";
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions paddle/fluid/pir/transforms/pd_op_to_kernel_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,23 @@ const std::unordered_set<std::string> SpecialLowerOps = {
SelectInputOp::name(),
"cinn_runtime.jit_kernel"};

const std::unordered_map<std::string, uint32_t> XShapeRelatedOps = {
{paddle::dialect::ReshapeOp::name(), /*xshape_idx*/ 1U},
{paddle::dialect::SqueezeOp::name(), /*xshape_idx*/ 1U},
{paddle::dialect::FlattenOp::name(), /*xshape_idx*/ 1U},
};

static bool NeedSkipPlaceTransfer(const pir::Operation* op) {
bool need_skip = false;
if (op->isa<paddle::dialect::FetchOp>()) {
auto define_op_name = op->operand_source(0).defining_op()->name();
uint32_t index = op->operand_source(0).dyn_cast<pir::OpResult>().index();
need_skip = XShapeRelatedOps.count(define_op_name) > 0 &&
(XShapeRelatedOps.at(define_op_name) == index);
}
return need_skip;
}

static bool NeedFallBackCpu(const pir::Operation* op,
const std::string& kernel,
const phi::KernelKey& kernel_key) {
Expand Down Expand Up @@ -1757,6 +1774,11 @@ std::vector<pir::Value> BuildInputs(
(op_item->isa<::pir::SetParameterOp>()) ||
(kernel.IsValid() && (!UnchangeOutputOps.count(op_item->name())));

// NOTE(Aurelius84): In case of Reshape/Squeeze/Flatten.XShape,
// Skip insert memcpyd2h for fetch op
check_place_transfer =
check_place_transfer && !NeedSkipPlaceTransfer(op_item);

if (check_place_transfer) {
if (new_in_type.isa<AllocatedDenseTensorType>()) {
// allocated type
Expand Down