tensorflow/core/kernels/quantized_matmul_op_test.cc
// This test multiplies a couple of small 8-bit matrices, and compares the
// results with hand-calculated expectations. It uses shifts and offsets to
// control the range of the outputs.
This test multiplies a couple of small 8-bit matrices, and compares the
results with hand-calculated expectations. It uses shifts and offsets to
control the range of the outputs.
This test multiplies a couple of small 8 - bit matrices and compares the results with hand - calculated expectations . It uses shifts and offsets to control the range of the outputs .
TEST_F(QuantizedMatMulTest, Small_WithParams) {
  const bool transpose_a = true;
  const int a_rows = 3;
  const int a_cols = 4;
  const int b_rows = 3;
  const int b_cols = 2;
  const bool transpose_b = false;
  TF_ASSERT_OK(NodeDefBuilder("quantized_mat_mul_op", "QuantizedMatMul")
                   .Input(FakeInput(DT_QUINT8))
                   .Input(FakeInput(DT_QUINT8))
                   .Input(FakeInput(DT_FLOAT))
                   .Input(FakeInput(DT_FLOAT))
                   .Input(FakeInput(DT_FLOAT))
                   .Input(FakeInput(DT_FLOAT))
                   .Attr("Toutput", DataTypeToEnum<qint32>::v())
                   .Attr("transpose_a", transpose_a)
                   .Attr("transpose_b", transpose_b)
                   .Finalize(node_def()));
  TF_ASSERT_OK(InitOp());
  AddInputFromArray<quint8>(TensorShape({a_rows, a_cols}), {
                                                               11,
                                                               10,
                                                               9,
                                                               8,
                                                               7,
                                                               6,
                                                               5,
                                                               4,
                                                               3,
                                                               2,
                                                               1,
                                                               0,
                                                           });
  AddInputFromArray<quint8>(TensorShape({b_rows, b_cols}), {
                                                               1,
                                                               4,
                                                               2,
                                                               5,
                                                               3,
                                                               6,
                                                           });
  AddInputFromArray<float>(TensorShape({1}), {-12.0f});
  AddInputFromArray<float>(TensorShape({1}), {243.0f});
  AddInputFromArray<float>(TensorShape({1}), {0});
  AddInputFromArray<float>(TensorShape({1}), {255.0f});
  TF_ASSERT_OK(RunOpKernel());
  Tensor expected(allocator(), DT_QINT32, TensorShape({a_cols, b_cols}));
  test::FillValues<qint32>(&expected, {
                                          -38,
                                          -83,
                                          -44,
                                          -98,
                                          -50,
                                          -113,
                                          -56,
                                          -128,
                                      });
  test::ExpectTensorEqual<qint32>(expected, *GetOutput(0));
}
TEST_F ( QuantizedMatMulTest , Small_WithParams ) { const bool transpose_a = true ; const int a_rows = 3 ; const int a_cols = 4 ; const int b_rows = 3 ; const int b_cols = 2 ; const bool transpose_b = false ; TF_ASSERT_OK ( NodeDefBuilder ( " " , " " ) . Input ( FakeInput ( DT_QUINT8 ) ) . Input ( FakeInput ( DT_QUINT8 ) ) . Input ( FakeInput ( DT_FLOAT ) ) . Input ( FakeInput ( DT_FLOAT ) ) . Input ( FakeInput ( DT_FLOAT ) ) . Input ( FakeInput ( DT_FLOAT ) ) . Attr ( " " , DataTypeToEnum < qint32 > :: v ( ) ) . Attr ( " " , transpose_a ) . Attr ( " " , transpose_b ) . Finalize ( node_def ( ) ) ) ; TF_ASSERT_OK ( InitOp ( ) ) ; AddInputFromArray < quint8 > ( TensorShape ( { a_rows , a_cols } ) , { 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 , } ) ; AddInputFromArray < quint8 > ( TensorShape ( { b_rows , b_cols } ) , { 1 , 4 , 2 , 5 , 3 , 6 , } ) ; AddInputFromArray < float > ( TensorShape ( { 1 } ) , { -12.0f } ) ; AddInputFromArray < float > ( TensorShape ( { 1 } ) , { 243.0f } ) ; AddInputFromArray < float > ( TensorShape ( { 1 } ) , { 0 } ) ; AddInputFromArray < float > ( TensorShape ( { 1 } ) , { 255.0f } ) ; TF_ASSERT_OK ( RunOpKernel ( ) ) ; Tensor expected ( allocator ( ) , DT_QINT32 , TensorShape ( { a_cols , b_cols } ) ) ; test :: FillValues < qint32 > ( & expected , { -38 , -83 , -44 , -98 , -50 , -113 , -56 , -128 , } ) ; test :: ExpectTensorEqual < qint32 > ( expected , * GetOutput ( 0 ) ) ; }
This test multiplies a couple of small 8-bit matrices, and compares the
results with hand-calculated expectations.
This test multiplies a couple of small 8 - bit matrices and compares the results with hand - calculated expectations .
// These parameters reflect a typical production usage of eight-bit matmuls // in an Inception-style network. // The A matrix is: // |  -1 |  -5 |  -9 | // |  -2 |  -6 | -10 | // |  -3 |  -7 | -11 | // |  -4 |  -8 | -12 | // The input array only contains unsigned bytes, so we specify the actual // values as n+a_offset, where a_offset is 12 above. For example that means -1 // is represented as -1 + 12, or 11. // We have set the transpose_a flag to true, so the matrix is transposed, and // for filling the values the in-memory storage order is effectively // column major, rather than the default row-major. // The B matrix is: // |   1 |   4| // |   2 |   5| // |   3 |   6| // We're requesting C = A.transposed() * B, // so we expect to get these results: // 1*-1 + 2*-5 + 3*-9 = -38 // 4*-1 + 5*-5 + 6*-9 = -83 // 1*-2 + 2*-6 + 3*-10 = -44 // 4*-2 + 5*-6 + 6*-10 = -98 // 1*-3 + 2*-7 + 3*-11 = -50 // 4*-3 + 5*-7 + 6*-11 = -113 // 1*-4 + 2*-8 + 3*-12 = -56 // 4*-4 + 5*-8 + 6*-12 = -128 // |  -38 |  -83 | // |  -44 |  -98 | // |  -50 | -113 | // |  -56 | -128 |