• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_COMPILER_XLA_SERVICE_RNG_BIT_GENERATOR_EXPANDER_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_RNG_BIT_GENERATOR_EXPANDER_H_
18 
19 #include "absl/container/flat_hash_map.h"
20 #include "tensorflow/compiler/xla/service/hlo_computation.h"
21 #include "tensorflow/compiler/xla/service/hlo_module.h"
22 #include "tensorflow/compiler/xla/service/op_expander_pass.h"
23 #include "tensorflow/compiler/xla/shape_util.h"
24 #include "tensorflow/compiler/xla/statusor.h"
25 #include "tensorflow/compiler/xla/xla_data.pb.h"
26 
27 namespace xla {
28 
29 class RngBitGeneratorExpander : public OpExpanderPass {
30  public:
RngBitGeneratorExpander(RandomAlgorithm default_algorithm)31   explicit RngBitGeneratorExpander(RandomAlgorithm default_algorithm)
32       : default_algorithm_(default_algorithm) {
33     CHECK_NE(default_algorithm_, RandomAlgorithm::RNG_DEFAULT);
34   }
35 
name()36   absl::string_view name() const override {
37     return "rng-bit-generator-expander";
38   }
39 
40  protected:
41   struct RngGeneratorKey {
42     Shape data_shape;
43     Shape state_shape;
44     RandomAlgorithm algorithm;
45     HloModule* module;
46 
47     template <typename H>
AbslHashValueRngGeneratorKey48     friend H AbslHashValue(H h, const RngGeneratorKey& c) {
49       return H::combine(std::move(h), c.state_shape, c.data_shape, c.algorithm,
50                         c.module);
51     }
52 
53     bool operator==(const RngGeneratorKey& o) const {
54       return data_shape == o.data_shape && state_shape == o.state_shape &&
55              algorithm == o.algorithm && module == o.module;
56     }
57   };
58 
59   bool InstructionMatchesPattern(HloInstruction* instruction) override;
60   StatusOr<HloInstruction*> ExpandInstruction(HloInstruction* hlo) override;
61   StatusOr<HloComputation*> GetGeneratorComputation(const Shape& data_shape,
62                                                     const Shape& state_shape,
63                                                     RandomAlgorithm algorithm,
64                                                     HloModule* module);
65 
66   const RandomAlgorithm default_algorithm_;
67   absl::flat_hash_map<RngGeneratorKey, HloComputation*> computation_cache_;
68 };
69 
70 }  // namespace xla
71 
72 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_RNG_BIT_GENERATOR_EXPANDER_H_
73