Skip to content

Commit

Permalink
Implement bilinear upsampling (CHW layout) for ARM architecture
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 353317573
  • Loading branch information
ablavatski authored and xnnpack-bot committed Jan 22, 2021
1 parent 2302ffd commit 2202c81
Show file tree
Hide file tree
Showing 12 changed files with 1,436 additions and 2 deletions.
8 changes: 8 additions & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1525,6 +1525,8 @@ NEON_UKERNELS = [
"src/f32-hswish/gen/hswish-neon-x16.c",
"src/f32-ibilinear/gen/neon-c4.c",
"src/f32-ibilinear/gen/neon-c8.c",
"src/f32-ibilinear-chw/gen/neon-p4.c",
"src/f32-ibilinear-chw/gen/neon-p8.c",
"src/f32-igemm/gen/1x8-minmax-neon-dup-ld64.c",
"src/f32-igemm/gen/1x8-minmax-neon-lane-ld64.c",
"src/f32-igemm/gen/1x8s4-minmax-neon.c",
Expand Down Expand Up @@ -1802,6 +1804,8 @@ NEONFMA_UKERNELS = [
"src/f32-gemm/gen/8x8s4-minmax-neonfma.c",
"src/f32-ibilinear/gen/neonfma-c4.c",
"src/f32-ibilinear/gen/neonfma-c8.c",
"src/f32-ibilinear-chw/gen/neonfma-p4.c",
"src/f32-ibilinear-chw/gen/neonfma-p8.c",
"src/f32-igemm/gen/1x8-minmax-neonfma-dup-ld64.c",
"src/f32-igemm/gen/1x8s4-minmax-neonfma.c",
"src/f32-igemm/gen/4x8-minmax-neonfma-dup-ld64.c",
Expand Down Expand Up @@ -6807,6 +6811,7 @@ xnnpack_unit_test(

xnnpack_unit_test(
name = "add_nd_test",
timeout = "moderate",
srcs = [
"test/add-nd.cc",
"test/binary-elementwise-operator-tester.h",
Expand Down Expand Up @@ -6879,6 +6884,7 @@ xnnpack_unit_test(

xnnpack_unit_test(
name = "convolution_nhwc_test",
timeout = "moderate",
srcs = [
"test/convolution-nhwc.cc",
"test/convolution-operator-tester.h",
Expand All @@ -6888,6 +6894,7 @@ xnnpack_unit_test(

xnnpack_unit_test(
name = "convolution_nchw_test",
timeout = "moderate",
srcs = [
"test/convolution-nchw.cc",
"test/convolution-operator-tester.h",
Expand Down Expand Up @@ -7006,6 +7013,7 @@ xnnpack_unit_test(

xnnpack_unit_test(
name = "max_pooling_nhwc_test",
timeout = "moderate",
srcs = [
"test/max-pooling-nhwc.cc",
"test/max-pooling-operator-tester.h",
Expand Down
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,8 @@ SET(XNNPACK_NEON_MICROKERNEL_SRCS
src/f32-hswish/gen/hswish-neon-x16.c
src/f32-ibilinear/gen/neon-c4.c
src/f32-ibilinear/gen/neon-c8.c
src/f32-ibilinear-chw/gen/neon-p4.c
src/f32-ibilinear-chw/gen/neon-p8.c
src/f32-igemm/gen/1x8-minmax-neon-dup-ld64.c
src/f32-igemm/gen/1x8-minmax-neon-lane-ld64.c
src/f32-igemm/gen/1x8s4-minmax-neon.c
Expand Down Expand Up @@ -1050,6 +1052,8 @@ SET(XNNPACK_NEONFMA_MICROKERNEL_SRCS
src/f32-gemm/gen/8x8s4-minmax-neonfma.c
src/f32-ibilinear/gen/neonfma-c4.c
src/f32-ibilinear/gen/neonfma-c8.c
src/f32-ibilinear-chw/gen/neonfma-p4.c
src/f32-ibilinear-chw/gen/neonfma-p8.c
src/f32-igemm/gen/1x8-minmax-neonfma-dup-ld64.c
src/f32-igemm/gen/1x8s4-minmax-neonfma.c
src/f32-igemm/gen/4x8-minmax-neonfma-dup-ld64.c
Expand Down
7 changes: 7 additions & 0 deletions scripts/generate-f32-ibilinear-chw.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,12 @@ tools/xngen src/f32-ibilinear-chw/scalar.c.in -D PIXEL_TILE=4 -o src/f32-ibiline
tools/xngen src/f32-ibilinear-chw/wasmsimd.c.in -D PIXEL_TILE=4 -o src/f32-ibilinear-chw/gen/wasmsimd-p4.c
tools/xngen src/f32-ibilinear-chw/wasmsimd.c.in -D PIXEL_TILE=8 -o src/f32-ibilinear-chw/gen/wasmsimd-p8.c

############################### ARM NEON ##############################
tools/xngen src/f32-ibilinear-chw/neon.c.in -D PIXEL_TILE=4 -D FMA=0 -o src/f32-ibilinear-chw/gen/neon-p4.c
tools/xngen src/f32-ibilinear-chw/neon.c.in -D PIXEL_TILE=8 -D FMA=0 -o src/f32-ibilinear-chw/gen/neon-p8.c

tools/xngen src/f32-ibilinear-chw/neon.c.in -D PIXEL_TILE=4 -D FMA=1 -o src/f32-ibilinear-chw/gen/neonfma-p4.c
tools/xngen src/f32-ibilinear-chw/neon.c.in -D PIXEL_TILE=8 -D FMA=1 -o src/f32-ibilinear-chw/gen/neonfma-p8.c

################################## Unit tests #################################
tools/generate-ibilinear-chw-test.py --spec test/f32-ibilinear-chw.yaml --output test/f32-ibilinear-chw.cc
167 changes: 167 additions & 0 deletions src/f32-ibilinear-chw/gen/neon-p4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
// Auto-generated file. Do not edit!
// Template: src/f32-ibilinear-chw/neon.c.in
// Generator: tools/xngen
//
// Copyright 2020 Google LLC
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

#include <assert.h>

#include <arm_neon.h>

#include <xnnpack/ibilinear.h>


void xnn_f32_ibilinear_chw_ukernel__neon_p4(
size_t output_pixels,
size_t channels,
const float**restrict input,
size_t input_offset,
const float*restrict weights,
float*restrict output,
size_t input_increment) XNN_DISABLE_TSAN
{
assert(output_pixels != 0);
assert(channels != 0);
assert(input_increment % sizeof(float) == 0);

do {
const float** i = input;
const float* w = weights;
size_t p = output_pixels;

for (; p >= 4; p -= 4) {
const float* itl0 = (const float*) ((uintptr_t) i[0] + input_offset);
const float* ibl0 = (const float*) ((uintptr_t) i[1] + input_offset);
const float* itl1 = (const float*) ((uintptr_t) i[2] + input_offset);
const float* ibl1 = (const float*) ((uintptr_t) i[3] + input_offset);
const float* itl2 = (const float*) ((uintptr_t) i[4] + input_offset);
const float* ibl2 = (const float*) ((uintptr_t) i[5] + input_offset);
const float* itl3 = (const float*) ((uintptr_t) i[6] + input_offset);
const float* ibl3 = (const float*) ((uintptr_t) i[7] + input_offset);
i += 8;

const float32x4x2_t vw = vld2q_f32(w);
w += 8;

const float32x2_t vtltr0 = vld1_f32(itl0);
const float32x2_t vblbr0 = vld1_f32(ibl0);
const float32x2_t vtltr1 = vld1_f32(itl1);
const float32x2_t vblbr1 = vld1_f32(ibl1);
const float32x2_t vtltr2 = vld1_f32(itl2);
const float32x2_t vblbr2 = vld1_f32(ibl2);
const float32x2_t vtltr3 = vld1_f32(itl3);
const float32x2_t vblbr3 = vld1_f32(ibl3);

const float32x4_t valphah = vw.val[0];
const float32x4_t valphav = vw.val[1];

const float32x4_t vtltr01 = vcombine_f32(vtltr0, vtltr1);
const float32x4_t vblbr01 = vcombine_f32(vblbr0, vblbr1);
const float32x4_t vtltr23 = vcombine_f32(vtltr2, vtltr3);
const float32x4_t vblbr23 = vcombine_f32(vblbr2, vblbr3);

const float32x4_t vldrd01 = vsubq_f32(vblbr01, vtltr01);
const float32x4_t vldrd23 = vsubq_f32(vblbr23, vtltr23);

const float32x4x2_t vld_t = vuzpq_f32(vldrd01, vldrd23);
const float32x4_t vld = vld_t.val[0];
const float32x4_t vrd = vld_t.val[1];

const float32x4x2_t vtl_t = vuzpq_f32(vtltr01, vtltr23);
const float32x4_t vtl = vtl_t.val[0];
const float32x4_t vtr = vtl_t.val[1];

const float32x4_t vl = vmlaq_f32(vtl, vld, valphav);
const float32x4_t vr = vmlaq_f32(vtr, vrd, valphav);

const float32x4_t vd = vsubq_f32(vr, vl);
const float32x4_t vo = vmlaq_f32(vl, vd, valphah);

vst1q_f32(output, vo);
output += 4;
}

if XNN_UNLIKELY(p != 0) {
if (p & 2) {
const float32x2x2_t vw = vld2_f32(w);
w += 4;

const float32x2_t valphah = vw.val[0];
const float32x2_t valphav = vw.val[1];

const float* itl0 = (const float*) ((uintptr_t) i[0] + input_offset);
const float* ibl0 = (const float*) ((uintptr_t) i[1] + input_offset);
const float* itl1 = (const float*) ((uintptr_t) i[2] + input_offset);
const float* ibl1 = (const float*) ((uintptr_t) i[3] + input_offset);
i += 4;

const float32x2_t vtltr0 = vld1_f32(itl0);
const float32x2_t vblbr0 = vld1_f32(ibl0);
const float32x2_t vtltr1 = vld1_f32(itl1);
const float32x2_t vblbr1 = vld1_f32(ibl1);

const float32x2_t vldrd0 = vsub_f32(vblbr0, vtltr0);
const float32x2_t vldrd1 = vsub_f32(vblbr1, vtltr1);

const float32x2x2_t vld_t = vuzp_f32(vldrd0, vldrd1);
const float32x2_t vld = vld_t.val[0];
const float32x2_t vrd = vld_t.val[1];

const float32x2x2_t vtl_t = vuzp_f32(vtltr0, vtltr1);
const float32x2_t vtl = vtl_t.val[0];
const float32x2_t vtr = vtl_t.val[1];

const float32x2_t vl = vmla_f32(vtl, vld, valphav);
const float32x2_t vr = vmla_f32(vtr, vrd, valphav);

const float32x2_t vd = vsub_f32(vr, vl);
const float32x2_t vo = vmla_f32(vl, vd, valphah);

vst1_f32(output, vo);
output += 2;
}

if (p & 1) {
// We are computing the following formula:
// result = (1 - alpha_h) * (1 - alpha_v) * top_left +
// alpha_h * (1 - alpha_v) * top_right +
// (1 - alpha_h) * alpha_v * bottom_left +
// alpha_h * alpha_v * bottom_right.
//
// Rearranging gives
// result = left + alpha_h * (right - left),
// where
// left = top_left + alpha_v * (bottom_left - top_left),
// right = top_right + alpha_v * (bottom_right - top_right).

const float alphah = *w;
const float32x2_t valphav = vld1_dup_f32(w + 1);
w += 2;

const float* itl = (const float*) ((uintptr_t) i[0] + input_offset);
const float* ibl = (const float*) ((uintptr_t) i[1] + input_offset);
i += 2;

const float32x2_t vtltr = vld1_f32(itl);
const float32x2_t vblbr = vld1_f32(ibl);

// Compute at once
// left_diff = bottom_left - top_left
// right_diff = bottom_right - top_right
const float32x2_t vldrd = vsub_f32(vblbr, vtltr);
const float32x2_t vlr = vmla_f32(vtltr, vldrd, valphav);

// Extract them and compute the result.
const float l = vget_lane_f32(vlr, 0);
const float r = vget_lane_f32(vlr, 1);

*output++ = l + alphah * (r - l);
}
}

input_offset += input_increment;
} while (--channels != 0);
}
Loading

0 comments on commit 2202c81

Please sign in to comment.