xref: /freebsd-src/contrib/llvm-project/llvm/lib/Analysis/InteractiveModelRunner.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
1*06c3fb27SDimitry Andric //===- InteractiveModelRunner.cpp - noop ML model runner   ----------------===//
2*06c3fb27SDimitry Andric //
3*06c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*06c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*06c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*06c3fb27SDimitry Andric //
7*06c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
8*06c3fb27SDimitry Andric //
9*06c3fb27SDimitry Andric // A runner that communicates with an external agent via 2 file descriptors.
10*06c3fb27SDimitry Andric //===----------------------------------------------------------------------===//
11*06c3fb27SDimitry Andric #include "llvm/Analysis/InteractiveModelRunner.h"
12*06c3fb27SDimitry Andric #include "llvm/Analysis/MLModelRunner.h"
13*06c3fb27SDimitry Andric #include "llvm/Analysis/TensorSpec.h"
14*06c3fb27SDimitry Andric #include "llvm/Support/CommandLine.h"
15*06c3fb27SDimitry Andric #include "llvm/Support/ErrorHandling.h"
16*06c3fb27SDimitry Andric #include "llvm/Support/FileSystem.h"
17*06c3fb27SDimitry Andric #include "llvm/Support/raw_ostream.h"
18*06c3fb27SDimitry Andric 
19*06c3fb27SDimitry Andric using namespace llvm;
20*06c3fb27SDimitry Andric 
21*06c3fb27SDimitry Andric static cl::opt<bool> DebugReply(
22*06c3fb27SDimitry Andric     "interactive-model-runner-echo-reply", cl::init(false), cl::Hidden,
23*06c3fb27SDimitry Andric     cl::desc("The InteractiveModelRunner will echo back to stderr "
24*06c3fb27SDimitry Andric              "the data received from the host (for debugging purposes)."));
25*06c3fb27SDimitry Andric 
InteractiveModelRunner(LLVMContext & Ctx,const std::vector<TensorSpec> & Inputs,const TensorSpec & Advice,StringRef OutboundName,StringRef InboundName)26*06c3fb27SDimitry Andric InteractiveModelRunner::InteractiveModelRunner(
27*06c3fb27SDimitry Andric     LLVMContext &Ctx, const std::vector<TensorSpec> &Inputs,
28*06c3fb27SDimitry Andric     const TensorSpec &Advice, StringRef OutboundName, StringRef InboundName)
29*06c3fb27SDimitry Andric     : MLModelRunner(Ctx, MLModelRunner::Kind::Interactive, Inputs.size()),
30*06c3fb27SDimitry Andric       InputSpecs(Inputs), OutputSpec(Advice),
31*06c3fb27SDimitry Andric       InEC(sys::fs::openFileForRead(InboundName, Inbound)),
32*06c3fb27SDimitry Andric       OutputBuffer(OutputSpec.getTotalTensorBufferSize()) {
33*06c3fb27SDimitry Andric   if (InEC) {
34*06c3fb27SDimitry Andric     Ctx.emitError("Cannot open inbound file: " + InEC.message());
35*06c3fb27SDimitry Andric     return;
36*06c3fb27SDimitry Andric   }
37*06c3fb27SDimitry Andric   {
38*06c3fb27SDimitry Andric     auto OutStream = std::make_unique<raw_fd_ostream>(OutboundName, OutEC);
39*06c3fb27SDimitry Andric     if (OutEC) {
40*06c3fb27SDimitry Andric       Ctx.emitError("Cannot open outbound file: " + OutEC.message());
41*06c3fb27SDimitry Andric       return;
42*06c3fb27SDimitry Andric     }
43*06c3fb27SDimitry Andric     Log = std::make_unique<Logger>(std::move(OutStream), InputSpecs, Advice,
44*06c3fb27SDimitry Andric                                    /*IncludeReward=*/false, Advice);
45*06c3fb27SDimitry Andric   }
46*06c3fb27SDimitry Andric   // Just like in the no inference case, this will allocate an appropriately
47*06c3fb27SDimitry Andric   // sized buffer.
48*06c3fb27SDimitry Andric   for (size_t I = 0; I < InputSpecs.size(); ++I)
49*06c3fb27SDimitry Andric     setUpBufferForTensor(I, InputSpecs[I], nullptr);
50*06c3fb27SDimitry Andric   Log->flush();
51*06c3fb27SDimitry Andric }
52*06c3fb27SDimitry Andric 
~InteractiveModelRunner()53*06c3fb27SDimitry Andric InteractiveModelRunner::~InteractiveModelRunner() {
54*06c3fb27SDimitry Andric   sys::fs::file_t FDAsOSHandle = sys::fs::convertFDToNativeFile(Inbound);
55*06c3fb27SDimitry Andric   sys::fs::closeFile(FDAsOSHandle);
56*06c3fb27SDimitry Andric }
57*06c3fb27SDimitry Andric 
evaluateUntyped()58*06c3fb27SDimitry Andric void *InteractiveModelRunner::evaluateUntyped() {
59*06c3fb27SDimitry Andric   Log->startObservation();
60*06c3fb27SDimitry Andric   for (size_t I = 0; I < InputSpecs.size(); ++I)
61*06c3fb27SDimitry Andric     Log->logTensorValue(I, reinterpret_cast<const char *>(getTensorUntyped(I)));
62*06c3fb27SDimitry Andric   Log->endObservation();
63*06c3fb27SDimitry Andric   Log->flush();
64*06c3fb27SDimitry Andric 
65*06c3fb27SDimitry Andric   size_t InsPoint = 0;
66*06c3fb27SDimitry Andric   char *Buff = OutputBuffer.data();
67*06c3fb27SDimitry Andric   const size_t Limit = OutputBuffer.size();
68*06c3fb27SDimitry Andric   while (InsPoint < Limit) {
69*06c3fb27SDimitry Andric     auto ReadOrErr = ::sys::fs::readNativeFile(
70*06c3fb27SDimitry Andric         sys::fs::convertFDToNativeFile(Inbound),
71*06c3fb27SDimitry Andric         {Buff + InsPoint, OutputBuffer.size() - InsPoint});
72*06c3fb27SDimitry Andric     if (ReadOrErr.takeError()) {
73*06c3fb27SDimitry Andric       Ctx.emitError("Failed reading from inbound file");
74*06c3fb27SDimitry Andric       break;
75*06c3fb27SDimitry Andric     }
76*06c3fb27SDimitry Andric     InsPoint += *ReadOrErr;
77*06c3fb27SDimitry Andric   }
78*06c3fb27SDimitry Andric   if (DebugReply)
79*06c3fb27SDimitry Andric     dbgs() << OutputSpec.name() << ": "
80*06c3fb27SDimitry Andric            << tensorValueToString(OutputBuffer.data(), OutputSpec) << "\n";
81*06c3fb27SDimitry Andric   return OutputBuffer.data();
82*06c3fb27SDimitry Andric }
83