Samonte's picture
Upload folder using huggingface_hub
b4b9425 verified
Raw History Blame Contribute Delete
11.9 kB
/* SPDX-License-Identifier: Apache-2.0
*
* CX3 runtime (campaign ABI 2). The compiler (compiler/cx3q2.py) emits one CX3 program for the whole network in
* payload/cx3.bin, a writable placement at CX3_IMAGE_ADDR that holds the program, the weights and the activation
* buffers; it is linked at CX3_LINK_BASE and the NPU adds RELOC to every DRAM address it issues.
*
* prepare: RELOC = image placement - link base.
* query: INPUT = the input's address as the NPU sees it (host address - DRAM base - RELOC: the NPU adds RELOC to
* every address), PROG = the program, CTRL = run; a STATUS read is held by the NPU until it is idle (every
* write acknowledged). The final layers write their results straight into cx3_record, in the record's own
* layout (REC passes, compiler/cx2/isa.py: NHWC or NCHW int8, YOLOX's decoded int32): the host does no
* per-byte output work. Only an output layer that another layer also reads is still gathered here from its
* region (cx3_seg; normally none).
* prepare also writes the record's link address into the REC instructions (cx3_rec_patch: the record is placed
* by the linker, the program by the compiler).
* Neither the input's nor the record's link address can fall in the NPU's on-chip scratchpad window (link
* 0x4000_0000 + 4 MiB, rtl/cx2_spm.sv): the compiler places cx3.bin so that the window's DRAM preimage
* lies above the 1 GiB host window (cx3q2.spm_window_check).
*
* CX3_INU (every panel model): the NPU's input unit reads the raw uint8 NHWC RGB input where the evaluator delivered it
* and applies the input tables itself (rtl/cx3_inu.sv): the host does O(1) work per query. Otherwise the host's input
* stage writes the int8 input map into the image's input streams (pixel (y, x), channel c at in_base + (y % P) *
* sstride + ((y / P) * W + x) * K3 + c): one 256-entry table per channel for a uint8 sensor input (the compiler folds
* the model's normalisation into them), or q = rne(x * 2^k) for an FP32 NCHW input.
*/
#include <stdint.h>
#include "cf_campaign.h"
#define CX3_RUNTIME /* the output gather table */
#include "cx3_model_gen.h"
extern int8_t cx3_record[];
#define NPU_REG(off) (*(volatile uint32_t *)(uintptr_t)(0x30000000u + (off)))
#define NPU_CTRL 0x00u /* W: bit 0 runs the program at PROG */
#define NPU_STATUS 0x10u /* R: bit 0 busy; held until idle */
#define NPU_PROG 0x20u
#define NPU_RELOC 0x40u
#define NPU_INPUT 0x50u /* the input unit's input address (NPU view) */
#ifndef CX3_INU
#define CX3_INU 0u
#endif
/* host view of an image (link) address */
#ifndef HOST
#define HOST(link) ((uintptr_t)(CX3_HOST_DRAM_BASE + CX3_IMAGE_ADDR + ((link) - CX3_LINK_BASE)))
#endif
#if !CX3_INU
static const int8_t *in_tables;
/* uint8 NHWC with 3 channels (every panel model): the tables live in the data TCM, the input is read as words and
* each pixel's 3 bytes are stored as one word (the 4th byte is K3 padding, never weighted) */
#define FAST3 (CX3_IN_MODE == 0u && CX3_IN_C == 3u && (CX3_IN_W % 4u) == 0u)
static uint32_t tw3[3][256]; /* input channel k, byte v -> its table value at its output channel's byte */
/* q = round_to_nearest_even(x * 2^k), saturated to int8; x is an IEEE fp32 bit pattern (the starter's). */
static int8_t quant_fp32(uint32_t b, int32_t k) {
uint32_t e = (b >> 23) & 0xffu, m = b & 0x7fffffu, mag;
int32_t sh;
if (e == 0xffu) {
if (m) return 0;
mag = 255;
} else {
uint32_t mant = e ? (m | 0x800000u) : m;
sh = e ? (int32_t)e - 150 + k : k - 149;
if (mant == 0) mag = 0;
else if (sh >= 0) mag = (sh >= 8 || mant > (255u >> sh)) ? 255u : mant << sh;
else {
uint32_t n = (uint32_t)(-sh);
if (n > 25) mag = 0;
else {
uint32_t q = mant >> n, rem = mant & ((1u << n) - 1u), half = 1u << (n - 1u);
if (rem > half || (rem == half && (q & 1u))) ++q;
mag = q;
}
}
}
int32_t v = (b >> 31) ? -(int32_t)(mag > 128u ? 128u : mag) : (int32_t)(mag > 127u ? 127u : mag);
return (int8_t)v;
}
/* The input map into the image's slot streams (bytes past C in each K3-byte pixel are never weighted). */
static void input_stage_fast3(const uint8_t *x) {
uint8_t *base = (uint8_t *)HOST(CX3_IN_BASE);
const uint32_t *xw = (const uint32_t *)x;
const uint32_t *t0 = tw3[0], *t1 = tw3[1], *t2 = tw3[2];
for (uint32_t y = 0; y < CX3_IN_H; ++y) {
uint32_t *pix = (uint32_t *)(base + (y % CX3_P) * CX3_IN_SSTRIDE + (y / CX3_P) * CX3_IN_W * CX3_IN_K3);
for (uint32_t xx = 0; xx < CX3_IN_W; xx += 4u, xw += 3) {
uint32_t a = xw[0], b = xw[1], c = xw[2]; /* 4 pixels: a0 a1 a2 | a3 b0 b1 | b2 b3 c0 | c1 c2 c3 */
pix[0] = t0[a & 0xffu] | t1[(a >> 8) & 0xffu] | t2[(a >> 16) & 0xffu];
pix += CX3_IN_K3 / 4u;
pix[0] = t0[a >> 24] | t1[b & 0xffu] | t2[(b >> 8) & 0xffu];
pix += CX3_IN_K3 / 4u;
pix[0] = t0[(b >> 16) & 0xffu] | t1[b >> 24] | t2[c & 0xffu];
pix += CX3_IN_K3 / 4u;
pix[0] = t0[(c >> 8) & 0xffu] | t1[(c >> 16) & 0xffu] | t2[c >> 24];
pix += CX3_IN_K3 / 4u;
}
}
}
#if CX3_IN_MODE == 3u
static uint8_t tt[CX3_IN_C][256]; /* token channel c, input byte v -> its table value (data TCM) */
static void input_stage_tokens(const uint8_t *x) {
static const uint32_t in_perm[] = {CX3_IN_PERM0, CX3_IN_PERM1, CX3_IN_PERM2};
const uint32_t k = CX3_TOK_K, wp = CX3_IN_W / CX3_TOK_K, c = CX3_IN_C;
uint8_t *base = (uint8_t *)HOST(CX3_IN_BASE);
for (uint32_t i = 0; i < c * k * k; ++i) base[i] = (uint8_t)CX3_TOK_ZP; /* token 0: slot 0, position 0 */
for (uint32_t y = 0; y < CX3_IN_H; ++y) {
const uint8_t *src = x + y * CX3_IN_W * c;
for (uint32_t px = 0; px < wp; ++px) {
const uint32_t t = 1u + (y / k) * wp + px;
uint8_t *tok = base + (t % CX3_P) * CX3_IN_SSTRIDE + (t / CX3_P) * CX3_IN_K3 + (y % k) * k;
for (uint32_t ch = 0; ch < c; ++ch) {
const uint8_t *s = src + in_perm[ch], *tab = tt[ch];
uint8_t *d = tok + ch * k * k;
for (uint32_t kx = 0; kx < k; ++kx) d[kx] = tab[s[kx * c]];
}
src += k * c;
}
}
}
#endif
static void input_stage(const uint8_t *x) {
#if CX3_IN_MODE == 3u
input_stage_tokens(x);
return;
#endif
if (FAST3) { input_stage_fast3(x); return; }
static const uint32_t in_perm[] = {CX3_IN_PERM0, CX3_IN_PERM1, CX3_IN_PERM2};
uint8_t *base = (uint8_t *)HOST(CX3_IN_BASE);
const uint32_t h = CX3_IN_H, w = CX3_IN_W, c = CX3_IN_C;
for (uint32_t y = 0; y < h; ++y) {
uint8_t *row = base + (y % CX3_P) * CX3_IN_SSTRIDE + (y / CX3_P) * w * CX3_IN_K3;
for (uint32_t xx = 0; xx < w; ++xx) {
uint8_t *pix = row + xx * CX3_IN_K3;
uint32_t i = y * w + xx;
for (uint32_t ch = 0; ch < c; ++ch) {
int8_t q;
if (CX3_IN_MODE == 2u)
q = quant_fp32(((const uint32_t *)x)[ch * h * w + i], (int32_t)CX3_IN_PARAM);
else if (CX3_IN_MODE == 1u)
q = in_tables[ch * 256u + x[in_perm[ch] * h * w + i]];
else
q = in_tables[ch * 256u + x[i * c + in_perm[ch]]];
pix[ch] = (uint8_t)q;
}
}
}
}
#endif /* !CX3_INU */
#if CX3_YOLOX
static int32_t sat32(int64_t v) {
return v > INT32_MAX ? INT32_MAX : v < INT32_MIN ? INT32_MIN : (int32_t)v;
}
#endif
#if CX3_YOLOX
/* YOLOX: decode the 3 x 3 prediction maps into [anchors, 5 + classes] Q16.16 int32 (cx3sem.py, op for op) */
static void yolox_decode(void) {
for (uint32_t l = 0; l < CX3_N_LVL; ++l) {
const uint32_t *v = cx3_lvl[l];
const int32_t *txy = cx3_tab[l][0], *twh = cx3_tab[l][1], *tob = cx3_tab[l][2], *tcl = cx3_tab[l][3];
const uint32_t h = v[9], w = v[10];
const int64_t st = (int64_t)v[11];
int32_t *o = (int32_t *)(void *)cx3_record + v[12] * (5u + CX3_N_CLS);
for (uint32_t y = 0; y < h; ++y) {
const volatile uint8_t *pb = (const volatile uint8_t *)HOST(v[0]) + (y % CX3_P) * v[2] + (y / CX3_P) * w * v[1];
const volatile uint8_t *po = (const volatile uint8_t *)HOST(v[3]) + (y % CX3_P) * v[5] + (y / CX3_P) * w * v[4];
const volatile uint8_t *pc = (const volatile uint8_t *)HOST(v[6]) + (y % CX3_P) * v[8] + (y / CX3_P) * w * v[7];
for (uint32_t x = 0; x < w; ++x, pb += v[1], po += v[4], pc += v[7]) {
o[0] = sat32(((int64_t)txy[pb[0]] + ((int64_t)x << 16)) * st);
o[1] = sat32(((int64_t)txy[pb[1]] + ((int64_t)y << 16)) * st);
o[2] = twh[pb[2]];
o[3] = twh[pb[3]];
o[4] = tob[po[0]];
for (uint32_t k = 0; k < CX3_N_CLS; ++k) o[5 + k] = tcl[pc[k]];
o += 5u + CX3_N_CLS;
}
}
}
}
#endif
int cf_runtime_init(void) {
/* Prepare (untimed): the input tables live in weights.bin (the input unit's copy in cx3.bin); the NPU relocates the
* image. */
#if !CX3_INU
in_tables = (const int8_t *)(uintptr_t)(CX3_HOST_DRAM_BASE + CX3_BLOB_ADDR + CX3_IN_PARAM);
if (FAST3) {
static const uint32_t perm[3] = {CX3_IN_PERM0, CX3_IN_PERM1, CX3_IN_PERM2}; /* output channel c reads input perm[c] */
for (uint32_t ch = 0; ch < 3u; ++ch)
for (uint32_t v = 0; v < 256u; ++v)
tw3[perm[ch]][v] = (uint32_t)(uint8_t)in_tables[ch * 256u + v] << (8u * ch);
}
#if CX3_IN_MODE == 3u
for (uint32_t ch = 0; ch < CX3_IN_C; ++ch)
for (uint32_t v = 0; v < 256u; ++v) tt[ch][v] = (uint8_t)in_tables[ch * 256u + v];
#endif
#endif
#if CX3_N_REC
{
/* the NPU adds RELOC to every address and sees host address - CX3_HOST_DRAM_BASE */
const uint32_t rec = (uint32_t)(uintptr_t)cx3_record - CX3_HOST_DRAM_BASE - (CX3_RELOC & 0xfffff000u);
for (uint32_t i = 0; i < CX3_N_REC; ++i)
*(volatile uint32_t *)HOST(cx3_rec_patch[i][0]) = rec + cx3_rec_patch[i][1];
}
#endif
NPU_REG(NPU_RELOC) = CX3_RELOC;
if (NPU_REG(NPU_RELOC) != (CX3_RELOC & 0xfffff000u))
return -1;
#if CX3_PROG_FILL
/* the constants' on-chip copies (the scratchpad's weight region): the fill program's WCPs, once, before any query */
NPU_REG(NPU_PROG) = CX3_PROG_FILL;
NPU_REG(NPU_CTRL) = 1u;
if (NPU_REG(NPU_STATUS) & 1u) /* returns once the NPU is idle and its writes are in the scratchpad */
return -1;
#endif
return 0;
}
int cf_runtime_begin(const void *inputs, uint32_t count, uint32_t input_bytes, uint32_t offline) {
(void)inputs;
(void)count;
(void)offline;
return input_bytes == CX3_INPUT_BYTES ? 0 : -1;
}
int cf_runtime_query(uint32_t query, const void *input) {
(void)query;
#if CX3_INU
NPU_REG(NPU_INPUT) = (uint32_t)((uintptr_t)input - CX3_HOST_DRAM_BASE - CX3_RELOC);
#else
input_stage((const uint8_t *)input);
#endif
NPU_REG(NPU_PROG) = CX3_PROG;
NPU_REG(NPU_CTRL) = 1u;
if (NPU_REG(NPU_STATUS) & 1u) /* returns once the NPU is idle and its writes are acknowledged */
return -1;
/* outputs the NPU did not write into the record itself (cx3_seg: none unless a layer reads an output) */
#if CX3_N_SEG
for (uint32_t s = 0; s < CX3_N_SEG; ++s) {
const uint32_t *g = cx3_seg[s]; /* base, K3, slot stride, H, W, C, record offset, NCHW */
int8_t *dst = cx3_record + g[6];
const uint32_t hw = g[3] * g[4];
for (uint32_t y = 0; y < g[3]; ++y) {
const volatile int8_t *row = (const volatile int8_t *)HOST(g[0]) + (y % CX3_P) * g[2] + (y / CX3_P) * g[4] * g[1];
for (uint32_t x = 0; x < g[4]; ++x, row += g[1]) {
if (g[7]) { /* a map output: the record is NCHW */
int8_t *d = dst + y * g[4] + x;
for (uint32_t c = 0; c < g[5]; ++c, d += hw) *d = row[c];
} else {
for (uint32_t c = 0; c < g[5]; ++c) *dst++ = row[c];
}
}
}
}
#endif
#if CX3_YOLOX
yolox_decode();
#endif
return 0;
}
const void *cf_runtime_record(void) { return cx3_record; }