mirror of
https://github.com/antirez/gguf-tools.git
synced 2025-09-17 02:28:07 +08:00
Mixtral experts extraction test.
This commit is contained in:
30
gguflib.c
30
gguflib.c
@@ -451,7 +451,7 @@ int gguf_append_kv(gguf_ctx *ctx, const char *keyname, uint64_t keylen, uint32_t
|
||||
|
||||
/* Append tensor metadata (but not the actual tensor weights data) to the
|
||||
* GGUF file identified by 'ctx'. */
|
||||
int gguf_append_tensor(gguf_ctx *ctx, const char *tensorname, uint64_t namelen, uint32_t num_dim, uint64_t *dim, uint32_t type, uint64_t offset)
|
||||
int gguf_append_tensor_info(gguf_ctx *ctx, const char *tensorname, uint64_t namelen, uint32_t num_dim, uint64_t *dim, uint32_t type, uint64_t offset)
|
||||
{
|
||||
if (write(ctx->fd,&namelen,sizeof(namelen)) != sizeof(namelen)) return 0;
|
||||
if (write(ctx->fd,tensorname,namelen) != (ssize_t)namelen) return 0;
|
||||
@@ -467,32 +467,14 @@ int gguf_append_tensor(gguf_ctx *ctx, const char *tensorname, uint64_t namelen,
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Append tensor data enforcing the GGUF file aligment. The user must specify
|
||||
* an offset that requires no more than ctx.alignemnt-1 padding bytes starting
|
||||
* from the current offset (this means that this function should be called
|
||||
* sequentially for all the tensors we want to store, after we already
|
||||
* computed the right offset for all the tensors). Also the offset must be
|
||||
* aligned. Otherwise the function will fail returning 0. On success, 1 is
|
||||
* returned. The function will take care to add the padding required to
|
||||
* start writing the tensor at the specified offset. */
|
||||
int gguf_append_tensor_data(gguf_ctx *ctx, uint64_t offset, void *tensor, uint64_t tensor_size) {
|
||||
/* Append tensor data enforcing the GGUF file aligment.
|
||||
* The function will take care to add the padding required to start writing
|
||||
* the tensor at an alignment multiple. */
|
||||
int gguf_append_tensor_data(gguf_ctx *ctx, void *tensor, uint64_t tensor_size) {
|
||||
char padding_data[1024] = {0};
|
||||
assert(sizeof(padding_data) >= ctx->alignment);
|
||||
|
||||
/* Is the offset aligned? */
|
||||
if (offset % ctx->alignment) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* We expect the offset of the context to be already where this tensor
|
||||
* should be stored, minus the padding. */
|
||||
if (offset < ctx->off || offset - ctx->off >= ctx->alignment) {
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t padding = gguf_get_alignment_padding(ctx->alignment,offset);
|
||||
uint64_t padding = gguf_get_alignment_padding(ctx->alignment,ctx->size);
|
||||
if (write(ctx->fd,padding_data,padding) != (ssize_t)padding) return 0;
|
||||
if (write(ctx->fd,tensor,tensor_size) != (ssize_t)tensor_size) return 0;
|
||||
gguf_remap(ctx);
|
||||
|
Reference in New Issue
Block a user