138 lines
8.2 KiB
Swift
138 lines
8.2 KiB
Swift
import Foundation
|
|
|
|
/// 模型文件清单中的一项:相对模型目录的路径 + 预期字节数(用于总进度计算与下载后大小校验)。
|
|
struct ModelFile: Equatable, Sendable {
|
|
let path: String
|
|
let bytes: Int
|
|
}
|
|
|
|
/// 硬编码的模型文件清单与下载源。
|
|
/// 只列加载必需的功能文件,排除 README.md / .gitattributes(省下载)。
|
|
/// 字节数与服务器素材逐一核对一致,见
|
|
/// docs/superpowers/specs/2026-05-29-model-download-design.md 附录 A。
|
|
nonisolated enum ModelManifest {
|
|
/// 自建 Caddy 静态镜像。现仅 `.vl`(已废弃,不再下载)回退用;主模型 / MLX 兜底已切魔搭官方。
|
|
/// 备选纯 IP(需 App 端 ATS 例外): http://101.132.124.52:5244/
|
|
static let baseURL = URL(string: "https://file.myv0.com/")!
|
|
|
|
/// 各模型的魔搭(ModelScope)官方仓库 id。大陆可达;`resolve/master` 会 302 跳阿里 OSS,
|
|
/// OSS 支持 Range,断点续传穿透重定向(实测 206 + 精确分片)。各文件大小与本清单逐一核对一致(2026-06 实测)。
|
|
/// 注意组织名:MNN 模型在魔搭组织为 `MNN`(非 HuggingFace 的 taobao-mnn);MLX 沿用 mlx-community。
|
|
static func modelScopeRepo(for kind: ModelKind) -> String? {
|
|
switch kind {
|
|
case .llm: return "mlx-community/gemma-4-e2b-it-4bit" // 主模型,大陆可达
|
|
case .mnnLLM: return "MNN/Qwen3.5-2B-MNN" // 已停用,保留源备查
|
|
case .vl: return nil // 已废弃,不再下载 / 分发,不提供魔搭源
|
|
}
|
|
}
|
|
|
|
/// hf-mirror(hf-mirror.com)回退源仓库 id。仅 mlx-community 系与 HF 同 org/name 可直接镜像;
|
|
/// MNN 源在 HuggingFace 组织名不同(taobao-mnn),且已停用,不配回退。revision 用 HF 的 `main`。
|
|
static func huggingFaceMirrorRepo(for kind: ModelKind) -> String? {
|
|
switch kind {
|
|
case .llm: return kind.huggingFaceRepo // mlx-community/gemma-4-e2b-it-4bit
|
|
case .vl, .mnnLLM: return nil
|
|
}
|
|
}
|
|
|
|
static func files(for kind: ModelKind) -> [ModelFile] {
|
|
switch kind {
|
|
case .llm:
|
|
// gemma-4-e2b-it-4bit:Gemma 4 E2B(instruct,4bit)。config.json 顶层 model_type = "gemma4",
|
|
// 经 LLMModelFactory 的 "gemma4" 路径加载(mlx-swift-lm ≥3.31.4 已注册;Gemma4Model 只取
|
|
// language_model 权重,跳过视觉/音频)。e2b 检查点无视觉权重(视觉在 gemma4_unified 12B)。
|
|
// 字节数取自 ModelScope mlx-community/gemma-4-e2b-it-4bit 仓库实际 blob 大小
|
|
//(Range 请求 Content-Range 总长,2026-07-14 与魔搭 / hf-mirror 双源核对一致)。
|
|
// 上游 2026-07 中旬更新过一次 checkpoint(权重 3.581G→3.551G,config/tokenizer_config/
|
|
// processor_config/index 同步变化;model_type 仍 gemma4、4bit,加载路径不受影响)——
|
|
// 清单值过期会让 FileDownloader 校验 sizeMismatch,表现为「下载失败」,更新须双源重新核对。
|
|
// 排除 README.md / .gitattributes / configuration.json(后者为 ModelScope 元数据,MLX 加载
|
|
// 用不到)。Gemma 4 布局与 3n 不同:无 tokenizer.model / special_tokens_map.json,
|
|
// tokenizer_config.json 精简(chat template 独立在 chat_template.jinja),新增
|
|
// processor_config.json。全部运行文件按上游 mlx-swift-lm 预置仓库快照镜像,避免漏文件。
|
|
return [
|
|
ModelFile(path: "config.json", bytes: 6_395),
|
|
ModelFile(path: "generation_config.json", bytes: 208),
|
|
ModelFile(path: "model.safetensors", bytes: 3_550_670_554),
|
|
ModelFile(path: "model.safetensors.index.json", bytes: 218_323),
|
|
ModelFile(path: "tokenizer.json", bytes: 32_169_626),
|
|
ModelFile(path: "tokenizer_config.json", bytes: 2_740),
|
|
ModelFile(path: "chat_template.jinja", bytes: 17_336),
|
|
ModelFile(path: "processor_config.json", bytes: 1_316),
|
|
]
|
|
case .vl:
|
|
// Qwen3-VL-4B-Instruct-4bit:字节数取自 mlx-community 仓库实际 blob 大小
|
|
// (HF API blobs=true,2026-05 核对),用于总进度计算与下载后大小校验。
|
|
// 策略:完整镜像仓库的全部运行文件(仅排除 README.md / .gitattributes),
|
|
// 与标准 mlx-vlm 加载环境保持一致,避免漏文件导致 VLMModelFactory 加载失败。
|
|
// 同时带两份 chat_template(.json 旧约定 + .jinja 新约定)与 video 预处理配置,
|
|
// 以兼容不同版本 swift-transformers / Qwen3VLProcessor 的读取路径。
|
|
return [
|
|
ModelFile(path: "config.json", bytes: 7_137),
|
|
ModelFile(path: "model.safetensors", bytes: 3_093_767_283),
|
|
ModelFile(path: "model.safetensors.index.json", bytes: 64_742),
|
|
ModelFile(path: "tokenizer.json", bytes: 11_422_654),
|
|
ModelFile(path: "tokenizer_config.json", bytes: 5_445),
|
|
ModelFile(path: "vocab.json", bytes: 2_776_833),
|
|
ModelFile(path: "merges.txt", bytes: 1_671_853),
|
|
ModelFile(path: "special_tokens_map.json", bytes: 613),
|
|
ModelFile(path: "added_tokens.json", bytes: 707),
|
|
ModelFile(path: "generation_config.json", bytes: 269),
|
|
ModelFile(path: "chat_template.json", bytes: 5_502),
|
|
ModelFile(path: "chat_template.jinja", bytes: 5_292),
|
|
ModelFile(path: "preprocessor_config.json", bytes: 782),
|
|
ModelFile(path: "video_preprocessor_config.json", bytes: 817),
|
|
]
|
|
case .mnnLLM:
|
|
// taobao-mnn/Qwen3.5-2B-MNN 预转换 MNN 格式(HF API 实测,2026-06)。
|
|
// 运行时必需:config.json(MNN llm 配置)+ llm_config.json(超参)+ llm.mnn(图)
|
|
// + llm.mnn.weight(量化权重 ~1.1GB)+ tokenizer.txt + visual.mnn(多模态,文本路径不用但配置含 mllm)。
|
|
// 排除 README/.gitattributes 与可读 dump(llm.mnn.json / export_args.json)。
|
|
return [
|
|
ModelFile(path: "config.json", bytes: 652),
|
|
ModelFile(path: "llm_config.json", bytes: 8_692),
|
|
ModelFile(path: "llm.mnn", bytes: 2_148_136),
|
|
ModelFile(path: "llm.mnn.weight", bytes: 1_176_647_702),
|
|
ModelFile(path: "tokenizer.txt", bytes: 6_465_727),
|
|
ModelFile(path: "visual.mnn", bytes: 488_096),
|
|
]
|
|
}
|
|
}
|
|
|
|
static func totalBytes(for kind: ModelKind) -> Int {
|
|
files(for: kind).reduce(0) { $0 + $1.bytes }
|
|
}
|
|
|
|
/// 单个文件的**首选**下载 URL(= 候选源列表的第一项)。主模型走魔搭官方 `resolve/master`。
|
|
/// 下载编排应改用 `fileURLs` 拿到「魔搭优先 + hf-mirror 回退」的有序候选源;此方法保留给
|
|
/// 只需首选源的调用方(如测试断言主源路径)。
|
|
static func fileURL(for kind: ModelKind, file: ModelFile) -> URL {
|
|
fileURLs(for: kind, file: file)[0]
|
|
}
|
|
|
|
/// 单个文件的有序候选下载源:**魔搭(ModelScope,resolve/master)优先,hf-mirror(resolve/main)回退**。
|
|
/// 两源同为 mlx-community 仓库同一份权重,字节完全一致 —— 断点续传的 `.part` 可跨源复用
|
|
/// (换源时发 `Range: bytes=offset-` 继续,不必重下)。用户选择:魔搭优先 + hf-mirror 回退。
|
|
/// 已废弃的 `.vl`(无魔搭 / 无 hf 回退)落到自建镜像 `baseURL`(实际不会被下载)。
|
|
static func fileURLs(for kind: ModelKind, file: ModelFile) -> [URL] {
|
|
var urls: [URL] = []
|
|
if let repo = modelScopeRepo(for: kind) {
|
|
urls.append(
|
|
URL(string: "https://modelscope.cn/models/\(repo)/resolve/master/")!
|
|
.appendingPathComponent(file.path))
|
|
}
|
|
if let hfRepo = huggingFaceMirrorRepo(for: kind) {
|
|
urls.append(
|
|
URL(string: "https://hf-mirror.com/\(hfRepo)/resolve/main/")!
|
|
.appendingPathComponent(file.path))
|
|
}
|
|
if urls.isEmpty {
|
|
urls.append(
|
|
baseURL
|
|
.appendingPathComponent(kind.rawValue, isDirectory: true)
|
|
.appendingPathComponent(file.path))
|
|
}
|
|
return urls
|
|
}
|
|
}
|