TrainingLogger.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/TensorSpec.h"
  14. #include "llvm/Config/config.h"
  15. #include "llvm/ADT/Twine.h"
  16. #include "llvm/Analysis/Utils/TrainingLogger.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Support/JSON.h"
  20. #include "llvm/Support/MemoryBuffer.h"
  21. #include "llvm/Support/Path.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include <cassert>
  24. #include <numeric>
  25. using namespace llvm;
  26. // FIXME(mtrofin): remove the flag altogether
  27. static cl::opt<bool>
  28. UseSimpleLogger("tfutils-use-simplelogger", cl::init(true), cl::Hidden,
  29. cl::desc("Output simple (non-protobuf) log."));
  30. void Logger::writeHeader() {
  31. json::OStream JOS(*OS);
  32. JOS.object([&]() {
  33. JOS.attributeArray("features", [&]() {
  34. for (const auto &TS : FeatureSpecs)
  35. TS.toJSON(JOS);
  36. });
  37. if (IncludeReward) {
  38. JOS.attributeBegin("score");
  39. RewardSpec.toJSON(JOS);
  40. JOS.attributeEnd();
  41. }
  42. });
  43. *OS << "\n";
  44. }
  45. void Logger::switchContext(StringRef Name) {
  46. CurrentContext = Name.str();
  47. json::OStream JOS(*OS);
  48. JOS.object([&]() { JOS.attribute("context", Name); });
  49. *OS << "\n";
  50. }
  51. void Logger::startObservation() {
  52. auto I = ObservationIDs.insert({CurrentContext, 0});
  53. size_t NewObservationID = I.second ? 0 : ++I.first->second;
  54. json::OStream JOS(*OS);
  55. JOS.object([&]() {
  56. JOS.attribute("observation", static_cast<int64_t>(NewObservationID));
  57. });
  58. *OS << "\n";
  59. }
  60. void Logger::endObservation() { *OS << "\n"; }
  61. void Logger::logRewardImpl(const char *RawData) {
  62. assert(IncludeReward);
  63. json::OStream JOS(*OS);
  64. JOS.object([&]() {
  65. JOS.attribute("outcome", static_cast<int64_t>(
  66. ObservationIDs.find(CurrentContext)->second));
  67. });
  68. *OS << "\n";
  69. writeTensor(RewardSpec, RawData);
  70. *OS << "\n";
  71. }
  72. Logger::Logger(std::unique_ptr<raw_ostream> OS,
  73. const std::vector<TensorSpec> &FeatureSpecs,
  74. const TensorSpec &RewardSpec, bool IncludeReward)
  75. : OS(std::move(OS)), FeatureSpecs(FeatureSpecs), RewardSpec(RewardSpec),
  76. IncludeReward(IncludeReward) {
  77. writeHeader();
  78. }