| 36 | namespace { |
| 37 | |
| 38 | void TestAdd(const std::vector<int64>& x_shape, |
| 39 | const std::vector<float>& x_values, float x_min_value, |
| 40 | float x_max_value, const std::vector<int64>& y_shape, |
| 41 | const std::vector<float>& y_values, float y_min_value, |
| 42 | float y_max_value, const std::vector<int64>& expected_shape, |
| 43 | const std::vector<float>& expected_values, double tolerance) { |
| 44 | Scope root = Scope::NewRootScope(); |
| 45 | |
| 46 | Tensor x_float_tensor(DT_FLOAT, TensorShape(x_shape)); |
| 47 | test::FillValues<float>(&x_float_tensor, x_values); |
| 48 | Tensor x_quantized_tensor(DT_QUINT8, x_float_tensor.shape()); |
| 49 | FloatTensorToQuantizedInPlace<quint8>(x_float_tensor, x_min_value, |
| 50 | x_max_value, &x_quantized_tensor); |
| 51 | Output x = |
| 52 | Const(root.WithOpName("x"), Input::Initializer(x_quantized_tensor)); |
| 53 | Output x_min = Const(root.WithOpName("x_min"), x_min_value); |
| 54 | Output x_max = Const(root.WithOpName("x_max"), x_max_value); |
| 55 | |
| 56 | Tensor y_float_tensor(DT_FLOAT, TensorShape(y_shape)); |
| 57 | test::FillValues<float>(&y_float_tensor, y_values); |
| 58 | Tensor y_quantized_tensor(DT_QUINT8, y_float_tensor.shape()); |
| 59 | FloatTensorToQuantizedInPlace<quint8>(y_float_tensor, y_min_value, |
| 60 | y_max_value, &y_quantized_tensor); |
| 61 | Output y = |
| 62 | Const(root.WithOpName("y"), Input::Initializer(y_quantized_tensor)); |
| 63 | Output y_min = Const(root.WithOpName("y_min"), y_min_value); |
| 64 | Output y_max = Const(root.WithOpName("y_max"), y_max_value); |
| 65 | |
| 66 | ops::QuantizedAdd add = ops::QuantizedAdd(root.WithOpName("add"), x, y, x_min, |
| 67 | x_max, y_min, y_max); |
| 68 | |
| 69 | TF_EXPECT_OK(root.status()); |
| 70 | |
| 71 | ClientSession session(root); |
| 72 | std::vector<Tensor> outputs; |
| 73 | |
| 74 | TF_EXPECT_OK(session.Run(ClientSession::FeedType(), |
| 75 | {add.z, add.min_z, add.max_z}, &outputs)); |
| 76 | |
| 77 | const Tensor& z_quantized = outputs[0]; |
| 78 | const float z_min = outputs[1].flat<float>()(0); |
| 79 | const float z_max = outputs[2].flat<float>()(0); |
| 80 | |
| 81 | Tensor z_float = QuantizedTensorToFloat<qint32>(z_quantized, z_min, z_max); |
| 82 | Tensor expected_z_float(DT_FLOAT, TensorShape(expected_shape)); |
| 83 | test::FillValues<float>(&expected_z_float, expected_values); |
| 84 | test::ExpectTensorNear<float>(expected_z_float, z_float, tolerance); |
| 85 | } |
| 86 | |
| 87 | void TestAddShape(const std::vector<int64>& x_shape, |
| 88 | const std::vector<int64>& y_shape) { |
no test coverage detected