xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/ModelUnderTrainingRunner.cpp (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 //===- ModelUnderTrainingRunner.cpp - 'development' mode 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 // Implementation of a MLModelRunner for 'development' mode, i.e. evaluation
10 // happens off a model that's provided from the command line and is interpreted.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Config/config.h"
15 #if defined(LLVM_HAVE_TF_API)
16 
17 #include "llvm/Analysis/ModelUnderTrainingRunner.h"
18 
19 using namespace llvm;
20 
21 ModelUnderTrainingRunner::ModelUnderTrainingRunner(
22     LLVMContext &Ctx, const std::string &ModelPath,
23     const std::vector<TensorSpec> &InputSpecs,
24     const std::vector<LoggedFeatureSpec> &OutputSpecs)
25     : MLModelRunner(Ctx, MLModelRunner::Kind::Development),
26       OutputSpecs(OutputSpecs) {
27   Evaluator = std::make_unique<TFModelEvaluator>(
28       ModelPath, InputSpecs, [&](size_t I) { return OutputSpecs[I].Spec; },
29       OutputSpecs.size());
30   if (!Evaluator || !Evaluator->isValid()) {
31     Ctx.emitError("Failed to create saved model evaluator");
32     Evaluator.reset();
33     return;
34   }
35 }
36 
37 void *ModelUnderTrainingRunner::evaluateUntyped() {
38   LastEvaluationResult = Evaluator->evaluate();
39   if (!LastEvaluationResult.hasValue()) {
40     Ctx.emitError("Error evaluating model.");
41     return nullptr;
42   }
43   return LastEvaluationResult->getUntypedTensorValue(0);
44 }
45 
46 void *ModelUnderTrainingRunner::getTensorUntyped(size_t Index) {
47   return Evaluator->getUntypedInput(Index);
48 }
49 
50 std::unique_ptr<ModelUnderTrainingRunner>
51 ModelUnderTrainingRunner::createAndEnsureValid(
52     LLVMContext &Ctx, const std::string &ModelPath, StringRef DecisionName,
53     const std::vector<TensorSpec> &InputSpecs,
54     StringRef OutputSpecsPathOverride) {
55   std::unique_ptr<ModelUnderTrainingRunner> MUTR;
56   if (auto MaybeOutputSpecs = loadOutputSpecs(Ctx, DecisionName, ModelPath,
57                                               OutputSpecsPathOverride))
58     MUTR.reset(new ModelUnderTrainingRunner(Ctx, ModelPath, InputSpecs,
59                                             *MaybeOutputSpecs));
60   if (MUTR && MUTR->isValid())
61     return MUTR;
62 
63   Ctx.emitError("Could not load the policy model from the provided path");
64   return nullptr;
65 }
66 
67 #endif // defined(LLVM_HAVE_TF_API)
68