1 /* Copyright 2016 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_CORE_UTIL_STRIDED_SLICE_OP_H_
16 #define TENSORFLOW_CORE_UTIL_STRIDED_SLICE_OP_H_
17 
18 #include "tensorflow/core/framework/tensor.h"
19 #include "tensorflow/core/framework/tensor_shape.h"
20 #include "tensorflow/core/framework/types.h"
21 #include "tensorflow/core/lib/core/status.h"
22 #include "tensorflow/core/lib/gtl/inlined_vector.h"
23 
24 namespace tensorflow {
25 
26 struct StridedSliceShapeSpec {
27   // Begin mask canonlized in dense form.
28   int32 begin_dense_mask;
29   // End mask canonlized in dense form.
30   int32 end_dense_mask;
31   // Shrink axis mask canonlized in dense form.
32   int32 shrink_axis_dense_mask;
33   // output_to_sparse_mapping[i] represents output[i]'s the corresponding dim
34   // index in the begin_tensor. If
35   // output_to_sparse_mapping[i] is -1, it means the dimension doesn't show up
36   // in sparse_mapping.
37   gtl::InlinedVector<int64, 4> output_to_sparse_mapping;
38   // output_to_processing_mapping is similar to output_to_sparse_mapping, but
39   // for processing shape.
40   gtl::InlinedVector<int64, 4> output_to_processing_mapping;
41   // processing_to_sparse_mapping[i] represents input_shape[i]'s corresponding
42   // dim index in the begin_tensor.
43   gtl::InlinedVector<int64, 4> processing_to_sparse_mapping;
44 };
45 
46 // Runs validation on the strided slice op parameters.
47 //
48 // Is a separate translation unit from the kernel so that:
49 // 1. The op's shape function can use it.
50 // 2. The code size is reduced vs templating this on the kernel's type.
51 //
52 // Note that when input_shape is not fully specified, only <final_shape> and
53 // <processing_shape> are valid; <is_identity>, <is_simple_slice> and other
54 // output parameters will not be accurate.
55 //
56 // If <begin_tensor> or <end_tensor> are nullptr, <begin> and <end> will not be
57 // valid. In this case, <slice_dim0> and <is_identity> will be true only if a
58 // determination can be made based on the information given. A best effort is
59 // made to set <processing_shape> and <final_shape> based on <input_shape>, but
60 // some dimensions of <processing_shape> and/or <final_shape> may be unknown
61 // (-1). Any validation that can be done without complete information is
62 // performed.
63 //
64 Status ValidateStridedSliceOp(
65     const Tensor* begin_tensor, const Tensor* end_tensor,
66     const Tensor& strides_tensor, const PartialTensorShape& input_shape,
67     int32 begin_mask_spec, int32 end_mask_spec, const int32 ellipsis_mask,
68     int32 new_axis_mask, int32 shrink_axis_mask,
69     PartialTensorShape* processing_shape, PartialTensorShape* final_shape,
70     bool* is_identity, bool* is_simple_slice, bool* slice_dim0,
71     gtl::InlinedVector<int64, 4>* begin, gtl::InlinedVector<int64, 4>* end,
72     gtl::InlinedVector<int64, 4>* strides,
73     StridedSliceShapeSpec* shape_spec = nullptr);
74 
75 // Same as above, but the outputs are TensorShape, not PartialTensorShape
76 Status ValidateStridedSliceOp(
77     const Tensor* begin_tensor, const Tensor* end_tensor,
78     const Tensor& strides_tensor, const PartialTensorShape& input_shape,
79     int32 begin_mask_spec, int32 end_mask_spec, const int32 ellipsis_mask,
80     int32 new_axis_mask, int32 shrink_axis_mask, TensorShape* processing_shape,
81     TensorShape* final_shape, bool* is_identity, bool* is_simple_slice,
82     bool* slice_dim0, gtl::InlinedVector<int64, 4>* begin,
83     gtl::InlinedVector<int64, 4>* end, gtl::InlinedVector<int64, 4>* strides,
84     StridedSliceShapeSpec* shape_spec = nullptr);
85 
86 }  // namespace tensorflow
87 
88 #endif  // TENSORFLOW_CORE_UTIL_STRIDED_SLICE_OP_H_
89