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