LLVM: lib/Analysis/TrainingLogger.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
14#include "llvm/Config/config.h"
15
24
25#include
26
27using namespace llvm;
28
29void Logger::writeHeader(std::optional AdviceSpec) {
30 json::OStream JOS(*OS);
31 JOS.object([&]() {
32 JOS.attributeArray("features", [&]() {
33 for (const auto &TS : FeatureSpecs)
34 TS.toJSON(JOS);
35 });
36 if (IncludeReward) {
37 JOS.attributeBegin("score");
38 RewardSpec.toJSON(JOS);
39 JOS.attributeEnd();
40 }
41 if (AdviceSpec.has_value()) {
42 JOS.attributeBegin("advice");
43 AdviceSpec->toJSON(JOS);
44 JOS.attributeEnd();
45 }
46 });
47 *OS << "\n";
48}
49
51 CurrentContext = Name.str();
54 *OS << "\n";
55}
56
58 auto I = ObservationIDs.insert({CurrentContext, 0});
59 size_t NewObservationID = I.second ? 0 : ++I.first->second;
62 JOS.attribute("observation", static_cast<int64_t>(NewObservationID));
63 });
64 *OS << "\n";
65}
66
68
69void Logger::logRewardImpl(const char *RawData) {
70 assert(IncludeReward);
72 JOS.object([&]() {
73 JOS.attribute("outcome", static_cast<int64_t>(
74 ObservationIDs.find(CurrentContext)->second));
75 });
76 *OS << "\n";
77 writeTensor(RewardSpec, RawData);
78 *OS << "\n";
79}
80
82 const std::vector &FeatureSpecs,
83 const TensorSpec &RewardSpec, bool IncludeReward,
84 std::optional AdviceSpec)
85 : OS(std::move(OS)), FeatureSpecs(FeatureSpecs), RewardSpec(RewardSpec),
86 IncludeReward(IncludeReward) {
87 writeHeader(AdviceSpec);
88}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file supports working with JSON data.
LLVM_ABI void startObservation()
Definition TrainingLogger.cpp:57
LLVM_ABI void switchContext(StringRef Name)
Definition TrainingLogger.cpp:50
LLVM_ABI void endObservation()
Definition TrainingLogger.cpp:67
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.
Definition TrainingLogger.cpp:81
iterator find(StringRef Key)
StringRef - Represent a constant reference to a string, i.e.
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
void object(Block Contents)
Emit an object whose elements are emitted in the provided Block.
void attribute(llvm::StringRef Key, const Value &Contents)
Emit an attribute whose value is self-contained (number, vector etc).
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.
Implement std::hash so that hash_code can be used in STL containers.