tf_1.8_xla_doc
parallel_task_assignment.h
Go to the documentation of this file.
1 
3 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
4 
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==============================================================================*/
17 
18 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_CPU_PARALLEL_TASK_ASSIGNMENT_H_
19 #define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_PARALLEL_TASK_ASSIGNMENT_H_
20 
21 #include "tensorflow/compiler/xla/service/hlo_cost_analysis.h"
23 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h"
24 
25 namespace xla {
26 namespace cpu {
27 
28 // Simple interface for different parallel cost model implementations.
29 class ParallelCostModel {
30  public:
31  virtual ~ParallelCostModel() = default;
32  virtual int64 GetParallelTaskCount(HloInstruction* instruction) = 0;
33 };
34 
35 // ParallelTaskAssignment computes parallel task counts for HLOs in 'module'.
36 class ParallelTaskAssignment {
37  public:
38  // 'max_parallelism': the maximum parallel task count per instruction.
39  // 'shape_size': shape size function used by HloCostAnalysis during parallel
40  // task assignment.
41  // 'module': the containing HloModule.
42  ParallelTaskAssignment(const int64 max_parallelism,
43  const HloCostAnalysis::ShapeSizeFunction& shape_size,
44  HloModule* module);
45  ~ParallelTaskAssignment() {}
46 
47  // Computes and returns the target parallel task count for 'instruction'.
48  int64 GetTargetParallelTaskCount(HloInstruction* instruction);
49 
50  private:
51  std::unique_ptr<ParallelCostModel> cost_model_;
52 };
64 class ParallelTaskAssigner : public HloPassInterface {
65  public:
66  // 'max_parallelism': the maximum parallel task count per instruction.
67  // 'shape_size': shape size function used by HloCostAnalysis during parallel
68  // task assignment.
69  ParallelTaskAssigner(const int64 max_parallelism,
70  const HloCostAnalysis::ShapeSizeFunction& shape_size)
71  : max_parallelism_(max_parallelism), shape_size_function_(shape_size) {}
72  ~ParallelTaskAssigner() override {}
73 
74  tensorflow::StringPiece name() const override {
75  return "cpu-parallel-task-assigner";
76  }
77 
78  // Run parallel task assigner on 'module'.
79  // Returns true if the computation was changed, false otherwise.
80  StatusOr<bool> Run(HloModule* module) override;
81 
82  private:
83  using HloToParallelTasks = std::unordered_map<const HloInstruction*, int64>;
84 
85  // Assigns target parallel tasks from 'hlo_to_parallel_tasks' to HLOs in
86  // 'module'.
87  // Returns true if the computation was changed, false otherwise.
88  bool AssignParallelTasks(HloModule* module,
89  const HloToParallelTasks& hlo_to_parallel_tasks);
90  bool AssignParallelTasksHelper(
91  HloModule* module, HloComputation* computation,
92  const HloToParallelTasks& hlo_to_parallel_tasks);
93 
94  // Computes target parallel task counts (returned in 'parallel_task_counts')
95  // for parallelizable instructions in 'module'.
96  void ComputeTargetParallelTasks(HloModule* module,
97  HloToParallelTasks* hlo_to_parallel_tasks);
98 
99  int64 max_parallelism_;
100  HloCostAnalysis::ShapeSizeFunction shape_size_function_;
101 };
102 
103 } // namespace cpu
104 } // namespace xla
105 
106 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_CPU_PARALLEL_TASK_ASSIGNMENT_H_
Definition: parallel_task_assignment.h:64
Definition: hlo_computation.h:60
namespace for xla
Definition: client_library.cc:26
Definition: hlo_module.h:52