tensorflow/core/grappler/optimizers/constant_folding_test.cc
// The test does not evalaute the optimized and original graphs to check if
// their outputs are the same. See b/78233179.
The test does not evalaute the optimized and original graphs to check if
their outputs are the same.
The test does not evalaute the optimized and original graphs to check if their outputs are the same .
TEST_F(ConstantFoldingTest, Enter) {
  GrapplerItem item;
  AttrValue frame_name;
  frame_name.set_s("foo");
  AttrValue is_constant_true;
  is_constant_true.set_b(true);
  AttrValue is_constant_false;
  is_constant_false.set_b(false);
  AttrValue type;
  type.set_type(DT_FLOAT);
  AttrValue value;
  Tensor value_tensor(DT_FLOAT, TensorShape({}));
  value_tensor.flat<float>()(0) = 1;
  value_tensor.AsProtoTensorContent(value.mutable_tensor());
  GraphDef& graph = item.graph;
  AddNode("x", "Placeholder", {}, {{"dtype", type}}, &graph);
  AddNode("c1", "Const", {"^x"}, {{"value", value}, {"dtype", type}}, &graph);
  AddNode("enter1", "Enter", {"x"},
          {{"T", type},
           {"frame_name", frame_name},
           {"is_constant", is_constant_true}},
          &graph);
  AddNode("enter2", "Enter", {"c1"},
          {{"T", type},
           {"frame_name", frame_name},
           {"is_constant", is_constant_true}},
          &graph);
  AddNode("enter3", "Enter", {"c1"},
          {{"T", type},
           {"frame_name", frame_name},
           {"is_constant", is_constant_false}},
          &graph);
  AddNode("id1", "Identity", {"enter1"}, {{"T", type}}, &graph);
  AddNode("id2", "Identity", {"enter2"}, {{"T", type}}, &graph);
  AddNode("id3", "Identity", {"enter2"}, {{"T", type}}, &graph);
  AddNode("id4", "Identity", {"enter3"}, {{"T", type}}, &graph);
  item.fetch.push_back("id1");
  item.fetch.push_back("id2");
  item.fetch.push_back("id3");
  item.fetch.push_back("id4");
  ConstantFolding optimizer(nullptr);
  GraphDef output;
  Status status = optimizer.Optimize(nullptr, item, &output);
  TF_EXPECT_OK(status);
  item.graph.Swap(&output);
  status = optimizer.Optimize(nullptr, item, &output);
  TF_EXPECT_OK(status);
  EXPECT_EQ(9, output.node_size());
  for (const NodeDef& node : output.node()) {
    if (node.name() == "id1") {
      EXPECT_EQ("Identity", node.op());
      EXPECT_EQ(1, node.input_size());
      EXPECT_EQ("enter1", node.input(0));
    }
    if (node.name() == "id2" || node.name() == "id3") {
      EXPECT_EQ("Const", node.op());
      EXPECT_EQ(1, node.input_size());
      EXPECT_EQ("^enter2", node.input(0));
    }
    if (node.name() == "id4") {
      EXPECT_EQ("Identity", node.op());
      EXPECT_EQ(1, node.input_size());
      EXPECT_EQ("enter3", node.input(0));
    }
  }
}
TEST_F ( ConstantFoldingTest , Enter ) { GrapplerItem item ; AttrValue frame_name ; frame_name . set_s ( " " ) ; AttrValue is_constant_true ; is_constant_true . set_b ( true ) ; AttrValue is_constant_false ; is_constant_false . set_b ( false ) ; AttrValue type ; type . set_type ( DT_FLOAT ) ; AttrValue value ; Tensor value_tensor ( DT_FLOAT , TensorShape ( { } ) ) ; value_tensor . flat < float > ( ) ( 0 ) = 1 ; value_tensor . AsProtoTensorContent ( value . mutable_tensor ( ) ) ; GraphDef & graph = item . graph ; AddNode ( " " , " " , { } , { { " " , type } } , & graph ) ; AddNode ( " " , " " , { " " } , { { " " , value } , { " " , type } } , & graph ) ; AddNode ( " " , " " , { " " } , { { " " , type } , { " " , frame_name } , { " " , is_constant_true } } , & graph ) ; AddNode ( " " , " " , { " " } , { { " " , type } , { " " , frame_name } , { " " , is_constant_true } } , & graph ) ; AddNode ( " " , " " , { " " } , { { " " , type } , { " " , frame_name } , { " " , is_constant_false } } , & graph ) ; AddNode ( " " , " " , { " " } , { { " " , type } } , & graph ) ; AddNode ( " " , " " , { " " } , { { " " , type } } , & graph ) ; AddNode ( " " , " " , { " " } , { { " " , type } } , & graph ) ; AddNode ( " " , " " , { " " } , { { " " , type } } , & graph ) ; item . fetch . push_back ( " " ) ; item . fetch . push_back ( " " ) ; item . fetch . push_back ( " " ) ; item . fetch . push_back ( " " ) ; ConstantFolding optimizer ( nullptr ) ; GraphDef output ; Status status = optimizer . Optimize ( nullptr , item , & output ) ; TF_EXPECT_OK ( status ) ; item . graph . Swap ( & output ) ; status = optimizer . Optimize ( nullptr , item , & output ) ; TF_EXPECT_OK ( status ) ; EXPECT_EQ ( 9 , output . node_size ( ) ) ; for ( const NodeDef & node : output . node ( ) ) { if ( node . name ( ) == " " ) { EXPECT_EQ ( " " , node . op ( ) ) ; EXPECT_EQ ( 1 , node . input_size ( ) ) ; EXPECT_EQ ( " " , node . input ( 0 ) ) ; } if ( node . name ( ) == " " || node . name ( ) == " " ) { EXPECT_EQ ( " " , node . op ( ) ) ; EXPECT_EQ ( 1 , node . input_size ( ) ) ; EXPECT_EQ ( " " , node . input ( 0 ) ) ; } if ( node . name ( ) == " " ) { EXPECT_EQ ( " " , node . op ( ) ) ; EXPECT_EQ ( 1 , node . input_size ( ) ) ; EXPECT_EQ ( " " , node . input ( 0 ) ) ; } } }
The test does not evalaute the optimized and original graphs to check if
their outputs are the same.
The test does not evalaute the optimized and original graphs to check if their outputs are the same .
/*cpu_device=*/ /*cluster=*/ // Run the optimizer twice to make sure the rewrite is idempotent. /*cluster=*/