tensorflow/core/common_runtime/eval_const_tensor.cc
// Tries to infer tensor output based on the input shapes of the node. In some
// cases, the shapes of the inputs are sufficient for inferring the contents of
// the output tensor. For example, a Shape op with fully defined input shapes
// can have its output tensor inferred.
Tries to infer tensor output based on the input shapes of the node. In some
cases, the shapes of the inputs are sufficient for inferring the contents of
the output tensor. For example, a Shape op with fully defined input shapes
can have its output tensor inferred.
Tries to infer tensor output based on the input shapes of the node . In some cases the shapes of the inputs are sufficient for inferring the contents of the output tensor . For example a Shape op with fully defined input shapes can have its output tensor inferred .
Status TryToInferTensorOutputFromInputShapes(const Edge& edge,
                                             const ShapeRefiner& refiner,
                                             Tensor* output, bool* success) {
  *success = false;
  const Node* node = edge.src();
  InferenceContext* c = refiner.GetContext(node);
  if (c == nullptr) {
    return Status::OK();
  }
  if (node->type_string() == "StridedSlice") {
    TF_RETURN_IF_ERROR(TryToInferTensorOutputFromStridedSliceNode(
        *node, refiner, output, success));
  } else if (node->type_string() == "Shape") {
    TF_RETURN_IF_ERROR(
        TryToInferTensorOutputFromShapeNode(*node, c, output, success));
  } else if (node->type_string() == "Rank") {
    bool rank_known = c->RankKnown(c->input(0));
    if (rank_known) {
      int32 input_rank = c->Rank(c->input(0));
      Tensor t(node->output_type(0), TensorShape({}));
      t.flat<int32>()(0) = input_rank;
      *output = t;
      *success = true;
    }
  } else if (node->type_string() == "Size") {
    bool fully_defined_inputs = c->FullyDefined(c->input(0));
    if (fully_defined_inputs) {
      int32 rank = c->Rank(c->input(0));
      Tensor t(node->output_type(0), TensorShape({}));
      int64 size = 1;
      for (int i = 0; i < rank; i++) {
        size *= c->Value(c->Dim(c->input(0), i));
      }
      if (node->output_type(0) == DT_INT32) {
        if (!FastBoundsCheck(size, std::numeric_limits<int32>::max())) {
          return errors::InvalidArgument(
              "Size has output type int32, but size exceeds maximum int32 "
              "value");
        }
        t.flat<int32>()(0) = static_cast<int32>(size);
      } else if (node->output_type(0) == DT_INT64) {
        t.flat<int64_t>()(0) = size;
      } else {
        return errors::FailedPrecondition(
            "Size has output type that is not int32 or int64");
      }
      *output = t;
      *success = true;
    }
  }
  return Status::OK();
}
Status TryToInferTensorOutputFromInputShapes ( const Edge & edge , const ShapeRefiner & refiner , Tensor * output , bool * success ) { * success = false ; const Node * node = edge . src ( ) ; InferenceContext * c = refiner . GetContext ( node ) ; if ( c == nullptr ) { return Status :: OK ( ) ; } if ( node -> type_string ( ) == " " ) { TF_RETURN_IF_ERROR ( TryToInferTensorOutputFromStridedSliceNode ( * node , refiner , output , success ) ) ; } else if ( node -> type_string ( ) == " " ) { TF_RETURN_IF_ERROR ( TryToInferTensorOutputFromShapeNode ( * node , c , output , success ) ) ; } else if ( node -> type_string ( ) == " " ) { bool rank_known = c -> RankKnown ( c -> input ( 0 ) ) ; if ( rank_known ) { int32 input_rank = c -> Rank ( c -> input ( 0 ) ) ; Tensor t ( node -> output_type ( 0 ) , TensorShape ( { } ) ) ; t . flat < int32 > ( ) ( 0 ) = input_rank ; * output = t ; * success = true ; } } else if ( node -> type_string ( ) == " " ) { bool fully_defined_inputs = c -> FullyDefined ( c -> input ( 0 ) ) ; if ( fully_defined_inputs ) { int32 rank = c -> Rank ( c -> input ( 0 ) ) ; Tensor t ( node -> output_type ( 0 ) , TensorShape ( { } ) ) ; int64 size = 1 ; for ( int i = 0 ; i < rank ; i ++ ) { size *= c -> Value ( c -> Dim ( c -> input ( 0 ) , i ) ) ; } if ( node -> output_type ( 0 ) == DT_INT32 ) { if ( ! FastBoundsCheck ( size , std :: numeric_limits < int32 > :: max ( ) ) ) { return errors :: InvalidArgument ( " " " " ) ; } t . flat < int32 > ( ) ( 0 ) = static_cast < int32 > ( size ) ; } else if ( node -> output_type ( 0 ) == DT_INT64 ) { t . flat < int64_t > ( ) ( 0 ) = size ; } else { return errors :: FailedPrecondition ( " " ) ; } * output = t ; * success = true ; } } return Status :: OK ( ) ; }
Tries to infer tensor output based on the input shapes of the node.
Tries to infer tensor output based on the input shapes of the node .
// An input without context is a soft failure; we sometimes need to break // control flow loops by running shape inference on a node without first // adding its input. // If input shapes to the shape op are fully defined, // we can infer the shape op's output tensor.