1 /* Copyright 2017 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 #include "tensorflow/compiler/xla/client/sharding_builder.h"
17
18 namespace xla {
19 namespace sharding_builder {
20
Replicate()21 OpSharding Replicate() {
22 OpSharding result;
23 result.set_type(OpSharding::REPLICATED);
24 return result;
25 }
26
Manual()27 OpSharding Manual() {
28 OpSharding result;
29 result.set_type(OpSharding::MANUAL);
30 return result;
31 }
32
AssignDevice(int device)33 OpSharding AssignDevice(int device) {
34 OpSharding result;
35 result.set_type(OpSharding::MAXIMAL);
36 result.add_tile_assignment_dimensions(1);
37 result.add_tile_assignment_devices(device);
38 return result;
39 }
40
Tile(const Shape & tile_shape,const TileAssignment & tile_assignment)41 OpSharding Tile(const Shape& tile_shape,
42 const TileAssignment& tile_assignment) {
43 OpSharding result;
44 result.set_type(OpSharding::OTHER);
45 *result.mutable_tile_shape() = tile_shape.ToProto();
46 for (int64 dim : tile_assignment.dimensions()) {
47 result.add_tile_assignment_dimensions(dim);
48 }
49 for (uint32 device : tile_assignment) {
50 result.add_tile_assignment_devices(device);
51 }
52 return result;
53 }
54
Tile1D(const Shape & tile_shape,int64 num_tiles)55 OpSharding Tile1D(const Shape& tile_shape, int64 num_tiles) {
56 OpSharding result;
57 result.set_type(OpSharding::OTHER);
58
59 CHECK_EQ(tile_shape.rank(), 1);
60 std::vector<int64> dimensions(1, num_tiles);
61 *result.mutable_tile_shape() = tile_shape.ToProto();
62 auto& tile_dimension =
63 (*result.mutable_tile_shape()->mutable_dimensions())[0];
64 tile_dimension = CeilOfRatio(static_cast<int64>(tile_dimension), num_tiles);
65 result.add_tile_assignment_dimensions(num_tiles);
66 for (int64 i = 0; i < num_tiles; ++i) {
67 result.add_tile_assignment_devices(i);
68 }
69 return result;
70 }
71
Tuple(const ShapeTree<OpSharding> & shardings)72 OpSharding Tuple(const ShapeTree<OpSharding>& shardings) {
73 OpSharding result;
74 result.set_type(OpSharding::TUPLE);
75 for (const auto& index_to_sharding : shardings.leaves()) {
76 *result.add_tuple_shardings() = index_to_sharding.second;
77 }
78 return result;
79 }
80
81 } // namespace sharding_builder
82 } // namespace xla
83