TFUtils.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- TFUtils.h - utilities for tensorflow C API ---------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. #ifndef LLVM_ANALYSIS_UTILS_TFUTILS_H
  15. #define LLVM_ANALYSIS_UTILS_TFUTILS_H
  16. #include "llvm/Config/llvm-config.h"
  17. #ifdef LLVM_HAVE_TFLITE
  18. #include "llvm/ADT/StringMap.h"
  19. #include "llvm/Analysis/TensorSpec.h"
  20. #include "llvm/IR/LLVMContext.h"
  21. #include "llvm/Support/JSON.h"
  22. #include <memory>
  23. #include <vector>
  24. namespace llvm {
  25. /// Load a SavedModel, find the given inputs and outputs, and setup storage
  26. /// for input tensors. The user is responsible for correctly dimensioning the
  27. /// input tensors and setting their values before calling evaluate().
  28. /// To initialize:
  29. /// - construct the object
  30. /// - initialize the input tensors using initInput. Indices must correspond to
  31. /// indices in the InputNames used at construction.
  32. /// To use:
  33. /// - set input values by using getInput to get each input tensor, and then
  34. /// setting internal scalars, for all dimensions (tensors are row-major:
  35. /// https://github.com/tensorflow/tensorflow/blob/r1.5/tensorflow/c/c_api.h#L205)
  36. /// - call evaluate. The input tensors' values are not consumed after this, and
  37. /// may still be read.
  38. /// - use the outputs in the output vector
  39. class TFModelEvaluatorImpl;
  40. class EvaluationResultImpl;
  41. class TFModelEvaluator final {
  42. public:
  43. /// The result of a model evaluation. Handles the lifetime of the output
  44. /// tensors, which means that their values need to be used before
  45. /// the EvaluationResult's dtor is called.
  46. class EvaluationResult {
  47. public:
  48. EvaluationResult(const EvaluationResult &) = delete;
  49. EvaluationResult &operator=(const EvaluationResult &Other) = delete;
  50. EvaluationResult(EvaluationResult &&Other);
  51. EvaluationResult &operator=(EvaluationResult &&Other);
  52. ~EvaluationResult();
  53. /// Get a (const) pointer to the first element of the tensor at Index.
  54. template <typename T> T *getTensorValue(size_t Index) {
  55. return static_cast<T *>(getUntypedTensorValue(Index));
  56. }
  57. template <typename T> const T *getTensorValue(size_t Index) const {
  58. return static_cast<T *>(getUntypedTensorValue(Index));
  59. }
  60. /// Get a (const) pointer to the untyped data of the tensor.
  61. void *getUntypedTensorValue(size_t Index);
  62. const void *getUntypedTensorValue(size_t Index) const;
  63. private:
  64. friend class TFModelEvaluator;
  65. EvaluationResult(std::unique_ptr<EvaluationResultImpl> Impl);
  66. std::unique_ptr<EvaluationResultImpl> Impl;
  67. };
  68. TFModelEvaluator(StringRef SavedModelPath,
  69. const std::vector<TensorSpec> &InputSpecs,
  70. const std::vector<TensorSpec> &OutputSpecs,
  71. const char *Tags = "serve");
  72. ~TFModelEvaluator();
  73. TFModelEvaluator(const TFModelEvaluator &) = delete;
  74. TFModelEvaluator(TFModelEvaluator &&) = delete;
  75. /// Evaluate the model, assuming it is valid. Returns std::nullopt if the
  76. /// evaluation fails or the model is invalid, or an EvaluationResult
  77. /// otherwise. The inputs are assumed to have been already provided via
  78. /// getInput(). When returning std::nullopt, it also invalidates this object.
  79. std::optional<EvaluationResult> evaluate();
  80. /// Provides access to the input vector.
  81. template <typename T> T *getInput(size_t Index) {
  82. return static_cast<T *>(getUntypedInput(Index));
  83. }
  84. /// Returns true if the tensorflow model was loaded successfully, false
  85. /// otherwise.
  86. bool isValid() const { return !!Impl; }
  87. /// Untyped access to input.
  88. void *getUntypedInput(size_t Index);
  89. private:
  90. std::unique_ptr<TFModelEvaluatorImpl> Impl;
  91. };
  92. } // namespace llvm
  93. #endif // LLVM_HAVE_TFLITE
  94. #endif // LLVM_ANALYSIS_UTILS_TFUTILS_H
  95. #ifdef __GNUC__
  96. #pragma GCC diagnostic pop
  97. #endif