Skip to content

Examples

zhuguabngya edited this page Aug 3, 2026 · 2 revisions

Examples / 示例代码

English | 中文


English

v0.1 Examples

C Example: Basic Arithmetic

#include "hc/hc8.h"
#include "hc/hc16.h"
#include <stdio.h>

int main(void) {
    /* HC8 operations */
    hc8_t a = HC8_FROM_DOUBLE(3.14);
    hc8_t b = HC8_FROM_DOUBLE(2.0);
    hc8_t sum = hc8_add(a, b);
    hc8_t diff = hc8_sub(a, b);

    printf("HC8: 3.14 + 2.0 = %f\n", HC8_TO_DOUBLE(sum));
    printf("HC8: 3.14 - 2.0 = %f\n", HC8_TO_DOUBLE(diff));

    /* HC16 for higher precision */
    hc16_t x = HC16_FROM_DOUBLE(100.5);
    hc16_t y = HC16_FROM_DOUBLE(50.25);
    hc16_t result = hc16_add(x, y);

    printf("HC16: 100.5 + 50.25 = %f\n", HC16_TO_DOUBLE(result));

    return 0;
}

C++ Example: HPDC Extension

#include "hc/hpdc_cpp.hpp"
#include <iostream>

int main() {
    // Use HPDC (Hierarchical Prefix-DC) extension
    hpdc::Engine engine;

    // Configure precision
    engine.set_level(hpdc::Level::FINE);

    // Run WTA competition
    auto winner = engine.compete(input_data);
    std::cout << "Winner: " << winner.id
              << " Score: " << winner.score << std::endl;

    return 0;
}

Python Example (v0.1)

import pysgn

# Create HC8 values
a = pysgn.HC8(1.5)
b = pysgn.HC8(2.5)

# Arithmetic
result = a + b
print(f"1.5 + 2.5 = {float(result)}")

# Use HC64 for high precision
x = pysgn.HC64(3.141592653589793)
y = pysgn.HC64(2.718281828459045)
print(f"pi + e = {float(x + y)}")

Soft Threshold Example

#include "hc/hc8.h"
#include <stdio.h>

int main(void) {
    hc8_t x = HC8_FROM_DOUBLE(10.0);
    hc8_t threshold = HC8_FROM_DOUBLE(3.0);

    /* Soft threshold: max(0, x - L) */
    hc8_t result = HC8_SOFT_THRESH(x, threshold);
    printf("soft_thresh(10.0, 3.0) = %f\n", HC8_TO_DOUBLE(result));
    /* Output: 7.0 */

    return 0;
}

v0.2 Examples

SBE Quantized Matmul (Python)

import numpy as np
import sgn_c_engine as sgn

# Input: (M, K) float32
x = np.random.randn(16, 256).astype(np.float32)
# Weight: (N, K) float32
w = np.random.randn(32, 256).astype(np.float32)

# Quantize weights with SBE (per-block along K dimension)
w_signed, w_sum_b, w_scales = sgn.sbe_quantize_weight_blocks(
    w, k_block=64
)

# SBE matmul: int8 VNNI kernel
y = sgn.sbe_matmul(x, w_signed, w_sum_b, w_scales, k_block=64)
# y ≈ x @ w.T  (float32)

Smoothed SBE Matmul (Higher Precision)

# Smoothing fusion: per-row mean shift before quantization
y = sgn.sbe_matmul_smoothed(x, w_signed, w_sum_b, w_scales, k_block=64)

Per-Channel Quantized Matmul (CNN)

w_signed_pc, w_sum_b_pc, w_scales_pc = sgn.sbe_quantize_weight_blocks_perchannel(
    w, k_block=64
)
y = sgn.sbe_matmul_perchannel(x, w_signed_pc, w_sum_b_pc, w_scales_pc, k_block=64)

Fused Conv2d Forward

# x: (B, C_in, H, W) float32
# w: (C_out, C_in, kh, kw) float32
y = sgn.sbe_conv2d_forward(
    x, w_signed, w_sum_b, w_scales,
    B, C_in, H, W,
    C_out, kh, kw, stride, padding,
    groups=1, k_block=64,
    bias=None,           # or np.array of shape (C_out,)
)
# y: (B, C_out, H_out, W_out) float32

Triple-int8 Scaling

# Convert float → 3 int8 components (24-bit precision)
# Useful for WEF+Triple training pipeline
triple = sgn.sbe_rescale_to_triple(x_float)

HC16 Neural Network Matmul

y = sgn.hc16_matmul(x_float, w_float)
# int16×int16→int32, ~256× precision over HC8 path

HC16MS Multi-View Zero-Copy

# Store once, read as HC16 / HC8 / HC4 without copying
y_hc16 = sgn.hc16ms_matmul(x_float, w_float, dtype='hc16')
y_hc8  = sgn.hc16ms_matmul(x_float, w_float, dtype='hc8')
y_hc4  = sgn.hc16ms_matmul(x_float, w_float, dtype='hc4')

HC4 PSHUFB Lookup Table Matmul

y = sgn.hc4_pshufb_matmul(x_float, w_float)
# int4×int4→int8 via _mm256_shuffle_epi8

col2im (Reverse of im2col)

import numpy as np

# cols: (C*kh*kw, N*H_out*W_out) float32
cols = np.random.randn(3*3*3, 4*4).astype(np.float32)
image = np.zeros((1, 3, 6, 6), dtype=np.float32)

# Scatter-add overlapping regions
sgn.col2im_add(cols, image, kh=3, kw=3, stride=1, padding=0)

中文

v0.1 示例

C 示例:基本运算

#include "hc/hc8.h"
#include "hc/hc16.h"
#include <stdio.h>

int main(void) {
    /* HC8 运算 */
    hc8_t a = HC8_FROM_DOUBLE(3.14);
    hc8_t b = HC8_FROM_DOUBLE(2.0);
    hc8_t sum = hc8_add(a, b);
    hc8_t diff = hc8_sub(a, b);

    printf("HC8: 3.14 + 2.0 = %f\n", HC8_TO_DOUBLE(sum));
    printf("HC8: 3.14 - 2.0 = %f\n", HC8_TO_DOUBLE(diff));

    /* HC16 更高精度 */
    hc16_t x = HC16_FROM_DOUBLE(100.5);
    hc16_t y = HC16_FROM_DOUBLE(50.25);
    hc16_t result = hc16_add(x, y);

    printf("HC16: 100.5 + 50.25 = %f\n", HC16_TO_DOUBLE(result));

    return 0;
}

C++ 示例:HPDC 扩展

#include "hc/hpdc_cpp.hpp"
#include <iostream>

int main() {
    // 使用 HPDC(层级前缀-DC)扩展
    hpdc::Engine engine;

    // 配置精度
    engine.set_level(hpdc::Level::FINE);

    // 运行 WTA 竞争
    auto winner = engine.compete(input_data);
    std::cout << "获胜者: " << winner.id
              << " 分数: " << winner.score << std::endl;

    return 0;
}

Python 示例(v0.1)

import pysgn

# 创建 HC8 值
a = pysgn.HC8(1.5)
b = pysgn.HC8(2.5)

# 算术运算
result = a + b
print(f"1.5 + 2.5 = {float(result)}")

# 使用 HC64 高精度
x = pysgn.HC64(3.141592653589793)
y = pysgn.HC64(2.718281828459045)
print(f"pi + e = {float(x + y)}")

软阈值示例

#include "hc/hc8.h"
#include <stdio.h>

int main(void) {
    hc8_t x = HC8_FROM_DOUBLE(10.0);
    hc8_t threshold = HC8_FROM_DOUBLE(3.0);

    /* 软阈值: max(0, x - L) */
    hc8_t result = HC8_SOFT_THRESH(x, threshold);
    printf("soft_thresh(10.0, 3.0) = %f\n", HC8_TO_DOUBLE(result));
    /* 输出: 7.0 */

    return 0;
}

v0.2 示例

SBE 量化矩阵乘(Python)

import numpy as np
import sgn_c_engine as sgn

# 输入: (M, K) float32
x = np.random.randn(16, 256).astype(np.float32)
# 权重: (N, K) float32
w = np.random.randn(32, 256).astype(np.float32)

# SBE 分块量化权重
w_signed, w_sum_b, w_scales = sgn.sbe_quantize_weight_blocks(
    w, k_block=64
)

# SBE 矩阵乘:int8 VNNI kernel
y = sgn.sbe_matmul(x, w_signed, w_sum_b, w_scales, k_block=64)
# y ≈ x @ w.T  (float32)

Smoothing 融合矩阵乘(更高精度)

y = sgn.sbe_matmul_smoothed(x, w_signed, w_sum_b, w_scales, k_block=64)

逐通道量化矩阵乘(CNN)

w_signed_pc, w_sum_b_pc, w_scales_pc = sgn.sbe_quantize_weight_blocks_perchannel(
    w, k_block=64
)
y = sgn.sbe_matmul_perchannel(x, w_signed_pc, w_sum_b_pc, w_scales_pc, k_block=64)

Conv2d 前向融合

# x: (B, C_in, H, W) float32
# w: (C_out, C_in, kh, kw) float32
y = sgn.sbe_conv2d_forward(
    x, w_signed, w_sum_b, w_scales,
    B, C_in, H, W,
    C_out, kh, kw, stride, padding,
    groups=1, k_block=64,
    bias=None,           # 或形状为 (C_out,) 的 np.array
)
# y: (B, C_out, H_out, W_out) float32

Triple-int8 缩放

# float → 3 个 int8 分量(24-bit 精度)
triple = sgn.sbe_rescale_to_triple(x_float)

HC16 神经网络矩阵乘

y = sgn.hc16_matmul(x_float, w_float)
# int16×int16→int32,精度比 HC8 路径高约 256 倍

HC16MS 多视角零拷贝

# 一次存储,以 HC16 / HC8 / HC4 读取,无需拷贝
y_hc16 = sgn.hc16ms_matmul(x_float, w_float, dtype='hc16')
y_hc8  = sgn.hc16ms_matmul(x_float, w_float, dtype='hc8')
y_hc4  = sgn.hc16ms_matmul(x_float, w_float, dtype='hc4')

HC4 PSHUFB 查表矩阵乘

y = sgn.hc4_pshufb_matmul(x_float, w_float)
# int4×int4→int8 通过 _mm256_shuffle_epi8

col2im(im2col 反向操作)

import numpy as np

# cols: (C*kh*kw, N*H_out*W_out) float32
cols = np.random.randn(3*3*3, 4*4).astype(np.float32)
image = np.zeros((1, 3, 6, 6), dtype=np.float32)

# 累加重叠区域
sgn.col2im_add(cols, image, kh=3, kw=3, stride=1, padding=0)