// Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include #include #include #include #include "absl/status/status.h" #include "absl/status/statusor.h" #include "absl/types/optional.h" #include "base_batch_sampler.h" #include "base_cv_result.h" #include "src/common/static_infer.h" #include "src/utils/func_register.h" #include "src/utils/pp_option.h" #include "src/utils/yaml_config.h" class BasePredictor { public: BasePredictor(const absl::optional &model_dir = absl::nullopt, const absl::optional &model_name = absl::nullopt, const absl::optional &device = absl::nullopt, const std::string &precision = "fp32", const bool enable_mkldnn = true, int mkldnn_cache_capacityint = 10, int cpu_threads = 8, int batch_size = 1, const std::string sample_type = ""); virtual ~BasePredictor() = default; std::vector> Predict(const std::string &input); template std::vector> Predict(const T &input); std::unique_ptr CreateStaticInfer(); const PaddlePredictorOption &PPOption(); absl::StatusOr ModelName() { return model_name_; }; std::string ConfigPath() { return config_.ConfigYamlPath(); }; void SetBatchSize(int batch_size); virtual std::vector> Process(std::vector &batch_data) = 0; virtual void ResetResult() = 0; absl::Status BuildBatchSampler(); void SetInputPath(const std::vector &input_path) { input_path_ = input_path; }; template void Register(const std::string &key, Args &&...args); static constexpr const char *MODEL_FILE_PREFIX = "inference"; static const std::unordered_set SAMPLER_TYPE; static bool print_flag; protected: absl::optional model_dir_; YamlConfig config_; int batch_size_; std::unique_ptr batch_sampler_ptr_; std::unique_ptr pp_option_ptr_; std::vector input_path_; std::string model_name_; std::string sampler_type_; std::unordered_map> pre_op_; }; template void BasePredictor::Register(const std::string &key, Args &&...args) { auto instance = std::unique_ptr(new T(std::forward(args)...)); pre_op_[key] = std::move(instance); }; template std::vector> BasePredictor::Predict(const T &input) { std::vector> result; ResetResult(); auto batches = batch_sampler_ptr_->Apply(input); if (!batches.ok()) { INFOE("Get sample fail : %s", batches.status().ToString().c_str()); exit(-1); } input_path_ = batch_sampler_ptr_->InputPath(); for (auto &batch_data : batches.value()) { auto predictions = Process(batch_data); for (auto &prediction : predictions) { result.emplace_back(std::move(prediction)); } } return result; }