LLVM  14.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 
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/PassManager.h"
15 
16 namespace llvm {
17 
18 /// MLModelRunner interface: abstraction of a mechanism for evaluating a
19 /// tensorflow "saved model".
20 /// NOTE: feature indices are expected to be consistent all accross
21 /// MLModelRunners (pertaining to the same model), and also Loggers (see
22 /// TFUtils.h)
24 public:
25  // Disallows copy and assign.
26  MLModelRunner(const MLModelRunner &) = delete;
27  MLModelRunner &operator=(const MLModelRunner &) = delete;
28  virtual ~MLModelRunner() = default;
29 
30  template <typename T> T evaluate() {
31  return *reinterpret_cast<T *>(evaluateUntyped());
32  }
33 
34  template <typename T, typename I> T *getTensor(I FeatureID) {
35  return reinterpret_cast<T *>(
36  getTensorUntyped(static_cast<size_t>(FeatureID)));
37  }
38 
39  template <typename T, typename I> const T *getTensor(I FeatureID) const {
40  return reinterpret_cast<const T *>(
41  getTensorUntyped(static_cast<size_t>(FeatureID)));
42  }
43 
44  virtual void *getTensorUntyped(size_t Index) = 0;
45  const void *getTensorUntyped(size_t Index) const {
46  return (const_cast<MLModelRunner *>(this))->getTensorUntyped(Index);
47  }
48 
49  enum class Kind : int { Unknown, Release, Development, NoOp };
50  Kind getKind() const { return Type; }
51 
52 protected:
55  }
56  virtual void *evaluateUntyped() = 0;
57 
59  const Kind Type;
60 };
61 } // namespace llvm
62 
63 #endif // LLVM_ANALYSIS_MLMODELRUNNER_H
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::MLModelRunner::Kind::NoOp
@ NoOp
llvm::MLModelRunner::~MLModelRunner
virtual ~MLModelRunner()=default
llvm::MLModelRunner::getTensor
const T * getTensor(I FeatureID) const
Definition: MLModelRunner.h:39
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::getTensor
T * getTensor(I FeatureID)
Definition: MLModelRunner.h:34
llvm::MLModelRunner::MLModelRunner
MLModelRunner(const MLModelRunner &)=delete
llvm::MLModelRunner::getTensorUntyped
const void * getTensorUntyped(size_t Index) const
Definition: MLModelRunner.h:45
llvm::MLModelRunner::evaluate
T evaluate()
Definition: MLModelRunner.h:30
llvm::MLModelRunner::Kind
Kind
Definition: MLModelRunner.h:49
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::MLModelRunner
MLModelRunner(LLVMContext &Ctx, Kind Type)
Definition: MLModelRunner.h:53
llvm::MLModelRunner::Kind::Development
@ Development
llvm::MLModelRunner::evaluateUntyped
virtual void * evaluateUntyped()=0
llvm::MLModelRunner::getTensorUntyped
virtual void * getTensorUntyped(size_t Index)=0
Index
uint32_t Index
Definition: ELFObjHandler.cpp:83
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:58
llvm::MLModelRunner::Type
const Kind Type
Definition: MLModelRunner.h:59
PassManager.h
LLVMContext.h
llvm::MLModelRunner::getKind
Kind getKind() const
Definition: MLModelRunner.h:50
llvm::MLModelRunner
MLModelRunner interface: abstraction of a mechanism for evaluating a tensorflow "saved model".
Definition: MLModelRunner.h:23