MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / ReferenceImpl

Function ReferenceImpl

tensorflow/core/kernels/quantized_instance_norm_test.cc:28–81  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

26namespace {
27
28void ReferenceImpl(const quint8* inp, float inp_min, float inp_max,
29 const TensorShape& shape, float var_eps, float* out) {
30 int N = shape.dim_size(0);
31 int H = shape.dim_size(1);
32 int W = shape.dim_size(2);
33 int C = shape.dim_size(3);
34
35 int total = N * H * W * C;
36 float inp_scale = (inp_max - inp_min) / 255.0f;
37 std::unique_ptr<float[]> dequantized(new float[total]);
38
39 for (int i = 0; i < total; ++i) {
40 dequantized[i] = inp_min + inp_scale * static_cast<float>(inp[i]);
41 }
42
43 std::unique_ptr<float[]> inp_mean(new float[N * C]);
44 std::unique_ptr<float[]> inp_var(new float[N * C]);
45
46 float img_size = static_cast<float>(H) * static_cast<float>(W);
47
48 // Compute mean
49 for (int n = 0; n < N; ++n) {
50 for (int c = 0; c < C; ++c) {
51 float sum = 0.0;
52 for (int i = 0; i < H * W; ++i) {
53 sum += dequantized[n * H * W * C + i * C + c];
54 }
55 inp_mean[n * C + c] = sum / img_size;
56 }
57 }
58
59 // Compute var
60 for (int n = 0; n < N; ++n) {
61 for (int c = 0; c < C; ++c) {
62 float sum = 0.0;
63 for (int i = 0; i < H * W; ++i) {
64 float tmp =
65 dequantized[n * H * W * C + i * C + c] - inp_mean[n * C + c];
66 sum += tmp * tmp;
67 }
68 inp_var[n * C + c] = sum / img_size;
69 }
70 }
71
72 for (int n = 0; n < N; ++n) {
73 for (int c = 0; c < C; ++c) {
74 for (int i = 0; i < H * W; ++i) {
75 out[n * H * W * C + i * C + c] =
76 (dequantized[n * H * W * C + i * C + c] - inp_mean[n * C + c]) /
77 std::sqrt(inp_var[n * C + c] + var_eps);
78 }
79 }
80 }
81}
82
83void Expect(const Tensor& input, float x_min, float x_max,
84 bool output_range_given, float give_y_min, float given_y_max) {

Callers 1

ExpectFunction · 0.85

Calls 2

sqrtClass · 0.70
dim_sizeMethod · 0.45

Tested by

no test coverage detected