File size: 6,286 Bytes
2e0d354
9b130f3
 
 
2e0d354
 
 
 
9b130f3
 
2e0d354
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b130f3
 
2e0d354
 
 
9b130f3
2e0d354
 
 
 
 
 
9b130f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e0d354
 
 
9b130f3
 
 
2e0d354
9b130f3
 
 
 
2e0d354
 
9b130f3
 
 
2e0d354
 
 
 
 
9b130f3
 
 
2e0d354
9b130f3
 
 
2e0d354
 
 
 
 
 
9b130f3
 
 
 
 
 
 
 
 
 
 
2e0d354
 
9b130f3
 
2e0d354
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "core.hpp"
#include "tflite/delegates/xnnpack/xnnpack_delegate.h"
#include <cstdlib>
#include <iomanip>
// 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<class T, void(*Delete)(T*)> using Handle = std::unique_ptr<T, decltype(Delete)>;
class CpuGraph final : public Graph {
    Handle<TfLiteModel, TfLiteModelDelete> model{nullptr, TfLiteModelDelete};
    bool decoder;
    Handle<TfLiteDelegate,TfLiteXNNPackDelegateDelete> delegate{nullptr,TfLiteXNNPackDelegateDelete};
    Handle<TfLiteInterpreter, TfLiteInterpreterDelete> interpreter{nullptr, TfLiteInterpreterDelete};
    Handle<TfLiteSignatureRunner, TfLiteSignatureRunnerDelete> 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<TfLiteInterpreterOptions, TfLiteInterpreterOptionsDelete> 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="<<TfLiteVersion()<<" interpreter_threads=4 explicit_delegate="
                             <<use_delegate<<" delegate_threads="<<(use_delegate?threads:0)
                             <<" weight_cache="<<(cache?cache:"unset")<<"\n";
        auto begin=std::chrono::steady_clock::now();
#endif
        interpreter.reset(TfLiteInterpreterCreate(model.get(), opts.get()));
        require(bool(interpreter), "create CPU interpreter");
        signature.reset(TfLiteInterpreterGetSignatureRunner(interpreter.get(), "serving_default"));
#ifdef ZONOS_DECODER_PROBE
        auto alloc=std::chrono::steady_clock::now();
#endif
        require(signature && TfLiteSignatureRunnerAllocateTensors(signature.get()) == 0, "allocate signature");
#ifdef ZONOS_DECODER_PROBE
        if (decoder) std::cerr<<std::setprecision(12)<<"DECODER_SETUP "<<std::chrono::duration<double>(alloc-begin).count()
                        <<" "<<std::chrono::duration<double>(std::chrono::steady_clock::now()-alloc).count()<<"\n";
#endif
    }
    void run(const std::vector<View>& inputs, const Outputs& outputs) override {
#ifdef ZONOS_DECODER_PROBE
        const auto start=std::chrono::steady_clock::now();
#endif
        for (size_t i=0; i<inputs.size(); ++i) {
            auto name = "args_" + std::to_string(i);
            auto t = TfLiteSignatureRunnerGetInputTensor(signature.get(), name.c_str());
            require(t && TfLiteTensorCopyFromBuffer(t, inputs[i].data, inputs[i].size*sizeof(float)) == 0, name);
        }
#ifdef ZONOS_DECODER_PROBE
        const auto invoke=std::chrono::steady_clock::now();
#endif
        require(TfLiteSignatureRunnerInvoke(signature.get()) == 0, "invoke signature");
#ifdef ZONOS_DECODER_PROBE
        const auto copy_out=std::chrono::steady_clock::now();
#endif
        for (size_t i=0; i<outputs.size(); ++i) {
            auto name = "output_" + std::to_string(i);
            auto t = TfLiteSignatureRunnerGetOutputTensor(signature.get(), name.c_str());
            auto& v = *outputs[i];
            require(t && TfLiteTensorCopyToBuffer(t, v.data(), v.size()*sizeof(float)) == 0, name);
        }
#ifdef ZONOS_DECODER_PROBE
        if (decoder) {
            size_t in_bytes=0,out_bytes=0;
            for (auto input:inputs) in_bytes+=input.size*sizeof(float);
            for (auto output:outputs) out_bytes+=output->size()*sizeof(float);
            std::cerr<<std::setprecision(12)<<"DECODER_CALL "<<std::chrono::duration<double>(invoke-start).count()
                     <<" "<<std::chrono::duration<double>(copy_out-invoke).count()
                     <<" "<<std::chrono::duration<double>(std::chrono::steady_clock::now()-copy_out).count()
                     <<" "<<inputs.size()<<" "<<in_bytes<<" "<<outputs.size()<<" "<<out_bytes<<"\n";
        }
#endif
    }
};
std::unique_ptr<Graph> open_graph(const fs::path& path, bool decoder) {
    return std::make_unique<CpuGraph>(path,decoder);
}