• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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_COMPILER_XLA_SERVICE_SPACE_TO_BATCH_CONVERTER_H_
16 #define TENSORFLOW_COMPILER_XLA_SERVICE_SPACE_TO_BATCH_CONVERTER_H_
17 
18 #include "absl/strings/string_view.h"
19 #include "tensorflow/compiler/xla/service/hlo_module.h"
20 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h"
21 #include "tensorflow/compiler/xla/status_macros.h"
22 
23 namespace xla {
24 
25 // Controller of various knobs.
26 struct SpaceToBatchController {
27   bool enable_propagations_on_base_dilations;
28   bool enable_propagations_on_window_dilations;
29   bool enable_propagations_on_trivial_window_dilations;
30   bool disable_starting_on_small_chains;
31   int64_t limit_on_batch_size;
32   int64_t dimension_from_end_to_convert = 1;
33   // We choose the new batch size to be number_of_splits times that of the old
34   // batch so that space-to-batch propagation through several convolutional
35   // layers is consistent.
36   int64_t number_of_splits = 8;
37   int64_t count_of_dimensions_to_convert = 1;
38 };
39 
40 // Represents the different dimension mappings. Can be extended as needed.
41 enum class SpaceToBatchDimMap : uint8_t {
42   kBatch = 0,
43   kFeature = 1,
44   kSpace0 = 2,
45 };
46 
NumMappedDims()47 inline constexpr int64_t NumMappedDims() { return 3; }
48 
49 // A pass which rewrites convolutions such that space dimension is turned into
50 // batch.
51 class SpaceToBatchConverter : public HloModulePass {
52  public:
SpaceToBatchConverter(SpaceToBatchController ctrl)53   explicit SpaceToBatchConverter(SpaceToBatchController ctrl) : ctrl_(ctrl) {}
54 
name()55   absl::string_view name() const override { return "space-to-batch-converter"; }
56 
57   // Run convolution rewriting on the given computation. Returns whether the
58   // computation was changed.
59   using HloPassInterface::Run;
60   StatusOr<bool> Run(
61       HloModule* module,
62       const absl::flat_hash_set<absl::string_view>& execution_threads) override;
63 
64   // Controller for various knobs.
65   SpaceToBatchController ctrl_;
66 };
67 
68 }  // namespace xla
69 
70 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_SPACE_TO_BATCH_CONVERTER_H_
71