MoonBit × HuggingFace 生态栈:与 huggingface_hub 兼容的 Hub 客户端、张量推理内核、GPT-2 推理与通用 ONNX 图执行器。
| 子包 | 定位 | 状态 | 验证方式 |
|---|---|---|---|
| QUHONGYE-gif/modelhub | HuggingFace Hub 客户端(下载/缓存/认证/离线/镜像) | ✅ | 与 huggingface_hub 1.31 缓存树逐文件 parity(本地 mock + 真实 hf-mirror,含 LFS) |
| moonkit/nn | 张量与推理内核(GEMM/激活/归一化/注意力/KV cache) | ✅ | numpy golden 逐元素对照 |
| moonkit/transformers | GPT-2 级模型推理(safetensors/前向/生成) | ✅ | numpy golden + 真实 tiny-random-gpt2 端到端生成 |
| moonkit/onnx | 通用 ONNX 图解释执行器(11 算子) | ✅ | numpy golden + 官方 ONNX backend test 子集 |
conda activate moonbit
moon test --target native # 全部测试(31 个)
moon test # wasm 目标(21 个)bash scripts/run_examples.sh # 张量 / ONNX / Hub 三个示例(Hub 用本地 mock)
bash scripts/demo_gpt2.sh # 端到端:拉模型 → 分词 → 生成 → 解码(需网络)
bash scripts/bench.sh # 基准(matmul / attention / GPT-2 / ONNX)
python scripts/parity_test.py # 与 Python huggingface_hub 的 parity 验证// 1) 拉模型(HF 兼容缓存)
///|
let config = @modelhub.HubConfig::new("https://hf-mirror.com", "/tmp/hf-cache")
///|
let path = @modelhub.hf_hub_download(
"openai-community/gpt2", "config.json", config,
)
///|
let snapshot = @modelhub.snapshot_download(
"sshleifer/tiny-gpt2",
config,
None,
None,
)
// 2) 张量与算子
///|
let y = @nn.Tensor::randn([64, 64], 42UL).matmul(
@nn.Tensor::randn([64, 64], 7UL),
)
///|
let probs = y.softmax()
// 3) GPT-2 推理(safetensors 权重 + KV cache 生成)
///|
let model = @transformers.Gpt2Model::load(
cfg,
@transformers.parse_safetensors(bytes),
)
///|
let text_ids = @transformers.Generator::new(model).generate(prompt_ids, 20, 0.0)
// 4) ONNX 图执行
///|
let graph = @onnx.parse_model(onnx_bytes)
///|
let outputs = @onnx.run_model(graph, inputs)pub struct HubConfig {
endpoint : String
cache_dir : String
revision : String
subfolder : String
token : String?
offline : Bool
}fn default_cache_dir() -> Stringasync fn download_resumable(url : String, token : String?, cache_root : String, repo_id : String) -> String raise HubErrorasync fn get_follow(url : String, headers : Map[CaseInsensitiveString, String], max_redirects : Int) -> (Int, Map[CaseInsensitiveString, String], Bytes) raise HubErrorasync fn get_stream_follow(url : String, headers : Map[CaseInsensitiveString, String], max_redirects : Int) -> (Int, String?, Client) raise HubErrorasync fn load_default_token() -> String?fn matches_glob(filename : String, pattern : String) -> Boolfn refs_path(cache_root : String, repo_id : String, revision : String) -> Stringfn repo_cache_dir(cache_root : String, repo_id : String) -> Stringasync fn resolve_sha(endpoint : String, repo_id : String, revision : String, cache_dir : String, token : String?, offline : Bool) -> String raise HubErrorfn snapshot_dir(cache_root : String, repo_id : String, commit_sha : String) -> StringInstall
Download zipMoonBit × HuggingFace 生态栈:与 huggingface_hub 兼容的 Hub 客户端、张量推理内核、GPT-2 推理与通用 ONNX 图执行器。