| 230 | } |
| 231 | |
| 232 | std::unique_ptr<pir::Program> ConstructForwardIrProgram( |
| 233 | const paddle::framework::BlockDesc *forward_global_block, |
| 234 | const paddle::framework::BlockDesc *backward_global_block, |
| 235 | const std::vector<std::string> &output_names, |
| 236 | const std::vector<paddle::Tensor> &x, |
| 237 | const std::vector<std::string> &x_names, |
| 238 | const std::vector<paddle::Tensor> ¶ms, |
| 239 | const phi::Place &place) { |
| 240 | std::set<std::string> set_output_names; |
| 241 | auto local_program = |
| 242 | paddle::framework::ProgramDesc(*(forward_global_block->Program())); |
| 243 | |
| 244 | for (auto op_desc : local_program.Block(0).AllOps()) { |
| 245 | for (const auto &n : op_desc->Outputs()) { |
| 246 | const auto &input_var_names = n.second; |
| 247 | for (const auto &var_name : input_var_names) { |
| 248 | set_output_names.insert(var_name); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // add data op to program |
| 254 | auto *block = local_program.MutableBlock(0); |
| 255 | for (size_t i = 0; i < x.size(); ++i) { |
| 256 | auto &name = x_names[i]; |
| 257 | auto &in_t = x[i]; |
| 258 | if (block->FindVarRecursive(name) == nullptr) { |
| 259 | continue; |
| 260 | } |
| 261 | auto p = in_t.place().GetType(); |
| 262 | |
| 263 | auto op_desc = block->PrependOp(); |
| 264 | op_desc->SetType("data"); |
| 265 | op_desc->SetAttr("shape", std::vector<int64_t>()); |
| 266 | // TODO(phlrain) : using tensor dtype |
| 267 | op_desc->SetAttr("dtype", 0); |
| 268 | op_desc->SetAttr("place", static_cast<int>(p)); |
| 269 | if (p == phi::AllocationType::CUSTOM) { |
| 270 | op_desc->SetAttr("place_device_id", in_t.place().GetDeviceId()); |
| 271 | op_desc->SetAttr("place_device_type", in_t.place().GetDeviceType()); |
| 272 | } |
| 273 | |
| 274 | op_desc->SetAttr("name", name); |
| 275 | op_desc->SetOutput("out", {name}); |
| 276 | } |
| 277 | |
| 278 | std::set<std::string> input_param_names; |
| 279 | auto sorted_params = params; |
| 280 | std::sort(sorted_params.begin(), sorted_params.end(), TensorSortHelper); |
| 281 | for (auto ¶m : sorted_params) { |
| 282 | auto &name = param.name(); |
| 283 | auto p = param.place().GetType(); |
| 284 | |
| 285 | auto op_desc = local_program.MutableBlock(0)->PrependOp(); |
| 286 | op_desc->SetType("data"); |
| 287 | op_desc->SetAttr("shape", std::vector<int64_t>()); |
| 288 | // TODO(phlrain) : using tensor dtype |
| 289 | op_desc->SetAttr("dtype", 0); |
no test coverage detected