tensorflow/compiler/xla/tests/broadcast_simple_test.cc
// Test r2 op1 r2_implicit_1 op2 r2_implicit_2
// where R2 is a rank-2 operand, and r2_implicit_2 are two
// rank-2 operands with degenerate dimensions:
Test r2 op1 r2_implicit_1 op2 r2_implicit_2
where R2 is a rank-2 operand, and r2_implicit_2 are two
rank-2 operands with degenerate dimensions.
Test r2 op1 r2_implicit_1 op2 r2_implicit_2 where R2 is a rank - 2 operand and r2_implicit_2 are two rank - 2 operands with degenerate dimensions .
XLA_TEST_P(BroadcastR2ImplicitTest, Doit) {
  const R2ImplicitBroadcastSpec& spec = GetParam();
  XlaBuilder builder(TestName());
  Shape r2_shape, r2_implicit_shape1, r2_implicit_shape2;
  Array2D<float> r2_array(spec.output_bounds[0], spec.output_bounds[1]);
  Array2D<float> r2_implicit_array1(spec.input_bounds1[0],
                                    spec.input_bounds1[1]);
  Array2D<float> r2_implicit_array2(spec.input_bounds2[0],
                                    spec.input_bounds2[1]);
  std::unique_ptr<GlobalData> r2_global_data =
      MakeR2Data(spec.output_bounds, spec.minor2major_layout, &r2_shape,
                 &r2_array, 1.0, 2.5, 56789);
  std::unique_ptr<GlobalData> r2_implicit_global_data1 =
      MakeR2Data(spec.input_bounds1, spec.minor2major_layout,
                 &r2_implicit_shape1, &r2_implicit_array1, 1.0, 0.2, 56789);
  std::unique_ptr<GlobalData> r2_implicit_global_data2 =
      MakeR2Data(spec.input_bounds2, spec.minor2major_layout,
                 &r2_implicit_shape2, &r2_implicit_array2, 0.8, 0.4, 56789);
  auto r2_implicit_parameter1 =
      Parameter(&builder, 0, r2_implicit_shape1, "input0");
  auto r2_parameter = Parameter(&builder, 1, r2_shape, "input1");
  auto r2_implicit_parameter2 =
      Parameter(&builder, 2, r2_implicit_shape2, "input2");
  XlaOp op1 =
      BuildBinOp(spec.op1, r2_implicit_parameter1, r2_parameter, &builder);
  BuildBinOp(spec.op2, op1, r2_implicit_parameter2, &builder);
  Array2D<float> expected_array(spec.output_bounds[0], spec.output_bounds[1]);
  expected_array.Each([&](int64_t i, int64_t j, float* v) {
    float v1 = r2_implicit_array1(i % spec.input_bounds1[0],
                                  j % spec.input_bounds1[1]);
    float v2 = r2_array(i, j);
    float v3 = r2_implicit_array2(i % spec.input_bounds2[0],
                                  j % spec.input_bounds2[1]);
    float tmp = ApplyOpToFloats(spec.op1, v1, v2);
    *v = ApplyOpToFloats(spec.op2, tmp, v3);
  });
  auto expected = LiteralUtil::CreateR2FromArray2D(expected_array);
  ComputeAndCompareLiteral(
      &builder, expected,
      {r2_implicit_global_data1.get(), r2_global_data.get(),
       r2_implicit_global_data2.get()},
      ErrorSpec(1e-6, 1e-6));
}
XLA_TEST_P ( BroadcastR2ImplicitTest , Doit ) { const R2ImplicitBroadcastSpec & spec = GetParam ( ) ; XlaBuilder builder ( TestName ( ) ) ; Shape r2_shape , r2_implicit_shape1 , r2_implicit_shape2 ; Array2D < float > r2_array ( spec . output_bounds [ 0 ] , spec . output_bounds [ 1 ] ) ; Array2D < float > r2_implicit_array1 ( spec . input_bounds1 [ 0 ] , spec . input_bounds1 [ 1 ] ) ; Array2D < float > r2_implicit_array2 ( spec . input_bounds2 [ 0 ] , spec . input_bounds2 [ 1 ] ) ; std :: unique_ptr < GlobalData > r2_global_data = MakeR2Data ( spec . output_bounds , spec . minor2major_layout , & r2_shape , & r2_array , 1.0 , 2.5 , 56789 ) ; std :: unique_ptr < GlobalData > r2_implicit_global_data1 = MakeR2Data ( spec . input_bounds1 , spec . minor2major_layout , & r2_implicit_shape1 , & r2_implicit_array1 , 1.0 , 0.2 , 56789 ) ; std :: unique_ptr < GlobalData > r2_implicit_global_data2 = MakeR2Data ( spec . input_bounds2 , spec . minor2major_layout , & r2_implicit_shape2 , & r2_implicit_array2 , 0.8 , 0.4 , 56789 ) ; auto r2_implicit_parameter1 = Parameter ( & builder , 0 , r2_implicit_shape1 , " " ) ; auto r2_parameter = Parameter ( & builder , 1 , r2_shape , " " ) ; auto r2_implicit_parameter2 = Parameter ( & builder , 2 , r2_implicit_shape2 , " " ) ; XlaOp op1 = BuildBinOp ( spec . op1 , r2_implicit_parameter1 , r2_parameter , & builder ) ; BuildBinOp ( spec . op2 , op1 , r2_implicit_parameter2 , & builder ) ; Array2D < float > expected_array ( spec . output_bounds [ 0 ] , spec . output_bounds [ 1 ] ) ; expected_array . Each ( [ & ] ( int64_t i , int64_t j , float * v ) { float v1 = r2_implicit_array1 ( i % spec . input_bounds1 [ 0 ] , j % spec . input_bounds1 [ 1 ] ) ; float v2 = r2_array ( i , j ) ; float v3 = r2_implicit_array2 ( i % spec . input_bounds2 [ 0 ] , j % spec . input_bounds2 [ 1 ] ) ; float tmp = ApplyOpToFloats ( spec . op1 , v1 , v2 ) ; * v = ApplyOpToFloats ( spec . op2 , tmp , v3 ) ; } ) ; auto expected = LiteralUtil :: CreateR2FromArray2D ( expected_array ) ; ComputeAndCompareLiteral ( & builder , expected , { r2_implicit_global_data1 . get ( ) , r2_global_data . get ( ) , r2_implicit_global_data2 . get ( ) } , ErrorSpec ( 1e-6 , 1e-6 ) ) ; }
Test r2 op1 r2_implicit_1 op2 r2_implicit_2
where R2 is a rank-2 operand, and r2_implicit_2 are two
rank-2 operands with degenerate dimensions:
Test r2 op1 r2_implicit_1 op2 r2_implicit_2 where R2 is a rank - 2 operand and r2_implicit_2 are two rank - 2 operands with degenerate dimensions :
// Operands with degenerate dimensions require implicit broadcasting: