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::Type::OpSharding_Type_REPLICATED);
24 return result;
25 }
26
AssignDevice(int device)27 OpSharding AssignDevice(int device) {
28 OpSharding result;
29 result.set_type(OpSharding::Type::OpSharding_Type_MAXIMAL);
30 result.add_tile_assignment_dimensions(1);
31 result.add_tile_assignment_devices(device);
32 return result;
33 }
34
Tile(const Shape & tile_shape,const TileAssignment & tile_assignment)35 OpSharding Tile(const Shape& tile_shape,
36 const TileAssignment& tile_assignment) {
37 OpSharding result;
38 result.set_type(OpSharding::Type::OpSharding_Type_OTHER);
39 *result.mutable_tile_shape() = tile_shape.ToProto();
40 for (int64 dim : tile_assignment.dimensions()) {
41 result.add_tile_assignment_dimensions(dim);
42 }
43 for (uint32 device : tile_assignment) {
44 result.add_tile_assignment_devices(device);
45 }
46 return result;
47 }
48
Tile1D(const Shape & tile_shape,int64 num_tiles)49 OpSharding Tile1D(const Shape& tile_shape, int64 num_tiles) {
50 OpSharding result;
51 result.set_type(OpSharding::Type::OpSharding_Type_OTHER);
52
53 CHECK_EQ(tile_shape.rank(), 1);
54 std::vector<int64> dimensions(1, num_tiles);
55 *result.mutable_tile_shape() = tile_shape.ToProto();
56 auto& tile_dimension =
57 (*result.mutable_tile_shape()->mutable_dimensions())[0];
58 tile_dimension = CeilOfRatio(static_cast<int64>(tile_dimension), num_tiles);
59 result.add_tile_assignment_dimensions(num_tiles);
60 for (int64 i = 0; i < num_tiles; ++i) {
61 result.add_tile_assignment_devices(i);
62 }
63 return result;
64 }
65
Tuple(const ShapeTree<OpSharding> & shardings)66 OpSharding Tuple(const ShapeTree<OpSharding>& shardings) {
67 OpSharding result;
68 result.set_type(OpSharding::Type::OpSharding_Type_TUPLE);
69 for (const auto& index_to_sharding : shardings.leaves()) {
70 *result.add_tuple_shardings() = index_to_sharding.second;
71 }
72 return result;
73 }
74
75 } // namespace sharding_builder
76 } // namespace xla
77