TODO(chentianyu03): Inplace with IntArray constructor
| 304 | |
| 305 | // TODO(chentianyu03): Inplace with IntArray constructor |
| 306 | phi::IntArray MakePhiIntArrayFromVarList( |
| 307 | const std::vector<framework::Variable*>& variable_list) { |
| 308 | if (variable_list.empty()) { |
| 309 | return phi::IntArray(); |
| 310 | } |
| 311 | auto expected_place = phi::TransToPhiPlace(phi::Backend::CPU); |
| 312 | |
| 313 | std::vector<int64_t> vector_data; |
| 314 | vector_data.reserve(variable_list.size()); |
| 315 | |
| 316 | for (auto* var : variable_list) { |
| 317 | phi::DataType data_type; |
| 318 | if (var->IsType<DenseTensor>()) { |
| 319 | const auto& tensor = var->Get<DenseTensor>(); |
| 320 | data_type = tensor.dtype(); |
| 321 | if (data_type == phi::DataType::INT64) { |
| 322 | const auto& tensor = var->Get<DenseTensor>(); |
| 323 | if (tensor.IsInitialized() && |
| 324 | !phi::is_same_place(tensor.place(), expected_place)) { |
| 325 | DenseTensor tmp_tensor; |
| 326 | framework::TensorCopySync(tensor, expected_place, &tmp_tensor); |
| 327 | vector_data.push_back(*tmp_tensor.data<int64_t>()); |
| 328 | } else { |
| 329 | vector_data.push_back(*tensor.data<int64_t>()); |
| 330 | } |
| 331 | } else if (data_type == phi::DataType::INT32) { |
| 332 | const auto& tensor = var->Get<DenseTensor>(); |
| 333 | if (tensor.IsInitialized() && |
| 334 | !phi::is_same_place(tensor.place(), expected_place)) { |
| 335 | DenseTensor tmp_tensor; |
| 336 | framework::TensorCopySync(tensor, expected_place, &tmp_tensor); |
| 337 | vector_data.push_back(*tmp_tensor.data<int32_t>()); |
| 338 | } else { |
| 339 | vector_data.push_back(*tensor.data<int32_t>()); |
| 340 | } |
| 341 | } else { |
| 342 | PADDLE_THROW(common::errors::InvalidArgument( |
| 343 | "Data type error. When cast a DenseTensor to VectorTensor, " |
| 344 | "the data type of DenseTensor must be int32 or int64, " |
| 345 | "but now data type is %s.", |
| 346 | data_type)); |
| 347 | } |
| 348 | } else { |
| 349 | PADDLE_THROW(common::errors::Unimplemented( |
| 350 | "Unsupported casting input `%s` type to VectorTensor when call pt " |
| 351 | "kernel.", |
| 352 | framework::ToTypeName(var->Type()))); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | phi::IntArray result{vector_data}; |
| 357 | result.SetFromTensor(true); |
| 358 | |
| 359 | return result; |
| 360 | } |
| 361 | |
| 362 | } // namespace paddle::framework |
no test coverage detected