#include "core.hpp" #include "tflite/delegates/xnnpack/xnnpack_delegate.h" #include #include // Opaque ABI subset of the LiteRT 2.3 TensorFlow Lite C signature-runner API. extern "C" { struct TfLiteModel; struct TfLiteInterpreterOptions; struct TfLiteInterpreter; struct TfLiteSignatureRunner; struct TfLiteTensor; const char* TfLiteVersion(); void TfLiteInterpreterOptionsAddDelegate(TfLiteInterpreterOptions*, TfLiteDelegate*); TfLiteModel* TfLiteModelCreateFromFile(const char*); void TfLiteModelDelete(TfLiteModel*); TfLiteInterpreterOptions* TfLiteInterpreterOptionsCreate(); void TfLiteInterpreterOptionsSetNumThreads(TfLiteInterpreterOptions*, int32_t); void TfLiteInterpreterOptionsDelete(TfLiteInterpreterOptions*); TfLiteInterpreter* TfLiteInterpreterCreate(const TfLiteModel*, const TfLiteInterpreterOptions*); void TfLiteInterpreterDelete(TfLiteInterpreter*); TfLiteSignatureRunner* TfLiteInterpreterGetSignatureRunner(const TfLiteInterpreter*, const char*); void TfLiteSignatureRunnerDelete(TfLiteSignatureRunner*); int TfLiteSignatureRunnerAllocateTensors(TfLiteSignatureRunner*); TfLiteTensor* TfLiteSignatureRunnerGetInputTensor(TfLiteSignatureRunner*, const char*); const TfLiteTensor* TfLiteSignatureRunnerGetOutputTensor(const TfLiteSignatureRunner*, const char*); int TfLiteSignatureRunnerInvoke(TfLiteSignatureRunner*); int TfLiteTensorCopyFromBuffer(TfLiteTensor*, const void*, size_t); int TfLiteTensorCopyToBuffer(const TfLiteTensor*, void*, size_t); } template using Handle = std::unique_ptr; class CpuGraph final : public Graph { Handle model{nullptr, TfLiteModelDelete}; bool decoder; Handle delegate{nullptr,TfLiteXNNPackDelegateDelete}; Handle interpreter{nullptr, TfLiteInterpreterDelete}; Handle signature{nullptr, TfLiteSignatureRunnerDelete}; public: CpuGraph(const fs::path& path, bool is_decoder):decoder(is_decoder) { model.reset(TfLiteModelCreateFromFile(path.c_str())); require(bool(model), "load " + path.string()); Handle opts( TfLiteInterpreterOptionsCreate(), TfLiteInterpreterOptionsDelete); require(bool(opts), "create CPU options"); TfLiteInterpreterOptionsSetNumThreads(opts.get(), 4); bool use_delegate=decoder; int threads=4; const char* cache=nullptr; #ifdef ZONOS_DECODER_PROBE const char* mode=std::getenv("ZONOS_PROBE_MODE"); if (mode && std::string(mode)=="original") use_delegate=false; if (mode && std::string(mode)=="single") threads=1; cache=std::getenv("ZONOS_PROBE_CACHE"); #endif if (use_delegate) { auto options=TfLiteXNNPackDelegateOptionsDefault(); options.num_threads=threads; options.weight_cache_file_path=cache; delegate.reset(TfLiteXNNPackDelegateCreate(&options)); require(bool(delegate),"create decoder XNNPACK delegate"); TfLiteInterpreterOptionsAddDelegate(opts.get(),delegate.get()); } #ifdef ZONOS_DECODER_PROBE if (decoder) std::cerr<<"DECODER_OPTIONS version="<(alloc-begin).count() <<" "<(std::chrono::steady_clock::now()-alloc).count()<<"\n"; #endif } void run(const std::vector& inputs, const Outputs& outputs) override { #ifdef ZONOS_DECODER_PROBE const auto start=std::chrono::steady_clock::now(); #endif for (size_t i=0; isize()*sizeof(float); std::cerr<(invoke-start).count() <<" "<(copy_out-invoke).count() <<" "<(std::chrono::steady_clock::now()-copy_out).count() <<" "< open_graph(const fs::path& path, bool decoder) { return std::make_unique(path,decoder); }