LLVM 22.0.0git
InteractiveModelRunner.h
Go to the documentation of this file.
1//===- InteractiveModelRunner.h ---- "gym" ML model runner -----*- C++ -*-===//
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
10#ifndef LLVM_ANALYSIS_INTERACTIVEMODELRUNNER_H
11#define LLVM_ANALYSIS_INTERACTIVEMODELRUNNER_H
12
17#include <system_error>
18
19namespace llvm {
20
21/// A MLModelRunner that asks for advice from an external agent, or host. It
22/// uses 2 files - ideally named pipes - one to send data to that agent, and
23/// one to receive advice.
24/// The data exchange uses the training logger (Utils/TrainingLogger.h) format.
25/// Specifically, the compiler will send the log header, set the context, and
26/// send observations; the host is expected to reply with a tensor value after
27/// each observation as a binary buffer that's conforming to the shape of the
28/// advice. Interleaved, the data closely resembles the training log for a
29/// log where we don't capture the reward signal.
30///
31/// Note that the correctness of the received data is the responsibility of the
32/// host. In particular, if insufficient data were sent, the compiler will block
33/// when waiting for an advice.
34///
35/// Note that the host can either open the pipes RW, or open first the pipe to
36/// the compiler - i.e. the "Inbound" - and then the "Outbound", to avoid
37/// deadlock. This is because the compiler first tries to open the inbound
38/// (which will hang until there's a writer on the other end).
40public:
42 const std::vector<TensorSpec> &Inputs,
43 const TensorSpec &Advice, StringRef OutboundName,
44 StringRef InboundName);
45
46 static bool classof(const MLModelRunner *R) {
47 return R->getKind() == MLModelRunner::Kind::Interactive;
48 }
49 void switchContext(StringRef Name) override {
50 Log->switchContext(Name);
51 Log->flush();
52 }
53
55
56private:
57 void *evaluateUntyped() override;
58 // This must be declared before InEC if we want to initialize it in the
59 // ctor initializer list.
60 int Inbound = -1;
61 const std::vector<TensorSpec> InputSpecs;
62 const TensorSpec OutputSpec;
63 std::error_code OutEC;
64 std::error_code InEC;
65 std::vector<char> OutputBuffer;
66 std::unique_ptr<Logger> Log;
67};
68} // namespace llvm
69#endif // LLVM_ANALYSIS_INTERACTIVEMODELRUNNER_H
#define LLVM_ABI
Definition Compiler.h:213
A MLModelRunner that asks for advice from an external agent, or host.
static bool classof(const MLModelRunner *R)
InteractiveModelRunner(LLVMContext &Ctx, const std::vector< TensorSpec > &Inputs, const TensorSpec &Advice, StringRef OutboundName, StringRef InboundName)
void switchContext(StringRef Name) override
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
MLModelRunner(const MLModelRunner &)=delete
LLVMContext & Ctx
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This is an optimization pass for GlobalISel generic memory operations.