| 69 | } |
| 70 | |
| 71 | Status SingleOpModel::Invoke(const CompilationOptions& compile_options, |
| 72 | const RuntimeOptions& runtime_options, |
| 73 | const NodeShader& shader) { |
| 74 | std::unique_ptr<EglEnvironment> env; |
| 75 | RETURN_IF_ERROR(EglEnvironment::NewEglEnvironment(&env)); |
| 76 | |
| 77 | ObjectManager objects; |
| 78 | |
| 79 | // Create buffers for input tensors. |
| 80 | { |
| 81 | std::unordered_map<int, uint32_t> tensor_to_id; |
| 82 | for (const auto* input : graph_.inputs()) { |
| 83 | tensor_to_id[input->tensor.ref] = input->id; |
| 84 | } |
| 85 | for (const auto& input : inputs_) { |
| 86 | GlBuffer buffer; |
| 87 | RETURN_IF_ERROR(CreatePHWC4BufferFromTensor(input, &buffer)); |
| 88 | RETURN_IF_ERROR( |
| 89 | objects.RegisterBuffer(tensor_to_id[input.id], std::move(buffer))); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // Create buffers for output tensors. |
| 94 | for (const auto* output : graph_.outputs()) { |
| 95 | GlBuffer buffer; |
| 96 | RETURN_IF_ERROR(CreatePHWC4BufferFromTensorRef(output->tensor, &buffer)); |
| 97 | RETURN_IF_ERROR(objects.RegisterBuffer(output->id, std::move(buffer))); |
| 98 | } |
| 99 | |
| 100 | // Compile model. |
| 101 | GpuInfo gpu_info; |
| 102 | RETURN_IF_ERROR(RequestGpuInfo(&gpu_info)); |
| 103 | std::unique_ptr<CompiledModel> compiled_model; |
| 104 | RETURN_IF_ERROR(Compile( |
| 105 | compile_options, graph_, /*tflite_graph_io=*/std::unordered_set<int>(), |
| 106 | shader, *NewDefaultWorkgroupsCalculator(gpu_info), &compiled_model)); |
| 107 | |
| 108 | // Get inference context. |
| 109 | auto command_queue = NewCommandQueue(gpu_info); |
| 110 | std::unique_ptr<InferenceContext> inference_context; |
| 111 | RETURN_IF_ERROR(compiled_model->NewRun( |
| 112 | runtime_options, &objects, command_queue.get(), &inference_context)); |
| 113 | RETURN_IF_ERROR(inference_context->Reset()); |
| 114 | |
| 115 | // Run inference. |
| 116 | RETURN_IF_ERROR(inference_context->Execute()); |
| 117 | |
| 118 | // Copy output tensors to `output_`. |
| 119 | for (const auto* output : graph_.outputs()) { |
| 120 | TensorFloat32 tensor; |
| 121 | tensor.id = output->tensor.ref; |
| 122 | tensor.shape = output->tensor.shape; |
| 123 | tensor.data.reserve(output->tensor.shape.DimensionsProduct()); |
| 124 | RETURN_IF_ERROR( |
| 125 | CopyFromPHWC4Buffer(*objects.FindBuffer(output->id), &tensor)); |
| 126 | outputs_.push_back(std::move(tensor)); |
| 127 | } |
| 128 | return OkStatus(); |
no test coverage detected