TinyLlama.cpp 1.0
A lightweight C++ implementation of the TinyLlama language model
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | List of all members
Shard Struct Reference

Represents a memory-mapped SafeTensors file (shard). More...

#include <safetensors_loader.h>

Collaboration diagram for Shard:
Collaboration graph

Public Member Functions

 Shard (const std::string &fp)
 Construct and memory-map a shard file.
 
 ~Shard ()
 Destructor. Cleans up memory mapping and file handles.
 
 Shard (Shard &&other) noexcept
 Move constructor.
 
Shardoperator= (Shard &&other) noexcept
 Move assignment operator.
 
const uint8_t * get_tensor_raw_data (size_t local_offset, size_t n_bytes) const
 Get a pointer to the raw tensor data within this shard.
 

Public Attributes

std::string file_path
 Path to the shard file.
 
void * mapped_data = nullptr
 Pointer to the memory-mapped data.
 
size_t file_size = 0
 Size of the mapped file in bytes.
 
uint64_t metadata_size = 0
 Size of the metadata block in bytes.
 
const uint8_t * metadata_ptr = nullptr
 Pointer to the start of the metadata block.
 
const uint8_t * tensor_data_block_ptr = nullptr
 Pointer to the start of the tensor data block.
 
int fd_ = -1
 

Detailed Description

Represents a memory-mapped SafeTensors file (shard).

Handles opening, memory-mapping, and cleanup for a single SafeTensors file. Used internally by SafeTensorsLoader to support sharded models.

Definition at line 42 of file safetensors_loader.h.

Constructor & Destructor Documentation

◆ Shard() [1/2]

Shard::Shard ( const std::string &  fp)
explicit

Construct and memory-map a shard file.

Parameters
fpPath to the shard file.
Exceptions
std::runtime_erroron failure.

Definition at line 85 of file safetensors_loader.cpp.

85 : file_path(fp) {
86 Logger::info("Shard: Initializing for file: " + file_path);
87#ifdef _WIN32
88 file_handle_ = CreateFileA(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ,
89 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
90 if (file_handle_ == INVALID_HANDLE_VALUE) {
91 throw std::runtime_error("Shard: Failed to open file (Windows): " + file_path + " Error: " + std::to_string(GetLastError()));
92 }
93
94 LARGE_INTEGER size_li;
95 if (!GetFileSizeEx(file_handle_, &size_li)) {
96 CloseHandle(file_handle_);
97 file_handle_ = INVALID_HANDLE_VALUE;
98 throw std::runtime_error("Shard: Failed to get file size (Windows): " + file_path);
99 }
100 file_size = static_cast<size_t>(size_li.QuadPart);
101 if (file_size == 0) {
102 CloseHandle(file_handle_);
103 file_handle_ = INVALID_HANDLE_VALUE;
104 throw std::runtime_error("Shard: File is empty: " + file_path);
105 }
106
107 mapping_handle_ = CreateFileMapping(file_handle_, NULL, PAGE_READONLY, 0, 0, NULL);
108 if (mapping_handle_ == NULL) {
109 CloseHandle(file_handle_);
110 file_handle_ = INVALID_HANDLE_VALUE;
111 throw std::runtime_error("Shard: Failed to create file mapping (Windows): " + file_path + " Error: " + std::to_string(GetLastError()));
112 }
113
114 mapped_data = MapViewOfFile(mapping_handle_, FILE_MAP_READ, 0, 0, file_size);
115 if (mapped_data == nullptr) {
116 CloseHandle(mapping_handle_);
117 mapping_handle_ = NULL;
118 CloseHandle(file_handle_);
119 file_handle_ = INVALID_HANDLE_VALUE;
120 throw std::runtime_error("Shard: Failed to map view of file (Windows): " + file_path + " Error: " + std::to_string(GetLastError()));
121 }
122#else // POSIX
123 fd_ = open(file_path.c_str(), O_RDONLY);
124 if (fd_ == -1) {
125 throw std::runtime_error("Shard: Failed to open file: " + file_path + " Error: " + strerror(errno));
126 }
127
128 struct stat sb;
129 if (fstat(fd_, &sb) == -1) {
130 close(fd_);
131 fd_ = -1;
132 throw std::runtime_error("Shard: Failed to get file size: " + file_path + " Error: " + strerror(errno));
133 }
134 file_size = sb.st_size;
135 if (file_size == 0) {
136 close(fd_);
137 fd_ = -1;
138 throw std::runtime_error("Shard: File is empty: " + file_path);
139 }
140
141 mapped_data = mmap(NULL, file_size, PROT_READ, MAP_SHARED, fd_, 0);
142 if (mapped_data == MAP_FAILED) {
143 close(fd_);
144 fd_ = -1;
145 mapped_data = nullptr;
146 throw std::runtime_error("Shard: Failed to memory map file: " + file_path + " Error: " + strerror(errno));
147 }
148#endif
149 Logger::debug("Shard: Successfully mapped file: " + file_path + ", size: " + std::to_string(file_size));
150
151 if (file_size < 8) {
152 throw std::runtime_error("Shard: File too small (" + std::to_string(file_size) + " bytes) to be a valid SafeTensors shard (min 8 bytes for metadata length): " + file_path);
153 }
154 metadata_size = *reinterpret_cast<const uint64_t*>(mapped_data);
155
156 if (metadata_size == 0) {
157 throw std::runtime_error("Shard: Metadata size is 0 in file header: " + file_path);
158 }
159 if (8 + metadata_size > file_size) {
160 throw std::runtime_error("Shard: Declared metadata size (" + std::to_string(metadata_size) + ") plus header (8 bytes) exceeds file size (" + std::to_string(file_size) + ") in: " + file_path);
161 }
162 metadata_ptr = static_cast<const uint8_t*>(mapped_data) + 8;
164 Logger::debug("Shard: Metadata size from header: " + std::to_string(metadata_size) + " for " + file_path);
165}
static void debug(const std::string &message)
Definition logger.cpp:131
static void info(const std::string &message)
Definition logger.cpp:135
uint64_t metadata_size
Size of the metadata block in bytes.
const uint8_t * tensor_data_block_ptr
Pointer to the start of the tensor data block.
void * mapped_data
Pointer to the memory-mapped data.
std::string file_path
Path to the shard file.
const uint8_t * metadata_ptr
Pointer to the start of the metadata block.
size_t file_size
Size of the mapped file in bytes.

References Logger::debug(), fd_, file_path, file_size, Logger::info(), mapped_data, metadata_ptr, metadata_size, and tensor_data_block_ptr.

◆ ~Shard()

Shard::~Shard ( )

Destructor. Cleans up memory mapping and file handles.

Definition at line 167 of file safetensors_loader.cpp.

167 {
168 Logger::debug("Shard: Cleaning up for file: " + (file_path.empty() ? "(moved or uninitialized)" : file_path) );
169#ifdef _WIN32
170 if (mapped_data != nullptr) {
171 if (!UnmapViewOfFile(mapped_data)) {
172 Logger::error("Shard: Failed to unmap view of file (Windows) for \"" + file_path + "\" Error: " + std::to_string(GetLastError()));
173 }
174 }
175 if (mapping_handle_ != NULL) {
176 if (!CloseHandle(mapping_handle_)) {
177 Logger::error("Shard: Failed to close mapping handle (Windows) for \"" + file_path + "\" Error: " + std::to_string(GetLastError()));
178 }
179 }
180 if (file_handle_ != INVALID_HANDLE_VALUE) {
181 if (!CloseHandle(file_handle_)) {
182 Logger::error("Shard: Failed to close file handle (Windows) for \"" + file_path + "\" Error: " + std::to_string(GetLastError()));
183 }
184 }
185 mapped_data = nullptr;
186 file_handle_ = INVALID_HANDLE_VALUE;
187 mapping_handle_ = NULL;
188#else // POSIX
189 if (mapped_data != nullptr && mapped_data != MAP_FAILED) {
190 if (munmap(mapped_data, file_size) == -1) {
191 Logger::error("Shard: Failed to munmap file: \"" + file_path + "\" Error: " + strerror(errno));
192 }
193 }
194 if (fd_ != -1) {
195 if (close(fd_) == -1) {
196 Logger::error("Shard: Failed to close file descriptor for \"" + file_path + "\" Error: " + strerror(errno));
197 }
198 }
199 mapped_data = nullptr;
200 fd_ = -1;
201#endif
202}
static void error(const std::string &message)
Definition logger.cpp:143

References Logger::debug(), Logger::error(), fd_, file_path, file_size, and mapped_data.

◆ Shard() [2/2]

Shard::Shard ( Shard &&  other)
noexcept

Move constructor.

Definition at line 204 of file safetensors_loader.cpp.

205 : file_path(std::move(other.file_path)),
207 file_size(other.file_size),
211#ifdef _WIN32
212 , file_handle_(other.file_handle_)
213 , mapping_handle_(other.mapping_handle_)
214#else
215 , fd_(other.fd_)
216#endif
217{
218 other.mapped_data = nullptr;
219 other.file_size = 0;
220 other.metadata_size = 0;
221 other.metadata_ptr = nullptr;
222 other.tensor_data_block_ptr = nullptr;
223#ifdef _WIN32
224 other.file_handle_ = INVALID_HANDLE_VALUE;
225 other.mapping_handle_ = NULL;
226#else
227 other.fd_ = -1;
228#endif
229}

Member Function Documentation

◆ get_tensor_raw_data()

const uint8_t * Shard::get_tensor_raw_data ( size_t  local_offset,
size_t  n_bytes 
) const

Get a pointer to the raw tensor data within this shard.

Parameters
local_offsetOffset from the start of the tensor data block.
n_bytesNumber of bytes to access.
Returns
Pointer to the tensor data.
Exceptions
std::out_of_rangeif the requested range is invalid.

Definition at line 261 of file safetensors_loader.cpp.

261 {
262#ifdef _WIN32
263 if (!mapped_data || mapped_data == NULL || !tensor_data_block_ptr) {
264#else // POSIX
265 if (!mapped_data || mapped_data == MAP_FAILED || !tensor_data_block_ptr) {
266#endif
267 throw std::logic_error("Shard not properly mapped or initialized to get tensor data: " + file_path);
268 }
269 const uint8_t* data_start = tensor_data_block_ptr + local_offset;
270 const uint8_t* shard_data_block_end = tensor_data_block_ptr + (file_size - (8 + metadata_size));
271
272 if (data_start < tensor_data_block_ptr || data_start + n_bytes > shard_data_block_end || n_bytes > (file_size - (8 + metadata_size))) {
273 throw std::out_of_range(
274 "Tensor data (local_offset: " + std::to_string(local_offset) +
275 ", n_bytes: " + std::to_string(n_bytes) +
276 ") out of bounds for data block of shard: " + file_path +
277 ". Shard data block size: " + std::to_string(file_size - (8 + metadata_size)) + " bytes."
278 );
279 }
280 return data_start;
281}

References file_path, file_size, mapped_data, metadata_size, and tensor_data_block_ptr.

Referenced by SafeTensorsLoader::get_tensor_bytes().

◆ operator=()

Shard & Shard::operator= ( Shard &&  other)
noexcept

Move assignment operator.

Definition at line 231 of file safetensors_loader.cpp.

231 {
232 if (this != &other) {
233 this->~Shard();
234 file_path = std::move(other.file_path);
235 mapped_data = other.mapped_data;
236 file_size = other.file_size;
240#ifdef _WIN32
241 file_handle_ = other.file_handle_;
242 mapping_handle_ = other.mapping_handle_;
243#else
244 fd_ = other.fd_;
245#endif
246 other.mapped_data = nullptr;
247 other.file_size = 0;
248 other.metadata_size = 0;
249 other.metadata_ptr = nullptr;
250 other.tensor_data_block_ptr = nullptr;
251#ifdef _WIN32
252 other.file_handle_ = INVALID_HANDLE_VALUE;
253 other.mapping_handle_ = NULL;
254#else
255 other.fd_ = -1;
256#endif
257 }
258 return *this;
259}
~Shard()
Destructor. Cleans up memory mapping and file handles.

Member Data Documentation

◆ fd_

int Shard::fd_ = -1

File descriptor for memory mapping

Definition at line 77 of file safetensors_loader.h.

Referenced by Shard(), and ~Shard().

◆ file_path

std::string Shard::file_path

Path to the shard file.

Definition at line 46 of file safetensors_loader.h.

Referenced by get_tensor_raw_data(), SafeTensorsLoader::parse_shard_metadata(), Shard(), and ~Shard().

◆ file_size

size_t Shard::file_size = 0

Size of the mapped file in bytes.

Definition at line 56 of file safetensors_loader.h.

Referenced by get_tensor_raw_data(), Shard(), and ~Shard().

◆ mapped_data

void* Shard::mapped_data = nullptr

Pointer to the memory-mapped data.

Definition at line 51 of file safetensors_loader.h.

Referenced by get_tensor_raw_data(), Shard(), and ~Shard().

◆ metadata_ptr

const uint8_t* Shard::metadata_ptr = nullptr

Pointer to the start of the metadata block.

Definition at line 66 of file safetensors_loader.h.

Referenced by SafeTensorsLoader::parse_shard_metadata(), and Shard().

◆ metadata_size

uint64_t Shard::metadata_size = 0

Size of the metadata block in bytes.

Definition at line 61 of file safetensors_loader.h.

Referenced by get_tensor_raw_data(), SafeTensorsLoader::parse_shard_metadata(), and Shard().

◆ tensor_data_block_ptr

const uint8_t* Shard::tensor_data_block_ptr = nullptr

Pointer to the start of the tensor data block.

Definition at line 71 of file safetensors_loader.h.

Referenced by get_tensor_raw_data(), and Shard().


The documentation for this struct was generated from the following files: