xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/convert_mover.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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_COMPILER_XLA_SERVICE_CONVERT_MOVER_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_CONVERT_MOVER_H_
18 
19 #include <functional>
20 #include <utility>
21 
22 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h"
23 
24 namespace xla {
25 
26 // Moves narrowing conversions up the graph and widening conversions down the
27 // graph, when we can do so with no effect on numerics. Motivations:
28 //
29 //  - It's preferable to spend more of our time in lower precision and less of
30 //    our time in higher precision.
31 //
32 //  - Moving these converts exposes optimization opportunities. For example, in
33 //    reshape(convert-big-to-small(reshape(convert-small-to-big(x)))), we can
34 //    commute one of the converts with one of the reshapes.  This leaves us with
35 //    convert(convert(reshape(reshape))), which can probably be simplified
36 //    further by algsimp.
37 class ConvertMover : public HloModulePass {
38  public:
39   ConvertMover() = default;
40 
name()41   absl::string_view name() const override { return "convert-mover"; }
42   StatusOr<bool> Run(
43       HloModule* module,
44       const absl::flat_hash_set<absl::string_view>& execution_threads) override;
45 };
46 
47 }  // namespace xla
48 
49 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_CONVERT_MOVER_H_
50