-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
zhuguabngya edited this page Aug 3, 2026
·
2 revisions
#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;
}#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;
}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)}")#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;
}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)# Smoothing fusion: per-row mean shift before quantization
y = sgn.sbe_matmul_smoothed(x, w_signed, w_sum_b, w_scales, k_block=64)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)# 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# Convert float → 3 int8 components (24-bit precision)
# Useful for WEF+Triple training pipeline
triple = sgn.sbe_rescale_to_triple(x_float)y = sgn.hc16_matmul(x_float, w_float)
# int16×int16→int32, ~256× precision over HC8 path# 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')y = sgn.hc4_pshufb_matmul(x_float, w_float)
# int4×int4→int8 via _mm256_shuffle_epi8import 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)#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;
}#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;
}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;
}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)y = sgn.sbe_matmul_smoothed(x, w_signed, w_sum_b, w_scales, k_block=64)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)# 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# float → 3 个 int8 分量(24-bit 精度)
triple = sgn.sbe_rescale_to_triple(x_float)y = sgn.hc16_matmul(x_float, w_float)
# int16×int16→int32,精度比 HC8 路径高约 256 倍# 一次存储,以 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')y = sgn.hc4_pshufb_matmul(x_float, w_float)
# int4×int4→int8 通过 _mm256_shuffle_epi8import 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)