tensorflow/core/grappler/optimizers/generic_layout_optimizer_transposer_test.cc
// Same as SqueezeTransposerTestSqueezeDimsUpdated but with the source and
// destination formats swapped (as is used in some cases when the data type is
// DT_HALF).
Same as SqueezeTransposerTestSqueezeDimsUpdated but with the source and
destination formats swapped (as is used in some cases when the data type is
DT_HALF).
Same as SqueezeTransposerTestSqueezeDimsUpdated but with the source and destination formats swapped ( as is used in some cases when the data type is DT_HALF ) .
TEST_F(TransposerTest, SqueezeTransposerTestNCHWToNHWCSqueezeDimsUpdated) {
#if !(GOOGLE_CUDA || TENSORFLOW_USE_ROCM)
  GTEST_SKIP() << "Neither CUDA nor ROCm is enabled";
#endif  
  GrapplerItem item;
  Scope scope = Scope::NewRootScope();
  auto input =
      ops::RandomUniform(scope.WithOpName("input"), {1, 8, 1, 1}, DT_FLOAT);
  auto filter =
      ops::RandomUniform(scope.WithOpName("filter"), {1, 1, 8, 1}, DT_FLOAT);
  auto conv2d = ops::Conv2D(
      scope.WithOpName("conv2d").WithDevice("/device:GPU:0"), input, filter,
      {1, 1, 1, 1}, "SAME", ops::Conv2D::DataFormat(kDstFormat));
  auto squeeze_op =
      ops::Squeeze(scope.WithOpName("squeeze").WithDevice("/device:GPU:0"),
                   conv2d, ops::Squeeze::Attrs().Axis({2, 3}));
  auto z = ops::Identity(scope.WithOpName("z"), squeeze_op);
  TF_ASSERT_OK(scope.ToGraphDef(&item.graph));
  TransposeContext context;
  TF_ASSERT_OK(TransposeContext::InitializeTransposeContext(
      item, virtual_cluster_.get(), &context));
  context.AssignDeviceAndDataFormats(kGPU, kDstFormat, kSrcFormat);
  DefaultLayoutSensitiveOpTransposer conv2d_transposer;
  auto* c2d = context.graph_view->GetNode("conv2d");
  ASSERT_NE(c2d, nullptr);
  TF_ASSERT_OK(conv2d_transposer.TransposeNode(&context, c2d));
  SqueezeTransposer squeeze_transposer;
  auto* squeeze = context.graph_view->GetNode("squeeze");
  ASSERT_NE(squeeze, nullptr);
  TF_ASSERT_OK(squeeze_transposer.TransposeNode(&context, squeeze));
  auto* input_transpose_node1 = context.graph_view->GetNode(
      "squeeze-0-TransposeNCHWToNHWC-LayoutOptimizer");
  ASSERT_NE(input_transpose_node1, nullptr);
  ASSERT_EQ(input_transpose_node1->NumRegularFanins(), 2);
  VerifyRegularFaninMatch(input_transpose_node1, 0,
                          "conv2d-0-0-TransposeNHWCToNCHW-LayoutOptimizer", 0);
  auto* squeeze_node = context.graph_view->GetNode("squeeze");
  ASSERT_NE(squeeze_node, nullptr);
  ASSERT_EQ(squeeze_node->NumRegularFanins(), 1);
  VerifyRegularFaninMatch(squeeze_node, 0, input_transpose_node1->GetName(), 0);
  const auto* squeeze_dims_attr = squeeze_node->GetAttr("squeeze_dims");
  const auto& list = squeeze_dims_attr->list();
  ASSERT_EQ(list.i_size(), 2);
  EXPECT_EQ(list.i(0), 1);
  EXPECT_EQ(list.i(1), 2);
  auto* output_transpose_node = context.graph_view->GetNode(
      "squeeze-0-0-TransposeNHWCToNCHW-LayoutOptimizer");
  EXPECT_EQ(output_transpose_node, nullptr);
  auto* z_output_node = context.graph_view->GetNode("z");
  ASSERT_NE(z_output_node, nullptr);
  ASSERT_EQ(z_output_node->NumRegularFanins(), 1);
  VerifyRegularFaninMatch(z_output_node, 0, squeeze_node->GetName(), 0);
}
TEST_F ( TransposerTest , SqueezeTransposerTestNCHWToNHWCSqueezeDimsUpdated ) { #if ! ( GOOGLE_CUDA || TENSORFLOW_USE_ROCM ) GTEST_SKIP ( ) << " " ; #endif GrapplerItem item ; Scope scope = Scope :: NewRootScope ( ) ; auto input = ops :: RandomUniform ( scope . WithOpName ( " " ) , { 1 , 8 , 1 , 1 } , DT_FLOAT ) ; auto filter = ops :: RandomUniform ( scope . WithOpName ( " " ) , { 1 , 1 , 8 , 1 } , DT_FLOAT ) ; auto conv2d = ops :: Conv2D ( scope . WithOpName ( " " ) . WithDevice ( " " ) , input , filter , { 1 , 1 , 1 , 1 } , " " , ops :: Conv2D :: DataFormat ( kDstFormat ) ) ; auto squeeze_op = ops :: Squeeze ( scope . WithOpName ( " " ) . WithDevice ( " " ) , conv2d , ops :: Squeeze :: Attrs ( ) . Axis ( { 2 , 3 } ) ) ; auto z = ops :: Identity ( scope . WithOpName ( " " ) , squeeze_op ) ; TF_ASSERT_OK ( scope . ToGraphDef ( & item . graph ) ) ; TransposeContext context ; TF_ASSERT_OK ( TransposeContext :: InitializeTransposeContext ( item , virtual_cluster_ . get ( ) , & context ) ) ; context . AssignDeviceAndDataFormats ( kGPU , kDstFormat , kSrcFormat ) ; DefaultLayoutSensitiveOpTransposer conv2d_transposer ; auto * c2d = context . graph_view -> GetNode ( " " ) ; ASSERT_NE ( c2d , nullptr ) ; TF_ASSERT_OK ( conv2d_transposer . TransposeNode ( & context , c2d ) ) ; SqueezeTransposer squeeze_transposer ; auto * squeeze = context . graph_view -> GetNode ( " " ) ; ASSERT_NE ( squeeze , nullptr ) ; TF_ASSERT_OK ( squeeze_transposer . TransposeNode ( & context , squeeze ) ) ; auto * input_transpose_node1 = context . graph_view -> GetNode ( " " ) ; ASSERT_NE ( input_transpose_node1 , nullptr ) ; ASSERT_EQ ( input_transpose_node1 -> NumRegularFanins ( ) , 2 ) ; VerifyRegularFaninMatch ( input_transpose_node1 , 0 , " " , 0 ) ; auto * squeeze_node = context . graph_view -> GetNode ( " " ) ; ASSERT_NE ( squeeze_node , nullptr ) ; ASSERT_EQ ( squeeze_node -> NumRegularFanins ( ) , 1 ) ; VerifyRegularFaninMatch ( squeeze_node , 0 , input_transpose_node1 -> GetName ( ) , 0 ) ; const auto * squeeze_dims_attr = squeeze_node -> GetAttr ( " " ) ; const auto & list = squeeze_dims_attr -> list ( ) ; ASSERT_EQ ( list . i_size ( ) , 2 ) ; EXPECT_EQ ( list . i ( 0 ) , 1 ) ; EXPECT_EQ ( list . i ( 1 ) , 2 ) ; auto * output_transpose_node = context . graph_view -> GetNode ( " " ) ; EXPECT_EQ ( output_transpose_node , nullptr ) ; auto * z_output_node = context . graph_view -> GetNode ( " " ) ; ASSERT_NE ( z_output_node , nullptr ) ; ASSERT_EQ ( z_output_node -> NumRegularFanins ( ) , 1 ) ; VerifyRegularFaninMatch ( z_output_node , 0 , squeeze_node -> GetName ( ) , 0 ) ; }
Same as SqueezeTransposerTestSqueezeDimsUpdated but with the source and
destination formats swapped (as is used in some cases when the data type is
DT_HALF).
Same as SqueezeTransposerTestSqueezeDimsUpdated but with the source and destination formats swapped ( as is used in some cases when the data type is DT_HALF ) .
// !(GOOGLE_CUDA || TENSORFLOW_USE_ROCM)