Skip to content
zhuguabngya edited this page Aug 3, 2026 · 3 revisions

HC — Hierarchical Coded Number System / SGN 超度量数系技术栈

English | 中文


English

SGN (Signed Globular Number) is a custom numerical type system based on ultrametric space. Core idea: represent numbers using multi-layer byte arrays, where each layer is an independent radix (256-ary / 65536-ary), naturally forming an ultrametric tree structure — lexicographic order equals ultrametric distance, prefix equals ultrametric ball.

SGN serves two worlds simultaneously:

  • Embedded / MCU scenarios: Minimal link footprint (only hc.c + hc8.c, no third-party dependencies), used for time variables, precision recording, Flash fault tolerance.
  • PC-side neural network quantized inference: Built around HC8/HC16 cores, providing int4/int8/int16 low-precision integer matrix multiply paths with AVX2 / FMA / AVX-VNNI instruction set acceleration.

Version Overview

The repository contains two independent version directories:

Version Focus Language Standard Build Target Status
v0.1 SGN full-stack implementation C11 / C++11 Static libs sgn_core / sgn_ext / sgn + tests Mainline (recommended for research/extension)
v0.2 HC neural network extension (C final version) C11 / C++23 pybind11 module sgn_c_engine*.so Final C version, no new features added

Core HC Types (HC = Hierarchical Coded)

All HC variants share a compile-time metadata table (hc_meta_t / hc_meta_get()), eliminating hard-coded layer counts and radices.

Type Structure Layers × Width Fractional Precision Range Typical Use
HC8 uint8_t v[6] (6 bytes packed) 6 × 8 bit 48 bit [0, 256) Embedded主力, symmetric quantized int8 matmul, HC4 asymmetric split base
HC16 uint16_t v[4] (8 bytes) 4 × 16 bit 64 bit [0, 65536) Timestamps, high-precision storage, int16×int16 quantized matmul (256× precision over HC8)
HC32 uint32_t v[3] (12 bytes) 3 × 32 bit 96 bit [0, 2³²) PC high-precision (v0.1 only)
HC64 uint64_t v[2] (16 bytes) 2 × 64 bit 128 bit [0, 2⁶⁴) Experimental high-precision (v0.1 only)

v0.1 Mainline: Full Technology Stack

Core capabilities (why HC exists):

  • Time variables: Composite mode level × 256 + HC as SGN time variable itself
  • Precision recording: Cross-platform byte-level consistent, compressible, verifiable persistent format
  • Ultrametric comparison & indexing: Lexicographic order = ultrametric distance, Trie prefix = ultrametric ball
  • Basic arithmetic: Saturated add / wrap add / composite carry / subtract / soft threshold / shift / checksum

Extension modules (link on demand):

  • Trie index: 256-ary ultrametric prefix tree with prefix pruning + candidate collection
  • WTA / LRU engine: Winner-Take-All competitive learning + LRU template capacity management + morphology + Bitonic sorting network
  • Storage reliability: RLE compression / Merkle tree / RS(8,6) error correction / TMR triple modular redundancy / Flash fault tolerance
  • Network distributed: UART framing / COBS zero-byte elimination / HC16 Lamport clock / watchdog
  • Plugin system: NORMATIVE / EXTENSION / HYBRID plugins + dynamic loading + capability mask
  • Projection sandbox: HC ↔ float64/float128 bidirectional projection / division / gradient / scaling
  • SIMD batch: SSE2 batch add / compare / soft threshold (automatic scalar fallback)
  • DC bridge: HC ↔ decimal fixed-point ↔ double conversion extension
  • C++ RAII: HC8/HC16/HC32/HC64 class wrappers + operator overloading

v0.2 Neural Network Extension: Quantized Inference Specialized

Modules:

  • HC8 Neural Network: Symmetric quantization (scale+offset 128) int8×int8→int32 matmul / integer ReLU / UFP-1 & UFP-2 residual precision / SBE semantic block encoding
  • HC16 Neural Network: int16 signed direct storage matmul / 256× precision over HC8
  • HC16MS Multi-View: 2-byte int16 container → HC16 / HC8 / HC4 precision zero-copy switching / 3× storage compression
  • HC4 PSHUFB: int4×int4→int8 lookup table multiplication (16×16 LUT 256 bytes) / PSHUFB 32 products per batch
  • col2im: im2col reverse scatter-add / BC<128 bypass libomp overhead
  • HC8 Coproduct: HC8 coproduct operation extension

SBE Semantic Block Encoding family (key v0.2 deliverable):

  • Per-block quantization + VNNI kernel
  • Smoothing fusion: per-row mean shift + quantization
  • Per-channel quantization for CNN precision
  • Conv2d forward fusion (im2col + SBE matmul + bias add all in C layer)
  • Triple-int8 scaling: float → 3 int8 components (24-bit precision)
  • Orthogonal multi-view matmul

Quick Links

  • Core Concepts — Ultrametric, HC types, v0.1/v0.2 modules
  • Quick Start — Build, test, first program
  • Examples — C/C++/Python code samples
  • FAQ — Common questions and troubleshooting

Repository


中文

SGN(Signed Globular Number)是一套基于超度量空间的自定义数值类型系统。核心思想:用多层字节数组表示数,每层是独立的进制位(256 叉 / 65536 叉),天然构成超度量树结构——字典序即超度量距离,前缀即超度量球。

SGN 同时服务于两个世界:

  • 嵌入式 / MCU 场景:最小化链接体积(仅需 hc.c + hc8.c,无第三方依赖),用于时间变量、精确记录、Flash 容错。
  • PC 端神经网络量化推理:围绕 HC8/HC16 核心构建 int4/int8/int16 低精度整数矩阵乘路径,支持 AVX2 / FMA / AVX-VNNI 指令集加速。

版本速览

仓库内包含两个独立版本目录,可按需使用:

版本 定位 语言标准 构建目标 维护状态
v0.1 SGN 主线完整实现 C11 / C++11 静态库 sgn_core / sgn_ext / sgn + 测试 主线(推荐研究/扩展使用)
v0.2 HC 神经网络运算扩展模块(C 末尾版) C11 / C++23 pybind11 模块 sgn_c_engine*.so C 语言实现末尾版,不再追加新功能

核心 HC 类型(HC = 层级小数)

所有 HC 变体共享同一张编译期元数据表(hc_meta_t / hc_meta_get()),消除硬编码层数/基数。

类型 结构 层 × 位宽 小数精度 数值范围 典型用途
HC8 uint8_t v[6](紧缩 6 字节) 6 × 8 bit 48 bit [0, 256) 嵌入式主力、对称量化 int8 矩阵乘、HC4 非对称拆分基础
HC16 uint16_t v[4](8 字节) 4 × 16 bit 64 bit [0, 65536) 时间戳、高精度存储、int16×int16 量化矩阵乘(精度比 HC8 高 256 倍)
HC32 uint32_t v[3](12 字节) 3 × 32 bit 96 bit [0, 2³²) PC 端高精度扩展(仅 v0.1 提供)
HC64 uint64_t v[2](16 字节) 2 × 64 bit 128 bit [0, 2⁶⁴) 高精度实验扩展(仅 v0.1 提供)

v0.1 主线:完整技术栈

本职工作(HC 之所以存在):

  • 时间变量:合体模式 level × 256 + HC 担任 SGN 时间变量本身
  • 精确记录:跨平台字节级一致、可压缩、可校验的持久化格式
  • 超度量比较与索引:字典序 = 超度量距离,Trie 前缀 = 超度量球
  • 基础数值运算:饱和加 / 回绕加 / 合体进位 / 减法 / 软阈值 / 移位 / 校验和

扩展能力(按需链接):

  • Trie 索引:256 叉超度量前缀树,前缀剪枝 + 候选收集
  • WTA / LRU 引擎:Winner-Take-All 竞争学习 + LRU 模板库容量管理
  • 存储可靠性:RLE 压缩 / Merkle 树 / RS(8,6) 纠错 / TMR 三模冗余
  • 网络分布式:UART 帧 / COBS 零字节消除 / HC16 Lamport 时钟 / 看门狗
  • 插件系统:NORMATIVE / EXTENSION / HYBRID 三类插件 + 动态加载
  • 投影沙盒:HC ↔ float64/float128 双向投影 / 除法 / 梯度 / 缩放
  • SIMD 批量:SSE2 批量加 / 比较 / 软阈值(自动标量回退)
  • C++ RAII:HC8/HC16/HC32/HC64 类包装 + 运算符重载

v0.2 神经网络扩展:量化推理专用

模块清单:

  • HC8 神经网络:对称量化(scale+offset 128)int8×int8→int32 矩阵乘 / 整数 ReLU / SBE 语义块编码
  • HC16 神经网络:int16 有符号直接存储矩阵乘 / 精度比 HC8 提升 256 倍
  • HC16MS 多视角:2 字节 int16 容器 → HC16 / HC8 / HC4 三档精度零拷贝切换
  • HC4 PSHUFB:int4×int4→int8 查表乘法(16×16 LUT)/ 每批 32 乘积
  • col2im:im2col 反向 scatter-add / 绕过 libomp 开销
  • SBE 语义块编码:分块量化 + VNNI kernel / Smoothing 融合 / Conv2d 前向融合 / Triple-int8

快速链接

仓库