LLVM 24.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//===----------------------------------------------------------------------===//
12#include "AnalysisOptions.h"
18
19using namespace llvm;
20
22 LLVMContext &Ctx, const std::vector<TensorSpec> &Inputs,
23 const TensorSpec &Advice, StringRef OutboundName, StringRef InboundName)
25 InputSpecs(Inputs), OutputSpec(Advice),
26 InEC(sys::fs::openFileForRead(InboundName, Inbound)),
27 OutputBuffer(OutputSpec.getTotalTensorBufferSize()) {
28 if (InEC) {
29 Ctx.emitError("Cannot open inbound file: " + InEC.message());
30 return;
31 }
32 {
33 auto OutStream = std::make_unique<raw_fd_ostream>(OutboundName, OutEC);
34 if (OutEC) {
35 Ctx.emitError("Cannot open outbound file: " + OutEC.message());
36 return;
37 }
38 Log = std::make_unique<Logger>(std::move(OutStream), InputSpecs, Advice,
39 /*IncludeReward=*/false, 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(I, InputSpecs[I], nullptr);
45 Log->flush();
46}
47
52
54 Log->startObservation();
55 for (size_t I = 0; I < InputSpecs.size(); ++I)
56 Log->logTensorValue(I, reinterpret_cast<const char *>(getTensorUntyped(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(
66 {Buff + InsPoint, OutputBuffer.size() - InsPoint});
67 if (ReadOrErr.takeError()) {
68 Ctx.emitError("Failed reading from inbound file");
69 break;
70 }
71 InsPoint += *ReadOrErr;
72 }
73 if (getAnalysisOptions(Ctx).interactive_model_runner_echo_reply)
74 dbgs() << OutputSpec.name() << ": "
75 << tensorValueToString(OutputBuffer.data(), OutputSpec) << "\n";
76 return OutputBuffer.data();
77}
#define I(x, y, z)
Definition MD5.cpp:57
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:76
virtual void * evaluateUntyped()=0
void * getTensorUntyped(size_t Index)
void setUpBufferForTensor(size_t Index, const TensorSpec &Spec, void *Buffer)
MLModelRunner(const MLModelRunner &)=delete
LLVMContext & Ctx
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM_ABI std::error_code closeFile(file_t &F)
Close the file object.
LLVM_ABI Expected< size_t > readNativeFile(file_t FileHandle, MutableArrayRef< char > Buf)
Reads Buf.size() bytes from FileHandle into Buf.
LLVM_ABI file_t convertFDToNativeFile(int FD)
Converts from a Posix file descriptor number to a native file handle.
This is an optimization pass for GlobalISel generic memory operations.
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:1685
const AnalysisOptions & getAnalysisOptions(const LLVMContext &C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI std::string tensorValueToString(const char *Buffer, const TensorSpec &Spec)
For debugging.
This class wraps the platform specific file handle/descriptor type to provide an unified representati...
Definition File.h:21