AbstractConfig.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. /*
  3. * Copyright (c) Meta Platforms, Inc. and affiliates.
  4. * All rights reserved.
  5. *
  6. * This source code is licensed under the BSD-style license found in the
  7. * LICENSE file in the root directory of this source tree.
  8. */
  9. #pragma once
  10. #include <chrono>
  11. #include <map>
  12. #include <memory>
  13. #include <string>
  14. #include <vector>
  15. namespace libkineto {
  16. class AbstractConfig {
  17. public:
  18. AbstractConfig& operator=(const AbstractConfig&) = delete;
  19. AbstractConfig(AbstractConfig&&) = delete;
  20. AbstractConfig& operator=(AbstractConfig&&) = delete;
  21. virtual ~AbstractConfig() {
  22. for (const auto& p : featureConfigs_) {
  23. delete p.second;
  24. }
  25. }
  26. // Return a copy of the full derived class
  27. virtual AbstractConfig* cloneDerived(AbstractConfig& parent) const = 0;
  28. // Returns true if successfully parsed the config string
  29. bool parse(const std::string& conf);
  30. // Default setup for signal-triggered profiling
  31. virtual void setSignalDefaults() {
  32. for (auto& p : featureConfigs_) {
  33. p.second->setSignalDefaults();
  34. }
  35. }
  36. // Default setup for client-triggered profiling
  37. virtual void setClientDefaults() {
  38. for (auto& p : featureConfigs_) {
  39. p.second->setClientDefaults();
  40. }
  41. }
  42. // Time config was created / updated
  43. [[nodiscard]] std::chrono::time_point<std::chrono::system_clock> timestamp()
  44. const {
  45. return timestamp_;
  46. }
  47. // Source config string that this was parsed from
  48. [[nodiscard]] const std::string& source() const {
  49. return source_;
  50. }
  51. [[nodiscard]] AbstractConfig& feature(const std::string& name) const {
  52. const auto& pos = featureConfigs_.find(name);
  53. return *pos->second;
  54. }
  55. // Transfers ownership of cfg arg
  56. void addFeature(const std::string& name, AbstractConfig* cfg) {
  57. featureConfigs_[name] = cfg;
  58. }
  59. protected:
  60. AbstractConfig() = default;
  61. AbstractConfig(const AbstractConfig& other) = default;
  62. // Return true if the option was recognized and successfully parsed.
  63. // Throw std::invalid_argument if val is invalid.
  64. virtual bool handleOption(const std::string& name, std::string& val);
  65. // Perform post-validation checks, typically conditons involving
  66. // multiple options.
  67. // Throw std::invalid_argument if automatic correction can not be made.
  68. //
  69. // @param fallbackProfileStartTime Specify a fallback profile start timestamp
  70. // in case it was never specified by the client
  71. virtual void validate(
  72. const std::chrono::time_point<std::chrono::system_clock>&
  73. fallbackProfileStartTime) = 0;
  74. // TODO: Separate out each profiler type into features?
  75. virtual void printActivityProfilerConfig(std::ostream& s) const;
  76. virtual void setActivityDependentConfig();
  77. // Helpers for use in handleOption
  78. // Split a string by delimiter and remove external white space
  79. [[nodiscard]] std::vector<std::string> splitAndTrim(
  80. const std::string& s,
  81. char delim) const;
  82. // Lowercase for case-insensitive comparisons
  83. std::string toLower(std::string& s) const;
  84. // Does string end with suffix
  85. [[nodiscard]] bool endsWith(const std::string& s, const std::string& suffix)
  86. const;
  87. // Conversions
  88. [[nodiscard]] int64_t
  89. toIntRange(const std::string& val, int64_t min, int64_t max) const;
  90. [[nodiscard]] int32_t toInt32(const std::string& val) const;
  91. [[nodiscard]] int64_t toInt64(const std::string& val) const;
  92. bool toBool(std::string& val) const;
  93. void cloneFeaturesInto(AbstractConfig& cfg) const {
  94. for (const auto& feature : featureConfigs_) {
  95. cfg.featureConfigs_[feature.first] = feature.second->cloneDerived(cfg);
  96. }
  97. }
  98. private:
  99. // Time config was created / updated
  100. std::chrono::time_point<std::chrono::system_clock> timestamp_;
  101. // Original configuration string, used for comparison
  102. std::string source_;
  103. // Configuration objects for optional features
  104. std::map<std::string, AbstractConfig*> featureConfigs_;
  105. };
  106. } // namespace libkineto
  107. #else
  108. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  109. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)