LLVM 20.0.0git
InteractiveModelRunner.cpp
Go to the documentation of this file.
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//===----------------------------------------------------------------------===//
18
19using namespace llvm;
20
22 "interactive-model-runner-echo-reply", cl::init(false), cl::Hidden,
23 cl::desc("The InteractiveModelRunner will echo back to stderr "
24 "the data received from the host (for debugging purposes)."));
25
27 LLVMContext &Ctx, const std::vector<TensorSpec> &Inputs,
28 const TensorSpec &Advice, StringRef OutboundName, StringRef InboundName)
29 : MLModelRunner(Ctx, MLModelRunner::Kind::Interactive, Inputs.size()),
30 InputSpecs(Inputs), OutputSpec(Advice),
31 InEC(sys::fs::openFileForRead(InboundName, Inbound)),
32 OutputBuffer(OutputSpec.getTotalTensorBufferSize()) {
33 if (InEC) {
34 Ctx.emitError("Cannot open inbound file: " + InEC.message());
35 return;
36 }
37 {
38 auto OutStream = std::make_unique<raw_fd_ostream>(OutboundName, OutEC);
39 if (OutEC) {
40 Ctx.emitError("Cannot open outbound file: " + OutEC.message());
41 return;
42 }
43 Log = std::make_unique<Logger>(std::move(OutStream), InputSpecs, Advice,
44 /*IncludeReward=*/false, Advice);
45 }
46 // Just like in the no inference case, this will allocate an appropriately
47 // sized buffer.
48 for (size_t I = 0; I < InputSpecs.size(); ++I)
49 setUpBufferForTensor(I, InputSpecs[I], nullptr);
50 Log->flush();
51}
52
54 sys::fs::file_t FDAsOSHandle = sys::fs::convertFDToNativeFile(Inbound);
55 sys::fs::closeFile(FDAsOSHandle);
56}
57
58void *InteractiveModelRunner::evaluateUntyped() {
59 Log->startObservation();
60 for (size_t I = 0; I < InputSpecs.size(); ++I)
61 Log->logTensorValue(I, reinterpret_cast<const char *>(getTensorUntyped(I)));
62 Log->endObservation();
63 Log->flush();
64
65 size_t InsPoint = 0;
66 char *Buff = OutputBuffer.data();
67 const size_t Limit = OutputBuffer.size();
68 while (InsPoint < Limit) {
69 auto ReadOrErr = ::sys::fs::readNativeFile(
71 {Buff + InsPoint, OutputBuffer.size() - InsPoint});
72 if (ReadOrErr.takeError()) {
73 Ctx.emitError("Failed reading from inbound file");
74 break;
75 }
76 InsPoint += *ReadOrErr;
77 }
78 if (DebugReply)
79 dbgs() << OutputSpec.name() << ": "
80 << tensorValueToString(OutputBuffer.data(), OutputSpec) << "\n";
81 return OutputBuffer.data();
82}
static cl::opt< bool > DebugReply("interactive-model-runner-echo-reply", cl::init(false), cl::Hidden, cl::desc("The InteractiveModelRunner will echo back to stderr " "the data received from the host (for debugging purposes)."))
#define I(x, y, z)
Definition: MD5.cpp:58
InteractiveModelRunner(LLVMContext &Ctx, const std::vector< TensorSpec > &Inputs, const TensorSpec &Advice, StringRef OutboundName, StringRef InboundName)
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
void emitError(const Instruction *I, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
MLModelRunner interface: abstraction of a mechanism for evaluating a ML model.
Definition: MLModelRunner.h:26
void * getTensorUntyped(size_t Index)
Definition: MLModelRunner.h:47
void setUpBufferForTensor(size_t Index, const TensorSpec &Spec, void *Buffer)
Definition: MLModelRunner.h:63
LLVMContext & Ctx
Definition: MLModelRunner.h:72
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
const std::string & name() const
Definition: TensorSpec.h:71
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
Expected< size_t > readNativeFile(file_t FileHandle, MutableArrayRef< char > Buf)
Reads Buf.size() bytes from FileHandle into Buf.
std::error_code closeFile(file_t &F)
Close the file object.
file_t convertFDToNativeFile(int FD)
Converts from a Posix file descriptor number to a native file handle.
Definition: FileSystem.h:990
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
Definition: STLExtras.h:1697
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
std::string tensorValueToString(const char *Buffer, const TensorSpec &Spec)
For debugging.
Definition: TensorSpec.cpp:107