TFLiteUtils.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. //===- TFUtils.cpp - tensorflow evaluation utilities ----------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements utilities for interfacing with tensorflow C APIs.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Config/config.h"
  13. #if defined(LLVM_HAVE_TFLITE)
  14. #include "llvm/ADT/Twine.h"
  15. #include "llvm/Analysis/Utils/TFUtils.h"
  16. #include "llvm/Support/Base64.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Support/JSON.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include "llvm/Support/Path.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #error #include "tensorflow/lite/interpreter.h"
  24. #error #include "tensorflow/lite/kernels/register.h"
  25. #error #include "tensorflow/lite/model.h"
  26. #error #include "tensorflow/lite/model_builder.h"
  27. #error #include "tensorflow/lite/op_resolver.h"
  28. #error #include "tensorflow/lite/logger.h"
  29. #include <cassert>
  30. #include <numeric>
  31. #include <optional>
  32. using namespace llvm;
  33. namespace llvm {
  34. class EvaluationResultImpl {
  35. public:
  36. EvaluationResultImpl(const std::vector<const TfLiteTensor *> &Outputs)
  37. : Outputs(Outputs){};
  38. const TfLiteTensor *getOutput(size_t I) { return Outputs[I]; }
  39. EvaluationResultImpl(const EvaluationResultImpl &) = delete;
  40. EvaluationResultImpl(EvaluationResultImpl &&Other) = delete;
  41. private:
  42. const std::vector<const TfLiteTensor *> Outputs;
  43. };
  44. class TFModelEvaluatorImpl {
  45. public:
  46. TFModelEvaluatorImpl(StringRef SavedModelPath,
  47. const std::vector<TensorSpec> &InputSpecs,
  48. const std::vector<TensorSpec> &OutputSpecs,
  49. const char *Tags);
  50. bool isValid() const { return IsValid; }
  51. size_t outputSize() const { return Output.size(); }
  52. std::unique_ptr<EvaluationResultImpl> evaluate() {
  53. Interpreter->Invoke();
  54. return std::make_unique<EvaluationResultImpl>(Output);
  55. }
  56. const std::vector<TfLiteTensor *> &getInput() const { return Input; }
  57. ~TFModelEvaluatorImpl();
  58. private:
  59. std::unique_ptr<tflite::FlatBufferModel> Model;
  60. /// The objects necessary for carrying out an evaluation of the SavedModel.
  61. /// They are expensive to set up, and we maintain them accross all the
  62. /// evaluations of the model.
  63. std::unique_ptr<tflite::Interpreter> Interpreter;
  64. /// The input tensors. We set up the tensors once and just mutate theirs
  65. /// scalars before each evaluation. The input tensors keep their value after
  66. /// an evaluation.
  67. std::vector<TfLiteTensor *> Input;
  68. /// The output nodes.
  69. std::vector<const TfLiteTensor *> Output;
  70. void invalidate() { IsValid = false; }
  71. bool IsValid = true;
  72. /// Reusable utility for ensuring we can bind the requested Name to a node in
  73. /// the SavedModel Graph.
  74. bool checkReportAndInvalidate(const TfLiteTensor *Tensor,
  75. const TensorSpec &Spec);
  76. };
  77. } // namespace llvm
  78. TFModelEvaluatorImpl::TFModelEvaluatorImpl(
  79. StringRef SavedModelPath, const std::vector<TensorSpec> &InputSpecs,
  80. const std::vector<TensorSpec> &OutputSpecs, const char *Tags = "serve")
  81. : Input(InputSpecs.size()), Output(OutputSpecs.size()) {
  82. // INFO and DEBUG messages could be numerous and not particularly interesting
  83. tflite::LoggerOptions::SetMinimumLogSeverity(tflite::TFLITE_LOG_WARNING);
  84. // FIXME: make ErrorReporter a member (may also need subclassing
  85. // StatefulErrorReporter) to easily get the latest error status, for
  86. // debugging.
  87. tflite::StderrReporter ErrorReporter;
  88. SmallVector<char, 128> TFLitePathBuff;
  89. llvm::sys::path::append(TFLitePathBuff, SavedModelPath, "model.tflite");
  90. StringRef TFLitePath(TFLitePathBuff.data(), TFLitePathBuff.size());
  91. Model = tflite::FlatBufferModel::BuildFromFile(TFLitePath.str().c_str(),
  92. &ErrorReporter);
  93. if (!Model) {
  94. invalidate();
  95. return;
  96. }
  97. tflite::ops::builtin::BuiltinOpResolver Resolver;
  98. tflite::InterpreterBuilder Builder(*Model, Resolver);
  99. Builder(&Interpreter);
  100. if (!Interpreter) {
  101. invalidate();
  102. return;
  103. }
  104. // We assume the input buffers are valid for the lifetime of the interpreter.
  105. // By default, tflite allocates memory in an arena and will periodically take
  106. // away memory and reallocate it in a different location after evaluations in
  107. // order to improve utilization of the buffers owned in the arena. So, we
  108. // explicitly mark our input buffers as persistent to avoid this behavior.
  109. for (size_t I = 0; I < Interpreter->inputs().size(); ++I)
  110. Interpreter->tensor(I)->allocation_type =
  111. TfLiteAllocationType::kTfLiteArenaRwPersistent;
  112. if (Interpreter->AllocateTensors() != TfLiteStatus::kTfLiteOk) {
  113. invalidate();
  114. return;
  115. }
  116. // Known inputs and outputs
  117. StringMap<int> InputsMap;
  118. StringMap<int> OutputsMap;
  119. for (size_t I = 0; I < Interpreter->inputs().size(); ++I)
  120. InputsMap[Interpreter->GetInputName(I)] = I;
  121. for (size_t I = 0; I < Interpreter->outputs().size(); ++I)
  122. OutputsMap[Interpreter->GetOutputName(I)] = I;
  123. size_t NumberFeaturesPassed = 0;
  124. for (size_t I = 0; I < InputSpecs.size(); ++I) {
  125. auto &InputSpec = InputSpecs[I];
  126. auto MapI = InputsMap.find(InputSpec.name() + ":" +
  127. std::to_string(InputSpec.port()));
  128. if (MapI == InputsMap.end()) {
  129. Input[I] = nullptr;
  130. continue;
  131. }
  132. Input[I] = Interpreter->tensor(MapI->second);
  133. if (!checkReportAndInvalidate(Input[I], InputSpec))
  134. return;
  135. std::memset(Input[I]->data.data, 0,
  136. InputSpecs[I].getTotalTensorBufferSize());
  137. ++NumberFeaturesPassed;
  138. }
  139. if (NumberFeaturesPassed < Interpreter->inputs().size()) {
  140. // we haven't passed all the required features to the model, throw an error.
  141. errs() << "Required feature(s) have not been passed to the ML model";
  142. invalidate();
  143. return;
  144. }
  145. for (size_t I = 0; I < OutputSpecs.size(); ++I) {
  146. const auto &OutputSpec = OutputSpecs[I];
  147. Output[I] = Interpreter->output_tensor(
  148. OutputsMap[OutputSpec.name() + ":" +
  149. std::to_string(OutputSpec.port())]);
  150. if (!checkReportAndInvalidate(Output[I], OutputSpec))
  151. return;
  152. }
  153. }
  154. TFModelEvaluator::TFModelEvaluator(StringRef SavedModelPath,
  155. const std::vector<TensorSpec> &InputSpecs,
  156. const std::vector<TensorSpec> &OutputSpecs,
  157. const char *Tags)
  158. : Impl(new TFModelEvaluatorImpl(SavedModelPath, InputSpecs, OutputSpecs,
  159. Tags)) {
  160. if (!Impl->isValid())
  161. Impl.reset();
  162. }
  163. TFModelEvaluatorImpl::~TFModelEvaluatorImpl() {}
  164. bool TFModelEvaluatorImpl::checkReportAndInvalidate(const TfLiteTensor *Tensor,
  165. const TensorSpec &Spec) {
  166. if (!Tensor) {
  167. errs() << "Could not find TF_Output named: " + Spec.name();
  168. IsValid = false;
  169. }
  170. if (Spec.getTotalTensorBufferSize() != Tensor->bytes)
  171. IsValid = false;
  172. // If the total sizes match, there could still be a mismatch in the shape.
  173. // We ignore that for now.
  174. return IsValid;
  175. }
  176. std::optional<TFModelEvaluator::EvaluationResult> TFModelEvaluator::evaluate() {
  177. if (!isValid())
  178. return std::nullopt;
  179. return EvaluationResult(Impl->evaluate());
  180. }
  181. void *TFModelEvaluator::getUntypedInput(size_t Index) {
  182. TfLiteTensor *T = Impl->getInput()[Index];
  183. if (!T)
  184. return nullptr;
  185. return T->data.data;
  186. }
  187. TFModelEvaluator::EvaluationResult::EvaluationResult(
  188. std::unique_ptr<EvaluationResultImpl> Impl)
  189. : Impl(std::move(Impl)) {}
  190. TFModelEvaluator::EvaluationResult::EvaluationResult(EvaluationResult &&Other)
  191. : Impl(std::move(Other.Impl)) {}
  192. TFModelEvaluator::EvaluationResult &
  193. TFModelEvaluator::EvaluationResult::operator=(EvaluationResult &&Other) {
  194. Impl = std::move(Other.Impl);
  195. return *this;
  196. }
  197. void *TFModelEvaluator::EvaluationResult::getUntypedTensorValue(size_t Index) {
  198. return Impl->getOutput(Index)->data.data;
  199. }
  200. const void *
  201. TFModelEvaluator::EvaluationResult::getUntypedTensorValue(size_t Index) const {
  202. return Impl->getOutput(Index)->data.data;
  203. }
  204. TFModelEvaluator::EvaluationResult::~EvaluationResult() {}
  205. TFModelEvaluator::~TFModelEvaluator() {}
  206. #endif // defined(LLVM_HAVE_TFLITE)