| 407 | } |
| 408 | |
| 409 | static void TF_Run_Helper( |
| 410 | Session* session, const char* handle, const TF_Buffer* run_options, |
| 411 | // Input tensors |
| 412 | const std::vector<std::pair<string, Tensor>>& input_pairs, |
| 413 | // Output tensors |
| 414 | const std::vector<string>& output_tensor_names, TF_Tensor** c_outputs, |
| 415 | // Target nodes |
| 416 | const std::vector<string>& target_oper_names, TF_Buffer* run_metadata, |
| 417 | TF_Status* status) { |
| 418 | const int noutputs = output_tensor_names.size(); |
| 419 | std::vector<Tensor> outputs(noutputs); |
| 420 | Status result; |
| 421 | |
| 422 | if (handle == nullptr) { |
| 423 | RunOptions run_options_proto; |
| 424 | if (run_options != nullptr && !run_options_proto.ParseFromArray( |
| 425 | run_options->data, run_options->length)) { |
| 426 | status->status = InvalidArgument("Unparseable RunOptions proto"); |
| 427 | return; |
| 428 | } |
| 429 | if (run_metadata != nullptr && run_metadata->data != nullptr) { |
| 430 | status->status = |
| 431 | InvalidArgument("Passing non-empty run_metadata is invalid."); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | RunMetadata run_metadata_proto; |
| 436 | result = session->Run(run_options_proto, input_pairs, output_tensor_names, |
| 437 | target_oper_names, &outputs, &run_metadata_proto); |
| 438 | |
| 439 | // Serialize back to upstream client, who now owns the new buffer |
| 440 | if (run_metadata != nullptr) { |
| 441 | status->status = MessageToBuffer(run_metadata_proto, run_metadata); |
| 442 | if (TF_GetCode(status) != TF_OK) return; |
| 443 | } |
| 444 | } else { |
| 445 | // NOTE(zongheng): PRun does not support RunOptions yet. |
| 446 | result = session->PRun(handle, input_pairs, output_tensor_names, &outputs); |
| 447 | } |
| 448 | if (!result.ok()) { |
| 449 | status->status = result; |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | // Store results in c_outputs[] |
| 454 | for (int i = 0; i < noutputs; ++i) { |
| 455 | const Tensor& src = outputs[i]; |
| 456 | if (!src.IsInitialized() || src.NumElements() == 0) { |
| 457 | c_outputs[i] = |
| 458 | EmptyTensor(static_cast<TF_DataType>(src.dtype()), src.shape()); |
| 459 | continue; |
| 460 | } |
| 461 | c_outputs[i] = TF_TensorFromTensor(src, status); |
| 462 | if (TF_GetCode(status) != TF_OK) return; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | extern "C" { |
no test coverage detected