| // Copyright 2019 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. |
| |
| $ABC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| #include <assert.h> |
| |
| #include <xnnpack/math.h> |
| #include <xnnpack/spmm.h> |
| |
| |
| void xnn_f32_spmm_minmax_ukernel_${MR}x${NR}__scalar${"_unroll" + str(UNROLL) if UNROLL > 1 else ""}( |
| uint32_t m, |
| uint32_t n, |
| const float*restrict a, |
| const float*restrict weights, |
| const int32_t*restrict widx_dmap, |
| const uint32_t*restrict nidx_nnzmap, |
| float*restrict c, |
| const union xnn_f32_minmax_params params[restrict XNN_MIN_ELEMENTS(1)]) |
| { |
| assert(m != 0); |
| |
| const float vmin = params->scalar.min; |
| const float vmax = params->scalar.max; |
| size_t i = m; |
| while (i >= ${MR}) { |
| const float*restrict w = weights; |
| const int32_t* dmap = widx_dmap; |
| const uint32_t* nnzmap = nidx_nnzmap; |
| size_t j = n; |
| while (j >= ${NR}) { |
| uint32_t nnz = *nnzmap++; |
| $for N in range(0, NR, 1): |
| float vacc0x${N} = *w++; |
| $for M in range(1, MR): |
| float vacc${ABC[M]}x${N} = vacc0x${N}; |
| if XNN_LIKELY(nnz != 0) { |
| do { |
| const intptr_t diff = *dmap++; |
| $for M in range(MR): |
| const float va${ABC[M]} = a[${M}]; |
| a = (const float*restrict) ((uintptr_t) a + (uintptr_t) diff); |
| $for N in range(0, NR, 1): |
| const float vb${N} = *w++; |
| $for N in range(0, NR, 1): |
| $for M in range(MR): |
| vacc${ABC[M]}x${N} += va${ABC[M]} * vb${N}; |
| } while (--nnz != 0); |
| } |
| $for N in range(NR): |
| $for M in range(MR): |
| float vout${ABC[M]}x${N} = math_min_f32(vacc${ABC[M]}x${N}, vmax); |
| $for N in range(NR): |
| $for M in range(MR): |
| vout${ABC[M]}x${N} = math_max_f32(vout${ABC[M]}x${N}, vmin); |
| $for N in range(NR): |
| $for M in range(MR): |
| c[${N} * m + ${M}] = vout${ABC[M]}x${N}; |
| c += ${NR} * m; |
| j -= ${NR}; |
| } |
| if XNN_UNLIKELY(j != 0) { |
| do { |
| uint32_t nnz = *nnzmap++; |
| float vacc0 = *w++; |
| $for M in range(1, MR): |
| float vacc${ABC[M]} = vacc0; |
| if XNN_LIKELY(nnz != 0) { |
| do { |
| const intptr_t diff = *dmap++; |
| $for M in range(MR): |
| const float va${ABC[M]} = a[${M}]; |
| a = (const float*restrict) ((uintptr_t) a + (uintptr_t) diff); |
| const float vb = *w++; |
| $for M in range(MR): |
| vacc${ABC[M]} += va${ABC[M]} * vb; |
| } while (--nnz != 0); |
| } |
| $for M in range(MR): |
| float vout${ABC[M]} = math_min_f32(vacc${ABC[M]}, vmax); |
| $for M in range(MR): |
| vout${ABC[M]} = math_max_f32(vout${ABC[M]}, vmin); |
| $for M in range(MR): |
| c[${M}] = vout${ABC[M]}; |
| c += m; |
| j -= 1; |
| } while (j != 0); |
| } |
| c -= m * n; |
| c += ${MR}; |
| a += ${MR}; |
| i -= ${MR}; |
| } |
| if XNN_UNLIKELY(i != 0) { |
| $for LOG2M in reversed(range((MR - 1).bit_length())): |
| $SUBMR = 1 << LOG2M |
| if (i & ${SUBMR}) { |
| const float*restrict w = weights; |
| const int32_t* dmap = widx_dmap; |
| const uint32_t* nnzmap = nidx_nnzmap; |
| size_t j = n; |
| while (j >= ${NR}) { |
| uint32_t nnz = *nnzmap++; |
| $for N in range(0, NR, 1): |
| float vacc0x${N} = *w++; |
| $for M in range(1, SUBMR): |
| float vacc${ABC[M]}x${N} = vacc0x${N}; |
| if XNN_LIKELY(nnz != 0) { |
| do { |
| const intptr_t diff = *dmap++; |
| $for M in range(SUBMR): |
| const float va${ABC[M]} = a[${M}]; |
| a = (const float*restrict) ((uintptr_t) a + (uintptr_t) diff); |
| $for N in range(0, NR, 1): |
| const float vb${N} = *w++; |
| $for N in range(0, NR, 1): |
| $for M in range(SUBMR): |
| vacc${ABC[M]}x${N} += va${ABC[M]} * vb${N}; |
| } while (--nnz != 0); |
| } |
| $for N in range(0, NR, 1): |
| $for M in range(SUBMR): |
| float vout${ABC[M]}x${N} = math_min_f32(vacc${ABC[M]}x${N}, vmax); |
| $for N in range(0, NR, 1): |
| $for M in range(SUBMR): |
| vout${ABC[M]}x${N} = math_max_f32(vout${ABC[M]}x${N}, vmin); |
| $for N in range(0, NR, 1): |
| $for M in range(SUBMR): |
| c[${N} * m + ${M}] = vout${ABC[M]}x${N}; |
| c += ${NR} * m; |
| j -= ${NR}; |
| } |
| if XNN_UNLIKELY(j != 0) { |
| do { |
| uint32_t nnz = *nnzmap++; |
| float vacc0 = *w++; |
| $for M in range(1, SUBMR): |
| float vacc${ABC[M]} = vacc0; |
| if XNN_LIKELY(nnz != 0) { |
| do { |
| const intptr_t diff = *dmap++; |
| $for M in range(SUBMR): |
| const float va${ABC[M]} = a[${M}]; |
| a = (const float*restrict) ((uintptr_t) a + (uintptr_t) diff); |
| const float vb = *w++; |
| $for M in range(SUBMR): |
| vacc${ABC[M]} += va${ABC[M]} * vb; |
| } while (--nnz != 0); |
| } |
| $for M in range(SUBMR): |
| float vout${ABC[M]} = math_min_f32(vacc${ABC[M]}, vmax); |
| $for M in range(SUBMR): |
| vout${ABC[M]} = math_max_f32(vout${ABC[M]}, vmin); |
| $for M in range(SUBMR): |
| c[${M}] = vout${ABC[M]}; |
| c += m; |
| j -= 1; |
| } while (j != 0); |
| } |
| c -= m * n; |
| c += ${SUBMR}; |
| a += ${SUBMR}; |
| } |
| } |
| } |