1 //===- DevelopmentModeInlineAdvisor.cpp - runtime-loadable model runner --===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements a model runner using TFLite, allowing the
10 // loading of a model from a command line option.
12 //===----------------------------------------------------------------------===//
13 #include "llvm/Analysis/TensorSpec.h"
14 #include "llvm/Config/config.h"
15 #if defined(LLVM_HAVE_TFLITE)
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/Analysis/CallGraph.h"
19 #include "llvm/Analysis/InlineSizeEstimatorAnalysis.h"
20 #include "llvm/Analysis/MLInlineAdvisor.h"
21 #include "llvm/Analysis/ModelUnderTrainingRunner.h"
22 #include "llvm/Analysis/NoInferenceModelRunner.h"
23 #include "llvm/Analysis/Utils/TFUtils.h"
24 #include "llvm/Analysis/Utils/TrainingLogger.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/ManagedStatic.h"
34 static cl::opt
<std::string
> TrainingLog(
35 "training-log", cl::Hidden
,
36 cl::desc("Path where the development - mode inlining log is saved."));
38 static cl::opt
<std::string
> TFModelUnderTrainingPath(
39 "ml-inliner-model-under-training", cl::Hidden
,
40 cl::desc(R
"(Path to SavedModel from the previous training iteration.
41 The directory is also expected to contain a JSON specification of the
42 outputs expected to be logged, where the first entry must be the
43 inlining decision. The file containing the specification should be
44 called output_spec.json. The expected JSON value is an array of
45 dictionaries. Each dictionary should have 2 keys:
47 - "tensor_spec
, followed by the TensorSpec description of the
49 - "logging_name", a string indicating the name to use when
50 logging the output values
.
55 "logging_name" : "some_name",
57 "name" : "model_name",
65 The first value must always correspond to the decision
.)"));
67 static cl::opt<std::string> TFOutputSpecOverride(
68 "ml
-inliner
-output
-spec
-override
", cl::Hidden,
69 cl::desc("Override the path to the output spec json file
. See
"
70 "-ml
-inliner
-model
-under
-training documentation
for the
"
71 "specification of that file
."));
73 static cl::opt<std::string> TFFeedPrefix("ml
-inliner
-trained
-model
-feed
-prefix
",
74 cl::Hidden, cl::init("action_
"),
75 cl::desc("Prefix
for feature names
."));
78 /// An InlineEvent, used by TrainingLogger.
80 /// What the default policy's decision would have been.
81 int64_t DefaultDecision = 0;
83 /// What we advised. When training off the default policy, this is the same as
85 int64_t AdvisedDecision = 0;
87 /// What actually happened. This would be 'false' in the case of an inline
88 /// error, even if AdvisedDecision were true, otherwise it agrees with
92 /// What the change in size was: size_after - size_before
96 /// Collect data we may use for training a model.
97 class TrainingLogger final {
99 TrainingLogger(StringRef LogFileName, const ModelUnderTrainingRunner *MUTR);
101 /// Log one inlining event.
102 void logInlineEvent(const InlineEvent &Event,
103 const MLModelRunner &ModelRunner);
106 StringRef LogFileName;
107 const ModelUnderTrainingRunner *const MUTR;
108 std::unique_ptr<Logger> L;
110 /// Set these 2 clearly OOB, to make sure we set them later.
111 size_t DefaultDecisionPos = std::numeric_limits<size_t>::max();
112 size_t DecisionPos = std::numeric_limits<size_t>::max();
115 /// An extension of the MLInlineAdvisor for the 'development' mode, targeting
116 /// the offline training scenario. Note that training happens outside of the
117 /// compiler, this facility is concerned with producing training data ("logs
").
118 /// This InlineAdvisor can operate in the following modes:
120 /// 1) collect logs for the default policy. This is useful for bootstrapping
121 /// training, which will be considerably faster by starting from a reasonable
124 /// 2) collect logs for the ML policy, using a model from a previous
125 /// training. Potentially, that model uses internally some small random
126 /// perturbation of its weights, to induce exploration (setting this up is the
127 /// responsibility of the training algorithm). The logs would then be used to
128 /// retrain and improve on this model.
130 /// 3) use the provided model, with no logging. This is useful for end to end
131 /// validation - the model, in this case, is a release candidate and shouldn't
132 /// have random perturbations. It is a convenience feature: rather than needing
133 /// to take the release candidate model and compile it in 'release' mode,
134 /// validate it, then potentially discard it, it's easier to just pass the model
135 /// to the compiler, albeit compilation would be slower, as a one-off. Once the
136 /// model behaves satisfactorily, it can be compiled AOT, for efficiency, in
137 /// release mode. The expectation is that a well-trained model provides a good
138 /// policy over a sufficiently diverse codebase, over many changes (i.e.
139 /// training happens seldom).
140 class DevelopmentModeMLInlineAdvisor : public MLInlineAdvisor {
142 DevelopmentModeMLInlineAdvisor(
143 Module &M, ModuleAnalysisManager &MAM,
144 std::unique_ptr<MLModelRunner> ModelRunner,
145 std::function<bool(CallBase &)> GetDefaultAdvice,
146 std::unique_ptr<TrainingLogger> Logger);
148 size_t getTotalSizeEstimate();
150 void updateNativeSizeEstimate(int64_t Change) {
151 *CurrentNativeSize += Change;
153 void resetNativeSize(Function *F) {
154 PreservedAnalyses PA = PreservedAnalyses::all();
155 PA.abandon<InlineSizeEstimatorAnalysis>();
156 FAM.invalidate(*F, PA);
159 std::unique_ptr<MLInlineAdvice>
160 getAdviceFromModel(CallBase &CB, OptimizationRemarkEmitter &ORE) override;
162 std::optional<size_t> getNativeSizeEstimate(const Function &F) const;
165 bool isLogging() const { return !!Logger; }
166 std::unique_ptr<MLInlineAdvice> getMandatoryAdviceImpl(CallBase &CB) override;
168 const bool IsDoingInference;
169 std::unique_ptr<TrainingLogger> Logger;
171 const std::optional<int32_t> InitialNativeSize;
172 std::optional<int32_t> CurrentNativeSize;
175 /// A variant of MLInlineAdvice that tracks all non-trivial inlining
176 /// decisions, for training/logging.
177 class LoggingMLInlineAdvice : public MLInlineAdvice {
179 LoggingMLInlineAdvice(DevelopmentModeMLInlineAdvisor *Advisor, CallBase &CB,
180 OptimizationRemarkEmitter &ORE, bool Recommendation,
181 TrainingLogger &Logger,
182 std::optional<size_t> CallerSizeEstimateBefore,
183 std::optional<size_t> CalleeSizeEstimateBefore,
184 bool DefaultDecision, bool Mandatory = false)
185 : MLInlineAdvice(Advisor, CB, ORE, Recommendation), Logger(Logger),
186 CallerSizeEstimateBefore(CallerSizeEstimateBefore),
187 CalleeSizeEstimateBefore(CalleeSizeEstimateBefore),
188 DefaultDecision(DefaultDecision), Mandatory(Mandatory) {}
190 virtual ~LoggingMLInlineAdvice() = default;
193 DevelopmentModeMLInlineAdvisor *getAdvisor() const {
194 return static_cast<DevelopmentModeMLInlineAdvisor *>(Advisor);
196 void recordInliningImpl() override {
197 MLInlineAdvice::recordInliningImpl();
198 getAdvisor()->resetNativeSize(Caller);
199 int Reward = std::numeric_limits<int>::max();
200 if (InlineSizeEstimatorAnalysis::isEvaluatorRequested() &&
201 !getAdvisor()->isForcedToStop()) {
202 int NativeSizeAfter = *getAdvisor()->getNativeSizeEstimate(*Caller) +
203 *CalleeSizeEstimateBefore;
204 Reward = NativeSizeAfter -
205 (*CallerSizeEstimateBefore + *CalleeSizeEstimateBefore);
206 getAdvisor()->updateNativeSizeEstimate(Reward);
208 log(Reward, /*Success=*/true);
211 void recordInliningWithCalleeDeletedImpl() override {
212 MLInlineAdvice::recordInliningWithCalleeDeletedImpl();
213 getAdvisor()->resetNativeSize(Caller);
214 if (InlineSizeEstimatorAnalysis::isEvaluatorRequested() &&
215 !getAdvisor()->isForcedToStop()) {
216 int NativeSizeAfter = *getAdvisor()->getNativeSizeEstimate(*Caller);
217 int Reward = NativeSizeAfter -
218 (*CallerSizeEstimateBefore + *CalleeSizeEstimateBefore);
219 getAdvisor()->updateNativeSizeEstimate(Reward);
220 log(Reward, /*Success=*/true);
222 log(NoReward, /*Success=*/true);
226 void recordUnsuccessfulInliningImpl(const InlineResult &Result) override {
227 MLInlineAdvice::recordUnsuccessfulInliningImpl(Result);
228 log(NoReward, /*Success=*/false);
231 void recordUnattemptedInliningImpl() override {
232 MLInlineAdvice::recordUnattemptedInliningImpl();
233 log(NoReward, /*Success=*/false);
236 void log(int64_t Reward, bool Success) {
240 Event.AdvisedDecision = isInliningRecommended();
241 Event.DefaultDecision = DefaultDecision;
242 Event.Effect = Success;
243 Event.Reward = Reward;
244 Logger.logInlineEvent(Event, getAdvisor()->getModelRunner());
247 static const int64_t NoReward = 0;
248 TrainingLogger &Logger;
249 const std::optional<size_t> CallerSizeEstimateBefore;
250 const std::optional<size_t> CalleeSizeEstimateBefore;
251 const int64_t DefaultDecision;
252 const int64_t Mandatory;
255 static const std::vector<TensorSpec> TrainingOnlyFeatures{
256 TensorSpec::createSpec<int64_t>(TFFeedPrefix + "inlining_default
", {1}),
257 TensorSpec::createSpec<float>(TFFeedPrefix + "discount
", {1}),
258 TensorSpec::createSpec<float>(TFFeedPrefix + "reward
", {1}),
259 TensorSpec::createSpec<int32_t>(TFFeedPrefix + "step_type
", {1})};
261 static const std::vector<TensorSpec> getInputFeatures() {
262 std::vector<TensorSpec> InputSpecs;
263 for (size_t I = 0; I < NumberOfFeatures; ++I)
264 InputSpecs.push_back(TensorSpec::createSpec<int64_t>(
265 TFFeedPrefix + FeatureMap[I].name(), FeatureMap[I].shape()));
266 append_range(InputSpecs, TrainingOnlyFeatures);
272 TrainingLogger::TrainingLogger(StringRef LogFileName,
273 const ModelUnderTrainingRunner *MUTR)
274 : LogFileName(LogFileName), MUTR(MUTR) {
275 // The first output is the inlining decision.
276 std::vector<TensorSpec> FT(FeatureMap.begin(), FeatureMap.end());
279 append_range(FT, MUTR->extraOutputsForLoggingSpecs());
281 DefaultDecisionPos = FT.size();
282 FT.push_back(DefaultDecisionSpec);
284 DecisionPos = FT.size();
285 FT.push_back(InlineDecisionSpec);
287 auto OS = std::make_unique<raw_fd_ostream>(TrainingLog, EC);
289 dbgs() << (EC.message() + ":" + TrainingLog);
291 L = std::make_unique<Logger>(
292 std::move(OS), FT, TensorSpec::createSpec<int64_t>(RewardName, {1}),
293 InlineSizeEstimatorAnalysis::isEvaluatorRequested());
294 L->switchContext("");
297 /// Log one inlining event.
298 void TrainingLogger::logInlineEvent(const InlineEvent &Event,
299 const MLModelRunner &ModelRunner) {
300 L->startObservation();
301 size_t CurrentFeature = 0;
302 for (; CurrentFeature < NumberOfFeatures; ++CurrentFeature)
303 L->logTensorValue(CurrentFeature,
304 reinterpret_cast<const char *>(
305 ModelRunner.getTensorUntyped(CurrentFeature)));
308 for (size_t I = 0; I < MUTR->extraOutputsForLoggingSpecs().size(); ++I) {
309 const char *RawData =
310 reinterpret_cast<const char *>(MUTR->getUntypedExtraOutputValue(I));
311 L->logTensorValue(CurrentFeature, RawData);
315 assert(CurrentFeature == DefaultDecisionPos);
316 L->logTensorValue(DefaultDecisionPos,
317 reinterpret_cast<const char *>(&Event.DefaultDecision));
318 L->logTensorValue(DecisionPos,
319 reinterpret_cast<const char *>(&Event.AdvisedDecision));
321 if (InlineSizeEstimatorAnalysis::isEvaluatorRequested())
322 L->logReward(Event.Reward);
324 // For debugging / later use
325 Effects.push_back(Event.Effect);
328 DevelopmentModeMLInlineAdvisor::DevelopmentModeMLInlineAdvisor(
329 Module &M, ModuleAnalysisManager &MAM,
330 std::unique_ptr<MLModelRunner> ModelRunner,
331 std::function<bool(CallBase &)> GetDefaultAdvice,
332 std::unique_ptr<TrainingLogger> Logger)
333 : MLInlineAdvisor(M, MAM, std::move(ModelRunner), GetDefaultAdvice),
334 IsDoingInference(isa<ModelUnderTrainingRunner>(getModelRunner())),
335 Logger(std::move(Logger)),
336 InitialNativeSize(isLogging() ? getTotalSizeEstimate() : 0),
337 CurrentNativeSize(InitialNativeSize) {
338 // We cannot have the case of neither inference nor logging.
339 assert(IsDoingInference || isLogging());
342 std::optional<size_t>
343 DevelopmentModeMLInlineAdvisor::getNativeSizeEstimate(const Function &F) const {
344 if (!InlineSizeEstimatorAnalysis::isEvaluatorRequested())
347 FAM.getResult<InlineSizeEstimatorAnalysis>(const_cast<Function &>(F));
349 F.getParent()->getContext().emitError(
350 "Native size estimator is
not present
.");
356 std::unique_ptr<MLInlineAdvice>
357 DevelopmentModeMLInlineAdvisor::getMandatoryAdviceImpl(CallBase &CB) {
358 return std::make_unique<LoggingMLInlineAdvice>(
360 /*CB=*/CB, /*ORE=*/getCallerORE(CB), /*Recommendation=*/true,
362 /*CallerSizeEstimateBefore=*/getNativeSizeEstimate(*CB.getCaller()),
363 /*CalleeSizeEstimateBefore=*/
364 getNativeSizeEstimate(*CB.getCalledFunction()),
365 /*DefaultDecision=*/true, /*Mandatory*/ true);
368 std::unique_ptr<MLInlineAdvice>
369 DevelopmentModeMLInlineAdvisor::getAdviceFromModel(
370 CallBase &CB, OptimizationRemarkEmitter &ORE) {
371 if (IsDoingInference && !isLogging())
372 return MLInlineAdvisor::getAdviceFromModel(CB, ORE);
374 bool DefaultAdvice = GetDefaultAdvice(CB);
375 auto Recommendation =
376 IsDoingInference ? static_cast<bool>(ModelRunner->evaluate<int64_t>())
378 return std::make_unique<LoggingMLInlineAdvice>(
380 /*CB=*/CB, /*ORE=*/ORE, /*Recommendation=*/Recommendation,
382 /*CallerSizeEstimateBefore=*/getNativeSizeEstimate(*CB.getCaller()),
383 /*CalleeSizeEstimateBefore=*/
384 getNativeSizeEstimate(*CB.getCalledFunction()),
385 /*DefaultDecision=*/DefaultAdvice);
388 size_t DevelopmentModeMLInlineAdvisor::getTotalSizeEstimate() {
389 if (!InlineSizeEstimatorAnalysis::isEvaluatorRequested())
393 if (F.isDeclaration())
395 Ret += *getNativeSizeEstimate(F);
400 std::unique_ptr<InlineAdvisor> llvm::getDevelopmentModeAdvisor(
401 Module &M, ModuleAnalysisManager &MAM,
402 std::function<bool(CallBase &)> GetDefaultAdvice) {
403 auto &Ctx = M.getContext();
404 std::unique_ptr<MLModelRunner> Runner;
405 if (TFModelUnderTrainingPath.empty())
406 Runner.reset(new NoInferenceModelRunner(Ctx, getInputFeatures()));
408 Runner = ModelUnderTrainingRunner::createAndEnsureValid(
409 Ctx, TFModelUnderTrainingPath, DecisionName, getInputFeatures(),
410 TFOutputSpecOverride);
413 std::unique_ptr<TrainingLogger> Logger;
414 if (!TrainingLog.empty())
415 Logger = std::make_unique<TrainingLogger>(
416 TrainingLog, dyn_cast<ModelUnderTrainingRunner>(Runner.get()));
418 return std::make_unique<DevelopmentModeMLInlineAdvisor>(
419 M, MAM, std::move(Runner), GetDefaultAdvice, std::move(Logger));
421 #endif // defined(LLVM_HAVE_TFLITE)