14#include "llvm/Config/config.h"
15#if defined(LLVM_HAVE_TFLITE)
36 cl::desc(
"Path where the development - mode inlining log is saved."));
40 cl::desc(R
"(Path to SavedModel from the previous training iteration.
41The directory is also expected to contain a JSON specification of the
42outputs expected to be logged, where the first entry must be the
43inlining decision. The file containing the specification should be
44called output_spec.json. The expected JSON value is an array of
45dictionaries. 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
50logging the output values.
55 "logging_name" : "some_name",
57 "name" : "model_name",
65The first value must always correspond to the decision.)"));
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."));
75 cl::desc(
"Prefix for feature names."));
81 int64_t DefaultDecision = 0;
85 int64_t AdvisedDecision = 0;
94class TrainingLogger final {
96 TrainingLogger(StringRef LogFileName,
const ModelUnderTrainingRunner *MUTR,
97 const std::vector<TensorSpec> &FeatureMap);
100 void logInlineEvent(
const InlineEvent &Event,
101 const MLModelRunner &ModelRunner);
104 StringRef LogFileName;
105 const ModelUnderTrainingRunner *
const MUTR;
106 const std::vector<TensorSpec> &FeatureMap;
108 std::unique_ptr<Logger>
L;
111 size_t DefaultDecisionPos = std::numeric_limits<size_t>::max();
112 size_t DecisionPos = std::numeric_limits<size_t>::max();
142 DevelopmentModeMLInlineAdvisor(
145 std::unique_ptr<MLModelRunner>(
const std::vector<TensorSpec> &)>
147 std::function<
bool(CallBase &)> GetDefaultAdvice);
149 std::unique_ptr<MLInlineAdvice>
150 getAdviceFromModel(CallBase &CB, OptimizationRemarkEmitter &ORE)
override;
153 bool isLogging()
const {
return !!Logger; }
154 std::unique_ptr<MLInlineAdvice> getMandatoryAdviceImpl(CallBase &CB)
override;
156 const bool IsDoingInference;
157 std::unique_ptr<TrainingLogger> Logger;
164 LoggingMLInlineAdvice(DevelopmentModeMLInlineAdvisor *Advisor, CallBase &CB,
165 OptimizationRemarkEmitter &ORE,
bool Recommendation,
166 TrainingLogger &Logger,
bool DefaultDecision,
167 bool Mandatory =
false)
168 : MLInlineAdvice(Advisor, CB, ORE, Recommendation), Logger(Logger),
169 DefaultDecision(DefaultDecision), Mandatory(Mandatory) {}
171 virtual ~LoggingMLInlineAdvice() =
default;
174 DevelopmentModeMLInlineAdvisor *getAdvisor()
const {
175 return static_cast<DevelopmentModeMLInlineAdvisor *
>(Advisor);
177 void recordInliningImpl()
override {
178 MLInlineAdvice::recordInliningImpl();
182 void recordInliningWithCalleeDeletedImpl()
override {
183 MLInlineAdvice::recordInliningWithCalleeDeletedImpl();
187 void recordUnsuccessfulInliningImpl(
const InlineResult &Result)
override {
188 MLInlineAdvice::recordUnsuccessfulInliningImpl(Result);
192 void recordUnattemptedInliningImpl()
override {
193 MLInlineAdvice::recordUnattemptedInliningImpl();
201 Event.AdvisedDecision = isInliningRecommended();
202 Event.DefaultDecision = DefaultDecision;
204 Logger.logInlineEvent(Event, getAdvisor()->getModelRunner());
207 TrainingLogger &Logger;
208 const int64_t DefaultDecision;
209 const int64_t Mandatory;
212static const std::vector<TensorSpec> TrainingOnlyFeatures{
220static const std::vector<TensorSpec>
221convertInputFeatures(
const std::vector<TensorSpec> &OriginalFeatures) {
222 std::vector<TensorSpec> InputSpecs;
223 for (
const auto &Feature : OriginalFeatures)
224 InputSpecs.push_back(
TensorSpec(TFFeedPrefix + Feature.name(), Feature));
231TrainingLogger::TrainingLogger(
StringRef LogFileName,
232 const ModelUnderTrainingRunner *MUTR,
233 const std::vector<TensorSpec> &FeatureMap)
234 : LogFileName(LogFileName), MUTR(MUTR), FeatureMap(FeatureMap) {
236 std::vector<TensorSpec> FT(FeatureMap.begin(), FeatureMap.end());
241 DefaultDecisionPos = FT.size();
244 DecisionPos = FT.size();
247 auto OS = std::make_unique<raw_fd_ostream>(TrainingLog, EC);
249 dbgs() << (
EC.message() +
":" + TrainingLog);
251 L = std::make_unique<Logger>(std::move(OS), FT,
254 L->switchContext(
"");
258void TrainingLogger::logInlineEvent(
const InlineEvent &Event,
260 L->startObservation();
261 size_t CurrentFeature = 0;
262 for (; CurrentFeature < FeatureMap.size(); ++CurrentFeature)
263 L->logTensorValue(CurrentFeature,
264 reinterpret_cast<const char *
>(
268 for (
size_t I = 0;
I < MUTR->extraOutputsForLoggingSpecs().
size(); ++
I) {
269 const char *RawData =
270 reinterpret_cast<const char *
>(MUTR->getUntypedExtraOutputValue(
I));
271 L->logTensorValue(CurrentFeature, RawData);
275 assert(CurrentFeature == DefaultDecisionPos);
276 L->logTensorValue(DefaultDecisionPos,
277 reinterpret_cast<const char *
>(&
Event.DefaultDecision));
278 L->logTensorValue(DecisionPos,
279 reinterpret_cast<const char *
>(&
Event.AdvisedDecision));
283 Effects.push_back(
Event.Effect);
286DevelopmentModeMLInlineAdvisor::DevelopmentModeMLInlineAdvisor(
289 std::unique_ptr<MLModelRunner>(
const std::vector<TensorSpec> &)>
291 std::function<
bool(
CallBase &)> GetDefaultAdvice)
293 IsDoingInference(
isa<ModelUnderTrainingRunner>(getModelRunner())) {
295 if (!TrainingLog.empty())
296 Logger = std::make_unique<TrainingLogger>(
299 assert(IsDoingInference || isLogging());
302std::unique_ptr<MLInlineAdvice>
303DevelopmentModeMLInlineAdvisor::getMandatoryAdviceImpl(
CallBase &CB) {
304 return std::make_unique<LoggingMLInlineAdvice>(
306 CB, getCallerORE(CB),
true,
311std::unique_ptr<MLInlineAdvice>
312DevelopmentModeMLInlineAdvisor::getAdviceFromModel(
314 if (IsDoingInference && !isLogging())
317 bool DefaultAdvice = GetDefaultAdvice(CB);
318 auto Recommendation =
319 IsDoingInference ?
static_cast<bool>(ModelRunner->
evaluate<int64_t>())
321 return std::make_unique<LoggingMLInlineAdvice>(
323 CB, ORE, Recommendation,
330 std::function<
bool(
CallBase &)> GetDefaultAdvice) {
331 auto &Ctx = M.getContext();
332 auto RunnerFactory = [&](
const std::vector<TensorSpec> &
InputFeatures)
333 -> std::unique_ptr<MLModelRunner> {
334 std::unique_ptr<MLModelRunner> Runner;
335 const std::vector<TensorSpec> ConvertedFeatures =
337 if (TFModelUnderTrainingPath.empty())
340 Runner = ModelUnderTrainingRunner::createAndEnsureValid(
341 Ctx, TFModelUnderTrainingPath,
DecisionName, ConvertedFeatures,
342 TFOutputSpecOverride);
347 return std::make_unique<DevelopmentModeMLInlineAdvisor>(M,
MAM, RunnerFactory,
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements the BitVector class.
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
Module.h This file contains the declarations for the Module class.
Machine Check Debug Module
ModuleAnalysisManager MAM
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Logging utility - given an ordered specification of features, and assuming a scalar reward,...
InlineAdvice that tracks changes post inlining.
virtual std::unique_ptr< MLInlineAdvice > getAdviceFromModel(CallBase &CB, OptimizationRemarkEmitter &ORE)
MLModelRunner interface: abstraction of a mechanism for evaluating a ML model.
void * getTensorUntyped(size_t Index)
StringRef - Represent a constant reference to a string, i.e.
static TensorSpec createSpec(const std::string &Name, const std::vector< int64_t > &Shape, int Port=0)
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
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.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI std::unique_ptr< InlineAdvisor > getDevelopmentModeAdvisor(Module &M, ModuleAnalysisManager &MAM, std::function< bool(CallBase &)> GetDefaultAdvice)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
LLVM_ABI const TensorSpec DefaultDecisionSpec
static const std::vector< TensorSpec > InputFeatures
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI const TensorSpec InlineDecisionSpec
LLVM_ABI const char *const RewardName
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.