-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_model_load.cpp
More file actions
55 lines (43 loc) · 1.76 KB
/
test_model_load.cpp
File metadata and controls
55 lines (43 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <fstream>
#include "llama-cpp.h"
int main() {
std::cout << "=== 模型加载测试 ===" << std::endl;
// 检查模型文件
const char* model_path = "model/model_1.8B.gguf";
std::ifstream file(model_path, std::ios::binary);
if (!file) {
std::cerr << "[ERROR] 无法打开模型文件: " << model_path << std::endl;
return 1;
}
file.seekg(0, std::ios::end);
size_t size = file.tellg();
std::cout << "[OK] 模型文件大小: " << size << " bytes (" << size / (1024*1024) << " MB)" << std::endl;
file.close();
// 尝试加载模型
std::cout << "正在加载模型..." << std::endl;
ggml_backend_load_all();
llama_model_params mparams = llama_model_default_params();
llama_model* model = llama_model_load_from_file(model_path, mparams);
if (model == nullptr) {
std::cerr << "[ERROR] 模型加载失败!" << std::endl;
std::cerr << "可能原因:" << std::endl;
std::cerr << "1. 内存不足(需要8GB+)" << std::endl;
std::cerr << "2. CPU不支持AVX2指令集" << std::endl;
std::cerr << "3. 模型文件损坏" << std::endl;
return 1;
}
std::cout << "[OK] 模型加载成功!" << std::endl;
// 获取模型信息
llama_context_params cparams = llama_context_default_params();
llama_context* ctx = llama_new_context_with_model(model, cparams);
if (ctx == nullptr) {
std::cerr << "[WARNING] 无法创建上下文(可能内存不足)" << std::endl;
} else {
std::cout << "[OK] 上下文创建成功" << std::endl;
llama_free(ctx);
}
llama_model_free(model);
std::cout << "=== 测试完成 ===" << std::endl;
return 0;
}