feat(ocr): 新增本地 OCR sidecar
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
#include "nhwc_internal.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
int lw_nhwc_depthwise_packed_weight_count(uint32_t channels,
|
||||
uint32_t kernel_h,
|
||||
uint32_t kernel_w,
|
||||
uint64_t* element_count) {
|
||||
uint64_t blocks;
|
||||
uint64_t taps;
|
||||
if (channels == 0u || kernel_h == 0u || kernel_w == 0u || element_count == NULL) return 0;
|
||||
blocks = ((uint64_t)channels + LW_NHWC_DEPTHWISE_BLOCK - 1u) / LW_NHWC_DEPTHWISE_BLOCK;
|
||||
taps = (uint64_t)kernel_h * kernel_w;
|
||||
if (blocks > UINT64_MAX / taps || blocks * taps > UINT64_MAX / LW_NHWC_DEPTHWISE_BLOCK) return 0;
|
||||
*element_count = blocks * taps * LW_NHWC_DEPTHWISE_BLOCK;
|
||||
return *element_count <= SIZE_MAX / sizeof(float);
|
||||
}
|
||||
|
||||
void lw_pack_nhwc_depthwise_f32(const float* weights, uint32_t channels,
|
||||
uint32_t kernel_h, uint32_t kernel_w,
|
||||
float* packed_weights) {
|
||||
uint32_t blocks;
|
||||
uint32_t taps;
|
||||
if (weights == NULL || packed_weights == NULL || channels == 0u || kernel_h == 0u || kernel_w == 0u) return;
|
||||
blocks = (channels + LW_NHWC_DEPTHWISE_BLOCK - 1u) / LW_NHWC_DEPTHWISE_BLOCK;
|
||||
taps = kernel_h * kernel_w;
|
||||
for (uint32_t block = 0u; block < blocks; ++block) {
|
||||
for (uint32_t tap = 0u; tap < taps; ++tap) {
|
||||
uint32_t ky = tap / kernel_w;
|
||||
uint32_t kx = tap - ky * kernel_w;
|
||||
for (uint32_t lane = 0u; lane < LW_NHWC_DEPTHWISE_BLOCK; ++lane) {
|
||||
uint32_t channel = block * LW_NHWC_DEPTHWISE_BLOCK + lane;
|
||||
size_t dst = ((size_t)block * taps + tap) * LW_NHWC_DEPTHWISE_BLOCK + lane;
|
||||
packed_weights[dst] = channel < channels
|
||||
? weights[((size_t)channel * kernel_h + ky) * kernel_w + kx]
|
||||
: 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
#ifndef LW_NHWC_INTERNAL_H
|
||||
#define LW_NHWC_INTERNAL_H
|
||||
|
||||
#include "lw_infer.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define LW_NHWC_OC_BLOCK 16u
|
||||
#define LW_NHWC_DEPTHWISE_BLOCK 32u
|
||||
#define LW_NHWC_PIXEL_TILE 6u
|
||||
#define LW_NHWC_POINTWISE_GROUP_TILES 8u
|
||||
#define LW_NHWC_DENSE_KC 512u
|
||||
#define LW_NHWC_PARTIAL_FLOATS (LW_NHWC_PIXEL_TILE * LW_NHWC_OC_BLOCK)
|
||||
|
||||
typedef enum lw_nhwc_activation {
|
||||
LW_NHWC_ACT_NONE = 0,
|
||||
LW_NHWC_ACT_RELU = 1,
|
||||
LW_NHWC_ACT_HARDSWISH = 2,
|
||||
LW_NHWC_ACT_GELU = 3
|
||||
} lw_nhwc_activation;
|
||||
|
||||
typedef struct lw_nhwc_epilogue {
|
||||
const float* bias;
|
||||
const float* residual;
|
||||
uint16_t activation;
|
||||
uint16_t reserved;
|
||||
float alpha;
|
||||
float beta;
|
||||
/* Optional channel bias folded from a separate Add node. Applied to the
|
||||
* finished accumulator AFTER accumulation (and before residual and
|
||||
* activation), preserving the bit-exact conv -> add -> ... ordering. */
|
||||
const float* post_bias;
|
||||
} lw_nhwc_epilogue;
|
||||
|
||||
float lw_nhwc_gelu_scalar_exact_f32(float value);
|
||||
#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
|
||||
#include <immintrin.h>
|
||||
__m256 lw_nhwc_avx2_gelu_vector_exact_f32(__m256 value);
|
||||
#endif
|
||||
|
||||
int lw_nhwc_dense_packed_weight_count(uint32_t input_channels,
|
||||
uint32_t output_channels,
|
||||
uint32_t kernel_h,
|
||||
uint32_t kernel_w,
|
||||
uint64_t* element_count);
|
||||
|
||||
void lw_pack_nhwc_dense_f32(const float* weights,
|
||||
uint32_t input_channels,
|
||||
uint32_t output_channels,
|
||||
uint32_t kernel_h,
|
||||
uint32_t kernel_w,
|
||||
float* packed_weights);
|
||||
|
||||
typedef struct lw_nhwc_dense_desc {
|
||||
uint32_t batch;
|
||||
uint32_t input_channels;
|
||||
uint32_t input_height;
|
||||
uint32_t input_width;
|
||||
uint32_t output_channels;
|
||||
uint32_t output_height;
|
||||
uint32_t output_width;
|
||||
uint32_t kernel_h;
|
||||
uint32_t kernel_w;
|
||||
uint32_t stride_h;
|
||||
uint32_t stride_w;
|
||||
uint32_t pad_top;
|
||||
uint32_t pad_left;
|
||||
uint32_t pad_bottom;
|
||||
uint32_t pad_right;
|
||||
uint32_t dense_kc;
|
||||
/* Optional sharded-row range: output rows [offset, offset + output_height)
|
||||
* of the full output. Zero keeps the whole-output serial semantics. */
|
||||
uint32_t output_row_offset;
|
||||
} lw_nhwc_dense_desc;
|
||||
|
||||
int lw_nhwc_dense_scratch_bytes(const lw_nhwc_dense_desc* desc, uint64_t* scratch_bytes);
|
||||
|
||||
int lw_nhwc_dense_prepared_scratch_bytes(const lw_nhwc_dense_desc* desc, uint64_t* scratch_bytes);
|
||||
|
||||
lw_status lw_avx2_fma_nhwc_dense_prepared_f32(const float* input,
|
||||
const float* packed_weights,
|
||||
const int32_t* input_offsets_k,
|
||||
const int32_t* patch_offsets_k,
|
||||
const lw_nhwc_epilogue* epilogue,
|
||||
float* output,
|
||||
const lw_nhwc_dense_desc* desc,
|
||||
void* scratch,
|
||||
uint64_t scratch_bytes);
|
||||
|
||||
lw_status lw_avx2_fma_nhwc_dense_f32(const float* input,
|
||||
const float* packed_weights,
|
||||
const lw_nhwc_epilogue* epilogue,
|
||||
float* output,
|
||||
const lw_nhwc_dense_desc* desc,
|
||||
void* scratch,
|
||||
uint64_t scratch_bytes);
|
||||
/* Input/output are NHWC. This API is experimental and production-disabled. */
|
||||
typedef struct lw_nhwc_depthwise_desc {
|
||||
uint32_t batch;
|
||||
uint32_t channels;
|
||||
uint32_t input_height;
|
||||
uint32_t input_width;
|
||||
uint32_t output_height;
|
||||
uint32_t output_width;
|
||||
uint32_t kernel_h;
|
||||
uint32_t kernel_w;
|
||||
uint32_t stride_h;
|
||||
uint32_t stride_w;
|
||||
uint32_t pad_top;
|
||||
uint32_t pad_left;
|
||||
uint32_t pad_bottom;
|
||||
uint32_t pad_right;
|
||||
/* Optional sharded-row range: output rows [offset, offset + output_height)
|
||||
* of the full output. Zero keeps the whole-output serial semantics. */
|
||||
uint32_t output_row_offset;
|
||||
/* Optional channel bias folded from a separate Add node: the accumulator
|
||||
* starts at zero (bias stays NULL) and post_bias is added lane-exactly
|
||||
* before the store, matching a standalone Add pass bit for bit. */
|
||||
const float* post_bias;
|
||||
} lw_nhwc_depthwise_desc;
|
||||
|
||||
int lw_nhwc_depthwise_packed_weight_count(uint32_t channels,
|
||||
uint32_t kernel_h,
|
||||
uint32_t kernel_w,
|
||||
uint64_t* element_count);
|
||||
|
||||
void lw_pack_nhwc_depthwise_f32(const float* weights,
|
||||
uint32_t channels,
|
||||
uint32_t kernel_h,
|
||||
uint32_t kernel_w,
|
||||
float* packed_weights);
|
||||
|
||||
lw_status lw_avx2_fma_nhwc_depthwise_f32(const float* input,
|
||||
const float* packed_weights,
|
||||
const float* bias,
|
||||
float* output,
|
||||
const lw_nhwc_depthwise_desc* desc);
|
||||
typedef struct lw_nhwc_depthwise_stats {
|
||||
uint64_t x1_invocations;
|
||||
uint64_t x2_invocations;
|
||||
} lw_nhwc_depthwise_stats;
|
||||
|
||||
lw_status lw_avx2_fma_nhwc_depthwise_profiled_f32(const float* input,
|
||||
const float* packed_weights,
|
||||
const float* bias,
|
||||
float* output,
|
||||
const lw_nhwc_depthwise_desc* desc,
|
||||
lw_nhwc_depthwise_stats* stats);
|
||||
void lw_avx2_fma_nhwc_pointwise_grouped_f32(const float* input,
|
||||
const float* packed_weights,
|
||||
const lw_nhwc_epilogue* epilogue,
|
||||
float* output,
|
||||
uint32_t pixels,
|
||||
uint32_t input_channels,
|
||||
uint32_t output_channels,
|
||||
uint32_t group_tiles);
|
||||
|
||||
void lw_avx2_fma_nhwc_pointwise_f32(const float* input,
|
||||
const float* packed_weights,
|
||||
const lw_nhwc_epilogue* epilogue,
|
||||
float* output,
|
||||
uint32_t pixels,
|
||||
uint32_t input_channels,
|
||||
uint32_t output_channels);
|
||||
void lw_avx2_fma_nhwc_pointwise_4x16_f32(const float* input,
|
||||
const float* packed_weights,
|
||||
const lw_nhwc_epilogue* epilogue,
|
||||
float* output,
|
||||
uint32_t pixels,
|
||||
uint32_t input_channels,
|
||||
uint32_t output_channels);
|
||||
void lw_avx2_fma_nhwc_pointwise_3x32_f32(const float* input,
|
||||
const float* packed_weights,
|
||||
const lw_nhwc_epilogue* epilogue,
|
||||
float* output,
|
||||
uint32_t pixels,
|
||||
uint32_t input_channels,
|
||||
uint32_t output_channels);
|
||||
void lw_avx2_fma_nhwc_pointwise_2x32_f32(const float* input,
|
||||
const float* packed_weights,
|
||||
const lw_nhwc_epilogue* epilogue,
|
||||
float* output,
|
||||
uint32_t pixels,
|
||||
uint32_t input_channels,
|
||||
uint32_t output_channels);
|
||||
|
||||
void lw_avx2_nhwc_affine_f32(const float* input, const float* mul, const float* add,
|
||||
float* output, uint32_t pixels, uint32_t channels);
|
||||
void lw_avx2_nchw_affine_f32(const float* input, const float* mul, const float* add,
|
||||
float* output, uint32_t channels, uint32_t spatial);
|
||||
void lw_avx2_nhwc_reduce_mean_hw_f32(const float* input, float* output,
|
||||
uint32_t batch, uint32_t height,
|
||||
uint32_t width, uint32_t channels);
|
||||
/* Channel-group variant for sharded execution: processes channels
|
||||
* [0, channels) of a tensor whose rows are input_stride wide, writing to
|
||||
* output rows of output_stride. Per-channel pixel visit order is identical
|
||||
* to lw_avx2_nhwc_reduce_mean_hw_f32, so results are bit-identical. */
|
||||
void lw_avx2_nhwc_reduce_mean_hw_strided_f32(const float* input, float* output,
|
||||
uint32_t batch, uint32_t height,
|
||||
uint32_t width, uint32_t channels,
|
||||
uint32_t input_stride, uint32_t output_stride);
|
||||
void lw_avx2_nhwc_pool_f32(const float* input, float* output,
|
||||
uint32_t batch, uint32_t input_height,
|
||||
uint32_t input_width, uint32_t output_height,
|
||||
uint32_t output_width, uint32_t channels,
|
||||
uint32_t kernel_h, uint32_t kernel_w,
|
||||
uint32_t stride_h, uint32_t stride_w,
|
||||
uint32_t pad_top, uint32_t pad_left,
|
||||
uint8_t count_include_pad, uint8_t is_max);
|
||||
/* Integer-scale nearest resize (planner-guaranteed scales): each output row
|
||||
* is a whole-row copy of the source row at oy / scale_h, each source pixel
|
||||
* repeated scale_w times. Pure data movement, bit-identical to the scalar
|
||||
* nearest path. */
|
||||
void lw_avx2_nhwc_resize_nearest_f32(const float* input, float* output,
|
||||
uint32_t batch, uint32_t channels,
|
||||
uint32_t input_height, uint32_t input_width,
|
||||
uint32_t output_height, uint32_t output_width);
|
||||
/* 2x2 stride-2 no-pad ConvTranspose as four per-tap 1x1 GEMMs. Weights are
|
||||
* packed [tap][oc/16][ic][16] from the ONNX [ic, oc, 2, 2] layout. The
|
||||
* epilogue supports bias and ReLU only; Sigmoid is deliberately excluded so
|
||||
* callers can defuse it into a separate bit-identical pass. */
|
||||
typedef struct lw_nhwc_convtranspose_desc {
|
||||
uint32_t batch;
|
||||
uint32_t input_channels;
|
||||
uint32_t input_height;
|
||||
uint32_t input_width;
|
||||
uint32_t output_channels;
|
||||
uint32_t output_height;
|
||||
uint32_t output_width;
|
||||
} lw_nhwc_convtranspose_desc;
|
||||
|
||||
int lw_nhwc_convtranspose_packed_weight_count(uint32_t input_channels,
|
||||
uint32_t output_channels,
|
||||
uint64_t* element_count);
|
||||
void lw_pack_nhwc_convtranspose2x2_f32(const float* weights, uint32_t input_channels,
|
||||
uint32_t output_channels, float* packed_weights);
|
||||
lw_status lw_wasm128_nhwc_convtranspose2x2_s2_f32(
|
||||
const float* input, const float* packed_weights, const lw_nhwc_epilogue* epilogue,
|
||||
float* output, const lw_nhwc_convtranspose_desc* desc);
|
||||
lw_status lw_wasm128_nhwc_convtranspose2x2_s2_c1_f32(
|
||||
const float* input, const float* weights, const lw_nhwc_epilogue* epilogue,
|
||||
float* output, const lw_nhwc_convtranspose_desc* desc);
|
||||
lw_status lw_avx2_fma_nhwc_convtranspose2x2_s2_f32(
|
||||
const float* input, const float* packed_weights, const lw_nhwc_epilogue* epilogue,
|
||||
float* output, const lw_nhwc_convtranspose_desc* desc);
|
||||
/* Single-output-channel probability-map variant; unpaced ONNX weights
|
||||
* [ic, 1, 2, 2], ReLU-only epilogue. */
|
||||
lw_status lw_avx2_fma_nhwc_convtranspose2x2_s2_c1_f32(
|
||||
const float* input, const float* weights, const lw_nhwc_epilogue* epilogue,
|
||||
float* output, const lw_nhwc_convtranspose_desc* desc);
|
||||
#endif
|
||||
@@ -0,0 +1,79 @@
|
||||
#include "nhwc_internal.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
int lw_nhwc_dense_packed_weight_count(uint32_t input_channels,
|
||||
uint32_t output_channels,
|
||||
uint32_t kernel_h,
|
||||
uint32_t kernel_w,
|
||||
uint64_t* element_count) {
|
||||
uint64_t output_blocks;
|
||||
uint64_t count;
|
||||
|
||||
if (input_channels == 0u || output_channels == 0u || kernel_h == 0u ||
|
||||
kernel_w == 0u || element_count == NULL) {
|
||||
return 0;
|
||||
}
|
||||
output_blocks = ((uint64_t)output_channels + LW_NHWC_OC_BLOCK - 1u) /
|
||||
LW_NHWC_OC_BLOCK;
|
||||
count = output_blocks;
|
||||
if (count > UINT64_MAX / kernel_h) {
|
||||
return 0;
|
||||
}
|
||||
count *= kernel_h;
|
||||
if (count > UINT64_MAX / kernel_w) {
|
||||
return 0;
|
||||
}
|
||||
count *= kernel_w;
|
||||
if (count > UINT64_MAX / input_channels) {
|
||||
return 0;
|
||||
}
|
||||
count *= input_channels;
|
||||
if (count > UINT64_MAX / LW_NHWC_OC_BLOCK) {
|
||||
return 0;
|
||||
}
|
||||
count *= LW_NHWC_OC_BLOCK;
|
||||
if (count > (uint64_t)(SIZE_MAX / sizeof(float))) {
|
||||
return 0;
|
||||
}
|
||||
*element_count = count;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void lw_pack_nhwc_dense_f32(const float* weights,
|
||||
uint32_t input_channels,
|
||||
uint32_t output_channels,
|
||||
uint32_t kernel_h,
|
||||
uint32_t kernel_w,
|
||||
float* packed_weights) {
|
||||
const uint32_t output_blocks =
|
||||
output_channels / LW_NHWC_OC_BLOCK +
|
||||
(output_channels % LW_NHWC_OC_BLOCK == 0u ? 0u : 1u);
|
||||
const uint32_t taps = kernel_h * kernel_w;
|
||||
uint32_t output_block;
|
||||
|
||||
for (output_block = 0u; output_block < output_blocks; ++output_block) {
|
||||
uint32_t input_channel;
|
||||
for (input_channel = 0u; input_channel < input_channels; ++input_channel) {
|
||||
uint32_t tap;
|
||||
for (tap = 0u; tap < taps; ++tap) {
|
||||
const uint32_t kh = tap / kernel_w;
|
||||
const uint32_t kw = tap - kh * kernel_w;
|
||||
uint32_t lane;
|
||||
for (lane = 0u; lane < LW_NHWC_OC_BLOCK; ++lane) {
|
||||
const uint64_t output_channel =
|
||||
(uint64_t)output_block * LW_NHWC_OC_BLOCK + lane;
|
||||
const uint64_t packed_index =
|
||||
((((uint64_t)output_block * input_channels + input_channel) * taps +
|
||||
tap) * LW_NHWC_OC_BLOCK) + lane;
|
||||
packed_weights[(size_t)packed_index] =
|
||||
output_channel < output_channels
|
||||
? weights[((((output_channel * input_channels + input_channel) *
|
||||
kernel_h + kh) * kernel_w) + kw)]
|
||||
: 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
#include "packed_conv_internal.h"
|
||||
|
||||
/* NCHW pointwise-Conv packing and the portable microkernel fallback. */
|
||||
|
||||
#include "cpu_features.h"
|
||||
#include "simd_kernels.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
int lw_packed_conv1x1_weight_count(uint32_t input_channels, uint32_t output_channels,
|
||||
uint64_t* weight_count) {
|
||||
uint64_t output_blocks;
|
||||
if (input_channels == 0u || output_channels == 0u || weight_count == NULL) {
|
||||
return 0;
|
||||
}
|
||||
output_blocks = ((uint64_t)output_channels + LW_PACKED_CONV1X1_OUTPUT_TILE - 1u) /
|
||||
LW_PACKED_CONV1X1_OUTPUT_TILE;
|
||||
if (output_blocks > UINT64_MAX / input_channels ||
|
||||
output_blocks * input_channels > UINT64_MAX / LW_PACKED_CONV1X1_OUTPUT_TILE) {
|
||||
return 0;
|
||||
}
|
||||
*weight_count = output_blocks * input_channels * LW_PACKED_CONV1X1_OUTPUT_TILE;
|
||||
return *weight_count <= (uint64_t)(SIZE_MAX / sizeof(float));
|
||||
}
|
||||
|
||||
void lw_pack_conv1x1_weights_f32(const float* weights, uint32_t input_channels,
|
||||
uint32_t output_channels, float* packed_weights) {
|
||||
uint32_t output_block;
|
||||
uint32_t output_blocks =
|
||||
(output_channels + LW_PACKED_CONV1X1_OUTPUT_TILE - 1u) / LW_PACKED_CONV1X1_OUTPUT_TILE;
|
||||
for (output_block = 0u; output_block < output_blocks; ++output_block) {
|
||||
uint32_t input_channel;
|
||||
for (input_channel = 0u; input_channel < input_channels; ++input_channel) {
|
||||
uint32_t output_lane;
|
||||
for (output_lane = 0u; output_lane < LW_PACKED_CONV1X1_OUTPUT_TILE; ++output_lane) {
|
||||
uint32_t output_channel =
|
||||
output_block * LW_PACKED_CONV1X1_OUTPUT_TILE + output_lane;
|
||||
uint64_t packed_index = ((uint64_t)output_block * input_channels + input_channel) *
|
||||
LW_PACKED_CONV1X1_OUTPUT_TILE +
|
||||
output_lane;
|
||||
packed_weights[(size_t)packed_index] =
|
||||
output_channel < output_channels
|
||||
? weights[(size_t)((uint64_t)output_channel * input_channels +
|
||||
input_channel)]
|
||||
: 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lw_scalar_packed_conv1x1_f32(const float* input, const float* packed_weights,
|
||||
const float* bias, float* output,
|
||||
const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4]) {
|
||||
uint32_t input_channels = (uint32_t)input_dimensions[1];
|
||||
uint32_t output_channels = (uint32_t)output_dimensions[1];
|
||||
uint64_t channel_plane =
|
||||
(uint64_t)(uint32_t)input_dimensions[2] * (uint32_t)input_dimensions[3];
|
||||
uint32_t output_blocks =
|
||||
(output_channels + LW_PACKED_CONV1X1_OUTPUT_TILE - 1u) / LW_PACKED_CONV1X1_OUTPUT_TILE;
|
||||
uint32_t batch;
|
||||
for (batch = 0u; batch < (uint32_t)input_dimensions[0]; ++batch) {
|
||||
const float* batch_input =
|
||||
input + (size_t)((uint64_t)batch * input_channels * channel_plane);
|
||||
float* batch_output = output + (size_t)((uint64_t)batch * output_channels * channel_plane);
|
||||
uint32_t output_block;
|
||||
for (output_block = 0u; output_block < output_blocks; ++output_block) {
|
||||
uint32_t output_base = output_block * LW_PACKED_CONV1X1_OUTPUT_TILE;
|
||||
uint32_t valid_outputs = output_channels - output_base;
|
||||
uint64_t spatial;
|
||||
if (valid_outputs > LW_PACKED_CONV1X1_OUTPUT_TILE) {
|
||||
valid_outputs = LW_PACKED_CONV1X1_OUTPUT_TILE;
|
||||
}
|
||||
for (spatial = 0u; spatial < channel_plane; ++spatial) {
|
||||
float accumulators[LW_PACKED_CONV1X1_OUTPUT_TILE] = {0.0f, 0.0f, 0.0f, 0.0f};
|
||||
uint32_t output_lane;
|
||||
uint32_t input_channel;
|
||||
for (output_lane = 0u; output_lane < valid_outputs; ++output_lane) {
|
||||
accumulators[output_lane] =
|
||||
bias == NULL ? 0.0f : bias[output_base + output_lane];
|
||||
}
|
||||
for (input_channel = 0u; input_channel < input_channels; ++input_channel) {
|
||||
float input_value =
|
||||
batch_input[(size_t)((uint64_t)input_channel * channel_plane + spatial)];
|
||||
const float* packed =
|
||||
packed_weights +
|
||||
(size_t)(((uint64_t)output_block * input_channels + input_channel) *
|
||||
LW_PACKED_CONV1X1_OUTPUT_TILE);
|
||||
for (output_lane = 0u; output_lane < valid_outputs; ++output_lane) {
|
||||
accumulators[output_lane] += input_value * packed[output_lane];
|
||||
}
|
||||
}
|
||||
for (output_lane = 0u; output_lane < valid_outputs; ++output_lane) {
|
||||
batch_output[(size_t)(((uint64_t)output_base + output_lane) * channel_plane +
|
||||
spatial)] = accumulators[output_lane];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Large Small/Medium REC pointwise layers dominate their graph time. The
|
||||
* remaining FMA candidates stay behind the experimental dispatch switch. */
|
||||
static uint32_t lw_large_rec_fma_variant(const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4]) {
|
||||
uint32_t input_channels;
|
||||
uint32_t output_channels;
|
||||
uint32_t height;
|
||||
if (input_dimensions == NULL || output_dimensions == NULL ||
|
||||
input_dimensions[0] != 1 || output_dimensions[0] != 1 ||
|
||||
input_dimensions[2] <= 0 || input_dimensions[3] <= 0 ||
|
||||
output_dimensions[2] != input_dimensions[2] ||
|
||||
output_dimensions[3] != input_dimensions[3]) {
|
||||
return 0u;
|
||||
}
|
||||
input_channels = (uint32_t)input_dimensions[1];
|
||||
output_channels = (uint32_t)output_dimensions[1];
|
||||
height = (uint32_t)input_dimensions[2];
|
||||
if (height == 6u && (uint32_t)input_dimensions[3] >= 80u) {
|
||||
if ((input_channels == 192u && output_channels == 384u) ||
|
||||
(input_channels == 384u && output_channels == 192u)) {
|
||||
return 4u;
|
||||
}
|
||||
if ((input_channels == 512u && output_channels == 1024u) ||
|
||||
(input_channels == 1024u && output_channels == 512u)) {
|
||||
return 8u;
|
||||
}
|
||||
}
|
||||
if (height == 3u && (uint32_t)input_dimensions[3] >= 160u &&
|
||||
((input_channels == 768u && output_channels == 384u) ||
|
||||
(input_channels == 1536u && output_channels == 768u))) {
|
||||
return 8u;
|
||||
}
|
||||
return 0u;
|
||||
}
|
||||
|
||||
#if defined(LW_EXPERIMENTAL_AVX2_FMA_CONV1X1_DISPATCH)
|
||||
/*
|
||||
* FMA can lower the CPU frequency on some x64 hosts. Keep the measured
|
||||
* medium/late shapes on the regular AVX2 path until a wider calibration set
|
||||
* proves otherwise. Only shapes with repeatable wins are enabled below; unknown
|
||||
* shapes remain on regular AVX2.
|
||||
*/
|
||||
static int lw_experimental_fma_shape_allowed(const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4]) {
|
||||
uint32_t input_channels;
|
||||
uint32_t output_channels;
|
||||
uint32_t height;
|
||||
if (input_dimensions == NULL || output_dimensions == NULL || input_dimensions[2] <= 0) {
|
||||
return 0;
|
||||
}
|
||||
input_channels = (uint32_t)input_dimensions[1];
|
||||
output_channels = (uint32_t)output_dimensions[1];
|
||||
height = (uint32_t)input_dimensions[2];
|
||||
if (height == 6u && input_channels == 1024u && output_channels == 512u) {
|
||||
return 0;
|
||||
}
|
||||
if ((height == 12u &&
|
||||
((input_channels == 96u && output_channels == 192u) ||
|
||||
(input_channels == 48u && output_channels == 96u) ||
|
||||
(input_channels == 96u && output_channels == 48u))) ||
|
||||
(height == 6u &&
|
||||
((input_channels == 96u && output_channels == 192u) ||
|
||||
(input_channels == 192u && output_channels == 96u) ||
|
||||
(input_channels == 192u && output_channels == 384u) ||
|
||||
(input_channels == 512u && output_channels == 1024u))) ||
|
||||
(height == 3u &&
|
||||
((input_channels == 160u && output_channels == 320u) ||
|
||||
(input_channels == 384u && output_channels == 768u) ||
|
||||
(input_channels == 768u && output_channels == 384u) ||
|
||||
(input_channels == 1536u && output_channels == 768u)))) {
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
#if defined(LW_EXPERIMENTAL_AVX2_FMA_CONV1X1_DISPATCH)
|
||||
/*
|
||||
* The 8x8 candidate keeps two packed four-output blocks live. It is useful
|
||||
* only for the large late feature maps where the extra output reuse offsets
|
||||
* the additional register pressure. Keep this list explicit: the regular
|
||||
* four-output FMA kernel remains the fallback for every other shape.
|
||||
*/
|
||||
static int lw_experimental_fma_8x8_shape_allowed(
|
||||
const int32_t input_dimensions[4], const int32_t output_dimensions[4]) {
|
||||
uint32_t input_channels;
|
||||
uint32_t output_channels;
|
||||
uint32_t height;
|
||||
if (input_dimensions == NULL || output_dimensions == NULL || input_dimensions[2] <= 0) {
|
||||
return 0;
|
||||
}
|
||||
input_channels = (uint32_t)input_dimensions[1];
|
||||
output_channels = (uint32_t)output_dimensions[1];
|
||||
height = (uint32_t)input_dimensions[2];
|
||||
return (height == 6u && input_channels == 512u && output_channels == 1024u) ||
|
||||
(height == 3u &&
|
||||
((input_channels == 768u && output_channels == 384u) ||
|
||||
(input_channels == 1536u && output_channels == 768u)));
|
||||
}
|
||||
#endif
|
||||
|
||||
void lw_packed_conv1x1_f32(const float* input, const float* packed_weights, const float* bias,
|
||||
float* output, const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4]) {
|
||||
const lw_cpu_capabilities capabilities = lw_get_cpu_capabilities();
|
||||
const lw_simd_level simd_level = capabilities.simd;
|
||||
uint32_t fma_variant = lw_large_rec_fma_variant(input_dimensions, output_dimensions);
|
||||
#if defined(LW_EXPERIMENTAL_AVX2_FMA_CONV1X1_DISPATCH)
|
||||
if (fma_variant == 0u &&
|
||||
lw_experimental_fma_shape_allowed(input_dimensions, output_dimensions)) {
|
||||
fma_variant = lw_experimental_fma_8x8_shape_allowed(input_dimensions,
|
||||
output_dimensions) ? 8u : 4u;
|
||||
}
|
||||
#endif
|
||||
if (capabilities.has_avx2_fma && fma_variant != 0u) {
|
||||
if (fma_variant == 8u) {
|
||||
lw_avx2_fma_packed_conv1x1_8x8_f32(input, packed_weights, bias, output,
|
||||
input_dimensions, output_dimensions);
|
||||
} else {
|
||||
lw_avx2_fma_packed_conv1x1_f32(input, packed_weights, bias, output,
|
||||
input_dimensions, output_dimensions);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (lw_simd_level_is_avx2(simd_level)) {
|
||||
lw_avx2_packed_conv1x1_f32(input, packed_weights, bias, output, input_dimensions,
|
||||
output_dimensions);
|
||||
} else if (lw_simd_level_is_neon(simd_level)) {
|
||||
lw_neon_packed_conv1x1_f32(input, packed_weights, bias, output, input_dimensions,
|
||||
output_dimensions);
|
||||
} else if (lw_simd_level_is_lsx(simd_level)) {
|
||||
lw_lsx_packed_conv1x1_f32(input, packed_weights, bias, output, input_dimensions,
|
||||
output_dimensions);
|
||||
} else if (lw_simd_level_is_sse2(simd_level)) {
|
||||
lw_sse2_packed_conv1x1_f32(input, packed_weights, bias, output, input_dimensions,
|
||||
output_dimensions);
|
||||
} else {
|
||||
lw_scalar_packed_conv1x1_f32(input, packed_weights, bias, output, input_dimensions,
|
||||
output_dimensions);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
#include "packed_conv3x3_internal.h"
|
||||
|
||||
#include "cpu_features.h"
|
||||
#include "simd_kernels.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
int lw_packed_conv3x3_stride2_weight_count(uint32_t input_channels,
|
||||
uint32_t output_channels,
|
||||
uint64_t* weight_count) {
|
||||
uint64_t output_blocks;
|
||||
if (input_channels == 0u || output_channels == 0u || weight_count == NULL) {
|
||||
return 0;
|
||||
}
|
||||
output_blocks =
|
||||
((uint64_t)output_channels + LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE - 1u) /
|
||||
LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE;
|
||||
if (output_blocks > UINT64_MAX / input_channels ||
|
||||
output_blocks * input_channels > UINT64_MAX / 9u ||
|
||||
output_blocks * input_channels * 9u >
|
||||
UINT64_MAX / LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE) {
|
||||
return 0;
|
||||
}
|
||||
*weight_count = output_blocks * input_channels * 9u *
|
||||
LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE;
|
||||
return *weight_count <= (uint64_t)(SIZE_MAX / sizeof(float));
|
||||
}
|
||||
|
||||
void lw_pack_conv3x3_stride2_weights_f32(const float* weights, uint32_t input_channels,
|
||||
uint32_t output_channels, float* packed_weights) {
|
||||
const uint32_t output_blocks =
|
||||
(output_channels + LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE - 1u) /
|
||||
LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE;
|
||||
uint32_t output_block;
|
||||
for (output_block = 0u; output_block < output_blocks; ++output_block) {
|
||||
uint32_t input_channel;
|
||||
for (input_channel = 0u; input_channel < input_channels; ++input_channel) {
|
||||
uint32_t kernel_index;
|
||||
for (kernel_index = 0u; kernel_index < 9u; ++kernel_index) {
|
||||
uint32_t output_lane;
|
||||
for (output_lane = 0u;
|
||||
output_lane < LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE;
|
||||
++output_lane) {
|
||||
const uint32_t output_channel =
|
||||
output_block * LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE + output_lane;
|
||||
const uint64_t packed_index =
|
||||
(((uint64_t)output_block * input_channels + input_channel) * 9u +
|
||||
kernel_index) *
|
||||
LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE +
|
||||
output_lane;
|
||||
packed_weights[(size_t)packed_index] =
|
||||
output_channel < output_channels
|
||||
? weights[(size_t)(((uint64_t)output_channel * input_channels +
|
||||
input_channel) *
|
||||
9u +
|
||||
kernel_index)]
|
||||
: 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lw_scalar_packed_conv3x3_stride2_pad1_f32(
|
||||
const float* input, const float* packed_weights, const float* bias, float* output,
|
||||
const int32_t input_dimensions[4], const int32_t output_dimensions[4]) {
|
||||
const uint32_t input_channels = (uint32_t)input_dimensions[1];
|
||||
const uint32_t input_height = (uint32_t)input_dimensions[2];
|
||||
const uint32_t input_width = (uint32_t)input_dimensions[3];
|
||||
const uint32_t output_channels = (uint32_t)output_dimensions[1];
|
||||
const uint32_t output_height = (uint32_t)output_dimensions[2];
|
||||
const uint32_t output_width = (uint32_t)output_dimensions[3];
|
||||
const uint64_t input_plane = (uint64_t)input_height * input_width;
|
||||
const uint64_t output_plane = (uint64_t)output_height * output_width;
|
||||
const uint32_t output_blocks =
|
||||
(output_channels + LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE - 1u) /
|
||||
LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE;
|
||||
uint32_t batch;
|
||||
for (batch = 0u; batch < (uint32_t)input_dimensions[0]; ++batch) {
|
||||
const float* batch_input =
|
||||
input + (size_t)((uint64_t)batch * input_channels * input_plane);
|
||||
float* batch_output =
|
||||
output + (size_t)((uint64_t)batch * output_channels * output_plane);
|
||||
uint32_t output_block;
|
||||
for (output_block = 0u; output_block < output_blocks; ++output_block) {
|
||||
const uint32_t output_base =
|
||||
output_block * LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE;
|
||||
uint32_t valid_outputs = output_channels - output_base;
|
||||
uint32_t output_lane;
|
||||
if (valid_outputs > LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE) {
|
||||
valid_outputs = LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE;
|
||||
}
|
||||
for (output_lane = 0u; output_lane < valid_outputs; ++output_lane) {
|
||||
uint32_t output_y;
|
||||
for (output_y = 0u; output_y < output_height; ++output_y) {
|
||||
uint32_t output_x;
|
||||
for (output_x = 0u; output_x < output_width; ++output_x) {
|
||||
float accumulator =
|
||||
bias == NULL ? 0.0f : bias[output_base + output_lane];
|
||||
uint32_t input_channel;
|
||||
for (input_channel = 0u; input_channel < input_channels;
|
||||
++input_channel) {
|
||||
const float* input_channel_data =
|
||||
batch_input + (size_t)((uint64_t)input_channel * input_plane);
|
||||
uint32_t kernel_y;
|
||||
for (kernel_y = 0u; kernel_y < 3u; ++kernel_y) {
|
||||
const int64_t input_y =
|
||||
(int64_t)output_y * 2 - 1 + kernel_y;
|
||||
uint32_t kernel_x;
|
||||
if (input_y < 0 || input_y >= (int64_t)input_height) {
|
||||
continue;
|
||||
}
|
||||
for (kernel_x = 0u; kernel_x < 3u; ++kernel_x) {
|
||||
const int64_t input_x =
|
||||
(int64_t)output_x * 2 - 1 + kernel_x;
|
||||
if (input_x >= 0 && input_x < (int64_t)input_width) {
|
||||
const uint32_t kernel_index = kernel_y * 3u + kernel_x;
|
||||
const uint64_t packed_index =
|
||||
(((uint64_t)output_block * input_channels +
|
||||
input_channel) *
|
||||
9u +
|
||||
kernel_index) *
|
||||
LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE +
|
||||
output_lane;
|
||||
accumulator +=
|
||||
input_channel_data[(size_t)((uint64_t)(uint32_t)input_y *
|
||||
input_width +
|
||||
(uint32_t)input_x)] *
|
||||
packed_weights[(size_t)packed_index];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
batch_output[(size_t)(((uint64_t)output_base + output_lane) *
|
||||
output_plane +
|
||||
(uint64_t)output_y * output_width + output_x)] =
|
||||
accumulator;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lw_packed_conv3x3_stride2_pad1_f32(
|
||||
const float* input, const float* packed_weights, const float* bias, float* output,
|
||||
const int32_t input_dimensions[4], const int32_t output_dimensions[4]) {
|
||||
const lw_simd_level simd_level = lw_detect_simd_level();
|
||||
#if defined(LW_AVX2_FMA_CONV3X3_DISPATCH)
|
||||
const lw_cpu_capabilities capabilities = lw_get_cpu_capabilities();
|
||||
const int tiny_rec_node6 =
|
||||
input_dimensions[0] == 1 && input_dimensions[1] == 24 &&
|
||||
input_dimensions[2] == 24 &&
|
||||
(input_dimensions[3] == 160 || input_dimensions[3] == 480) &&
|
||||
output_dimensions[1] == 48 && output_dimensions[2] == 12 &&
|
||||
(output_dimensions[3] == 80 || output_dimensions[3] == 240);
|
||||
const int small_rec_early =
|
||||
input_dimensions[0] == 1 && input_dimensions[1] == 96 &&
|
||||
input_dimensions[2] == 24 &&
|
||||
(input_dimensions[3] == 320 || input_dimensions[3] == 480) &&
|
||||
output_dimensions[1] == 48 && output_dimensions[2] == 12 &&
|
||||
output_dimensions[3] == input_dimensions[3] / 2;
|
||||
if (lw_simd_level_is_avx2(simd_level) && capabilities.has_avx2_fma &&
|
||||
(tiny_rec_node6 || small_rec_early)) {
|
||||
lw_avx2_fma_packed_conv3x3_stride2_pad1_f32(
|
||||
input, packed_weights, bias, output, input_dimensions, output_dimensions);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (lw_simd_level_is_avx2(simd_level) &&
|
||||
(uint32_t)output_dimensions[1] % LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE == 0u) {
|
||||
lw_avx2_packed_conv3x3_stride2_pad1_f32(
|
||||
input, packed_weights, bias, output, input_dimensions, output_dimensions);
|
||||
} else {
|
||||
lw_scalar_packed_conv3x3_stride2_pad1_f32(
|
||||
input, packed_weights, bias, output, input_dimensions, output_dimensions);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#ifndef LW_PACKED_CONV3X3_INTERNAL_H
|
||||
#define LW_PACKED_CONV3X3_INTERNAL_H
|
||||
|
||||
/*
|
||||
* Private packed-weight contract for NCHW group-1 3x3 Conv with stride 2 and
|
||||
* pad 1. LWM keeps canonical OIHW weights; eligible sessions may convert them
|
||||
* once to [OC/8][IC][3][3][8].
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define LW_PACKED_CONV3X3_STRIDE2_OUTPUT_TILE 8u
|
||||
|
||||
typedef void (*lw_packed_conv3x3_kernel_fn)(
|
||||
const float* input, const float* packed_weights, const float* bias, float* output,
|
||||
const int32_t input_dimensions[4], const int32_t output_dimensions[4]);
|
||||
|
||||
int lw_packed_conv3x3_stride2_weight_count(uint32_t input_channels,
|
||||
uint32_t output_channels,
|
||||
uint64_t* weight_count);
|
||||
void lw_pack_conv3x3_stride2_weights_f32(const float* weights, uint32_t input_channels,
|
||||
uint32_t output_channels, float* packed_weights);
|
||||
void lw_scalar_packed_conv3x3_stride2_pad1_f32(
|
||||
const float* input, const float* packed_weights, const float* bias, float* output,
|
||||
const int32_t input_dimensions[4], const int32_t output_dimensions[4]);
|
||||
void lw_packed_conv3x3_stride2_pad1_f32(
|
||||
const float* input, const float* packed_weights, const float* bias, float* output,
|
||||
const int32_t input_dimensions[4], const int32_t output_dimensions[4]);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
#ifndef LW_PACKED_CONV_INTERNAL_H
|
||||
#define LW_PACKED_CONV_INTERNAL_H
|
||||
|
||||
/*
|
||||
* Private packed-weight contract for NCHW 1x1 Conv. LWM keeps canonical OIHW
|
||||
* weights; a session converts eligible constant weights to [OC/4][IC][4]
|
||||
* once and reuses them for every execution.
|
||||
*/
|
||||
|
||||
#include "lw_infer.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define LW_PACKED_CONV1X1_OUTPUT_TILE 4u
|
||||
|
||||
typedef void (*lw_packed_conv1x1_kernel_fn)(
|
||||
const float* input, const float* packed_weights, const float* bias, float* output,
|
||||
const int32_t input_dimensions[4], const int32_t output_dimensions[4]);
|
||||
|
||||
int lw_packed_conv1x1_weight_count(uint32_t input_channels, uint32_t output_channels,
|
||||
uint64_t* weight_count);
|
||||
void lw_pack_conv1x1_weights_f32(const float* weights, uint32_t input_channels,
|
||||
uint32_t output_channels, float* packed_weights);
|
||||
|
||||
void lw_scalar_packed_conv1x1_f32(const float* input, const float* packed_weights,
|
||||
const float* bias, float* output,
|
||||
const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4]);
|
||||
void lw_packed_conv1x1_f32(const float* input, const float* packed_weights, const float* bias,
|
||||
float* output, const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4]);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,84 @@
|
||||
#include "packed_matmul_internal.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
int lw_packed_matmul_weight_count(uint32_t inner_dimension, uint32_t columns,
|
||||
uint64_t* weight_count) {
|
||||
uint64_t column_panels;
|
||||
if (inner_dimension == 0u || columns == 0u || weight_count == NULL) {
|
||||
return 0;
|
||||
}
|
||||
column_panels = ((uint64_t)columns + LW_PACKED_MATMUL_COLUMN_TILE - 1u) /
|
||||
LW_PACKED_MATMUL_COLUMN_TILE;
|
||||
if (column_panels > UINT64_MAX / inner_dimension ||
|
||||
column_panels * inner_dimension > UINT64_MAX / LW_PACKED_MATMUL_COLUMN_TILE) {
|
||||
return 0;
|
||||
}
|
||||
*weight_count = column_panels * inner_dimension * LW_PACKED_MATMUL_COLUMN_TILE;
|
||||
return *weight_count <= (uint64_t)(SIZE_MAX / sizeof(float));
|
||||
}
|
||||
|
||||
void lw_pack_matmul_weights_f32(const float* weights, uint32_t inner_dimension,
|
||||
uint32_t columns, float* packed_weights) {
|
||||
uint32_t column_panel;
|
||||
const uint32_t column_panels =
|
||||
(columns + LW_PACKED_MATMUL_COLUMN_TILE - 1u) / LW_PACKED_MATMUL_COLUMN_TILE;
|
||||
for (column_panel = 0u; column_panel < column_panels; ++column_panel) {
|
||||
uint32_t inner;
|
||||
for (inner = 0u; inner < inner_dimension; ++inner) {
|
||||
uint32_t lane;
|
||||
for (lane = 0u; lane < LW_PACKED_MATMUL_COLUMN_TILE; ++lane) {
|
||||
const uint32_t column =
|
||||
column_panel * LW_PACKED_MATMUL_COLUMN_TILE + lane;
|
||||
const uint64_t packed_index =
|
||||
((uint64_t)column_panel * inner_dimension + inner) *
|
||||
LW_PACKED_MATMUL_COLUMN_TILE +
|
||||
lane;
|
||||
packed_weights[(size_t)packed_index] =
|
||||
column < columns
|
||||
? weights[(size_t)((uint64_t)inner * columns + column)]
|
||||
: 0.0f;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lw_scalar_packed_matmul_shared_f32(const float* input, const float* packed_weights,
|
||||
float* output, uint32_t batch_count, uint32_t rows,
|
||||
uint32_t inner_dimension, uint32_t columns) {
|
||||
uint32_t batch;
|
||||
const uint32_t column_panels =
|
||||
(columns + LW_PACKED_MATMUL_COLUMN_TILE - 1u) / LW_PACKED_MATMUL_COLUMN_TILE;
|
||||
for (batch = 0u; batch < batch_count; ++batch) {
|
||||
uint32_t row;
|
||||
for (row = 0u; row < rows; ++row) {
|
||||
uint32_t column_panel;
|
||||
for (column_panel = 0u; column_panel < column_panels; ++column_panel) {
|
||||
float accumulators[LW_PACKED_MATMUL_COLUMN_TILE] = {0.0f};
|
||||
const uint32_t column_base = column_panel * LW_PACKED_MATMUL_COLUMN_TILE;
|
||||
const uint32_t valid_columns =
|
||||
columns - column_base < LW_PACKED_MATMUL_COLUMN_TILE
|
||||
? columns - column_base
|
||||
: LW_PACKED_MATMUL_COLUMN_TILE;
|
||||
uint32_t inner;
|
||||
for (inner = 0u; inner < inner_dimension; ++inner) {
|
||||
const float input_value =
|
||||
input[(size_t)(((uint64_t)batch * rows + row) * inner_dimension + inner)];
|
||||
const float* packed =
|
||||
packed_weights +
|
||||
(size_t)(((uint64_t)column_panel * inner_dimension + inner) *
|
||||
LW_PACKED_MATMUL_COLUMN_TILE);
|
||||
uint32_t lane;
|
||||
for (lane = 0u; lane < valid_columns; ++lane) {
|
||||
accumulators[lane] += input_value * packed[lane];
|
||||
}
|
||||
}
|
||||
for (uint32_t lane = 0u; lane < valid_columns; ++lane) {
|
||||
output[(size_t)(((uint64_t)batch * rows + row) * columns + column_base +
|
||||
lane)] = accumulators[lane];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef LW_PACKED_MATMUL_INTERNAL_H
|
||||
#define LW_PACKED_MATMUL_INTERNAL_H
|
||||
|
||||
/*
|
||||
* Private packed-weight contract for wide shared-weight MatMul. LWM keeps B
|
||||
* in canonical [K][N] order; eligible x64 AVX2 sessions convert it once to
|
||||
* [ceil(N/16)][K][16] and reuse the packed copy for every graph execution.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define LW_PACKED_MATMUL_COLUMN_TILE 16u
|
||||
|
||||
int lw_packed_matmul_weight_count(uint32_t inner_dimension, uint32_t columns,
|
||||
uint64_t* weight_count);
|
||||
void lw_pack_matmul_weights_f32(const float* weights, uint32_t inner_dimension,
|
||||
uint32_t columns, float* packed_weights);
|
||||
void lw_scalar_packed_matmul_shared_f32(const float* input, const float* packed_weights,
|
||||
float* output, uint32_t batch_count, uint32_t rows,
|
||||
uint32_t inner_dimension, uint32_t columns);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,94 @@
|
||||
#include "scalar_kernels.h"
|
||||
|
||||
/* Portable activation functions used as the reference implementation. */
|
||||
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static lw_status validate_elementwise(const float* input, float* output, uint64_t element_count) {
|
||||
if (element_count > (uint64_t)(SIZE_MAX / sizeof(float))) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
if (element_count != 0u && (input == NULL || output == NULL)) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
lw_status lw_scalar_relu_f32(const float* input, float* output, uint64_t element_count) {
|
||||
uint64_t index;
|
||||
lw_status status = validate_elementwise(input, output, element_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
float value = input[(size_t)index];
|
||||
output[(size_t)index] = value > 0.0f ? value : 0.0f;
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
lw_status lw_scalar_erf_f32(const float* input, float* output, uint64_t element_count) {
|
||||
uint64_t index;
|
||||
lw_status status = validate_elementwise(input, output, element_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
output[(size_t)index] = erff(input[(size_t)index]);
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
lw_status lw_scalar_hard_sigmoid_f32(const float* input, float* output, uint64_t element_count,
|
||||
float alpha, float beta) {
|
||||
uint64_t index;
|
||||
lw_status status = validate_elementwise(input, output, element_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
if (!isfinite(alpha) || !isfinite(beta)) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
float value = alpha * input[(size_t)index] + beta;
|
||||
if (value < 0.0f) {
|
||||
value = 0.0f;
|
||||
} else if (value > 1.0f) {
|
||||
value = 1.0f;
|
||||
}
|
||||
output[(size_t)index] = value;
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
lw_status lw_scalar_sigmoid_f32(const float* input, float* output, uint64_t element_count) {
|
||||
uint64_t index;
|
||||
lw_status status = validate_elementwise(input, output, element_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
float value = input[(size_t)index];
|
||||
if (value >= 0.0f) {
|
||||
output[(size_t)index] = 1.0f / (1.0f + expf(-value));
|
||||
} else {
|
||||
float exponential = expf(value);
|
||||
output[(size_t)index] = exponential / (1.0f + exponential);
|
||||
}
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
lw_status lw_scalar_sqrt_f32(const float* input, float* output, uint64_t element_count) {
|
||||
uint64_t index;
|
||||
lw_status status = validate_elementwise(input, output, element_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
output[(size_t)index] = sqrtf(input[(size_t)index]);
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
+1128
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,292 @@
|
||||
#include "scalar_kernels.h"
|
||||
|
||||
/* Elementwise arithmetic with NumPy-style broadcasting and SIMD fast paths. */
|
||||
#include "cpu_features.h"
|
||||
#include "simd_kernels.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static lw_status make_contiguous_strides(uint32_t rank, const int32_t* dimensions,
|
||||
uint64_t* strides, uint64_t* element_count) {
|
||||
uint64_t count = 1u;
|
||||
uint32_t axis;
|
||||
if (rank > LW_MAX_DIMS || (rank != 0u && dimensions == NULL)) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
for (axis = rank; axis > 0u; --axis) {
|
||||
int32_t dimension = dimensions[axis - 1u];
|
||||
if (dimension <= 0 || count > UINT64_MAX / (uint32_t)dimension) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
strides[axis - 1u] = count;
|
||||
count *= (uint32_t)dimension;
|
||||
}
|
||||
if (count > (uint64_t)(SIZE_MAX / sizeof(float))) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
*element_count = count;
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
void lw_scalar_binary_contiguous_f32(lw_scalar_binary_op operation, const float* left,
|
||||
const float* right, float* output, uint64_t element_count) {
|
||||
uint64_t index;
|
||||
if (operation == LW_SCALAR_BINARY_ADD) {
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
output[(size_t)index] = left[(size_t)index] + right[(size_t)index];
|
||||
}
|
||||
} else if (operation == LW_SCALAR_BINARY_MUL) {
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
output[(size_t)index] = left[(size_t)index] * right[(size_t)index];
|
||||
}
|
||||
} else if (operation == LW_SCALAR_BINARY_DIV) {
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
output[(size_t)index] = left[(size_t)index] / right[(size_t)index];
|
||||
}
|
||||
} else if (operation == LW_SCALAR_BINARY_SUB) {
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
output[(size_t)index] = left[(size_t)index] - right[(size_t)index];
|
||||
}
|
||||
} else {
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
output[(size_t)index] = powf(left[(size_t)index], right[(size_t)index]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lw_scalar_binary_right_scalar_f32(lw_scalar_binary_op operation, const float* left,
|
||||
float right, float* output, uint64_t element_count) {
|
||||
uint64_t index;
|
||||
if (operation == LW_SCALAR_BINARY_ADD) {
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
output[(size_t)index] = left[(size_t)index] + right;
|
||||
}
|
||||
} else if (operation == LW_SCALAR_BINARY_MUL) {
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
output[(size_t)index] = left[(size_t)index] * right;
|
||||
}
|
||||
} else if (operation == LW_SCALAR_BINARY_DIV) {
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
output[(size_t)index] = left[(size_t)index] / right;
|
||||
}
|
||||
} else if (operation == LW_SCALAR_BINARY_SUB) {
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
output[(size_t)index] = left[(size_t)index] - right;
|
||||
}
|
||||
} else {
|
||||
for (index = 0u; index < element_count; ++index) {
|
||||
output[(size_t)index] = powf(left[(size_t)index], right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void dispatch_binary_contiguous_for_level_f32(lw_scalar_binary_op operation,
|
||||
const float* left, const float* right,
|
||||
float* output, uint64_t element_count,
|
||||
lw_simd_level simd_level) {
|
||||
if (operation != LW_SCALAR_BINARY_ADD && operation != LW_SCALAR_BINARY_MUL &&
|
||||
operation != LW_SCALAR_BINARY_DIV) {
|
||||
lw_scalar_binary_contiguous_f32(operation, left, right, output, element_count);
|
||||
} else if (lw_simd_level_is_avx2(simd_level)) {
|
||||
lw_avx2_binary_contiguous_f32(operation, left, right, output, element_count);
|
||||
} else if (lw_simd_level_is_sse2(simd_level)) {
|
||||
lw_sse2_binary_contiguous_f32(operation, left, right, output, element_count);
|
||||
} else {
|
||||
lw_scalar_binary_contiguous_f32(operation, left, right, output, element_count);
|
||||
}
|
||||
}
|
||||
|
||||
static void dispatch_binary_contiguous_f32(lw_scalar_binary_op operation, const float* left,
|
||||
const float* right, float* output,
|
||||
uint64_t element_count) {
|
||||
dispatch_binary_contiguous_for_level_f32(operation, left, right, output, element_count,
|
||||
lw_detect_simd_level());
|
||||
}
|
||||
|
||||
static void dispatch_binary_right_scalar_for_level_f32(lw_scalar_binary_op operation,
|
||||
const float* left, float right,
|
||||
float* output, uint64_t element_count,
|
||||
lw_simd_level simd_level) {
|
||||
if (operation != LW_SCALAR_BINARY_ADD && operation != LW_SCALAR_BINARY_MUL &&
|
||||
operation != LW_SCALAR_BINARY_DIV) {
|
||||
lw_scalar_binary_right_scalar_f32(operation, left, right, output, element_count);
|
||||
} else if (lw_simd_level_is_avx2(simd_level)) {
|
||||
lw_avx2_binary_right_scalar_f32(operation, left, right, output, element_count);
|
||||
} else if (lw_simd_level_is_sse2(simd_level)) {
|
||||
lw_sse2_binary_right_scalar_f32(operation, left, right, output, element_count);
|
||||
} else {
|
||||
lw_scalar_binary_right_scalar_f32(operation, left, right, output, element_count);
|
||||
}
|
||||
}
|
||||
|
||||
static void dispatch_binary_right_scalar_f32(lw_scalar_binary_op operation, const float* left,
|
||||
float right, float* output, uint64_t element_count) {
|
||||
dispatch_binary_right_scalar_for_level_f32(operation, left, right, output, element_count,
|
||||
lw_detect_simd_level());
|
||||
}
|
||||
|
||||
static int try_dispatch_binary_single_axis_broadcast_f32(
|
||||
lw_scalar_binary_op operation, const float* left, const float* right, float* output,
|
||||
uint32_t right_rank, const int32_t* right_dimensions, uint32_t output_rank,
|
||||
const int32_t* output_dimensions, uint64_t output_count) {
|
||||
uint32_t right_padding = output_rank - right_rank;
|
||||
uint32_t non_unit_dimensions = 0u;
|
||||
uint32_t broadcast_axis = 0u;
|
||||
uint32_t axis;
|
||||
uint64_t inner_count = 1u;
|
||||
uint64_t right_dimension;
|
||||
uint64_t outer_count;
|
||||
uint64_t outer_index;
|
||||
lw_simd_level simd_level;
|
||||
|
||||
/* Many OCR graph biases vary on exactly one axis. Recognizing that shape
|
||||
* turns a general coordinate walk into contiguous vector operations. */
|
||||
for (axis = 0u; axis < output_rank; ++axis) {
|
||||
int32_t dimension = axis < right_padding ? 1 : right_dimensions[axis - right_padding];
|
||||
if (dimension != 1) {
|
||||
++non_unit_dimensions;
|
||||
broadcast_axis = axis;
|
||||
}
|
||||
}
|
||||
if (non_unit_dimensions != 1u) {
|
||||
return 0;
|
||||
}
|
||||
for (axis = broadcast_axis + 1u; axis < output_rank; ++axis) {
|
||||
inner_count *= (uint32_t)output_dimensions[axis];
|
||||
}
|
||||
right_dimension = (uint32_t)output_dimensions[broadcast_axis];
|
||||
outer_count = output_count / (right_dimension * inner_count);
|
||||
simd_level = lw_detect_simd_level();
|
||||
for (outer_index = 0u; outer_index < outer_count; ++outer_index) {
|
||||
uint64_t outer_offset = outer_index * right_dimension * inner_count;
|
||||
if (inner_count == 1u) {
|
||||
dispatch_binary_contiguous_for_level_f32(operation, left + (size_t)outer_offset, right,
|
||||
output + (size_t)outer_offset, right_dimension,
|
||||
simd_level);
|
||||
} else {
|
||||
uint64_t right_index;
|
||||
for (right_index = 0u; right_index < right_dimension; ++right_index) {
|
||||
uint64_t block_offset = outer_offset + right_index * inner_count;
|
||||
dispatch_binary_right_scalar_for_level_f32(
|
||||
operation, left + (size_t)block_offset, right[(size_t)right_index],
|
||||
output + (size_t)block_offset, inner_count, simd_level);
|
||||
}
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
lw_status lw_scalar_binary_f32(lw_scalar_binary_op operation, const float* left, uint32_t left_rank,
|
||||
const int32_t* left_dimensions, const float* right,
|
||||
uint32_t right_rank, const int32_t* right_dimensions, float* output,
|
||||
uint32_t output_rank, const int32_t* output_dimensions) {
|
||||
uint64_t left_contiguous[LW_MAX_DIMS] = {0u};
|
||||
uint64_t right_contiguous[LW_MAX_DIMS] = {0u};
|
||||
uint64_t left_strides[LW_MAX_DIMS] = {0u};
|
||||
uint64_t right_strides[LW_MAX_DIMS] = {0u};
|
||||
uint32_t coordinates[LW_MAX_DIMS] = {0u};
|
||||
uint64_t left_count;
|
||||
uint64_t right_count;
|
||||
uint64_t output_count = 1u;
|
||||
uint64_t left_offset = 0u;
|
||||
uint64_t right_offset = 0u;
|
||||
uint64_t output_index;
|
||||
uint32_t axis;
|
||||
lw_status status;
|
||||
|
||||
if (operation != LW_SCALAR_BINARY_ADD && operation != LW_SCALAR_BINARY_MUL &&
|
||||
operation != LW_SCALAR_BINARY_DIV && operation != LW_SCALAR_BINARY_SUB &&
|
||||
operation != LW_SCALAR_BINARY_POW) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (left == NULL || right == NULL || output == NULL || output == left || output == right) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (left_rank > LW_MAX_DIMS || right_rank > LW_MAX_DIMS || output_rank > LW_MAX_DIMS ||
|
||||
output_rank != (left_rank > right_rank ? left_rank : right_rank) ||
|
||||
(output_rank != 0u && output_dimensions == NULL)) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
status = make_contiguous_strides(left_rank, left_dimensions, left_contiguous, &left_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
status = make_contiguous_strides(right_rank, right_dimensions, right_contiguous, &right_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
for (axis = 0u; axis < output_rank; ++axis) {
|
||||
uint32_t left_padding = output_rank - left_rank;
|
||||
uint32_t right_padding = output_rank - right_rank;
|
||||
int32_t left_dimension = axis < left_padding ? 1 : left_dimensions[axis - left_padding];
|
||||
int32_t right_dimension = axis < right_padding ? 1 : right_dimensions[axis - right_padding];
|
||||
int32_t expected_dimension;
|
||||
if (left_dimension <= 0 || right_dimension <= 0 ||
|
||||
(left_dimension != right_dimension && left_dimension != 1 && right_dimension != 1)) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
expected_dimension = left_dimension > right_dimension ? left_dimension : right_dimension;
|
||||
if (output_dimensions[axis] != expected_dimension) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
if (output_count > UINT64_MAX / (uint32_t)expected_dimension) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
output_count *= (uint32_t)expected_dimension;
|
||||
if (axis >= left_padding && left_dimension != 1) {
|
||||
left_strides[axis] = left_contiguous[axis - left_padding];
|
||||
}
|
||||
if (axis >= right_padding && right_dimension != 1) {
|
||||
right_strides[axis] = right_contiguous[axis - right_padding];
|
||||
}
|
||||
}
|
||||
if (output_count > (uint64_t)(SIZE_MAX / sizeof(float))) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
|
||||
/* Prefer simple contiguous cases before the generic broadcasting loop. */
|
||||
if (left_count == output_count && right_count == output_count) {
|
||||
dispatch_binary_contiguous_f32(operation, left, right, output, output_count);
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
if (left_count == output_count && right_count == 1u) {
|
||||
dispatch_binary_right_scalar_f32(operation, left, right[0], output, output_count);
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
if (left_count == output_count &&
|
||||
try_dispatch_binary_single_axis_broadcast_f32(operation, left, right, output, right_rank,
|
||||
right_dimensions, output_rank,
|
||||
output_dimensions, output_count)) {
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
for (output_index = 0u; output_index < output_count; ++output_index) {
|
||||
float left_value = left[(size_t)left_offset];
|
||||
float right_value = right[(size_t)right_offset];
|
||||
if (operation == LW_SCALAR_BINARY_ADD) {
|
||||
output[(size_t)output_index] = left_value + right_value;
|
||||
} else if (operation == LW_SCALAR_BINARY_MUL) {
|
||||
output[(size_t)output_index] = left_value * right_value;
|
||||
} else {
|
||||
output[(size_t)output_index] = left_value / right_value;
|
||||
}
|
||||
|
||||
for (axis = output_rank; axis > 0u; --axis) {
|
||||
uint32_t current_axis = axis - 1u;
|
||||
++coordinates[current_axis];
|
||||
if (coordinates[current_axis] < (uint32_t)output_dimensions[current_axis]) {
|
||||
left_offset += left_strides[current_axis];
|
||||
right_offset += right_strides[current_axis];
|
||||
break;
|
||||
}
|
||||
coordinates[current_axis] = 0u;
|
||||
left_offset -=
|
||||
left_strides[current_axis] * ((uint32_t)output_dimensions[current_axis] - 1u);
|
||||
right_offset -=
|
||||
right_strides[current_axis] * ((uint32_t)output_dimensions[current_axis] - 1u);
|
||||
}
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
#ifndef LW_SCALAR_KERNELS_H
|
||||
#define LW_SCALAR_KERNELS_H
|
||||
|
||||
/*
|
||||
* Internal numeric-kernel contract. The scalar entry points are the portable
|
||||
* correctness baseline; selected wrappers may dispatch to SSE2 or AVX2 only
|
||||
* after validation has proved that dimensions and buffers are safe.
|
||||
*/
|
||||
|
||||
#include "lw_infer.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum lw_scalar_binary_op {
|
||||
LW_SCALAR_BINARY_ADD = 1,
|
||||
LW_SCALAR_BINARY_MUL = 2,
|
||||
LW_SCALAR_BINARY_DIV = 3,
|
||||
LW_SCALAR_BINARY_SUB = 4,
|
||||
LW_SCALAR_BINARY_POW = 5
|
||||
} lw_scalar_binary_op;
|
||||
|
||||
void lw_scalar_binary_contiguous_f32(lw_scalar_binary_op operation, const float* left,
|
||||
const float* right, float* output, uint64_t element_count);
|
||||
void lw_scalar_binary_right_scalar_f32(lw_scalar_binary_op operation, const float* left,
|
||||
float right, float* output, uint64_t element_count);
|
||||
|
||||
lw_status lw_scalar_binary_f32(lw_scalar_binary_op operation, const float* left, uint32_t left_rank,
|
||||
const int32_t* left_dimensions, const float* right,
|
||||
uint32_t right_rank, const int32_t* right_dimensions, float* output,
|
||||
uint32_t output_rank, const int32_t* output_dimensions);
|
||||
|
||||
lw_status lw_scalar_relu_f32(const float* input, float* output, uint64_t element_count);
|
||||
lw_status lw_scalar_erf_f32(const float* input, float* output, uint64_t element_count);
|
||||
lw_status lw_scalar_hard_sigmoid_f32(const float* input, float* output, uint64_t element_count,
|
||||
float alpha, float beta);
|
||||
lw_status lw_scalar_sigmoid_f32(const float* input, float* output, uint64_t element_count);
|
||||
lw_status lw_scalar_sqrt_f32(const float* input, float* output, uint64_t element_count);
|
||||
lw_status lw_scalar_softmax_f32(const float* input, float* output, uint32_t rank,
|
||||
const int32_t* dimensions, int32_t axis);
|
||||
lw_status lw_ctc_greedy_softmax_contiguous_f32(const float* input, uint32_t* best_indices,
|
||||
float* emitted_probabilities,
|
||||
uint64_t row_count, uint64_t axis_count);
|
||||
lw_status lw_ctc_emitted_softmax_contiguous_f32(const float* input,
|
||||
const uint32_t* best_indices,
|
||||
float* emitted_probabilities,
|
||||
uint64_t row_count, uint64_t axis_count);
|
||||
lw_status lw_scalar_transpose_f32(const float* input, float* output, uint32_t rank,
|
||||
const int32_t* input_dimensions, uint32_t permutation_count,
|
||||
const int32_t* permutation, const int32_t* output_dimensions);
|
||||
lw_status lw_scalar_squeeze_f32(const float* input, float* output, uint32_t input_rank,
|
||||
const int32_t* input_dimensions, uint32_t axes_count,
|
||||
const int32_t* axes, uint32_t output_rank,
|
||||
const int32_t* output_dimensions);
|
||||
lw_status lw_scalar_unsqueeze_f32(const float* input, float* output, uint32_t input_rank,
|
||||
const int32_t* input_dimensions, uint32_t axes_count,
|
||||
const int32_t* axes, uint32_t output_rank,
|
||||
const int32_t* output_dimensions);
|
||||
lw_status lw_scalar_reshape_f32(const float* input, float* output, uint32_t input_rank,
|
||||
const int32_t* input_dimensions, uint32_t output_rank,
|
||||
const int32_t* output_dimensions);
|
||||
lw_status lw_scalar_concat_f32(const float* const* inputs, uint32_t input_count,
|
||||
const uint32_t* input_ranks, const int32_t* const* input_dimensions,
|
||||
float* output, uint32_t output_rank,
|
||||
const int32_t* output_dimensions, int32_t axis);
|
||||
lw_status lw_scalar_resize_nearest_f32(const float* input, float* output, uint32_t rank,
|
||||
const int32_t* input_dimensions,
|
||||
const int32_t* output_dimensions, const float* scales);
|
||||
lw_status lw_scalar_slice_f32(const float* input, float* output, uint32_t rank,
|
||||
const int32_t* input_dimensions, const int32_t* output_dimensions,
|
||||
uint32_t slice_count, const int32_t* starts, const int32_t* ends,
|
||||
const int32_t* axes, const int32_t* steps);
|
||||
lw_status lw_scalar_reduce_mean_f32(const float* input, float* output, uint32_t input_rank,
|
||||
const int32_t* input_dimensions, uint32_t axes_count,
|
||||
const int32_t* axes, uint32_t keep_dimensions,
|
||||
uint32_t no_op_with_empty_axes, uint32_t output_rank,
|
||||
const int32_t* output_dimensions);
|
||||
lw_status lw_scalar_average_pool2d_f32(const float* input, float* output,
|
||||
const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4], const int32_t kernel[2],
|
||||
const int32_t strides[2], const int32_t pads[4],
|
||||
uint32_t ceil_mode, uint32_t count_include_pad);
|
||||
lw_status lw_scalar_max_pool2d_f32(const float* input, float* output,
|
||||
const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4], const int32_t kernel[2],
|
||||
const int32_t strides[2], const int32_t pads[4],
|
||||
uint32_t ceil_mode);
|
||||
lw_status lw_scalar_matmul_shared_f32(const float* input, const float* weights, float* output,
|
||||
uint32_t batch_count, uint32_t rows, uint32_t inner_dimension,
|
||||
uint32_t columns);
|
||||
lw_status lw_scalar_matmul_f32(const float* input, const float* weights, float* output,
|
||||
uint32_t input_rank, const int32_t* input_dimensions,
|
||||
uint32_t weights_rank, const int32_t* weights_dimensions,
|
||||
uint32_t output_rank, const int32_t* output_dimensions);
|
||||
lw_status lw_matmul_shared_f32(const float* input, const float* weights, float* output,
|
||||
uint32_t batch_count, uint32_t rows, uint32_t inner_dimension,
|
||||
uint32_t columns);
|
||||
lw_status lw_scalar_conv2d_f32(const float* input, const float* weights, const float* bias,
|
||||
uint32_t bias_count, float* output,
|
||||
const int32_t input_dimensions[4],
|
||||
const int32_t weight_dimensions[4],
|
||||
const int32_t output_dimensions[4], const int32_t kernel[2],
|
||||
const int32_t strides[2], const int32_t dilations[2],
|
||||
const int32_t pads[4], uint32_t groups);
|
||||
lw_status lw_scalar_conv_transpose2d_f32(
|
||||
const float* input, const float* weights, const float* bias, uint32_t bias_count, float* output,
|
||||
const int32_t input_dimensions[4], const int32_t weight_dimensions[4],
|
||||
const int32_t output_dimensions[4], const int32_t kernel[2], const int32_t strides[2],
|
||||
const int32_t dilations[2], const int32_t pads[4], uint32_t groups);
|
||||
void lw_scalar_conv_transpose2x2_stride2_f32(
|
||||
const float* input, const float* weights, const float* bias, float* output,
|
||||
const int32_t input_dimensions[4], const int32_t output_dimensions[4]);
|
||||
void lw_scalar_conv_transpose2x2_stride2_range_f32(
|
||||
const float* input, const float* weights, const float* bias, float* output,
|
||||
const int32_t input_dimensions[4], const int32_t output_dimensions[4],
|
||||
uint32_t output_channel_begin, uint32_t output_channel_end);
|
||||
void lw_scalar_conv1x1_unit_f32(const float* input, const float* weights, const float* bias,
|
||||
float* output, const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4], uint32_t groups,
|
||||
uint32_t input_channels_per_group,
|
||||
uint32_t output_channels_per_group);
|
||||
void lw_scalar_depthwise_conv3x3_unit_pad1_f32(const float* input, const float* weights,
|
||||
const float* bias, float* output,
|
||||
const int32_t dimensions[4]);
|
||||
void lw_scalar_depthwise_conv3x3_stride2x1_pad1_f32(
|
||||
const float* input, const float* weights, const float* bias, float* output,
|
||||
const int32_t input_dimensions[4], const int32_t output_dimensions[4]);
|
||||
void lw_scalar_depthwise_conv5x5_unit_pad2_f32(const float* input, const float* weights,
|
||||
const float* bias, float* output,
|
||||
const int32_t dimensions[4]);
|
||||
void lw_scalar_conv3x3_unit_pad1_f32(const float* input, const float* weights, const float* bias,
|
||||
float* output, const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4]);
|
||||
void lw_scalar_conv2x2_unit_pad_end1_f32(const float* input, const float* weights,
|
||||
const float* bias, float* output,
|
||||
const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4]);
|
||||
void lw_scalar_conv3x3_stride2_pad1_f32(const float* input, const float* weights, const float* bias,
|
||||
float* output, const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4]);
|
||||
lw_status lw_scalar_batch_normalization_f32(const float* input, const float* scale,
|
||||
const float* bias, const float* mean,
|
||||
const float* variance, uint32_t parameter_count,
|
||||
float epsilon, float* output, uint32_t rank,
|
||||
const int32_t* dimensions);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,506 @@
|
||||
#include "scalar_kernels.h"
|
||||
|
||||
/* Shape-preserving data movement: transpose, reshape, concat and resize. */
|
||||
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
static int normalize_axis(int32_t axis, uint32_t rank, uint32_t* normalized) {
|
||||
int64_t value = axis;
|
||||
if (value < 0) {
|
||||
value += rank;
|
||||
}
|
||||
if (value < 0 || value >= rank) {
|
||||
return 0;
|
||||
}
|
||||
*normalized = (uint32_t)value;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static lw_status tensor_element_count(uint32_t rank, const int32_t* dimensions, uint64_t* count) {
|
||||
uint64_t value = 1u;
|
||||
uint32_t index;
|
||||
if (rank > LW_MAX_DIMS || (rank != 0u && dimensions == NULL)) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
for (index = 0u; index < rank; ++index) {
|
||||
if (dimensions[index] <= 0 || value > UINT64_MAX / (uint32_t)dimensions[index]) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
value *= (uint32_t)dimensions[index];
|
||||
}
|
||||
if (value > (uint64_t)(SIZE_MAX / sizeof(float))) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
*count = value;
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
static lw_status copy_reshape(const float* input, float* output, uint64_t element_count) {
|
||||
if (input == NULL || output == NULL) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (input != output) {
|
||||
memcpy(output, input, (size_t)element_count * sizeof(float));
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
static int integer_resize_factor(float scale, int32_t input_dimension, int32_t output_dimension,
|
||||
uint32_t* factor) {
|
||||
uint32_t candidate;
|
||||
if (output_dimension % input_dimension != 0) {
|
||||
return 0;
|
||||
}
|
||||
candidate = (uint32_t)(output_dimension / input_dimension);
|
||||
if (candidate == 0u || (float)candidate != scale) {
|
||||
return 0;
|
||||
}
|
||||
*factor = candidate;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void resize_nchw_integer_spatial_f32(const float* input, float* output,
|
||||
const int32_t* input_dimensions,
|
||||
const int32_t* output_dimensions,
|
||||
uint32_t height_factor, uint32_t width_factor) {
|
||||
const uint32_t batch_count = (uint32_t)input_dimensions[0];
|
||||
const uint32_t channels = (uint32_t)input_dimensions[1];
|
||||
const uint32_t input_height = (uint32_t)input_dimensions[2];
|
||||
const uint32_t input_width = (uint32_t)input_dimensions[3];
|
||||
const uint32_t output_height = (uint32_t)output_dimensions[2];
|
||||
const uint32_t output_width = (uint32_t)output_dimensions[3];
|
||||
uint32_t batch;
|
||||
for (batch = 0u; batch < batch_count; ++batch) {
|
||||
uint32_t channel;
|
||||
for (channel = 0u; channel < channels; ++channel) {
|
||||
uint64_t input_plane =
|
||||
((uint64_t)batch * channels + channel) * input_height * input_width;
|
||||
uint64_t output_plane =
|
||||
((uint64_t)batch * channels + channel) * output_height * output_width;
|
||||
uint32_t input_y;
|
||||
for (input_y = 0u; input_y < input_height; ++input_y) {
|
||||
const float* source_row = input + (size_t)(input_plane + input_y * input_width);
|
||||
float* first_output_row =
|
||||
output +
|
||||
(size_t)(output_plane + (uint64_t)input_y * height_factor * output_width);
|
||||
uint32_t input_x;
|
||||
for (input_x = 0u; input_x < input_width; ++input_x) {
|
||||
uint32_t repeat_x;
|
||||
for (repeat_x = 0u; repeat_x < width_factor; ++repeat_x) {
|
||||
first_output_row[(size_t)input_x * width_factor + repeat_x] =
|
||||
source_row[input_x];
|
||||
}
|
||||
}
|
||||
for (uint32_t repeat_y = 1u; repeat_y < height_factor; ++repeat_y) {
|
||||
memcpy(first_output_row + (size_t)repeat_y * output_width, first_output_row,
|
||||
(size_t)output_width * sizeof(float));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lw_status lw_scalar_transpose_f32(const float* input, float* output, uint32_t rank,
|
||||
const int32_t* input_dimensions, uint32_t permutation_count,
|
||||
const int32_t* permutation, const int32_t* output_dimensions) {
|
||||
uint64_t input_strides[LW_MAX_DIMS] = {0u};
|
||||
int used[LW_MAX_DIMS] = {0};
|
||||
uint64_t element_count;
|
||||
uint64_t stride = 1u;
|
||||
uint64_t output_index;
|
||||
uint32_t axis;
|
||||
lw_status status;
|
||||
if (input == NULL || output == NULL || input == output) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (rank > LW_MAX_DIMS || permutation_count != rank ||
|
||||
(rank != 0u && (permutation == NULL || output_dimensions == NULL))) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
status = tensor_element_count(rank, input_dimensions, &element_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
for (axis = rank; axis > 0u; --axis) {
|
||||
input_strides[axis - 1u] = stride;
|
||||
stride *= (uint32_t)input_dimensions[axis - 1u];
|
||||
}
|
||||
for (axis = 0u; axis < rank; ++axis) {
|
||||
int32_t source_axis = permutation[axis];
|
||||
if (source_axis < 0 || (uint32_t)source_axis >= rank || used[source_axis] ||
|
||||
output_dimensions[axis] != input_dimensions[source_axis]) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
used[source_axis] = 1;
|
||||
}
|
||||
for (output_index = 0u; output_index < element_count; ++output_index) {
|
||||
uint64_t remaining = output_index;
|
||||
uint64_t input_offset = 0u;
|
||||
for (axis = rank; axis > 0u; --axis) {
|
||||
uint32_t output_axis = axis - 1u;
|
||||
uint32_t coordinate = (uint32_t)(remaining % (uint32_t)output_dimensions[output_axis]);
|
||||
remaining /= (uint32_t)output_dimensions[output_axis];
|
||||
input_offset +=
|
||||
(uint64_t)coordinate * input_strides[(uint32_t)permutation[output_axis]];
|
||||
}
|
||||
output[(size_t)output_index] = input[(size_t)input_offset];
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
lw_status lw_scalar_squeeze_f32(const float* input, float* output, uint32_t input_rank,
|
||||
const int32_t* input_dimensions, uint32_t axes_count,
|
||||
const int32_t* axes, uint32_t output_rank,
|
||||
const int32_t* output_dimensions) {
|
||||
int squeezed[LW_MAX_DIMS] = {0};
|
||||
uint64_t input_count;
|
||||
uint64_t output_count;
|
||||
uint32_t axis;
|
||||
uint32_t output_axis = 0u;
|
||||
lw_status status;
|
||||
if (axes_count > LW_MAX_DIMS || (axes_count != 0u && axes == NULL)) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
status = tensor_element_count(input_rank, input_dimensions, &input_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
if (axes_count == 0u) {
|
||||
for (axis = 0u; axis < input_rank; ++axis) {
|
||||
squeezed[axis] = input_dimensions[axis] == 1;
|
||||
}
|
||||
} else {
|
||||
for (axis = 0u; axis < axes_count; ++axis) {
|
||||
uint32_t normalized;
|
||||
if (!normalize_axis(axes[axis], input_rank, &normalized) || squeezed[normalized] ||
|
||||
input_dimensions[normalized] != 1) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
squeezed[normalized] = 1;
|
||||
}
|
||||
}
|
||||
for (axis = 0u; axis < input_rank; ++axis) {
|
||||
if (!squeezed[axis]) {
|
||||
if (output_axis >= output_rank || output_dimensions == NULL ||
|
||||
output_dimensions[output_axis] != input_dimensions[axis]) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
++output_axis;
|
||||
}
|
||||
}
|
||||
if (output_axis != output_rank) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
status = tensor_element_count(output_rank, output_dimensions, &output_count);
|
||||
if (status != LW_STATUS_OK || output_count != input_count) {
|
||||
return status == LW_STATUS_OK ? LW_STATUS_INVALID_SHAPE : status;
|
||||
}
|
||||
return copy_reshape(input, output, input_count);
|
||||
}
|
||||
|
||||
lw_status lw_scalar_unsqueeze_f32(const float* input, float* output, uint32_t input_rank,
|
||||
const int32_t* input_dimensions, uint32_t axes_count,
|
||||
const int32_t* axes, uint32_t output_rank,
|
||||
const int32_t* output_dimensions) {
|
||||
int inserted[LW_MAX_DIMS] = {0};
|
||||
uint64_t input_count;
|
||||
uint64_t output_count;
|
||||
uint32_t axis;
|
||||
uint32_t input_axis = 0u;
|
||||
lw_status status;
|
||||
if (input_rank > LW_MAX_DIMS || axes_count > LW_MAX_DIMS ||
|
||||
input_rank + axes_count != output_rank || output_rank > LW_MAX_DIMS ||
|
||||
(axes_count != 0u && axes == NULL)) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
status = tensor_element_count(input_rank, input_dimensions, &input_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
for (axis = 0u; axis < axes_count; ++axis) {
|
||||
uint32_t normalized;
|
||||
if (!normalize_axis(axes[axis], output_rank, &normalized) || inserted[normalized]) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
inserted[normalized] = 1;
|
||||
}
|
||||
for (axis = 0u; axis < output_rank; ++axis) {
|
||||
int32_t expected = inserted[axis] ? 1 : input_dimensions[input_axis++];
|
||||
if (output_dimensions == NULL || output_dimensions[axis] != expected) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
}
|
||||
status = tensor_element_count(output_rank, output_dimensions, &output_count);
|
||||
if (status != LW_STATUS_OK || output_count != input_count) {
|
||||
return status == LW_STATUS_OK ? LW_STATUS_INVALID_SHAPE : status;
|
||||
}
|
||||
return copy_reshape(input, output, input_count);
|
||||
}
|
||||
|
||||
lw_status lw_scalar_reshape_f32(const float* input, float* output, uint32_t input_rank,
|
||||
const int32_t* input_dimensions, uint32_t output_rank,
|
||||
const int32_t* output_dimensions) {
|
||||
uint64_t input_count;
|
||||
uint64_t output_count;
|
||||
lw_status status = tensor_element_count(input_rank, input_dimensions, &input_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
status = tensor_element_count(output_rank, output_dimensions, &output_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
if (input_count != output_count) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
return copy_reshape(input, output, input_count);
|
||||
}
|
||||
|
||||
lw_status lw_scalar_concat_f32(const float* const* inputs, uint32_t input_count,
|
||||
const uint32_t* input_ranks, const int32_t* const* input_dimensions,
|
||||
float* output, uint32_t output_rank,
|
||||
const int32_t* output_dimensions, int32_t axis) {
|
||||
uint32_t normalized_axis;
|
||||
uint64_t outer = 1u;
|
||||
uint64_t inner = 1u;
|
||||
uint64_t output_count;
|
||||
uint64_t concatenated_axis = 0u;
|
||||
uint32_t input_index;
|
||||
uint32_t dimension_index;
|
||||
uint64_t outer_index;
|
||||
lw_status status;
|
||||
if (inputs == NULL || input_ranks == NULL || input_dimensions == NULL || output == NULL ||
|
||||
output_dimensions == NULL || input_count < 1u ||
|
||||
!normalize_axis(axis, output_rank, &normalized_axis)) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
status = tensor_element_count(output_rank, output_dimensions, &output_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
(void)output_count;
|
||||
for (dimension_index = 0u; dimension_index < normalized_axis; ++dimension_index) {
|
||||
outer *= (uint32_t)output_dimensions[dimension_index];
|
||||
}
|
||||
for (dimension_index = normalized_axis + 1u; dimension_index < output_rank; ++dimension_index) {
|
||||
inner *= (uint32_t)output_dimensions[dimension_index];
|
||||
}
|
||||
for (input_index = 0u; input_index < input_count; ++input_index) {
|
||||
uint64_t ignored_count;
|
||||
if (inputs[input_index] == NULL || inputs[input_index] == output ||
|
||||
input_ranks[input_index] != output_rank || input_dimensions[input_index] == NULL) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
status = tensor_element_count(input_ranks[input_index], input_dimensions[input_index],
|
||||
&ignored_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
for (dimension_index = 0u; dimension_index < output_rank; ++dimension_index) {
|
||||
if (dimension_index != normalized_axis &&
|
||||
input_dimensions[input_index][dimension_index] !=
|
||||
output_dimensions[dimension_index]) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
}
|
||||
concatenated_axis += (uint32_t)input_dimensions[input_index][normalized_axis];
|
||||
}
|
||||
if (concatenated_axis != (uint32_t)output_dimensions[normalized_axis]) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
for (outer_index = 0u; outer_index < outer; ++outer_index) {
|
||||
uint64_t output_axis_offset = 0u;
|
||||
for (input_index = 0u; input_index < input_count; ++input_index) {
|
||||
uint64_t axis_elements =
|
||||
(uint64_t)(uint32_t)input_dimensions[input_index][normalized_axis] * inner;
|
||||
uint64_t input_offset = outer_index * axis_elements;
|
||||
uint64_t output_offset =
|
||||
(outer_index * (uint32_t)output_dimensions[normalized_axis] * inner) +
|
||||
output_axis_offset;
|
||||
memcpy(output + (size_t)output_offset, inputs[input_index] + (size_t)input_offset,
|
||||
(size_t)axis_elements * sizeof(float));
|
||||
output_axis_offset += axis_elements;
|
||||
}
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
lw_status lw_scalar_resize_nearest_f32(const float* input, float* output, uint32_t rank,
|
||||
const int32_t* input_dimensions,
|
||||
const int32_t* output_dimensions, const float* scales) {
|
||||
uint64_t input_count;
|
||||
uint64_t output_count;
|
||||
uint64_t output_index;
|
||||
uint64_t input_strides[LW_MAX_DIMS] = {0u};
|
||||
uint64_t stride = 1u;
|
||||
uint32_t height_factor;
|
||||
uint32_t width_factor;
|
||||
uint32_t axis;
|
||||
lw_status status;
|
||||
if (input == NULL || output == NULL || input == output || scales == NULL || rank == 0u ||
|
||||
rank > LW_MAX_DIMS || input_dimensions == NULL || output_dimensions == NULL) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
status = tensor_element_count(rank, input_dimensions, &input_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
status = tensor_element_count(rank, output_dimensions, &output_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
(void)input_count;
|
||||
for (axis = rank; axis > 0u; --axis) {
|
||||
input_strides[axis - 1u] = stride;
|
||||
stride *= (uint32_t)input_dimensions[axis - 1u];
|
||||
}
|
||||
for (axis = 0u; axis < rank; ++axis) {
|
||||
double expected;
|
||||
if (!isfinite(scales[axis]) || scales[axis] <= 0.0f) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
expected = floor((double)input_dimensions[axis] * scales[axis]);
|
||||
if (expected != output_dimensions[axis]) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
}
|
||||
if (rank == 4u && input_dimensions[0] == output_dimensions[0] &&
|
||||
input_dimensions[1] == output_dimensions[1] && scales[0] == 1.0f && scales[1] == 1.0f &&
|
||||
integer_resize_factor(scales[2], input_dimensions[2], output_dimensions[2],
|
||||
&height_factor) &&
|
||||
integer_resize_factor(scales[3], input_dimensions[3], output_dimensions[3],
|
||||
&width_factor)) {
|
||||
resize_nchw_integer_spatial_f32(input, output, input_dimensions, output_dimensions,
|
||||
height_factor, width_factor);
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
for (output_index = 0u; output_index < output_count; ++output_index) {
|
||||
uint64_t remaining = output_index;
|
||||
uint64_t input_offset = 0u;
|
||||
for (axis = rank; axis > 0u; --axis) {
|
||||
uint32_t current_axis = axis - 1u;
|
||||
uint32_t coordinate = (uint32_t)(remaining % (uint32_t)output_dimensions[current_axis]);
|
||||
uint32_t input_coordinate = (uint32_t)floor((double)coordinate / scales[current_axis]);
|
||||
if (input_coordinate >= (uint32_t)input_dimensions[current_axis]) {
|
||||
input_coordinate = (uint32_t)input_dimensions[current_axis] - 1u;
|
||||
}
|
||||
remaining /= (uint32_t)output_dimensions[current_axis];
|
||||
input_offset += (uint64_t)input_coordinate * input_strides[current_axis];
|
||||
}
|
||||
output[(size_t)output_index] = input[(size_t)input_offset];
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
static int slice_bounds(int32_t dimension, int32_t start, int32_t end, int32_t step,
|
||||
int32_t* first, int32_t* length) {
|
||||
int64_t normalized_start = start;
|
||||
int64_t normalized_end = end;
|
||||
int64_t count;
|
||||
if (dimension <= 0 || step <= 0) {
|
||||
return 0;
|
||||
}
|
||||
if (normalized_start == INT32_MIN) {
|
||||
normalized_start = 0;
|
||||
} else if (normalized_start < 0) {
|
||||
normalized_start += dimension;
|
||||
}
|
||||
if (normalized_end == INT32_MAX) {
|
||||
normalized_end = dimension;
|
||||
} else if (normalized_end < 0) {
|
||||
normalized_end += dimension;
|
||||
}
|
||||
if (normalized_start < 0) {
|
||||
normalized_start = 0;
|
||||
} else if (normalized_start > dimension) {
|
||||
normalized_start = dimension;
|
||||
}
|
||||
if (normalized_end < 0) {
|
||||
normalized_end = 0;
|
||||
} else if (normalized_end > dimension) {
|
||||
normalized_end = dimension;
|
||||
}
|
||||
count = normalized_end <= normalized_start
|
||||
? 0
|
||||
: (normalized_end - normalized_start + step - 1) / step;
|
||||
if (count <= 0 || count > INT32_MAX) {
|
||||
return 0;
|
||||
}
|
||||
*first = (int32_t)normalized_start;
|
||||
*length = (int32_t)count;
|
||||
return 1;
|
||||
}
|
||||
|
||||
lw_status lw_scalar_slice_f32(const float* input, float* output, uint32_t rank,
|
||||
const int32_t* input_dimensions, const int32_t* output_dimensions,
|
||||
uint32_t slice_count, const int32_t* starts, const int32_t* ends,
|
||||
const int32_t* axes, const int32_t* steps) {
|
||||
uint64_t input_count;
|
||||
uint64_t output_count;
|
||||
uint64_t input_strides[LW_MAX_DIMS] = {0u};
|
||||
int32_t first[LW_MAX_DIMS];
|
||||
int32_t lengths[LW_MAX_DIMS];
|
||||
int32_t slice_steps[LW_MAX_DIMS];
|
||||
int selected[LW_MAX_DIMS] = {0};
|
||||
uint64_t stride = 1u;
|
||||
uint64_t output_index;
|
||||
uint32_t axis;
|
||||
lw_status status;
|
||||
if (input == NULL || output == NULL || input == output || input_dimensions == NULL ||
|
||||
output_dimensions == NULL || starts == NULL || ends == NULL || axes == NULL ||
|
||||
steps == NULL || rank == 0u || rank > LW_MAX_DIMS || slice_count == 0u ||
|
||||
slice_count > rank) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
status = tensor_element_count(rank, input_dimensions, &input_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
status = tensor_element_count(rank, output_dimensions, &output_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
for (axis = rank; axis > 0u; --axis) {
|
||||
input_strides[axis - 1u] = stride;
|
||||
stride *= (uint32_t)input_dimensions[axis - 1u];
|
||||
}
|
||||
for (axis = 0u; axis < rank; ++axis) {
|
||||
first[axis] = 0;
|
||||
lengths[axis] = input_dimensions[axis];
|
||||
slice_steps[axis] = 1;
|
||||
}
|
||||
for (axis = 0u; axis < slice_count; ++axis) {
|
||||
uint32_t normalized_axis;
|
||||
if (!normalize_axis(axes[axis], rank, &normalized_axis) || selected[normalized_axis] ||
|
||||
!slice_bounds(input_dimensions[normalized_axis], starts[axis], ends[axis],
|
||||
steps[axis], &first[normalized_axis], &lengths[normalized_axis])) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
selected[normalized_axis] = 1;
|
||||
slice_steps[normalized_axis] = steps[axis];
|
||||
}
|
||||
for (axis = 0u; axis < rank; ++axis) {
|
||||
if (output_dimensions[axis] != lengths[axis]) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
}
|
||||
(void)input_count;
|
||||
for (output_index = 0u; output_index < output_count; ++output_index) {
|
||||
uint64_t remaining = output_index;
|
||||
uint64_t input_offset = 0u;
|
||||
for (axis = rank; axis > 0u; --axis) {
|
||||
uint32_t current_axis = axis - 1u;
|
||||
uint32_t coordinate =
|
||||
(uint32_t)(remaining % (uint32_t)output_dimensions[current_axis]);
|
||||
remaining /= (uint32_t)output_dimensions[current_axis];
|
||||
input_offset += (uint64_t)(first[current_axis] +
|
||||
coordinate * (uint32_t)slice_steps[current_axis]) *
|
||||
input_strides[current_axis];
|
||||
}
|
||||
output[(size_t)output_index] = input[(size_t)input_offset];
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
#include "scalar_kernels.h"
|
||||
|
||||
/* Shared-weight matrix multiplication used by the converted OCR graphs. */
|
||||
#include "cpu_features.h"
|
||||
#include "simd_kernels.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static int multiply_fits(uint64_t left, uint64_t right, uint64_t* result) {
|
||||
if (right != 0u && left > UINT64_MAX / right) {
|
||||
return 0;
|
||||
}
|
||||
*result = left * right;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int buffer_fits(uint32_t first, uint32_t second, uint32_t third) {
|
||||
uint64_t count;
|
||||
if (!multiply_fits(first, second, &count) || !multiply_fits(count, third, &count)) {
|
||||
return 0;
|
||||
}
|
||||
return count <= (uint64_t)(SIZE_MAX / sizeof(float));
|
||||
}
|
||||
|
||||
static lw_status validate_matmul_shared_f32(const float* input, const float* weights, float* output,
|
||||
uint32_t batch_count, uint32_t rows,
|
||||
uint32_t inner_dimension, uint32_t columns) {
|
||||
if (input == NULL || weights == NULL || output == NULL || output == input ||
|
||||
output == weights) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (batch_count == 0u || rows == 0u || inner_dimension == 0u || columns == 0u) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
if (!buffer_fits(batch_count, rows, inner_dimension) ||
|
||||
!buffer_fits(1u, inner_dimension, columns) || !buffer_fits(batch_count, rows, columns)) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
static void scalar_matmul_shared_f32(const float* input, const float* weights, float* output,
|
||||
uint32_t batch_count, uint32_t rows, uint32_t inner_dimension,
|
||||
uint32_t columns) {
|
||||
uint32_t batch;
|
||||
uint32_t row;
|
||||
uint32_t column;
|
||||
/* Four output rows share each sequential scan of the weight matrix. This
|
||||
* improves cache locality while remaining the scalar correctness path. */
|
||||
for (batch = 0u; batch < batch_count; ++batch) {
|
||||
for (row = 0u; row < rows;) {
|
||||
uint32_t row_end = rows - row < 4u ? rows : row + 4u;
|
||||
uint32_t inner;
|
||||
uint32_t current_row;
|
||||
for (current_row = row; current_row < row_end; ++current_row) {
|
||||
uint64_t output_base = ((uint64_t)batch * rows + current_row) * columns;
|
||||
for (column = 0u; column < columns; ++column) {
|
||||
output[(size_t)(output_base + column)] = 0.0f;
|
||||
}
|
||||
}
|
||||
for (inner = 0u; inner < inner_dimension; ++inner) {
|
||||
uint64_t weight_base = (uint64_t)inner * columns;
|
||||
for (current_row = row; current_row < row_end; ++current_row) {
|
||||
uint64_t input_base = ((uint64_t)batch * rows + current_row) * inner_dimension;
|
||||
uint64_t output_base = ((uint64_t)batch * rows + current_row) * columns;
|
||||
float input_value = input[(size_t)(input_base + inner)];
|
||||
for (column = 0u; column < columns; ++column) {
|
||||
output[(size_t)(output_base + column)] +=
|
||||
input_value * weights[(size_t)(weight_base + column)];
|
||||
}
|
||||
}
|
||||
}
|
||||
row = row_end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int matmul_shape_product(const int32_t* dimensions, uint32_t rank, uint64_t* result) {
|
||||
uint32_t index;
|
||||
uint64_t product = 1u;
|
||||
if (dimensions == NULL || rank == 0u || rank > LW_MAX_DIMS) {
|
||||
return 0;
|
||||
}
|
||||
for (index = 0u; index < rank; ++index) {
|
||||
if (dimensions[index] <= 0 || product > UINT64_MAX / (uint32_t)dimensions[index]) {
|
||||
return 0;
|
||||
}
|
||||
product *= (uint32_t)dimensions[index];
|
||||
}
|
||||
*result = product;
|
||||
return product <= (uint64_t)(SIZE_MAX / sizeof(float));
|
||||
}
|
||||
|
||||
/* Portable ONNX MatMul for rank >= 2 tensors with broadcast batch dimensions.
|
||||
* The common rank-N x rank-2 constant-weight case stays on the optimized
|
||||
* shared-weight path in the executor; this fallback covers attention-style
|
||||
* batched matrices used by larger REC graphs. */
|
||||
lw_status lw_scalar_matmul_f32(const float* input, const float* weights, float* output,
|
||||
uint32_t input_rank, const int32_t* input_dimensions,
|
||||
uint32_t weights_rank, const int32_t* weights_dimensions,
|
||||
uint32_t output_rank, const int32_t* output_dimensions) {
|
||||
uint32_t batch_rank;
|
||||
uint32_t input_batch_rank;
|
||||
uint32_t weights_batch_rank;
|
||||
uint32_t input_offset;
|
||||
uint32_t weights_offset;
|
||||
uint32_t batch_index;
|
||||
uint32_t batch_count;
|
||||
uint32_t row;
|
||||
uint32_t column;
|
||||
uint32_t inner;
|
||||
uint64_t input_elements;
|
||||
uint64_t weights_elements;
|
||||
uint64_t output_elements;
|
||||
uint64_t input_matrix_size;
|
||||
uint64_t weights_matrix_size;
|
||||
uint64_t output_matrix_size;
|
||||
if (input == NULL || weights == NULL || output == NULL || output == input ||
|
||||
output == weights || input_dimensions == NULL || weights_dimensions == NULL ||
|
||||
output_dimensions == NULL || input_rank < 2u || weights_rank < 2u ||
|
||||
input_rank > LW_MAX_DIMS || weights_rank > LW_MAX_DIMS || output_rank < 2u ||
|
||||
output_rank > LW_MAX_DIMS) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
input_batch_rank = input_rank - 2u;
|
||||
weights_batch_rank = weights_rank - 2u;
|
||||
batch_rank = input_batch_rank > weights_batch_rank ? input_batch_rank : weights_batch_rank;
|
||||
if (batch_rank > LW_MAX_DIMS - 2u || output_rank != batch_rank + 2u ||
|
||||
input_dimensions[input_rank - 1u] != weights_dimensions[weights_rank - 2u] ||
|
||||
input_dimensions[input_rank - 2u] != output_dimensions[batch_rank] ||
|
||||
weights_dimensions[weights_rank - 1u] != output_dimensions[batch_rank + 1u]) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
input_offset = batch_rank - input_batch_rank;
|
||||
weights_offset = batch_rank - weights_batch_rank;
|
||||
batch_count = 1u;
|
||||
for (batch_index = 0u; batch_index < batch_rank; ++batch_index) {
|
||||
int32_t input_dimension = batch_index < input_offset
|
||||
? 1
|
||||
: input_dimensions[batch_index - input_offset];
|
||||
int32_t weights_dimension = batch_index < weights_offset
|
||||
? 1
|
||||
: weights_dimensions[batch_index - weights_offset];
|
||||
if (input_dimension <= 0 || weights_dimension <= 0 ||
|
||||
(input_dimension != weights_dimension && input_dimension != 1 &&
|
||||
weights_dimension != 1) || output_dimensions[batch_index] <= 0 ||
|
||||
(output_dimensions[batch_index] != input_dimension &&
|
||||
output_dimensions[batch_index] != weights_dimension)) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
if (batch_count > UINT32_MAX / (uint32_t)output_dimensions[batch_index]) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
batch_count *= (uint32_t)output_dimensions[batch_index];
|
||||
}
|
||||
if (!matmul_shape_product(input_dimensions, input_rank, &input_elements) ||
|
||||
!matmul_shape_product(weights_dimensions, weights_rank, &weights_elements) ||
|
||||
!matmul_shape_product(output_dimensions, output_rank, &output_elements)) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
(void)input_elements;
|
||||
(void)weights_elements;
|
||||
(void)output_elements;
|
||||
input_matrix_size = (uint64_t)(uint32_t)input_dimensions[input_rank - 2u] *
|
||||
(uint32_t)input_dimensions[input_rank - 1u];
|
||||
weights_matrix_size = (uint64_t)(uint32_t)weights_dimensions[weights_rank - 2u] *
|
||||
(uint32_t)weights_dimensions[weights_rank - 1u];
|
||||
output_matrix_size = (uint64_t)(uint32_t)output_dimensions[batch_rank] *
|
||||
(uint32_t)output_dimensions[batch_rank + 1u];
|
||||
for (batch_index = 0u; batch_index < batch_count; ++batch_index) {
|
||||
uint32_t remaining = batch_index;
|
||||
uint64_t input_batch = 0u;
|
||||
uint64_t weights_batch = 0u;
|
||||
uint32_t axis;
|
||||
for (axis = batch_rank; axis-- > 0u;) {
|
||||
uint32_t coordinate = remaining % (uint32_t)output_dimensions[axis];
|
||||
int32_t input_dimension = axis < input_offset
|
||||
? 1
|
||||
: input_dimensions[axis - input_offset];
|
||||
int32_t weights_dimension = axis < weights_offset
|
||||
? 1
|
||||
: weights_dimensions[axis - weights_offset];
|
||||
remaining /= (uint32_t)output_dimensions[axis];
|
||||
if (axis >= input_offset) {
|
||||
input_batch = input_batch * (uint32_t)input_dimension +
|
||||
(input_dimension == 1 ? 0u : coordinate);
|
||||
}
|
||||
if (axis >= weights_offset) {
|
||||
weights_batch = weights_batch * (uint32_t)weights_dimension +
|
||||
(weights_dimension == 1 ? 0u : coordinate);
|
||||
}
|
||||
}
|
||||
for (row = 0u; row < (uint32_t)input_dimensions[input_rank - 2u]; ++row) {
|
||||
for (column = 0u; column < (uint32_t)weights_dimensions[weights_rank - 1u];
|
||||
++column) {
|
||||
float value = 0.0f;
|
||||
for (inner = 0u; inner < (uint32_t)input_dimensions[input_rank - 1u]; ++inner) {
|
||||
size_t input_index = (size_t)(input_batch * input_matrix_size +
|
||||
(uint64_t)row *
|
||||
(uint32_t)input_dimensions[input_rank - 1u] +
|
||||
inner);
|
||||
size_t weights_index = (size_t)(weights_batch * weights_matrix_size +
|
||||
(uint64_t)inner *
|
||||
(uint32_t)weights_dimensions[weights_rank - 1u] +
|
||||
column);
|
||||
value += input[input_index] * weights[weights_index];
|
||||
}
|
||||
output[(size_t)(batch_index * output_matrix_size +
|
||||
(uint64_t)row *
|
||||
(uint32_t)weights_dimensions[weights_rank - 1u] +
|
||||
column)] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
lw_status lw_scalar_matmul_shared_f32(const float* input, const float* weights, float* output,
|
||||
uint32_t batch_count, uint32_t rows, uint32_t inner_dimension,
|
||||
uint32_t columns) {
|
||||
lw_status status = validate_matmul_shared_f32(input, weights, output, batch_count, rows,
|
||||
inner_dimension, columns);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
scalar_matmul_shared_f32(input, weights, output, batch_count, rows, inner_dimension, columns);
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
lw_status lw_matmul_shared_f32(const float* input, const float* weights, float* output,
|
||||
uint32_t batch_count, uint32_t rows, uint32_t inner_dimension,
|
||||
uint32_t columns) {
|
||||
lw_simd_level simd_level;
|
||||
lw_status status = validate_matmul_shared_f32(input, weights, output, batch_count, rows,
|
||||
inner_dimension, columns);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
/* Detection happens at runtime so one binary remains safe on older CPUs. */
|
||||
simd_level = lw_detect_simd_level();
|
||||
if (lw_simd_level_is_avx2(simd_level)) {
|
||||
lw_avx2_matmul_shared_f32(input, weights, output, batch_count, rows, inner_dimension,
|
||||
columns);
|
||||
} else if (lw_simd_level_is_sse2(simd_level)) {
|
||||
lw_sse2_matmul_shared_f32(input, weights, output, batch_count, rows, inner_dimension,
|
||||
columns);
|
||||
} else {
|
||||
scalar_matmul_shared_f32(input, weights, output, batch_count, rows, inner_dimension,
|
||||
columns);
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
@@ -0,0 +1,424 @@
|
||||
#include "scalar_kernels.h"
|
||||
|
||||
/* Reduction and pooling kernels with explicit shape and bounds validation. */
|
||||
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
static int normalize_axis(int32_t axis, uint32_t rank, uint32_t* normalized) {
|
||||
int64_t value = axis;
|
||||
if (value < 0) {
|
||||
value += rank;
|
||||
}
|
||||
if (value < 0 || value >= rank) {
|
||||
return 0;
|
||||
}
|
||||
*normalized = (uint32_t)value;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static lw_status tensor_element_count(uint32_t rank, const int32_t* dimensions, uint64_t* count) {
|
||||
uint64_t value = 1u;
|
||||
uint32_t index;
|
||||
if (rank > LW_MAX_DIMS || (rank != 0u && dimensions == NULL)) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
for (index = 0u; index < rank; ++index) {
|
||||
if (dimensions[index] <= 0 || value > UINT64_MAX / (uint32_t)dimensions[index]) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
value *= (uint32_t)dimensions[index];
|
||||
}
|
||||
if (value > (uint64_t)(SIZE_MAX / sizeof(float))) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
*count = value;
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
lw_status lw_scalar_reduce_mean_f32(const float* input, float* output, uint32_t input_rank,
|
||||
const int32_t* input_dimensions, uint32_t axes_count,
|
||||
const int32_t* axes, uint32_t keep_dimensions,
|
||||
uint32_t no_op_with_empty_axes, uint32_t output_rank,
|
||||
const int32_t* output_dimensions) {
|
||||
int reduced[LW_MAX_DIMS] = {0};
|
||||
uint32_t input_to_output[LW_MAX_DIMS];
|
||||
uint32_t coordinates[LW_MAX_DIMS] = {0u};
|
||||
uint64_t output_strides[LW_MAX_DIMS] = {0u};
|
||||
uint64_t input_count;
|
||||
uint64_t output_count;
|
||||
uint64_t reduction_count = 1u;
|
||||
uint64_t input_index;
|
||||
uint64_t output_offset = 0u;
|
||||
uint64_t stride = 1u;
|
||||
uint32_t axis;
|
||||
uint32_t expected_output_rank = 0u;
|
||||
lw_status status;
|
||||
if (input == NULL || output == NULL || input == output) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (axes_count > LW_MAX_DIMS || (axes_count != 0u && axes == NULL) || keep_dimensions > 1u ||
|
||||
no_op_with_empty_axes > 1u) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
status = tensor_element_count(input_rank, input_dimensions, &input_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
if (axes_count == 0u && no_op_with_empty_axes == 0u) {
|
||||
for (axis = 0u; axis < input_rank; ++axis) {
|
||||
reduced[axis] = 1;
|
||||
}
|
||||
} else {
|
||||
for (axis = 0u; axis < axes_count; ++axis) {
|
||||
uint32_t normalized;
|
||||
if (!normalize_axis(axes[axis], input_rank, &normalized) || reduced[normalized]) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
reduced[normalized] = 1;
|
||||
}
|
||||
}
|
||||
for (axis = 0u; axis < input_rank; ++axis) {
|
||||
input_to_output[axis] = UINT32_MAX;
|
||||
if (reduced[axis]) {
|
||||
if (reduction_count > UINT64_MAX / (uint32_t)input_dimensions[axis]) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
reduction_count *= (uint32_t)input_dimensions[axis];
|
||||
if (keep_dimensions != 0u) {
|
||||
if (expected_output_rank >= output_rank || output_dimensions == NULL ||
|
||||
output_dimensions[expected_output_rank] != 1) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
++expected_output_rank;
|
||||
}
|
||||
} else {
|
||||
if (expected_output_rank >= output_rank || output_dimensions == NULL ||
|
||||
output_dimensions[expected_output_rank] != input_dimensions[axis]) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
input_to_output[axis] = expected_output_rank++;
|
||||
}
|
||||
}
|
||||
if (expected_output_rank != output_rank) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
status = tensor_element_count(output_rank, output_dimensions, &output_count);
|
||||
if (status != LW_STATUS_OK || output_count > input_count) {
|
||||
return status == LW_STATUS_OK ? LW_STATUS_INVALID_SHAPE : status;
|
||||
}
|
||||
/*
|
||||
* GlobalAveragePool nodes in the bundled models are represented as a
|
||||
* keep-dimension ReduceMean over NCHW spatial axes. Each output is one
|
||||
* contiguous input plane, so coordinate decoding is unnecessary. The
|
||||
* inner loop retains the original row-major addition order exactly.
|
||||
*/
|
||||
if (input_rank == 4u && output_rank == 4u && keep_dimensions != 0u && !reduced[0] &&
|
||||
!reduced[1] && reduced[2] && reduced[3] && output_dimensions[0] == input_dimensions[0] &&
|
||||
output_dimensions[1] == input_dimensions[1] && output_dimensions[2] == 1 &&
|
||||
output_dimensions[3] == 1) {
|
||||
const uint64_t spatial_count =
|
||||
(uint64_t)(uint32_t)input_dimensions[2] * (uint32_t)input_dimensions[3];
|
||||
uint64_t plane;
|
||||
for (plane = 0u; plane < output_count; ++plane) {
|
||||
const uint64_t base = plane * spatial_count;
|
||||
float sum = 0.0f;
|
||||
uint64_t spatial_index;
|
||||
for (spatial_index = 0u; spatial_index < spatial_count; ++spatial_index) {
|
||||
sum += input[(size_t)(base + spatial_index)];
|
||||
}
|
||||
output[(size_t)plane] = sum / (float)spatial_count;
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
for (axis = output_rank; axis > 0u; --axis) {
|
||||
output_strides[axis - 1u] = stride;
|
||||
stride *= (uint32_t)output_dimensions[axis - 1u];
|
||||
}
|
||||
for (input_index = 0u; input_index < output_count; ++input_index) {
|
||||
output[(size_t)input_index] = 0.0f;
|
||||
}
|
||||
for (input_index = 0u; input_index < input_count; ++input_index) {
|
||||
output[(size_t)output_offset] += input[(size_t)input_index];
|
||||
for (axis = input_rank; axis > 0u; --axis) {
|
||||
uint32_t input_axis = axis - 1u;
|
||||
uint32_t mapped_axis = input_to_output[input_axis];
|
||||
++coordinates[input_axis];
|
||||
if (coordinates[input_axis] < (uint32_t)input_dimensions[input_axis]) {
|
||||
if (mapped_axis != UINT32_MAX) {
|
||||
output_offset += output_strides[mapped_axis];
|
||||
}
|
||||
break;
|
||||
}
|
||||
coordinates[input_axis] = 0u;
|
||||
if (mapped_axis != UINT32_MAX) {
|
||||
output_offset -=
|
||||
output_strides[mapped_axis] * ((uint32_t)input_dimensions[input_axis] - 1u);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (input_index = 0u; input_index < output_count; ++input_index) {
|
||||
output[(size_t)input_index] /= (float)reduction_count;
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
static int spatial_output(int32_t input, int32_t kernel, int32_t stride, int32_t pad_before,
|
||||
int32_t pad_after, uint32_t ceil_mode, int32_t* output) {
|
||||
int64_t numerator;
|
||||
int64_t value;
|
||||
if (input <= 0 || kernel <= 0 || stride <= 0 || pad_before < 0 || pad_after < 0) {
|
||||
return 0;
|
||||
}
|
||||
numerator = (int64_t)input + pad_before + pad_after - kernel;
|
||||
if (numerator < 0) {
|
||||
return 0;
|
||||
}
|
||||
value = (numerator + (ceil_mode != 0u ? stride - 1 : 0)) / stride + 1;
|
||||
if (ceil_mode != 0u && (value - 1) * stride >= (int64_t)input + pad_before) {
|
||||
--value;
|
||||
}
|
||||
if (value <= 0 || value > INT32_MAX) {
|
||||
return 0;
|
||||
}
|
||||
*output = (int32_t)value;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* PP-OCR DET uses one large 2x2, stride-1 SAME_UPPER MaxPool. The general
|
||||
* pooling loop below must repeatedly derive coordinates and check four bounds
|
||||
* for every output element. This exact specialization handles the unpadded
|
||||
* interior with direct row pointers, then writes the right and bottom borders
|
||||
* separately. Comparisons remain in row-major kernel order so finite values,
|
||||
* infinities and the existing first-value NaN behavior are preserved.
|
||||
*/
|
||||
static void max_pool2x2_stride1_same_upper_f32(const float* input, float* output,
|
||||
const int32_t dimensions[4]) {
|
||||
const uint64_t plane_count = (uint64_t)(uint32_t)dimensions[0] * (uint32_t)dimensions[1];
|
||||
const uint32_t height = (uint32_t)dimensions[2];
|
||||
const uint32_t width = (uint32_t)dimensions[3];
|
||||
const size_t plane_elements = (size_t)height * width;
|
||||
uint64_t plane;
|
||||
for (plane = 0u; plane < plane_count; ++plane) {
|
||||
const float* input_plane = input + (size_t)plane * plane_elements;
|
||||
float* output_plane = output + (size_t)plane * plane_elements;
|
||||
uint32_t y;
|
||||
for (y = 0u; y + 1u < height; ++y) {
|
||||
const float* top_row = input_plane + (size_t)y * width;
|
||||
const float* bottom_row = top_row + width;
|
||||
float* output_row = output_plane + (size_t)y * width;
|
||||
uint32_t x;
|
||||
for (x = 0u; x + 1u < width; ++x) {
|
||||
float maximum = top_row[x];
|
||||
if (top_row[x + 1u] > maximum) {
|
||||
maximum = top_row[x + 1u];
|
||||
}
|
||||
if (bottom_row[x] > maximum) {
|
||||
maximum = bottom_row[x];
|
||||
}
|
||||
if (bottom_row[x + 1u] > maximum) {
|
||||
maximum = bottom_row[x + 1u];
|
||||
}
|
||||
output_row[x] = maximum;
|
||||
}
|
||||
output_row[width - 1u] = top_row[width - 1u];
|
||||
if (bottom_row[width - 1u] > output_row[width - 1u]) {
|
||||
output_row[width - 1u] = bottom_row[width - 1u];
|
||||
}
|
||||
}
|
||||
{
|
||||
const float* last_input_row = input_plane + (size_t)(height - 1u) * width;
|
||||
float* last_output_row = output_plane + (size_t)(height - 1u) * width;
|
||||
uint32_t x;
|
||||
for (x = 0u; x + 1u < width; ++x) {
|
||||
float maximum = last_input_row[x];
|
||||
if (last_input_row[x + 1u] > maximum) {
|
||||
maximum = last_input_row[x + 1u];
|
||||
}
|
||||
last_output_row[x] = maximum;
|
||||
}
|
||||
last_output_row[width - 1u] = last_input_row[width - 1u];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lw_status lw_scalar_average_pool2d_f32(const float* input, float* output,
|
||||
const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4], const int32_t kernel[2],
|
||||
const int32_t strides[2], const int32_t pads[4],
|
||||
uint32_t ceil_mode, uint32_t count_include_pad) {
|
||||
uint64_t input_count;
|
||||
uint64_t output_count;
|
||||
int32_t expected_height;
|
||||
int32_t expected_width;
|
||||
uint32_t batch;
|
||||
uint32_t channel;
|
||||
uint32_t output_y;
|
||||
uint32_t output_x;
|
||||
lw_status status;
|
||||
if (input == NULL || output == NULL || input == output || input_dimensions == NULL ||
|
||||
output_dimensions == NULL || kernel == NULL || strides == NULL || pads == NULL) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (ceil_mode > 1u || count_include_pad > 1u) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
status = tensor_element_count(4u, input_dimensions, &input_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
status = tensor_element_count(4u, output_dimensions, &output_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
(void)input_count;
|
||||
(void)output_count;
|
||||
if (!spatial_output(input_dimensions[2], kernel[0], strides[0], pads[0], pads[2], ceil_mode,
|
||||
&expected_height) ||
|
||||
!spatial_output(input_dimensions[3], kernel[1], strides[1], pads[1], pads[3], ceil_mode,
|
||||
&expected_width) ||
|
||||
output_dimensions[0] != input_dimensions[0] ||
|
||||
output_dimensions[1] != input_dimensions[1] || output_dimensions[2] != expected_height ||
|
||||
output_dimensions[3] != expected_width) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
for (batch = 0u; batch < (uint32_t)input_dimensions[0]; ++batch) {
|
||||
for (channel = 0u; channel < (uint32_t)input_dimensions[1]; ++channel) {
|
||||
for (output_y = 0u; output_y < (uint32_t)expected_height; ++output_y) {
|
||||
int64_t input_y_start = (int64_t)output_y * strides[0] - pads[0];
|
||||
for (output_x = 0u; output_x < (uint32_t)expected_width; ++output_x) {
|
||||
int64_t input_x_start = (int64_t)output_x * strides[1] - pads[1];
|
||||
float sum = 0.0f;
|
||||
uint64_t valid_count = 0u;
|
||||
int32_t kernel_y;
|
||||
int32_t kernel_x;
|
||||
uint64_t output_offset =
|
||||
(((uint64_t)batch * (uint32_t)input_dimensions[1] + channel) *
|
||||
(uint32_t)expected_height +
|
||||
output_y) *
|
||||
(uint32_t)expected_width +
|
||||
output_x;
|
||||
for (kernel_y = 0; kernel_y < kernel[0]; ++kernel_y) {
|
||||
int64_t input_y = input_y_start + kernel_y;
|
||||
for (kernel_x = 0; kernel_x < kernel[1]; ++kernel_x) {
|
||||
int64_t input_x = input_x_start + kernel_x;
|
||||
if (input_y >= 0 && input_y < input_dimensions[2] && input_x >= 0 &&
|
||||
input_x < input_dimensions[3]) {
|
||||
uint64_t input_offset =
|
||||
(((uint64_t)batch * (uint32_t)input_dimensions[1] + channel) *
|
||||
(uint32_t)input_dimensions[2] +
|
||||
(uint32_t)input_y) *
|
||||
(uint32_t)input_dimensions[3] +
|
||||
(uint32_t)input_x;
|
||||
sum += input[(size_t)input_offset];
|
||||
++valid_count;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (count_include_pad != 0u) {
|
||||
valid_count = (uint64_t)(uint32_t)kernel[0] * (uint32_t)kernel[1];
|
||||
}
|
||||
if (valid_count == 0u) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
output[(size_t)output_offset] = sum / (float)valid_count;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
lw_status lw_scalar_max_pool2d_f32(const float* input, float* output,
|
||||
const int32_t input_dimensions[4],
|
||||
const int32_t output_dimensions[4], const int32_t kernel[2],
|
||||
const int32_t strides[2], const int32_t pads[4],
|
||||
uint32_t ceil_mode) {
|
||||
uint64_t element_count;
|
||||
int32_t expected_height;
|
||||
int32_t expected_width;
|
||||
uint32_t batch;
|
||||
uint32_t channel;
|
||||
uint32_t output_y;
|
||||
uint32_t output_x;
|
||||
lw_status status;
|
||||
if (input == NULL || output == NULL || input == output || input_dimensions == NULL ||
|
||||
output_dimensions == NULL || kernel == NULL || strides == NULL || pads == NULL) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (ceil_mode > 1u) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
status = tensor_element_count(4u, input_dimensions, &element_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
status = tensor_element_count(4u, output_dimensions, &element_count);
|
||||
if (status != LW_STATUS_OK) {
|
||||
return status;
|
||||
}
|
||||
if (!spatial_output(input_dimensions[2], kernel[0], strides[0], pads[0], pads[2], ceil_mode,
|
||||
&expected_height) ||
|
||||
!spatial_output(input_dimensions[3], kernel[1], strides[1], pads[1], pads[3], ceil_mode,
|
||||
&expected_width) ||
|
||||
output_dimensions[0] != input_dimensions[0] ||
|
||||
output_dimensions[1] != input_dimensions[1] || output_dimensions[2] != expected_height ||
|
||||
output_dimensions[3] != expected_width) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
if (kernel[0] == 2 && kernel[1] == 2 && strides[0] == 1 && strides[1] == 1 &&
|
||||
pads[0] == 0 && pads[1] == 0 && pads[2] == 1 && pads[3] == 1 && ceil_mode == 0u &&
|
||||
output_dimensions[2] == input_dimensions[2] &&
|
||||
output_dimensions[3] == input_dimensions[3]) {
|
||||
max_pool2x2_stride1_same_upper_f32(input, output, input_dimensions);
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
for (batch = 0u; batch < (uint32_t)input_dimensions[0]; ++batch) {
|
||||
for (channel = 0u; channel < (uint32_t)input_dimensions[1]; ++channel) {
|
||||
for (output_y = 0u; output_y < (uint32_t)expected_height; ++output_y) {
|
||||
int64_t input_y_start = (int64_t)output_y * strides[0] - pads[0];
|
||||
for (output_x = 0u; output_x < (uint32_t)expected_width; ++output_x) {
|
||||
int64_t input_x_start = (int64_t)output_x * strides[1] - pads[1];
|
||||
float maximum = -INFINITY;
|
||||
int found = 0;
|
||||
int32_t kernel_y;
|
||||
int32_t kernel_x;
|
||||
uint64_t output_offset =
|
||||
(((uint64_t)batch * (uint32_t)input_dimensions[1] + channel) *
|
||||
(uint32_t)expected_height +
|
||||
output_y) *
|
||||
(uint32_t)expected_width +
|
||||
output_x;
|
||||
for (kernel_y = 0; kernel_y < kernel[0]; ++kernel_y) {
|
||||
int64_t input_y = input_y_start + kernel_y;
|
||||
for (kernel_x = 0; kernel_x < kernel[1]; ++kernel_x) {
|
||||
int64_t input_x = input_x_start + kernel_x;
|
||||
if (input_y >= 0 && input_y < input_dimensions[2] && input_x >= 0 &&
|
||||
input_x < input_dimensions[3]) {
|
||||
uint64_t input_offset =
|
||||
(((uint64_t)batch * (uint32_t)input_dimensions[1] + channel) *
|
||||
(uint32_t)input_dimensions[2] +
|
||||
(uint32_t)input_y) *
|
||||
(uint32_t)input_dimensions[3] +
|
||||
(uint32_t)input_x;
|
||||
float value = input[(size_t)input_offset];
|
||||
if (!found || value > maximum) {
|
||||
maximum = value;
|
||||
}
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
output[(size_t)output_offset] = maximum;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
#include "scalar_kernels.h"
|
||||
#include "cpu_features.h"
|
||||
#include "simd_kernels.h"
|
||||
|
||||
/* Numerically stable softmax: subtract the row maximum before exponentiation. */
|
||||
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
lw_status lw_scalar_softmax_f32(const float* input, float* output, uint32_t rank,
|
||||
const int32_t* dimensions, int32_t axis) {
|
||||
uint64_t outer_count = 1u;
|
||||
uint64_t inner_count = 1u;
|
||||
uint64_t axis_count;
|
||||
uint64_t outer;
|
||||
uint64_t inner;
|
||||
uint32_t normalized_axis;
|
||||
uint32_t index;
|
||||
|
||||
if (input == NULL || output == NULL) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (rank == 0u || rank > LW_MAX_DIMS || dimensions == NULL || axis < -(int32_t)rank ||
|
||||
axis >= (int32_t)rank) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
normalized_axis = axis < 0 ? (uint32_t)((int32_t)rank + axis) : (uint32_t)axis;
|
||||
for (index = 0u; index < rank; ++index) {
|
||||
uint64_t dimension;
|
||||
if (dimensions[index] <= 0) {
|
||||
return LW_STATUS_INVALID_SHAPE;
|
||||
}
|
||||
dimension = (uint32_t)dimensions[index];
|
||||
if (index < normalized_axis) {
|
||||
if (outer_count > UINT64_MAX / dimension) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
outer_count *= dimension;
|
||||
} else if (index > normalized_axis) {
|
||||
if (inner_count > UINT64_MAX / dimension) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
inner_count *= dimension;
|
||||
}
|
||||
}
|
||||
axis_count = (uint32_t)dimensions[normalized_axis];
|
||||
if (outer_count > UINT64_MAX / axis_count ||
|
||||
outer_count * axis_count > UINT64_MAX / inner_count ||
|
||||
outer_count * axis_count * inner_count > (uint64_t)(SIZE_MAX / sizeof(float))) {
|
||||
return LW_STATUS_OUT_OF_BOUNDS;
|
||||
}
|
||||
|
||||
/* The REC classifier axis is the contiguous final dimension. Direct row
|
||||
* pointers remove the general strided offset arithmetic while preserving
|
||||
* the maximum, expf, accumulation and division order exactly. */
|
||||
if (inner_count == 1u) {
|
||||
if (axis_count >= 256u &&
|
||||
lw_simd_level_is_avx2(lw_detect_simd_level())) {
|
||||
lw_avx2_softmax_contiguous_f32(input, output, outer_count, axis_count);
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
for (outer = 0u; outer < outer_count; ++outer) {
|
||||
const float* input_row = input + (size_t)(outer * axis_count);
|
||||
float* output_row = output + (size_t)(outer * axis_count);
|
||||
float maximum = input_row[0];
|
||||
float sum = 0.0f;
|
||||
uint64_t axis_index;
|
||||
for (axis_index = 1u; axis_index < axis_count; ++axis_index) {
|
||||
if (input_row[(size_t)axis_index] > maximum) {
|
||||
maximum = input_row[(size_t)axis_index];
|
||||
}
|
||||
}
|
||||
for (axis_index = 0u; axis_index < axis_count; ++axis_index) {
|
||||
float value = expf(input_row[(size_t)axis_index] - maximum);
|
||||
output_row[(size_t)axis_index] = value;
|
||||
sum += value;
|
||||
}
|
||||
for (axis_index = 0u; axis_index < axis_count; ++axis_index) {
|
||||
output_row[(size_t)axis_index] /= sum;
|
||||
}
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
for (outer = 0u; outer < outer_count; ++outer) {
|
||||
for (inner = 0u; inner < inner_count; ++inner) {
|
||||
uint64_t axis_index;
|
||||
uint64_t base = outer * axis_count * inner_count + inner;
|
||||
/* Shifting by the maximum preserves the result and prevents expf
|
||||
* from overflowing on large logits. */
|
||||
float maximum = input[(size_t)base];
|
||||
float sum = 0.0f;
|
||||
for (axis_index = 1u; axis_index < axis_count; ++axis_index) {
|
||||
float value = input[(size_t)(base + axis_index * inner_count)];
|
||||
if (value > maximum) {
|
||||
maximum = value;
|
||||
}
|
||||
}
|
||||
for (axis_index = 0u; axis_index < axis_count; ++axis_index) {
|
||||
size_t offset = (size_t)(base + axis_index * inner_count);
|
||||
float value = expf(input[offset] - maximum);
|
||||
output[offset] = value;
|
||||
sum += value;
|
||||
}
|
||||
for (axis_index = 0u; axis_index < axis_count; ++axis_index) {
|
||||
size_t offset = (size_t)(base + axis_index * inner_count);
|
||||
output[offset] /= sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
|
||||
lw_status lw_ctc_greedy_softmax_contiguous_f32(const float* input, uint32_t* best_indices,
|
||||
float* emitted_probabilities,
|
||||
uint64_t row_count, uint64_t axis_count) {
|
||||
uint64_t row;
|
||||
if (input == NULL || best_indices == NULL || emitted_probabilities == NULL || row_count == 0u ||
|
||||
axis_count == 0u || axis_count > UINT32_MAX ||
|
||||
row_count > (uint64_t)(SIZE_MAX / sizeof(*best_indices)) ||
|
||||
row_count > (uint64_t)(SIZE_MAX / sizeof(*emitted_probabilities)) ||
|
||||
row_count > UINT64_MAX / axis_count ||
|
||||
row_count * axis_count > (uint64_t)(SIZE_MAX / sizeof(float))) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
/* Argmax is required for every time step, but the CTC confidence score
|
||||
* consumes Softmax only for non-blank, non-repeated emitted classes. */
|
||||
for (row = 0u; row < row_count; ++row) {
|
||||
const float* input_row = input + (size_t)(row * axis_count);
|
||||
uint32_t best_index = 0u;
|
||||
float maximum = input_row[0];
|
||||
uint64_t index;
|
||||
if (!isfinite(maximum)) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
for (index = 1u; index < axis_count; ++index) {
|
||||
float value = input_row[(size_t)index];
|
||||
if (!isfinite(value)) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
if (value > maximum) {
|
||||
maximum = value;
|
||||
best_index = (uint32_t)index;
|
||||
}
|
||||
}
|
||||
best_indices[(size_t)row] = best_index;
|
||||
}
|
||||
return lw_ctc_emitted_softmax_contiguous_f32(input, best_indices,
|
||||
emitted_probabilities, row_count, axis_count);
|
||||
}
|
||||
|
||||
lw_status lw_ctc_emitted_softmax_contiguous_f32(const float* input,
|
||||
const uint32_t* best_indices,
|
||||
float* emitted_probabilities,
|
||||
uint64_t row_count, uint64_t axis_count) {
|
||||
uint64_t row;
|
||||
if (input == NULL || best_indices == NULL || emitted_probabilities == NULL ||
|
||||
row_count == 0u || axis_count == 0u || axis_count > UINT32_MAX ||
|
||||
row_count > (uint64_t)(SIZE_MAX / sizeof(*best_indices)) ||
|
||||
row_count > (uint64_t)(SIZE_MAX / sizeof(*emitted_probabilities)) ||
|
||||
row_count > UINT64_MAX / axis_count ||
|
||||
row_count * axis_count > (uint64_t)(SIZE_MAX / sizeof(float))) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
for (row = 0u; row < row_count; ++row) {
|
||||
if (best_indices[(size_t)row] >= axis_count) {
|
||||
return LW_STATUS_INVALID_ARGUMENT;
|
||||
}
|
||||
emitted_probabilities[(size_t)row] = 0.0f;
|
||||
}
|
||||
if (axis_count >= 256u && lw_simd_level_is_avx2(lw_detect_simd_level())) {
|
||||
lw_avx2_ctc_emitted_softmax_contiguous_f32(input, best_indices,
|
||||
emitted_probabilities, row_count, axis_count);
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
for (row = 0u; row < row_count; ++row) {
|
||||
uint32_t best_index = best_indices[(size_t)row];
|
||||
if (best_index != 0u && (row == 0u || best_index != best_indices[(size_t)row - 1u])) {
|
||||
const float* input_row = input + (size_t)(row * axis_count);
|
||||
float maximum = input_row[best_index];
|
||||
float sum = 0.0f;
|
||||
uint64_t index;
|
||||
for (index = 0u; index < axis_count; ++index) {
|
||||
sum += expf(input_row[(size_t)index] - maximum);
|
||||
}
|
||||
emitted_probabilities[(size_t)row] = 1.0f / sum;
|
||||
}
|
||||
}
|
||||
return LW_STATUS_OK;
|
||||
}
|
||||
Reference in New Issue
Block a user