Universal Advanced Model Format 📦 UAMF v2.0
C++17
跨平台
ZSTD/LZ4/ZLIB
内存映射
异步I/O
一个高性能、可扩展的通用模型格式,专为深度学习模型部署优化,支持多种压缩算法、内存映射、分布式存储和版本控制。
📖 概述
UAMF (Universal Advanced Model Format) 是一个用C++17实现的通用模型格式库,旨在解决深度学习模型在存储、传输和部署中的各种挑战。它提供了统一的接口来管理模型权重、元数据和运行时配置,支持多种压缩算法、内存映射、异步I/O和分布式存储。
🎯 设计目标
统一的模型格式标准,支持PyTorch、ONNX、TensorRT等多种框架的模型转换。
⚡ 高性能
内存映射文件、异步I/O、智能缓存、多线程并行处理。
🔧 可扩展
模块化架构,支持自定义压缩算法、加密方法和数据格式。
✨ 核心特性
- 多框架支持 — 支持PyTorch、ONNX、TensorRT、TensorFlow等主流框架模型转换
- 智能压缩 — ZSTD、LZ4、ZLIB多种压缩算法,自动检测最优压缩策略
- 内存映射I/O — 零拷贝读取,大幅提升大模型加载速度
- 异步I/O — 多线程异步读写,充分利用现代硬件性能
- 智能缓存 — LRU缓存策略,热数据预热,访问频率追踪
- 分布式存储 — 支持模型分片存储,跨多文件分布式加载
- 版本控制 — 内置版本控制系统,支持快照、分支、合并
- 查询引擎 — 基于张量属性(大小、类型、形状)的高级查询
- 模型部署 — 针对CPU/GPU/Edge TPU等平台的自动优化部署
- 性能监控 — 实时统计I/O性能、缓存命中率、操作延迟
🏗️ 架构设计
⚡ 快速开始
编译与安装
# 克隆仓库
git clone https://github.com/example/uamf.git
cd uamf
# 创建构建目录
mkdir build && cd build
# CMake配置
cmake .. -DCMAKE_BUILD_TYPE=Release \
-DUAMF_USE_ZSTD=ON \
-DUAMF_USE_LZ4=ON \
-DUAMF_USE_OPENMP=ON
# 编译
make -j4
# 安装
sudo make install
基本使用示例
#include "uamf/core.h"
using namespace uamf;
int main() {
// 创建UAMF文件
auto file = CreateUAMFFile();
// 模型元数据
ModelMetadata metadata;
metadata.model_name = "ResNet50";
metadata.model_version = "1.0";
metadata.author = "AI Research Team";
metadata.total_parameters = 25557032;
// 创建文件
if (!file->Create("model.uamf", metadata)) {
std::cerr << "Failed to create file" << std::endl;
return 1;
}
// 添加张量
std::vector weights(10000, 0.1f);
file->AddTensor("conv1.weight", weights.data(), {64, 3, 7, 7},
DT_FLOAT32, COMPRESS_ZSTD);
// 保存文件
file->Save();
file->Close();
// 重新打开读取
auto file2 = CreateUAMFFile();
file2->Open("model.uamf", true);
// 读取张量
auto info = file2->GetTensorInfo("conv1.weight");
std::vector read_weights(info.uncompressed_size / sizeof(float));
file2->GetTensor("conv1.weight", read_weights.data(), read_weights.size() * sizeof(float));
file2->Close();
return 0;
}
📚 API 参考
核心类: UAMFFile
class UAMFFile {
public:
// 文件操作
bool Create(const std::string& filename, const ModelMetadata& metadata = {});
bool Open(const std::string& filename, bool read_only = false);
bool Close();
bool Save();
// 张量操作
uint64_t AddTensor(const std::string& name, const void* data,
const std::vector& shape, DataType dtype,
CompressionType compression = COMPRESS_AUTO);
bool GetTensor(const std::string& name, void* data, size_t max_size);
bool RemoveTensor(const std::string& name);
bool UpdateTensor(const std::string& name, const void* new_data);
// 查询
std::vector ListTensors() const;
TensorInfo GetTensorInfo(const std::string& name) const;
// 元数据
bool SetMetadata(const ModelMetadata& metadata);
ModelMetadata GetMetadata() const;
// 性能统计
PerformanceStats GetPerformanceStats() const;
};
数据类型枚举
| 枚举值 | 说明 | 字节大小 |
|---|---|---|
| DT_FLOAT32 | 32位浮点数 | 4 |
| DT_FLOAT16 | 16位浮点数 | 2 |
| DT_INT8 | 8位有符号整数 | 1 |
| DT_INT16 | 16位有符号整数 | 2 |
| DT_INT32 | 32位有符号整数 | 4 |
| DT_INT64 | 64位有符号整数 | 8 |
| DT_UINT8 | 8位无符号整数 | 1 |
| DT_BFLOAT16 | Brain浮点数 | 2 |
压缩类型枚举
| 枚举值 | 说明 | 适用场景 |
|---|---|---|
| COMPRESS_NONE | 无压缩 | 小数据、高频访问 |
| COMPRESS_LZ4 | LZ4压缩 | 平衡速度与压缩率 |
| COMPRESS_ZSTD | ZSTD压缩 | 高压缩率 |
| COMPRESS_AUTO | 自动选择 | 根据数据类型自动判断 |
| COMPRESS_QUANT8 | 8位量化 | 浮点模型量化 |
| COMPRESS_SPARSE | 稀疏压缩 | 稀疏权重张量 |
🗜️ 压缩系统
UAMF内置多种压缩算法,并支持智能压缩策略选择:
压缩算法对比
| 算法 | 压缩率 | 压缩速度 | 解压速度 | 适用场景 |
|---|---|---|---|---|
| LZ4 | ~40-60% | 极快 | 极快 | 实时加载 |
| ZSTD | ~30-50% | 快 | 快 | 存储优化 |
| ZLIB | ~35-55% | 中 | 中 | 兼容性 |
| 量化(INT8) | 25% | 中 | 极快 | 推理部署 |
| 稀疏压缩 | 10-30% | 中 | 快 | 剪枝模型 |
💡 提示:使用
COMPRESS_AUTO 让库自动选择最优压缩方法,基于数据类型和大小动态决策。🌊 流式处理
UAMF支持流式读写,适用于处理超大模型:
// 流式读取大型模型
auto file = CreateUAMFFile();
file->Open("large_model.uamf", true);
// 分批读取张量
for (const auto& tensor_name : file->ListTensors()) {
auto info = file->GetTensorInfo(tensor_name);
// 分块读取大张量
const size_t chunk_size = 1024 * 1024; // 1MB chunks
std::vector buffer(chunk_size);
// 使用异步预取
file->PrefetchTensors({tensor_name});
// 获取数据
file->GetTensor(tensor_name, buffer.data(), info.uncompressed_size);
}
🌐 分布式存储支持
DistributedUAMF 类支持将模型分片存储在多个文件中:
// 创建分布式模型
DistributedUAMF distributed(4); // 4个分片
ModelMetadata metadata;
metadata.model_name = "DistributedModel";
if (distributed.Create("/path/to/model", metadata)) {
// 张量会自动分布到不同分片
distributed.AddTensor("layer1.weight", data1, shape1, DT_FLOAT32);
distributed.AddTensor("layer2.weight", data2, shape2, DT_FLOAT32);
// 读取时自动定位
std::vector read_data;
distributed.GetTensor("layer1.weight", read_data.data(), read_data.size());
distributed.Close();
}
📌 内置版本控制系统
UAMFVersionControl vc("/path/to/repo");
vc.Init();
// 提交新版本
vc.Commit("model_v1.uamf", "Initial version");
// 修改模型后再次提交
vc.Commit("model_v1.uamf", "Added quantization");
// 查看历史
auto history = vc.GetHistory("model_v1.uamf");
// 回滚到指定版本
vc.Checkout("model_v1.uamf", history[0].version_id);
⚡ 性能测试结果
测试环境:Intel i7-12700H, 32GB RAM, NVMe SSD, Ubuntu 22.04
| 测试场景 | 文件大小 | 读取速度 | 写入速度 | 压缩率 |
|---|---|---|---|---|
| ResNet50 (FP32) | 98 MB | 1.2 GB/s | 850 MB/s | 32% |
| BERT-base (FP32) | 438 MB | 980 MB/s | 720 MB/s | 28% |
| LLaMA-7B (FP16) | 13.5 GB | 1.5 GB/s | 1.1 GB/s | 35% |
| YOLOv8 (FP32) | 84 MB | 1.3 GB/s | 910 MB/s | 30% |
📊 内存映射模式下,读取速度可达到NVMe SSD的理论带宽上限。
🛠️ 命令行工具
uamf_tool - 信息查看
# 显示模型信息
uamf_tool info model.uamf
# 列出所有张量
uamf_tool list model.uamf
# 提取指定张量
uamf_tool extract model.uamf conv1.weight
uamf_convert - 格式转换
# 转换PyTorch模型
uamf_convert model.pth model.uamf --format pytorch
# 转换ONNX模型
uamf_convert model.onnx model.uamf --format onnx
# 指定压缩方法
uamf_convert model.pth model.uamf --compress zstd
uamf_info - 详细信息
# 显示完整模型信息
uamf_info model.uamf
输出示例:
=== UAMF File Information ===
File: model.uamf
Size: 98.32 MB
Compression Ratio: 32%
Tensor Count: 312
Model Metadata:
Name: ResNet50
Version: 2.0
Parameters: 25,557,032
Accuracy: 76.1%
⚖️ 许可证
MIT License
Copyright (c) 2025 UAMF Contributors
特此免费授予任何获得本软件及相关文档文件副本的人无限制使用本软件的权利,包括但不限于使用、复制、修改、合并、出版、分发、再许可和/或销售本软件副本的权利。
本软件按"原样"提供,不提供任何明示或暗示的保证。
📦 UAMF v2.0 · Universal Advanced Model Format
从PyTorch到TensorRT,从云端到边缘 — 统一的模型存储解决方案