LLVM 24.0.0git
TrainingLogger.cpp
Go to the documentation of this file.
1//===- TrainingLogger.cpp - mlgo feature/reward logging -------------------===//
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// This file implements logging infrastructure for extracting features and
10// rewards for mlgo policy training.
11//
12//===----------------------------------------------------------------------===//
14#include "llvm/Config/config.h"
15
16#include "llvm/ADT/Twine.h"
18#include "llvm/Support/Debug.h"
19#include "llvm/Support/JSON.h"
21
22#include <cassert>
23
24using namespace llvm;
25
26void Logger::writeHeader(std::optional<TensorSpec> AdviceSpec) {
27 json::OStream JOS(*OS);
28 JOS.object([&]() {
29 JOS.attributeArray("features", [&]() {
30 for (const auto &TS : FeatureSpecs)
31 TS.toJSON(JOS);
32 });
33 if (IncludeReward) {
34 JOS.attributeBegin("score");
35 RewardSpec.toJSON(JOS);
36 JOS.attributeEnd();
37 }
38 if (AdviceSpec.has_value()) {
39 JOS.attributeBegin("advice");
40 AdviceSpec->toJSON(JOS);
41 JOS.attributeEnd();
42 }
43 });
44 *OS << "\n";
45}
46
48 CurrentContext = Name.str();
49 json::OStream JOS(*OS);
50 JOS.object([&]() { JOS.attribute("context", Name); });
51 *OS << "\n";
52}
53
55 auto I = ObservationIDs.insert({CurrentContext, 0});
56 size_t NewObservationID = I.second ? 0 : ++I.first->second;
57 json::OStream JOS(*OS);
58 JOS.object([&]() {
59 JOS.attribute("observation", static_cast<int64_t>(NewObservationID));
60 });
61 *OS << "\n";
62}
63
64void Logger::endObservation() { *OS << "\n"; }
65
66void Logger::logRewardImpl(const char *RawData) {
67 assert(IncludeReward);
68 json::OStream JOS(*OS);
69 JOS.object([&]() {
70 JOS.attribute("outcome", static_cast<int64_t>(
71 ObservationIDs.find(CurrentContext)->second));
72 });
73 *OS << "\n";
74 writeTensor(RewardSpec, RawData);
75 *OS << "\n";
76}
77
78Logger::Logger(std::unique_ptr<raw_ostream> OS,
79 const std::vector<TensorSpec> &FeatureSpecs,
80 const TensorSpec &RewardSpec, bool IncludeReward,
81 std::optional<TensorSpec> AdviceSpec)
82 : OS(std::move(OS)), FeatureSpecs(FeatureSpecs), RewardSpec(RewardSpec),
83 IncludeReward(IncludeReward) {
84 writeHeader(AdviceSpec);
85}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file supports working with JSON data.
#define I(x, y, z)
Definition MD5.cpp:57
LLVM_ABI void startObservation()
LLVM_ABI void switchContext(StringRef Name)
LLVM_ABI void endObservation()
LLVM_ABI Logger(std::unique_ptr< raw_ostream > OS, const std::vector< TensorSpec > &FeatureSpecs, const TensorSpec &RewardSpec, bool IncludeReward, std::optional< TensorSpec > AdviceSpec=std::nullopt)
Construct a Logger.
iterator find(StringRef Key)
Definition StringMap.h:227
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
Definition JSON.h:983
void object(Block Contents)
Emit an object whose elements are emitted in the provided Block.
Definition JSON.h:1013
void attribute(llvm::StringRef Key, const Value &Contents)
Emit an attribute whose value is self-contained (number, vector<int> etc).
Definition JSON.h:1038
This is an optimization pass for GlobalISel generic memory operations.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878