aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/nnapi_delegate.h
blob: 22359d557e61e3ca3a977803276f1c67a2229c22 (plain)
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
/* Copyright 2017 The TensorFlow 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.
==============================================================================*/
#ifndef TENSORFLOW_CONTRIB_LITE_NNAPI_DELEGATE_H_
#define TENSORFLOW_CONTRIB_LITE_NNAPI_DELEGATE_H_

#include "tensorflow/contrib/lite/allocation.h"
#include "tensorflow/contrib/lite/c/c_api_internal.h"
#include "tensorflow/contrib/lite/core/api/error_reporter.h"
#include "tensorflow/contrib/lite/interpreter.h"

class ANeuralNetworksModel;
class ANeuralNetworksMemory;
class ANeuralNetworksCompilation;

namespace tflite {

class NNAPIAllocation : public MMAPAllocation {
 public:
  NNAPIAllocation(const char* filename, ErrorReporter* error_reporter);
  ~NNAPIAllocation();

  size_t offset(const void* ptr) const {
    auto signed_offset = reinterpret_cast<const uint8_t*>(ptr) -
                         reinterpret_cast<const uint8_t*>(mmapped_buffer_);

    return static_cast<size_t>(signed_offset);
  }

  ANeuralNetworksMemory* memory() const { return handle_; }
  bool valid() const override { return handle_ != nullptr; }

 private:
  mutable ANeuralNetworksMemory* handle_ = nullptr;
};

class NNAPIDelegate {
 public:
  ~NNAPIDelegate();

  // Convert a tflite graph to NNAPI
  TfLiteStatus BuildGraph(Interpreter* interpreter);

  // Run
  TfLiteStatus Invoke(Interpreter* interpreter);

  // Whether the current platform supports NNAPI delegation.
  static bool IsSupported();

 private:
  // The NN API model handle
  ANeuralNetworksModel* nn_model_ = nullptr;
  // The NN API compilation handle
  ANeuralNetworksCompilation* nn_compiled_model_ = nullptr;
  // Model status
  TfLiteStatus model_status_ = kTfLiteOk;

  // List of state tensors for LSTM, RNN, SVDF.
  // NN API does not allow ops to maintain states across multiple
  // invocations. We need to manually create state input tensors from
  // corresponding state output tensors of TFLite operations, and map them
  // correctly.
  std::vector<int> model_states_inputs_;   // holds NNAPI operand ids
  std::vector<int> model_states_outputs_;  // holds TFLite tensor ids
};

}  // namespace tflite

#endif  // TENSORFLOW_CONTRIB_LITE_NNAPI_DELEGATE_H_