Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 1 | // Copyright 2019 Google LLC |
| 2 | // |
| 3 | // This source code is licensed under the BSD-style license found in the |
| 4 | // LICENSE file in the root directory of this source tree. |
| 5 | |
| 6 | #include <assert.h> |
| 7 | |
Erich Elsen | eda9c11 | 2020-05-11 04:40:25 -0700 | [diff] [blame] | 8 | #include <stdio.h> |
| 9 | |
Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 10 | #include <xnnpack/dwconv.h> |
| 11 | #include <xnnpack/math.h> |
| 12 | |
| 13 | |
| 14 | void xnn_f32_dwconv_spchw_ukernel_3x3s2p1__scalar( |
Erich Elsen | eda9c11 | 2020-05-11 04:40:25 -0700 | [diff] [blame] | 15 | size_t input_height, |
| 16 | size_t input_width, |
Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 17 | const float* input, |
| 18 | const float* weights, |
Erich Elsen | 4e5db3d | 2020-05-07 08:57:47 -0700 | [diff] [blame] | 19 | const float* zero, |
Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 20 | float* output, |
Erich Elsen | 4e5db3d | 2020-05-07 08:57:47 -0700 | [diff] [blame] | 21 | uint32_t padding_top, |
Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 22 | size_t input_tuple_stride, |
| 23 | size_t output_tuple_stride, |
| 24 | size_t input_width_stride, |
| 25 | size_t output_width_stride, |
Marat Dukhan | f196d01 | 2020-04-15 11:50:03 -0700 | [diff] [blame] | 26 | const union xnn_f32_spchw_params params[restrict XNN_MIN_ELEMENTS(1)]) |
Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 27 | { |
Erich Elsen | eda9c11 | 2020-05-11 04:40:25 -0700 | [diff] [blame] | 28 | assert(input_height!= 0); |
| 29 | assert(input_width != 0); |
| 30 | assert(padding_top >= 0 && padding_top <= 1); |
Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 31 | |
Erich Elsen | eda9c11 | 2020-05-11 04:40:25 -0700 | [diff] [blame] | 32 | const size_t padded_input_height = input_height + padding_top + 1 /* padding_bottom */; |
| 33 | const size_t output_height = (padded_input_height - 3) / 2 + 1; |
| 34 | |
| 35 | const size_t input_width_decrement_single = (input_width/2) * 2 * input_tuple_stride;; |
Erich Elsen | 4e5db3d | 2020-05-07 08:57:47 -0700 | [diff] [blame] | 36 | const size_t input_width_increment = 2 * input_width_stride - input_width_decrement_single; |
Erich Elsen | eda9c11 | 2020-05-11 04:40:25 -0700 | [diff] [blame] | 37 | const size_t output_width_increment = output_width_stride - (input_width/2) * output_tuple_stride; |
Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 38 | |
| 39 | const float params_min = params->scalar.min; |
| 40 | const float params_max = params->scalar.max; |
| 41 | |
Erich Elsen | 4e5db3d | 2020-05-07 08:57:47 -0700 | [diff] [blame] | 42 | const float* i0; |
| 43 | const float* i1; |
| 44 | const float* i2; |
| 45 | |
| 46 | if (padding_top == 0) { |
| 47 | i0 = input; |
| 48 | i1 = (const float*) ((uintptr_t) i0 + input_width_stride); |
| 49 | i2 = (const float*) ((uintptr_t) i1 + input_width_stride); |
Erich Elsen | eda9c11 | 2020-05-11 04:40:25 -0700 | [diff] [blame] | 50 | if (input_height <= 2) { |
Erich Elsen | 4e5db3d | 2020-05-07 08:57:47 -0700 | [diff] [blame] | 51 | i2 = zero; |
| 52 | } |
Erich Elsen | eda9c11 | 2020-05-11 04:40:25 -0700 | [diff] [blame] | 53 | if (input_height == 1) { |
| 54 | i1 = zero; |
| 55 | } |
Erich Elsen | 4e5db3d | 2020-05-07 08:57:47 -0700 | [diff] [blame] | 56 | } else { |
| 57 | i0 = zero; |
| 58 | i1 = input; |
| 59 | i2 = (const float*) ((uintptr_t) i1 + input_width_stride); |
Erich Elsen | eda9c11 | 2020-05-11 04:40:25 -0700 | [diff] [blame] | 60 | if (input_height == 1) { |
Erich Elsen | 4e5db3d | 2020-05-07 08:57:47 -0700 | [diff] [blame] | 61 | i2 = zero; |
| 62 | } |
| 63 | } |
Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 64 | |
| 65 | float* output0 = output; |
| 66 | |
| 67 | const float vw0 = weights[0]; |
| 68 | const float vw1 = weights[1]; |
| 69 | const float vw2 = weights[2]; |
| 70 | const float vw3 = weights[3]; |
| 71 | const float vw4 = weights[4]; |
| 72 | const float vw5 = weights[5]; |
| 73 | const float vw6 = weights[6]; |
| 74 | const float vw7 = weights[7]; |
| 75 | const float vw8 = weights[8]; |
| 76 | const float vw9 = weights[9]; |
| 77 | |
Erich Elsen | eda9c11 | 2020-05-11 04:40:25 -0700 | [diff] [blame] | 78 | size_t m = output_height; |
Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 79 | while (m > 0) { |
| 80 | float vi0x0 = 0.0f; |
| 81 | float vi1x0 = 0.0f; |
| 82 | float vi2x0 = 0.0f; |
| 83 | |
Erich Elsen | eda9c11 | 2020-05-11 04:40:25 -0700 | [diff] [blame] | 84 | size_t k = input_width; |
Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 85 | for (; k >= 2; k -= 2) { |
| 86 | const float vi0x1 = *i0; i0 = (const float*) ((uintptr_t) i0 + input_tuple_stride); |
| 87 | const float vi1x1 = *i1; i1 = (const float*) ((uintptr_t) i1 + input_tuple_stride); |
| 88 | const float vi2x1 = *i2; i2 = (const float*) ((uintptr_t) i2 + input_tuple_stride); |
| 89 | const float vi0x2 = *i0; i0 = (const float*) ((uintptr_t) i0 + input_tuple_stride); |
| 90 | const float vi1x2 = *i1; i1 = (const float*) ((uintptr_t) i1 + input_tuple_stride); |
| 91 | const float vi2x2 = *i2; i2 = (const float*) ((uintptr_t) i2 + input_tuple_stride); |
| 92 | |
| 93 | const float vrow0_accum = vw1 * vi0x0 + vw2 * vi0x1 + vw3 * vi0x2; |
| 94 | vi0x0 = vi0x2; |
| 95 | const float vrow1_accum = vw4 * vi1x0 + vw5 * vi1x1 + vw6 * vi1x2; |
| 96 | vi1x0 = vi1x2; |
| 97 | const float vrow2_accum = vw7 * vi2x0 + vw8 * vi2x1 + vw9 * vi2x2; |
| 98 | vi2x0 = vi2x2; |
| 99 | |
| 100 | float voutput = (vw0 + vrow0_accum) + (vrow1_accum + vrow2_accum); |
| 101 | |
| 102 | voutput = math_max_f32(voutput, params_min); |
| 103 | voutput = math_min_f32(voutput, params_max); |
| 104 | |
| 105 | *output0 = voutput; output0 = (float *) ((uintptr_t) output0 + output_tuple_stride); |
| 106 | } |
| 107 | // Possibly process the last pixel separately to account for right edge. |
| 108 | if (k == 1) |
| 109 | { |
| 110 | const float vi0x1 = i0[0]; |
| 111 | const float vi1x1 = i1[0]; |
| 112 | const float vi2x1 = i2[0]; |
| 113 | const float vrow0_accum = vw1 * vi0x0 + vw2 * vi0x1; |
| 114 | const float vrow1_accum = vw4 * vi1x0 + vw5 * vi1x1; |
| 115 | const float vrow2_accum = vw7 * vi2x0 + vw8 * vi2x1; |
| 116 | |
| 117 | float voutput = (vw0 + vrow0_accum) + (vrow1_accum + vrow2_accum); |
| 118 | |
| 119 | voutput = math_max_f32(voutput, params_min); |
| 120 | voutput = math_min_f32(voutput, params_max); |
| 121 | |
| 122 | *output0 = voutput; |
| 123 | } |
| 124 | |
Erich Elsen | 4e5db3d | 2020-05-07 08:57:47 -0700 | [diff] [blame] | 125 | i0 = (const float*) ((uintptr_t) i2 - input_width_decrement_single); |
Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 126 | i1 = (const float*) ((uintptr_t) i1 + input_width_increment); |
| 127 | i2 = (const float*) ((uintptr_t) i2 + input_width_increment); |
| 128 | output0 = (float*) ((uintptr_t) output0 + output_width_increment); |
Erich Elsen | 4e5db3d | 2020-05-07 08:57:47 -0700 | [diff] [blame] | 129 | m -= 1; |
Erich Elsen | eda9c11 | 2020-05-11 04:40:25 -0700 | [diff] [blame] | 130 | if (m == 1 && padding_top == input_height % 2) { |
| 131 | // to mimic the following code with only one if, we do some small |
| 132 | // shenanigans... |
| 133 | // if (padding_top == 0 && input_height % 2 == 0) { |
| 134 | // i2 = zero; |
| 135 | // } else if (padding_top == 1 && input_height % 2 == 1) { |
| 136 | // i2 = zero; |
| 137 | // } |
Erich Elsen | 4e5db3d | 2020-05-07 08:57:47 -0700 | [diff] [blame] | 138 | i2 = zero; |
| 139 | } |
Erich Elsen | ac4de80 | 2019-10-16 04:35:30 -0700 | [diff] [blame] | 140 | } |
| 141 | } |