xref: /aosp_15_r20/external/tensorflow/tensorflow/core/profiler/utils/step_intersection.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 
16 #ifndef TENSORFLOW_CORE_PROFILER_UTILS_STEP_INTERSECTION_H_
17 #define TENSORFLOW_CORE_PROFILER_UTILS_STEP_INTERSECTION_H_
18 
19 #include <algorithm>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "tensorflow/core/platform/types.h"
23 #include "tensorflow/core/profiler/protobuf/steps_db.pb.h"
24 
25 namespace tensorflow {
26 namespace profiler {
27 
28 // Description of how two step sequences are aligned.
29 struct StepsAlignment {
30   uint32 begin_subordinate_idx;  // where the alignment begins on the
31                                  // subordinate steps.
32   uint32 begin_chief_idx;  // where the alignment begins on the chief steps.
33   uint32 num_steps;        // aligned for how many steps.
34 };
35 
36 class StepIntersection {
37  public:
38   StepIntersection(
39       uint32 max_steps,
40       const absl::flat_hash_map</*host_id=*/uint32, const StepDatabaseResult*>&
41           perhost_stepdb);
42 
43   // Returns the number of steps in the intersection.
NumSteps()44   uint32 NumSteps() const { return end_chief_idx_ - begin_chief_idx_; }
45 
46   // Returns the value of empty_intersect_ (see the explanation of
47   // empty_intersect_ below).
EmptyIntersect()48   bool EmptyIntersect() const { return empty_intersect_; }
49 
50   // Returns the step numbers for the destination (i.e. the intersection
51   // result).
52   std::vector<uint32> DstStepNumbers() const;
53 
54   // Returns the index to the step in the given host that corresponds to the
55   // first step in the intersection.
56   uint32 FirstStepIndex(uint32 host_id) const;
57 
58   // Returns the number of steps dropped due to the max_steps constraint
59   // specified in the constructor.
StepsDropped()60   uint32 StepsDropped() const { return steps_dropped_; }
61 
62   std::string DebugString() const;
63 
64  private:
65   absl::flat_hash_map</*host_id=*/uint32, StepsAlignment> perhost_alignment_;
66   uint32
67       chief_host_id_;  // the host whose step sequence is selected as the chief.
68   uint32 steps_dropped_;  // number of steps dropped.
69   // If NumSteps() is 0, empty_intersect indicates one of two possible reasons:
70   //   (i) At least one host has some steps, but the intersection over all hosts
71   //   is empty. In this case, empty_intersect is true,
72   //   (ii) None of the hosts has any steps. In this case, empty_intersect is
73   //   false.
74   // If NumSteps() > 0, empty_intersect is don't care.
75   bool empty_intersect_;
76   // The begin and end indices to the chief step sequence for this step
77   // intersection. Note that the begin index is inclusive but the end index is
78   // exclusive.
79   uint32 begin_chief_idx_;
80   uint32 end_chief_idx_;
81 };
82 
83 }  // namespace profiler
84 }  // namespace tensorflow
85 
86 #endif  // TENSORFLOW_CORE_PROFILER_UTILS_STEP_INTERSECTION_H_
87