LLVM 19.0.0git
ReleaseModeModelRunner.h
Go to the documentation of this file.
1//===- ReleaseModeModelRunner.h - Fast, precompiled model runner ---------===//
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 a model runner wrapping an AOT compiled ML model.
10// Only inference is supported.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_RELEASEMODEMODELRUNNER_H
15#define LLVM_ANALYSIS_RELEASEMODEMODELRUNNER_H
16
20
21#include <memory>
22#include <vector>
23
24namespace llvm {
25
26/// ReleaseModeModelRunner - production mode implementation of the
27/// MLModelRunner. It uses an AOT-compiled SavedModel for efficient execution.
28template <class TGen>
30public:
31 /// FeatureNames' type should be an indexed collection of std::string, like
32 /// std::array or std::vector, that has a size() method.
33 template <class FType>
34 ReleaseModeModelRunner(LLVMContext &Ctx, const FType &InputSpec,
35 StringRef DecisionName, StringRef FeedPrefix = "feed_",
36 StringRef FetchPrefix = "fetch_")
38 CompiledModel(std::make_unique<TGen>()) {
39 assert(CompiledModel && "The CompiledModel should be valid");
40
41 for (size_t I = 0; I < InputSpec.size(); ++I) {
42 const int Index =
43 CompiledModel->LookupArgIndex(FeedPrefix.str() + InputSpec[I].name());
44 void *Buffer = nullptr;
45 if (Index >= 0)
46 Buffer = CompiledModel->arg_data(Index);
47 setUpBufferForTensor(I, InputSpec[I], Buffer);
48 }
49
50 ResultIndex = CompiledModel->LookupResultIndex(FetchPrefix.str() +
51 DecisionName.str());
52 assert(ResultIndex >= 0 && "Cannot find DecisionName in inlining model");
53 }
54
55 virtual ~ReleaseModeModelRunner() = default;
56
57 static bool classof(const MLModelRunner *R) {
58 return R->getKind() == MLModelRunner::Kind::Release;
59 }
60
61private:
62 void *evaluateUntyped() override {
63 CompiledModel->Run();
64 return CompiledModel->result_data(ResultIndex);
65 }
66
67 int32_t ResultIndex = -1;
68 std::unique_ptr<TGen> CompiledModel;
69};
70
71/// A mock class satisfying the interface expected by ReleaseModeModelRunner for
72/// its `TGen` parameter. Useful to avoid conditional compilation complexity, as
73/// a compile-time replacement for a real AOT-ed model.
74class NoopSavedModelImpl final {
75#define NOOP_MODEL_ERRMSG \
76 "The mock AOT-ed saved model is a compile-time stub and should not be " \
77 "called."
78
79public:
80 NoopSavedModelImpl() = default;
81 int LookupArgIndex(const std::string &) { llvm_unreachable(NOOP_MODEL_ERRMSG); }
86#undef NOOP_MODEL_ERRMSG
87};
88
89template <class T> bool isEmbeddedModelEvaluatorValid() { return true; }
90
92 return false;
93}
94} // namespace llvm
95
96#endif // LLVM_ANALYSIS_RELEASEMODEMODELRUNNER_H
#define I(x, y, z)
Definition: MD5.cpp:58
#define NOOP_MODEL_ERRMSG
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
MLModelRunner interface: abstraction of a mechanism for evaluating a ML model.
Definition: MLModelRunner.h:26
void setUpBufferForTensor(size_t Index, const TensorSpec &Spec, void *Buffer)
Definition: MLModelRunner.h:63
LLVMContext & Ctx
Definition: MLModelRunner.h:72
A mock class satisfying the interface expected by ReleaseModeModelRunner for its TGen parameter.
int LookupArgIndex(const std::string &)
int LookupResultIndex(const std::string &)
ReleaseModeModelRunner - production mode implementation of the MLModelRunner.
static bool classof(const MLModelRunner *R)
virtual ~ReleaseModeModelRunner()=default
ReleaseModeModelRunner(LLVMContext &Ctx, const FType &InputSpec, StringRef DecisionName, StringRef FeedPrefix="feed_", StringRef FetchPrefix="fetch_")
FeatureNames' type should be an indexed collection of std::string, like std::array or std::vector,...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool isEmbeddedModelEvaluatorValid()
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1680
bool isEmbeddedModelEvaluatorValid< NoopSavedModelImpl >()
const char *const DecisionName
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858