1//===- InteractiveModelRunner.cpp - noop ML model 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// A runner that communicates with an external agent via 2 file descriptors.
10//===----------------------------------------------------------------------===//
11#include "llvm/Analysis/InteractiveModelRunner.h"
12#include "AnalysisOptions.h"
13#include "llvm/Analysis/MLModelRunner.h"
14#include "llvm/Analysis/TensorSpec.h"
15#include "llvm/Support/ErrorHandling.h"
16#include "llvm/Support/FileSystem.h"
17#include "llvm/Support/raw_ostream.h"
18
19using namespace llvm;
20
21InteractiveModelRunner::InteractiveModelRunner(
22 LLVMContext &Ctx, const std::vector<TensorSpec> &Inputs,
23 const TensorSpec &Advice, StringRef OutboundName, StringRef InboundName)
24 : MLModelRunner(Ctx, MLModelRunner::Kind::Interactive, Inputs.size()),
25 InputSpecs(Inputs), OutputSpec(Advice),
26 InEC(sys::fs::openFileForRead(Name: InboundName, ResultFD&: Inbound)),
27 OutputBuffer(OutputSpec.getTotalTensorBufferSize()) {
28 if (InEC) {
29 Ctx.emitError(ErrorStr: "Cannot open inbound file: " + InEC.message());
30 return;
31 }
32 {
33 auto OutStream = std::make_unique<raw_fd_ostream>(args&: OutboundName, args&: OutEC);
34 if (OutEC) {
35 Ctx.emitError(ErrorStr: "Cannot open outbound file: " + OutEC.message());
36 return;
37 }
38 Log = std::make_unique<Logger>(args: std::move(OutStream), args: InputSpecs, args: Advice,
39 /*IncludeReward=*/args: false, args: Advice);
40 }
41 // Just like in the no inference case, this will allocate an appropriately
42 // sized buffer.
43 for (size_t I = 0; I < InputSpecs.size(); ++I)
44 setUpBufferForTensor(Index: I, Spec: InputSpecs[I], Buffer: nullptr);
45 Log->flush();
46}
47
48InteractiveModelRunner::~InteractiveModelRunner() {
49 sys::fs::file_t FDAsOSHandle = sys::fs::convertFDToNativeFile(FD: Inbound);
50 sys::fs::closeFile(F&: FDAsOSHandle);
51}
52
53void *InteractiveModelRunner::evaluateUntyped() {
54 Log->startObservation();
55 for (size_t I = 0; I < InputSpecs.size(); ++I)
56 Log->logTensorValue(FeatureID: I, RawData: reinterpret_cast<const char *>(getTensorUntyped(Index: I)));
57 Log->endObservation();
58 Log->flush();
59
60 size_t InsPoint = 0;
61 char *Buff = OutputBuffer.data();
62 const size_t Limit = OutputBuffer.size();
63 while (InsPoint < Limit) {
64 auto ReadOrErr = ::sys::fs::readNativeFile(
65 FileHandle: sys::fs::convertFDToNativeFile(FD: Inbound),
66 Buf: {Buff + InsPoint, OutputBuffer.size() - InsPoint});
67 if (ReadOrErr.takeError()) {
68 Ctx.emitError(ErrorStr: "Failed reading from inbound file");
69 break;
70 }
71 InsPoint += *ReadOrErr;
72 }
73 if (getAnalysisOptions(C: Ctx).interactive_model_runner_echo_reply)
74 dbgs() << OutputSpec.name() << ": "
75 << tensorValueToString(Buffer: OutputBuffer.data(), Spec: OutputSpec) << "\n";
76 return OutputBuffer.data();
77}
78