专栏名称: 企业存储技术
企业存储、服务器、SSD、灾备等领域技术分享,交流 | @唐僧_huangliang (新浪微博 )
目录
相关文章推荐
河南发布  ·  40℃+!本周河南高温持续打卡→ ·  10 小时前  
河南发布  ·  在河南,为啥先上桌的是“八道凉菜”? ·  昨天  
河南发布  ·  来了!2025年河南高考作文题出炉 ·  2 天前  
51好读  ›  专栏  ›  企业存储技术

DeepSeek-671B纯CPU部署:配置选型、性能测试与量化对比

企业存储技术  · 公众号  ·  · 2025-03-18 18:40

正文

请到「今天看啥」查看全文


include
# include
# include

# ifdef __has_include
# if __has_include( )
# include
# if defined(_POSIX_MAPPED_FILES)
# include
# include
# endif
# if defined(_POSIX_MEMLOCK_RANGE)
# include
# endif
# endif
# endif

# if defined(_WIN32)
# define WIN32_LEAN_AND_MEAN
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include
# ifndef PATH_MAX
# define PATH_MAX MAX_PATH
# endif
# include
# endif

// TODO: consider moving to llama-impl.h if needed in more places
# if defined(_WIN32)
static std :: string llama_format_win_err (DWORD err) {
LPSTR buf;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL , err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buf, 0 , NULL );
if (!size) {
return "FormatMessageA failed" ;
}
std :: string ret (buf, size) ;
LocalFree(buf);
return ret;
}
# endif

// llama_file

struct llama_file : :impl {
# if defined(_WIN32)
HANDLE fp_win32;
std :: string GetErrorMessageWin32 (DWORD error_code) const {
std :: string ret;
LPSTR lpMsgBuf = NULL ;
DWORD bufLen = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL , error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&lpMsgBuf, 0 , NULL );
if (!bufLen) {
ret = format( "Win32 error code: %lx" , error_code);
} else {
ret = lpMsgBuf;
LocalFree(lpMsgBuf);
}

return ret;
}

impl( const char * fname, const char * mode) {
fp = ggml_fopen(fname, mode);
if (fp == NULL ) {
throw std ::runtime_error(format( "failed to open %s: %s" , fname, strerror(errno)));
}
fp_win32 = (HANDLE) _get_osfhandle(_fileno(fp));
seek( 0 , SEEK_END);
size = tell();
seek( 0 , SEEK_SET);
}

size_t tell () const {
LARGE_INTEGER li;
li.QuadPart = 0 ;
BOOL ret = SetFilePointerEx(fp_win32, li, &li, FILE_CURRENT);
if (!ret) {
throw std ::runtime_error(format( "read error: %s" , GetErrorMessageWin32(GetLastError()).c_str()));
}

return li.QuadPart;
}

void seek ( size_t offset, int whence) const {
static_assert (SEEK_SET == FILE_BEGIN, "SEEK_SET != FILE_BEGIN" );
static_assert (SEEK_CUR == FILE_CURRENT, "SEEK_CUR != FILE_CURRENT" );
static_assert (SEEK_END == FILE_END, "SEEK_END != FILE_END" );

LARGE_INTEGER li;
li.QuadPart = offset;
BOOL ret = SetFilePointerEx(fp_win32, li, NULL , whence);
if (!ret) {
throw std ::runtime_error(format( "read error: %s" , GetErrorMessageWin32(GetLastError()).c_str()));
}
}

void read_raw ( void * ptr, size_t len) const {
size_t bytes_read = 0 ;
while (bytes_read < len) {
size_t chunk_size = std ::min< size_t >(len - bytes_read, 64 * 1024 * 1024 );
DWORD chunk_read = 0 ;
BOOL result = ReadFile(fp_win32, reinterpret_cast < char *>(ptr) + bytes_read, chunk_size, &chunk_read, NULL );
if (!result) {
throw std ::runtime_error(format( "read error: %s" , GetErrorMessageWin32(GetLastError()).c_str()));
}
if (chunk_read < chunk_size || chunk_read == 0 ) {
throw std ::runtime_error( "unexpectedly reached end of file" );
}

bytes_read += chunk_read;
}
}

uint32_t read_u32 () const {
uint32_t val;
read_raw(&val, sizeof (val));
return val;
}

void write_raw ( const void * ptr, size_t len) const {
size_t bytes_written = 0 ;
while (bytes_written < len) {
size_t chunk_size = std ::min< size_t >(len - bytes_written, 64 * 1024 * 1024 );
DWORD chunk_written = 0 ;
BOOL result = WriteFile(fp_win32, reinterpret_cast < char const *>(ptr) + bytes_written, chunk_size, &chunk_written, NULL );
if (!result) {
throw std ::runtime_error(format( "write error: %s" , GetErrorMessageWin32(GetLastError()).c_str()));
}
if (chunk_written < chunk_size || chunk_written == 0 ) {
throw std ::runtime_error( "unexpectedly failed to write bytes" );
}

bytes_written += chunk_written;
}
}

void write_u32 ( uint32_t val) const {
write_raw(&val, sizeof (val));
}

~impl() {
if (fp) {
std ::fclose(fp);
}
}
# else
impl( const char * fname, const char * mode) {
fp = ggml_fopen(fname, mode);
if (fp == NULL ) {
throw std ::runtime_error(format( "failed to open %s: %s" , fname, strerror(errno)));
}
seek( 0 , SEEK_END);
size = tell();
seek( 0 , SEEK_SET);
}

size_t tell () const {
// TODO: this ifdef is never true?
# ifdef _WIN32
__int64 ret = _ftelli64(fp);
# else
long ret = std ::ftell(fp);
# endif
if (ret == -1 ) {
throw std ::runtime_error(format( "ftell error: %s" , strerror(errno)));
}

return ( size_t ) ret;
}

void seek ( size_t offset, int whence) const {
// TODO: this ifdef is never true?
# ifdef _WIN32
int ret = _fseeki64(fp, (__int64) offset, whence);
# else
int





请到「今天看啥」查看全文