LLVM  15.0.0git
MLModelRunner.h
Go to the documentation of this file.
1 //===- MLModelRunner.h ---- ML model runner interface -----------*- C++ -*-===//
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 
10 #ifndef LLVM_ANALYSIS_MLMODELRUNNER_H
11 #define LLVM_ANALYSIS_MLMODELRUNNER_H
12 
14 #include "llvm/IR/PassManager.h"
15 
16 namespace llvm {
17 class LLVMContext;
18 
19 /// MLModelRunner interface: abstraction of a mechanism for evaluating a
20 /// tensorflow "saved model".
21 /// NOTE: feature indices are expected to be consistent all accross
22 /// MLModelRunners (pertaining to the same model), and also Loggers (see
23 /// TFUtils.h)
25 public:
26  // Disallows copy and assign.
27  MLModelRunner(const MLModelRunner &) = delete;
28  MLModelRunner &operator=(const MLModelRunner &) = delete;
29  virtual ~MLModelRunner() = default;
30 
31  template <typename T> T evaluate() {
32  return *reinterpret_cast<T *>(evaluateUntyped());
33  }
34 
35  template <typename T, typename I> T *getTensor(I FeatureID) {
36  return reinterpret_cast<T *>(
37  getTensorUntyped(static_cast<size_t>(FeatureID)));
38  }
39 
40  template <typename T, typename I> const T *getTensor(I FeatureID) const {
41  return reinterpret_cast<const T *>(
42  getTensorUntyped(static_cast<size_t>(FeatureID)));
43  }
44 
45  void *getTensorUntyped(size_t Index) { return InputBuffers[Index]; }
46  const void *getTensorUntyped(size_t Index) const {
47  return (const_cast<MLModelRunner *>(this))->getTensorUntyped(Index);
48  }
49 
50  enum class Kind : int { Unknown, Release, Development, NoOp };
51  Kind getKind() const { return Type; }
52 
53 protected:
54  MLModelRunner(LLVMContext &Ctx, Kind Type, size_t NrInputs)
55  : Ctx(Ctx), Type(Type), InputBuffers(NrInputs) {
57  }
58  virtual void *evaluateUntyped() = 0;
59 
60  void setUpBufferForTensor(size_t Index, const TensorSpec &Spec,
61  void *Buffer) {
62  if (!Buffer) {
63  OwnedBuffers.emplace_back(Spec.getTotalTensorBufferSize());
64  Buffer = OwnedBuffers.back().data();
65  }
66  InputBuffers[Index] = Buffer;
67  }
68 
70  const Kind Type;
71 
72 private:
73  std::vector<void *> InputBuffers;
74  std::vector<std::vector<char *>> OwnedBuffers;
75 };
76 } // namespace llvm
77 
78 #endif // LLVM_ANALYSIS_MLMODELRUNNER_H
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::MLModelRunner::getTensorUntyped
void * getTensorUntyped(size_t Index)
Definition: MLModelRunner.h:45
llvm::MLModelRunner::Kind::NoOp
@ NoOp
llvm::MLModelRunner::~MLModelRunner
virtual ~MLModelRunner()=default
TensorSpec.h
llvm::MLModelRunner::getTensor
const T * getTensor(I FeatureID) const
Definition: MLModelRunner.h:40
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::MLModelRunner::Kind::Release
@ Release
llvm::MLModelRunner::MLModelRunner
MLModelRunner(LLVMContext &Ctx, Kind Type, size_t NrInputs)
Definition: MLModelRunner.h:54
llvm::MLModelRunner::getTensor
T * getTensor(I FeatureID)
Definition: MLModelRunner.h:35
llvm::MLModelRunner::MLModelRunner
MLModelRunner(const MLModelRunner &)=delete
llvm::MLModelRunner::getTensorUntyped
const void * getTensorUntyped(size_t Index) const
Definition: MLModelRunner.h:46
llvm::MLModelRunner::evaluate
T evaluate()
Definition: MLModelRunner.h:31
llvm::MLModelRunner::Kind
Kind
Definition: MLModelRunner.h:50
int
Clang compiles this i1 i64 store i64 i64 store i64 i64 store i64 i64 store i64 align Which gets codegen d xmm0 movaps rbp movaps rbp movaps rbp movaps rbp rbp rbp rbp rbp It would be better to have movq s of instead of the movaps s LLVM produces ret int
Definition: README.txt:536
llvm::MLModelRunner::Kind::Unknown
@ Unknown
llvm::MLModelRunner::Kind::Development
@ Development
llvm::MLModelRunner::evaluateUntyped
virtual void * evaluateUntyped()=0
llvm::dwarf::Index
Index
Definition: Dwarf.h:472
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
I
#define I(x, y, z)
Definition: MD5.cpp:58
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MLModelRunner::operator=
MLModelRunner & operator=(const MLModelRunner &)=delete
llvm::MLModelRunner::Ctx
LLVMContext & Ctx
Definition: MLModelRunner.h:69
llvm::MLModelRunner::Type
const Kind Type
Definition: MLModelRunner.h:70
llvm::MLModelRunner::setUpBufferForTensor
void setUpBufferForTensor(size_t Index, const TensorSpec &Spec, void *Buffer)
Definition: MLModelRunner.h:60
PassManager.h
llvm::MLModelRunner::getKind
Kind getKind() const
Definition: MLModelRunner.h:51
llvm::MLModelRunner
MLModelRunner interface: abstraction of a mechanism for evaluating a tensorflow "saved model".
Definition: MLModelRunner.h:24
llvm::TensorSpec
Definition: TensorSpec.h:52
llvm::TensorSpec::getTotalTensorBufferSize
size_t getTotalTensorBufferSize() const
Get the total size of a memory buffer needed to store the whole tensor.
Definition: TensorSpec.h:78