xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/tf2xla/layout_util.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 // Utilities for working with XLA layout and shapes.
17 
18 #ifndef TENSORFLOW_COMPILER_TF2XLA_LAYOUT_UTIL_H_
19 #define TENSORFLOW_COMPILER_TF2XLA_LAYOUT_UTIL_H_
20 
21 #include <vector>
22 
23 #include "tensorflow/compiler/tf2xla/xla_argument.h"
24 #include "tensorflow/compiler/tf2xla/xla_helpers.h"
25 #include "tensorflow/compiler/xla/shape.h"
26 #include "tensorflow/compiler/xla/statusor.h"
27 #include "tensorflow/compiler/xla/xla_data.pb.h"
28 #include "tensorflow/core/framework/tensor_shape.h"
29 #include "tensorflow/core/framework/types.pb.h"
30 
31 namespace tensorflow {
32 
33 class XlaShapeLayoutHelpers {
34  public:
35   // The following defines the layout preference of an xla tensor.
36   // The return value of LayoutPreferenceFn can be used in
37   // XlaHelper::ShapeRepresentationFn.
38   typedef std::function<XlaLayoutPreference(const TensorShape&, DataType,
39                                             std::optional<XlaArgument::Kind>)>
40       LayoutPreferenceFn;
41 
42   // A bundle of LayoutPreferenceFn and ShapeRepresentationFn.
43   struct ShapeDeterminationFns {
44     // Use no preference function, and identity shape representation function,
45     // as default value.
46     ShapeDeterminationFns();
47 
ShapeDeterminationFnsShapeDeterminationFns48     ShapeDeterminationFns(
49         LayoutPreferenceFn layout_preference_fn,
50         XlaHelpers::ShapeRepresentationFn shape_representation_fn)
51         : layout_preference_fn(layout_preference_fn),
52           shape_representation_fn(shape_representation_fn) {}
53 
54     LayoutPreferenceFn layout_preference_fn;
55     XlaHelpers::ShapeRepresentationFn shape_representation_fn;
56   };
57 };
58 
59 // Return a LayoutPreferenceFn that always uses kNoPreference layout.
60 XlaShapeLayoutHelpers::LayoutPreferenceFn UseNoPreferenceLayoutFn();
61 
62 // Rewrites the layout of xla_shape if there is tiled sharding.
63 Status RewriteLayoutWithShardedShape(
64     const std::optional<xla::HloSharding>& sharding, bool use_fast_memory,
65     XlaShapeLayoutHelpers::ShapeDeterminationFns shape_determination_fns,
66     xla::Shape* xla_shape);
67 
68 // Adds reshapes to fix the layout of an output, if a shape_representation_fn or
69 // sharding is present.
70 StatusOr<xla::XlaOp> ReshapeWithCorrectRepresentationAndSharding(
71     xla::XlaBuilder* builder, xla::XlaOp original, xla::Shape original_shape,
72     XlaShapeLayoutHelpers::ShapeDeterminationFns shape_determination_fns,
73     std::optional<xla::OpSharding> sharding, bool fast_mem);
74 
75 }  // namespace tensorflow
76 
77 #endif  // TENSORFLOW_COMPILER_TF2XLA_SHAPE_UTIL_H_
78