Home | History | Annotate | Download | only in kernels
      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/tf2xla/kernels/shape_util.h"
     17 
     18 #include <limits>
     19 
     20 #include "tensorflow/core/kernels/bounds_check.h"
     21 
     22 namespace tensorflow {
     23 
     24 Status TensorShapeToConstant(const TensorShape& input_shape,
     25                              Tensor* shape_constant) {
     26   const int dims = input_shape.dims();
     27   if (shape_constant->dtype() == DT_INT32) {
     28     auto vec = shape_constant->vec<int32>();
     29     for (int i = 0; i < dims; ++i) {
     30       int64 dim_size = input_shape.dim_size(i);
     31       if (!FastBoundsCheck(dim_size, std::numeric_limits<int32>::max())) {
     32         return errors::InvalidArgument(
     33             "Shape with out_type=int32 does not support tensors > int32max",
     34             " but dim ", i, " is ", dim_size);
     35       }
     36       vec(i) = static_cast<int32>(dim_size);
     37     }
     38   } else {
     39     auto vec = shape_constant->vec<int64>();
     40     for (int i = 0; i < dims; ++i) {
     41       int64 dim_size = input_shape.dim_size(i);
     42       vec(i) = dim_size;
     43     }
     44   }
     45   return Status::OK();
     46 }
     47 
     48 }  // namespace tensorflow
     49