xref: /aosp_15_r20/external/tensorflow/tensorflow/core/profiler/utils/cost_utils.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 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 #ifndef TENSORFLOW_CORE_PROFILER_UTILS_COST_UTILS_H_
16 #define TENSORFLOW_CORE_PROFILER_UTILS_COST_UTILS_H_
17 
18 #include <string>
19 
20 #include "absl/container/flat_hash_set.h"
21 #include "tensorflow/core/grappler/costs/cost_estimator.h"
22 #include "tensorflow/core/grappler/costs/op_level_cost_estimator.h"
23 #include "tensorflow/core/platform/macros.h"
24 #include "tensorflow/core/platform/types.h"
25 #include "tensorflow/core/profiler/utils/xplane_visitor.h"
26 
27 namespace tensorflow {
28 namespace profiler {
29 
30 // This is a wrapper of tensorflow::grappler::OpLevelCostEstimator and use
31 // tracing time information to estimate the roof line stats for each traced
32 // tensorflow op.
33 class TfOpRoofLineCostEstimator
34     : public tensorflow::grappler::OpLevelCostEstimator {
35  public:
36   TfOpRoofLineCostEstimator() = default;
37   ~TfOpRoofLineCostEstimator() override;
38 
39   grappler::DeviceInfo GetDeviceInfo(
40       const DeviceProperties& device) const override;
41 
42   struct OpRoofLineStats {
43     uint64 flops = 0LL;
44     uint64 bytes_accessed = 0LL;
45     bool inaccurate = false;
46   };
47   OpRoofLineStats Predict(const XEventVisitor& event);
48 
49  private:
50   absl::flat_hash_set<std::string>
51       unsupported_ops_;  // summary for unsupported ops.
52 
53   TF_DISALLOW_COPY_AND_ASSIGN(TfOpRoofLineCostEstimator);
54 };
55 
56 }  // namespace profiler
57 }  // namespace tensorflow
58 
59 #endif  // TENSORFLOW_CORE_PROFILER_UTILS_COST_UTILS_H_
60