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 | #include <string> |
17 | #include <utility> |
18 | #include <vector> |
19 | |
20 | #include "tensorflow/core/framework/common_shape_fns.h" |
21 | #include "tensorflow/core/framework/op.h" |
22 | #include "tensorflow/core/framework/shape_inference.h" |
23 | #include "tensorflow/core/framework/tensor_shape.h" |
24 | #include "tensorflow/core/framework/tensor_shape.pb.h" |
25 | #include "tensorflow/core/framework/tensor_slice.h" |
26 | #include "tensorflow/core/util/saved_tensor_slice_util.h" |
27 | |
28 | namespace tensorflow { |
29 | namespace dtensor { |
30 | |
31 | using shape_inference::InferenceContext; |
32 | using shape_inference::ShapeHandle; |
33 | using shape_inference::UnchangedShape; |
34 | |
35 | // Change layout of input to target layout inside the same mesh cluster. |
36 | REGISTER_OP("Relayout" ) |
37 | .Input("input: T" ) |
38 | .Output("output: T" ) |
39 | .Attr("layout: string" ) |
40 | .Attr("T: type" ) |
41 | .SetShapeFn(UnchangedShape); |
42 | |
43 | // Copy `input` to the given mesh and layout. |
44 | REGISTER_OP("CopyToMesh" ) |
45 | .Input("input: T" ) |
46 | .Output("output: T" ) |
47 | .Attr("layout: string" ) |
48 | .Attr("source_layout: string = ''" ) |
49 | .Attr("T: type" ) |
50 | .SetShapeFn(UnchangedShape); |
51 | |
52 | // DTensorRestoreV2 that is pretty much RestoreV2 but with extra global shapes |
53 | // and layouts. |
54 | REGISTER_OP("DTensorRestoreV2" ) |
55 | .Input("prefix: string" ) |
56 | .Input("tensor_names: string" ) |
57 | .Input("shape_and_slices: string" ) |
58 | .Output("tensors: dtypes" ) |
59 | .Attr("input_shapes: list(shape)" ) |
60 | .Attr("input_layouts: list(string)" ) |
61 | .Attr("dtypes: list(type)" ) |
62 | .SetIsStateful() |
63 | .SetShapeFn([](InferenceContext* c) { |
64 | ShapeHandle shape0, shape1, shape2; |
65 | TF_RETURN_IF_ERROR(c->WithRank(c->input(0), 0, &shape0)); |
66 | TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 1, &shape1)); |
67 | TF_RETURN_IF_ERROR(c->WithRank(c->input(2), 1, &shape2)); |
68 | TF_RETURN_IF_ERROR(c->Merge(shape1, shape2, &shape0)); |
69 | |
70 | std::vector<PartialTensorShape> input_shapes; |
71 | TF_RETURN_IF_ERROR(c->GetAttr("input_shapes" , &input_shapes)); |
72 | std::vector<std::string> input_layouts; |
73 | TF_RETURN_IF_ERROR(c->GetAttr("input_layouts" , &input_layouts)); |
74 | |
75 | if (input_shapes.size() != input_layouts.size()) { |
76 | return errors::InvalidArgument( |
77 | "Size of input_shapes and input_layouts is expected to match, but " |
78 | "got " , |
79 | input_shapes.size(), " for input_shapes and " , input_layouts.size(), |
80 | " for input_layouts" ); |
81 | } |
82 | |
83 | // TODO(hthu): We should be able to infer from layout and global_shape |
84 | // field. |
85 | return UnknownShape(c); |
86 | }); |
87 | |
88 | } // namespace dtensor |
89 | } // namespace tensorflow |
90 | |