tensorflow/core/common_runtime/eval_const_tensor.cc
// Tries to infer the tensor output based on the input dims of a
// Shape node.
// [allow_partial = false]
//   Can infer the Shape op's output tensor only when the
//   input shapes to the Shape op are fully defined.
// [allow_partial = true]
//   Can infer the Shape op's output tensor as long as the rank of the input
//   shapes to the Shape op are known. Uses kUnknownDim for unknown dims.
Tries to infer the tensor output based on the input dims of a
Shape node.
[allow_partial = false]
Can infer the Shape op's output tensor only when the
input shapes to the Shape op are fully defined.
[allow_partial = true]
Can infer the Shape op's output tensor as long as the rank of the input
shapes to the Shape op are known. Uses kUnknownDim for unknown dims.
Tries to infer the tensor output based on the input dims of a Shape node . [ allow_partial = false ] Can infer the Shape op ' s output tensor only when the input shapes to the Shape op are fully defined . [ allow_partial = true ] Can infer the Shape op ' s output tensor as long as the rank of the input shapes to the Shape op are known . Uses kUnknownDim for unknown dims .
Status TryToInferTensorOutputFromShapeNode(const Node& shape_node,
                                           InferenceContext* shape_c,
                                           Tensor* output, bool* success,
                                           bool allow_partial = false) {
  *success = false;
  if (shape_node.type_string() != "Shape") return Status::OK();
  if (shape_c == nullptr) return Status::OK();
  if (!shape_c->FullyDefined(shape_c->input(0)) && !allow_partial)
    return Status::OK();
  if (!shape_c->RankKnown(shape_c->input(0))) return Status::OK();
  int src_rank = shape_c->Rank(shape_c->input(0));
  Tensor t(shape_node.output_type(0), TensorShape({src_rank}));
  if (shape_node.output_type(0) == DT_INT32) {
    auto flat = t.flat<int>();
    for (int i = 0; i < src_rank; i++) {
      int64_t dimension;
      if (shape_c->ValueKnown(shape_c->Dim(shape_c->input(0), i))) {
        dimension = shape_c->Value(shape_c->Dim(shape_c->input(0), i));
        if (!FastBoundsCheck(dimension, std::numeric_limits<int32>::max())) {
          return errors::InvalidArgument(
              "Shape has output type int32, but dimension exceeds maximum "
              "int32 value");
        }
      } else {
        dimension = shape_c->kUnknownDim;
      }
      flat(i) = static_cast<int32>(dimension);
    }
  } else if (shape_node.output_type(0) == DT_INT64) {
    auto flat = t.flat<int64_t>();
    for (int i = 0; i < src_rank; i++) {
      if (shape_c->ValueKnown(shape_c->Dim(shape_c->input(0), i))) {
        flat(i) = shape_c->Value(shape_c->Dim(shape_c->input(0), i));
      } else {
        flat(i) = shape_c->kUnknownDim;
      }
    }
  } else {
    return errors::FailedPrecondition(
        "Shape has output type that is not int32 or int64");
  }
  *output = t;
  *success = true;
  return Status::OK();
}
Status TryToInferTensorOutputFromShapeNode ( const Node & shape_node , InferenceContext * shape_c , Tensor * output , bool * success , bool allow_partial = false ) { * success = false ; if ( shape_node . type_string ( ) != " " ) return Status :: OK ( ) ; if ( shape_c == nullptr ) return Status :: OK ( ) ; if ( ! shape_c -> FullyDefined ( shape_c -> input ( 0 ) ) && ! allow_partial ) return Status :: OK ( ) ; if ( ! shape_c -> RankKnown ( shape_c -> input ( 0 ) ) ) return Status :: OK ( ) ; int src_rank = shape_c -> Rank ( shape_c -> input ( 0 ) ) ; Tensor t ( shape_node . output_type ( 0 ) , TensorShape ( { src_rank } ) ) ; if ( shape_node . output_type ( 0 ) == DT_INT32 ) { auto flat = t . flat < int > ( ) ; for ( int i = 0 ; i < src_rank ; i ++ ) { int64_t dimension ; if ( shape_c -> ValueKnown ( shape_c -> Dim ( shape_c -> input ( 0 ) , i ) ) ) { dimension = shape_c -> Value ( shape_c -> Dim ( shape_c -> input ( 0 ) , i ) ) ; if ( ! FastBoundsCheck ( dimension , std :: numeric_limits < int32 > :: max ( ) ) ) { return errors :: InvalidArgument ( " " " " ) ; } } else { dimension = shape_c -> kUnknownDim ; } flat ( i ) = static_cast < int32 > ( dimension ) ; } } else if ( shape_node . output_type ( 0 ) == DT_INT64 ) { auto flat = t . flat < int64_t > ( ) ; for ( int i = 0 ; i < src_rank ; i ++ ) { if ( shape_c -> ValueKnown ( shape_c -> Dim ( shape_c -> input ( 0 ) , i ) ) ) { flat ( i ) = shape_c -> Value ( shape_c -> Dim ( shape_c -> input ( 0 ) , i ) ) ; } else { flat ( i ) = shape_c -> kUnknownDim ; } } } else { return errors :: FailedPrecondition ( " " ) ; } * output = t ; * success = true ; return Status :: OK ( ) ; }
Tries to infer the tensor output based on the input dims of a
Shape node.
Tries to infer the tensor output based on the input dims of a Shape node .