xref: /aosp_15_r20/external/tensorflow/tensorflow/core/common_runtime/cost_measurement_registry.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_COMMON_RUNTIME_COST_MEASUREMENT_REGISTRY_H_
17 #define TENSORFLOW_CORE_COMMON_RUNTIME_COST_MEASUREMENT_REGISTRY_H_
18 
19 #include <functional>
20 #include <memory>
21 #include <string>
22 #include <utility>
23 
24 #include "absl/memory/memory.h"
25 #include "absl/strings/string_view.h"
26 #include "tensorflow/core/common_runtime/cost_measurement.h"
27 
28 namespace tensorflow {
29 
30 // CostMeasurementRegistry allows to
31 // - register a CostMeasurement type to the global map
32 // - create an instance of registered CostMeasurement.
33 class CostMeasurementRegistry {
34  public:
35   // Creates an instance of registered CostMeasurement by name. If the named
36   // CostMeasurement is not registered yet, returns nullptr. Any returned
37   // std::unique_ptr<CostMeasurement> should not be moved.
38   // TODO(b/185852990): create a non-moveable wrapper class for the returned
39   // unique_ptr<CostMeasurement>.
40   static std::unique_ptr<CostMeasurement> CreateByNameOrNull(
41       const std::string& name, const CostMeasurement::Context& context);
42 
43   using Creator = std::function<std::unique_ptr<CostMeasurement>(
44       const CostMeasurement::Context&)>;
45 
46   // Registers a CostMeasurement type to the global map. Registering different
47   // types of CostMeasurement with the same name is prohibited.
48   static void RegisterCostMeasurement(absl::string_view name, Creator creator);
49 };
50 
51 // Registers a CostMeasurement type to the global map. Registering different
52 // types of CostMeasurement with the same name is prohibited.
53 class CostMeasurementRegistrar {
54  public:
CostMeasurementRegistrar(absl::string_view name,CostMeasurementRegistry::Creator creator)55   explicit CostMeasurementRegistrar(absl::string_view name,
56                                     CostMeasurementRegistry::Creator creator) {
57     CostMeasurementRegistry::RegisterCostMeasurement(name, std::move(creator));
58   }
59 };
60 
61 #define REGISTER_COST_MEASUREMENT(name, MyCostMeasurementClass)        \
62   namespace {                                                          \
63   static ::tensorflow::CostMeasurementRegistrar                        \
64       MyCostMeasurementClass##_registrar(                              \
65           (name), [](const CostMeasurement::Context& context) {        \
66             return std::make_unique<MyCostMeasurementClass>(context); \
67           });                                                          \
68   }  // namespace
69 
70 }  // namespace tensorflow
71 
72 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_COST_MEASUREMENT_REGISTRY_H_
73