tensorflow/core/framework/shape_inference_test.cc
// Tests different context data added when Run returns error.
Tests different context data added when Run returns error.
Tests different context data added when Run returns error .
TEST_F(ShapeInferenceTest, AttachContext) {
  NodeDef def;
  def.set_name("foo");
  def.set_op("foo_op");
  {
    InferenceContext c(kVersion, def, MakeOpDef(1, 2), {S({1, 2, 3})}, {}, {},
                       {});
    TF_ASSERT_OK(c.construction_status());
    auto fn = [](InferenceContext* c) {
      ShapeHandle h;
      TF_RETURN_IF_ERROR(c->WithRankAtMost(c->input(0), 0, &h));
      c->set_output(0, c->input(0));
      return Status::OK();
    };
    auto s = c.Run(fn).ToString();
    EXPECT_CONTAINS(s, "Shape must be at most rank 0 but is rank 3");
    EXPECT_CONTAINS(s, "node foo");
    EXPECT_CONTAINS(s, "foo_op");
    EXPECT_CONTAINS(s, "input shapes: [1,2,3]");
  }
  {
    Tensor input_t =
        ::tensorflow::test::AsTensor<float>({1.1, 2.2, 3.3, 4.4, 5.5});
    InferenceContext c(kVersion, def, MakeOpDef(2, 2),
                       {S({1, 2, 3}), S({4, 5})}, {nullptr, &input_t}, {}, {});
    TF_ASSERT_OK(c.construction_status());
    auto fn = [](InferenceContext* c) {
      c->input_tensor(0);  
      c->input_tensor(1);  
      ShapeHandle h;
      TF_RETURN_IF_ERROR(c->WithRankAtMost(c->input(0), 0, &h));
      c->set_output(0, c->input(0));
      return Status::OK();
    };
    auto s = c.Run(fn).ToString();
    EXPECT_CONTAINS(s, "Shape must be at most rank 0 but is rank 3");
    EXPECT_CONTAINS(s, "node foo");
    EXPECT_CONTAINS(s, "foo_op");
    EXPECT_CONTAINS(
        s,
        "input shapes: [1,2,3], [4,5] and with computed input tensors: "
        "input[1] = <1.1 2.2 3.3 4.4 5.5>.");
  }
  {
    Tensor input_t = ::tensorflow::test::AsTensor<int32>({1, 2, 3, 4, 5});
    InferenceContext c(kVersion, def, MakeOpDef(2, 2), {S({3}), S({4})},
                       {nullptr, &input_t}, {}, {});
    TF_ASSERT_OK(c.construction_status());
    auto fn = [](InferenceContext* c) {
      ShapeHandle s;
      TF_RETURN_IF_ERROR(c->MakeShapeFromShapeTensor(0, &s));
      TF_RETURN_IF_ERROR(c->MakeShapeFromShapeTensor(1, &s));
      ShapeHandle h;
      TF_RETURN_IF_ERROR(c->WithRankAtMost(c->input(0), 0, &h));
      c->set_output(0, c->input(0));
      return Status::OK();
    };
    auto s = c.Run(fn).ToString();
    EXPECT_CONTAINS(s, "Shape must be at most rank 0 but is rank 1");
    EXPECT_CONTAINS(s, "node foo");
    EXPECT_CONTAINS(s, "foo_op");
    EXPECT_CONTAINS(
        s,
        "with input shapes: [3], [4] and with computed input tensors: input[1] "
        "= <1 2 3 4 5>.");
  }
  {
    Tensor input_t = ::tensorflow::test::AsTensor<int32>({1, 2, 3, 4, 5});
    InferenceContext c(kVersion, def, MakeOpDef(2, 2), {S({3}), S({4})},
                       {nullptr, &input_t}, {S({10, -1, 5}), Unknown()}, {});
    TF_ASSERT_OK(c.construction_status());
    auto fn = [](InferenceContext* c) {
      ShapeHandle s;
      TF_RETURN_IF_ERROR(c->MakeShapeFromShapeTensor(0, &s));
      TF_RETURN_IF_ERROR(c->MakeShapeFromShapeTensor(1, &s));
      ShapeHandle h;
      TF_RETURN_IF_ERROR(c->WithRankAtMost(c->input(0), 0, &h));
      c->set_output(0, c->input(0));
      return Status::OK();
    };
    auto s = c.Run(fn).ToString();
    EXPECT_CONTAINS(s, "Shape must be at most rank 0 but is rank 1");
    EXPECT_CONTAINS(s, "node foo");
    EXPECT_CONTAINS(s, "foo_op");
    EXPECT_CONTAINS(
        s,
        "with input shapes: [3], [4] and with computed "
        "input tensors: input[1] = <1 2 3 4 5> and with input tensors computed "
        "as partial shapes: input[0] = [10,?,5].");
  }
}
TEST_F ( ShapeInferenceTest , AttachContext ) { NodeDef def ; def . set_name ( " " ) ; def . set_op ( " " ) ; { InferenceContext c ( kVersion , def , MakeOpDef ( 1 , 2 ) , { S ( { 1 , 2 , 3 } ) } , { } , { } , { } ) ; TF_ASSERT_OK ( c . construction_status ( ) ) ; auto fn = [ ] ( InferenceContext * c ) { ShapeHandle h ; TF_RETURN_IF_ERROR ( c -> WithRankAtMost ( c -> input ( 0 ) , 0 , & h ) ) ; c -> set_output ( 0 , c -> input ( 0 ) ) ; return Status :: OK ( ) ; } ; auto s = c . Run ( fn ) . ToString ( ) ; EXPECT_CONTAINS ( s , " " ) ; EXPECT_CONTAINS ( s , " " ) ; EXPECT_CONTAINS ( s , " " ) ; EXPECT_CONTAINS ( s , " " ) ; } { Tensor input_t = :: tensorflow :: test :: AsTensor < float > ( { 1.1 , 2.2 , 3.3 , 4.4 , 5.5 } ) ; InferenceContext c ( kVersion , def , MakeOpDef ( 2 , 2 ) , { S ( { 1 , 2 , 3 } ) , S ( { 4 , 5 } ) } , { nullptr , & input_t } , { } , { } ) ; TF_ASSERT_OK ( c . construction_status ( ) ) ; auto fn = [ ] ( InferenceContext * c ) { c -> input_tensor ( 0 ) ; c -> input_tensor ( 1 ) ; ShapeHandle h ; TF_RETURN_IF_ERROR ( c -> WithRankAtMost ( c -> input ( 0 ) , 0 , & h ) ) ; c -> set_output ( 0 , c -> input ( 0 ) ) ; return Status :: OK ( ) ; } ; auto s = c . Run ( fn ) . ToString ( ) ; EXPECT_CONTAINS ( s , " " ) ; EXPECT_CONTAINS ( s , " " ) ; EXPECT_CONTAINS ( s , " " ) ; EXPECT_CONTAINS ( s , " " " " ) ; } { Tensor input_t = :: tensorflow :: test :: AsTensor < int32 > ( { 1 , 2 , 3 , 4 , 5 } ) ; InferenceContext c ( kVersion , def , MakeOpDef ( 2 , 2 ) , { S ( { 3 } ) , S ( { 4 } ) } , { nullptr , & input_t } , { } , { } ) ; TF_ASSERT_OK ( c . construction_status ( ) ) ; auto fn = [ ] ( InferenceContext * c ) { ShapeHandle s ; TF_RETURN_IF_ERROR ( c -> MakeShapeFromShapeTensor ( 0 , & s ) ) ; TF_RETURN_IF_ERROR ( c -> MakeShapeFromShapeTensor ( 1 , & s ) ) ; ShapeHandle h ; TF_RETURN_IF_ERROR ( c -> WithRankAtMost ( c -> input ( 0 ) , 0 , & h ) ) ; c -> set_output ( 0 , c -> input ( 0 ) ) ; return Status :: OK ( ) ; } ; auto s = c . Run ( fn ) . ToString ( ) ; EXPECT_CONTAINS ( s , " " ) ; EXPECT_CONTAINS ( s , " " ) ; EXPECT_CONTAINS ( s , " " ) ; EXPECT_CONTAINS ( s , " " " " ) ; } { Tensor input_t = :: tensorflow :: test :: AsTensor < int32 > ( { 1 , 2 , 3 , 4 , 5 } ) ; InferenceContext c ( kVersion , def , MakeOpDef ( 2 , 2 ) , { S ( { 3 } ) , S ( { 4 } ) } , { nullptr , & input_t } , { S ( { 10 , -1 , 5 } ) , Unknown ( ) } , { } ) ; TF_ASSERT_OK ( c . construction_status ( ) ) ; auto fn = [ ] ( InferenceContext * c ) { ShapeHandle s ; TF_RETURN_IF_ERROR ( c -> MakeShapeFromShapeTensor ( 0 , & s ) ) ; TF_RETURN_IF_ERROR ( c -> MakeShapeFromShapeTensor ( 1 , & s ) ) ; ShapeHandle h ; TF_RETURN_IF_ERROR ( c -> WithRankAtMost ( c -> input ( 0 ) , 0 , & h ) ) ; c -> set_output ( 0 , c -> input ( 0 ) ) ; return Status :: OK ( ) ; } ; auto s = c . Run ( fn ) . ToString ( ) ; EXPECT_CONTAINS ( s , " " ) ; EXPECT_CONTAINS ( s , " " ) ; EXPECT_CONTAINS ( s , " " ) ; EXPECT_CONTAINS ( s , " " " " " " ) ; } }
Tests different context data added when Run returns error.
Tests different context data added when Run returns error .
// Error when no constant tensors were requested. // Error when a constant tensor value was requested. // get this one, but it's null - won't be in error. // get this one, will now be in error. // Error when a constant tensor value as shape was requested, but no partial // shapes provided. // Error when a constant tensor value as shape was requested, and a partial // shape was provided.