![]() |
Nilorea Library
C utilities for networking, threading, graphics
|
Reading the tensors of a PyTorch checkpoint without Python. More...
#include <stdint.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "nilorea/n_log.h"#include "nilorea/n_pth.h"#include "nilorea/n_zip.h"Go to the source code of this file.
Data Structures | |
| struct | PK_OBJ |
| One pickle value. More... | |
| struct | PK_VM |
| The interpreter. More... | |
Macros | |
| #define | LEFT (len - at) |
| #define | PTH_OBJECTS_MAX 4000000u |
| Most values the interpreter makes, and most memo entries: a guard, far above any real checkpoint's. | |
| #define | PTH_PICKLE_MAX (64u * 1024u * 1024u) |
| Largest data.pkl read: a state dictionary's is a few kilobytes, a large model's a few hundred. | |
| #define | PTH_STORAGE_MAX ((size_t)4 * 1024u * 1024u * 1024u) |
| Largest single storage read. | |
Enumerations | |
| enum | PK_TYPE { PK_NONE = 0 , PK_BOOL , PK_INT , PK_FLOAT , PK_STR , PK_BYTES , PK_TUPLE , PK_LIST , PK_DICT , PK_GLOBAL , PK_STORAGE , PK_TENSOR , PK_OPAQUE , PK_MARK } |
| What a pickle value is. More... | |
Functions | |
| static int | collect (const char *path, const char *dir, const PK_OBJ *o, const char *prefix, N_PTH *pth, int depth) |
| collect the tensors under a dictionary, depth first | |
| static float | element_value (const unsigned char *p, int element, const char *dtype) |
| convert one stored element to a float | |
| static uint64_t | le (const unsigned char *p, int n) |
| read a little endian unsigned value | |
| const N_PTH_TENSOR * | n_pth_find (const N_PTH *pth, const char *name) |
| find a tensor by its path | |
| void | n_pth_free (N_PTH **pth) |
| release a checkpoint's tensors | |
| N_PTH * | n_pth_load (const char *path) |
| read every tensor of a checkpoint | |
| static int | pk_add (PK_OBJ *o, PK_OBJ *item) |
| add an item to a tuple, list or dictionary | |
| static PK_OBJ * | pk_from_mark (PK_VM *vm, int type) |
| pop the values above the top mark into a new container | |
| static long | pk_mark (const PK_VM *vm) |
| where the top mark is on the stack | |
| static int | pk_memo_put (PK_VM *vm, size_t index, PK_OBJ *o) |
| store a value in the memo | |
| static PK_OBJ * | pk_new (PK_VM *vm, int type) |
| make a value, owned by the interpreter | |
| static PK_OBJ * | pk_pop (PK_VM *vm) |
| pop a value | |
| static int | pk_push (PK_VM *vm, PK_OBJ *o) |
| push a value | |
| static PK_OBJ * | pk_reduce (PK_VM *vm, const PK_OBJ *callable, PK_OBJ *args) |
| the call a REDUCE or NEWOBJ makes, done as data | |
| static PK_OBJ * | pk_run (PK_VM *vm, const unsigned char *p, size_t len) |
| run a pickle | |
| static PK_OBJ * | pk_string (PK_VM *vm, int type, const unsigned char *p, size_t len) |
| make a string value from bytes | |
| static int | storage_element (const char *name, const char **dtype) |
| a storage type's element size, from its global's name | |
| static int | tensor_read (const char *path, const char *dir, const PK_OBJ *t, N_PTH_TENSOR *out) |
| read a tensor's values out of its storage file | |
Reading the tensors of a PyTorch checkpoint without Python.
Definition in file n_pth.c.
| struct PK_OBJ |
One pickle value.
Dictionaries keep keys and values alternately in items.
| Data Fields | ||
|---|---|---|
| size_t | cap | room for |
| const char * | dtype | a storage's type name, for the log |
| int | element | a storage's element size in bytes |
| double | f | a float's value |
| int64_t | i | an integer's value, a storage's element count, a tensor's offset |
| struct PK_OBJ ** | items | items of a tuple, list or dictionary; a tensor's storage, then its size and stride tuples |
| size_t | n | how many |
| char * | s | a string, a global's "module name", a storage's key; owned |
| int | type | a PK_TYPE |
| struct PK_VM |
| #define LEFT (len - at) |
Referenced by pk_run().
| #define PTH_OBJECTS_MAX 4000000u |
| #define PTH_PICKLE_MAX (64u * 1024u * 1024u) |
Largest data.pkl read: a state dictionary's is a few kilobytes, a large model's a few hundred.
Definition at line 39 of file n_pth.c.
Referenced by n_pth_load().
| #define PTH_STORAGE_MAX ((size_t)4 * 1024u * 1024u * 1024u) |
| enum PK_TYPE |
|
static |
collect the tensors under a dictionary, depth first
| path | the archive |
| dir | the folder data.pkl is in |
| o | a value |
| prefix | its path so far |
| pth | receives the tensors |
| depth | how deep, a guard against a dictionary inside itself |
Definition at line 707 of file n_pth.c.
References collect(), N_PTH::count, PK_OBJ::items, key, PK_OBJ::n, N_PTH_TENSOR::name, PK_DICT, PK_STR, PK_TENSOR, tensor_read(), N_PTH::tensors, and PK_OBJ::type.
Referenced by collect(), main(), n_pth_load(), and run().
|
static |
convert one stored element to a float
| p | the element's bytes |
| element | its size |
| dtype | its type name |
Definition at line 599 of file n_pth.c.
References le().
Referenced by tensor_read().
|
static |
read a little endian unsigned value
| p | the bytes |
| n | how many, up to 8 |
Definition at line 233 of file n_pth.c.
Referenced by _draw_syntaxview(), element_value(), n_http_request_parse(), n_url_safe_head_parse(), and pk_run().
add an item to a tuple, list or dictionary
| o | the container |
| item | the item |
Definition at line 135 of file n_pth.c.
References PK_OBJ::cap, PK_OBJ::items, PK_OBJ::n, and PTH_OBJECTS_MAX.
Referenced by pk_from_mark(), pk_reduce(), and pk_run().
pop the values above the top mark into a new container
| vm | the interpreter |
| type | PK_TUPLE or PK_LIST |
Definition at line 298 of file n_pth.c.
References pk_add(), pk_mark(), pk_new(), PK_VM::sp, PK_VM::stack, and type().
Referenced by pk_run().
|
static |
where the top mark is on the stack
| vm | the interpreter |
Definition at line 181 of file n_pth.c.
References PK_MARK, PK_VM::sp, PK_VM::stack, and PK_OBJ::type.
Referenced by pk_from_mark(), and pk_run().
store a value in the memo
| vm | the interpreter |
| index | where |
| o | the value |
Definition at line 214 of file n_pth.c.
References PK_VM::memo, PK_VM::memo_n, and PTH_OBJECTS_MAX.
Referenced by pk_run().
make a value, owned by the interpreter
| vm | the interpreter |
| type | its PK_TYPE |
Definition at line 112 of file n_pth.c.
References PK_VM::all, PK_VM::all_cap, PK_VM::all_n, PTH_OBJECTS_MAX, PK_OBJ::type, and type().
Referenced by pk_from_mark(), pk_reduce(), pk_run(), and pk_string().
pop a value
| vm | the interpreter |
Definition at line 173 of file n_pth.c.
References PK_MARK, PK_VM::sp, PK_VM::stack, and PK_OBJ::type.
Referenced by pk_run().
push a value
| vm | the interpreter |
| o | the value; NULL fails |
Definition at line 154 of file n_pth.c.
References PTH_OBJECTS_MAX, PK_VM::sp, PK_VM::stack, and PK_VM::stack_cap.
Referenced by pk_run().
the call a REDUCE or NEWOBJ makes, done as data
| vm | the interpreter |
| callable | what is called |
| args | its arguments, a tuple |
Definition at line 272 of file n_pth.c.
References PK_OBJ::i, PK_OBJ::items, PK_OBJ::n, pk_add(), PK_DICT, PK_GLOBAL, PK_INT, pk_new(), PK_OPAQUE, PK_STORAGE, PK_TENSOR, PK_TUPLE, PK_OBJ::s, and PK_OBJ::type.
Referenced by pk_run().
run a pickle
| vm | the interpreter |
| p | the pickle |
| len | its length |
Definition at line 317 of file n_pth.c.
References PK_OBJ::f, PK_OBJ::i, PK_OBJ::items, le(), LEFT, LOG_ERR, PK_VM::memo, PK_VM::memo_n, PK_OBJ::n, n_log, pk_add(), PK_BOOL, PK_BYTES, PK_DICT, PK_FLOAT, pk_from_mark(), PK_GLOBAL, PK_INT, PK_LIST, PK_MARK, pk_mark(), pk_memo_put(), pk_new(), PK_NONE, PK_OPAQUE, pk_pop(), pk_push(), pk_reduce(), PK_STORAGE, PK_STR, pk_string(), PK_TUPLE, PK_OBJ::s, PK_VM::sp, PK_VM::stack, storage_element(), PK_OBJ::type, and type().
Referenced by n_pth_load().
make a string value from bytes
| vm | the interpreter |
| type | PK_STR or PK_BYTES |
| p | the bytes |
| len | how many |
Definition at line 197 of file n_pth.c.
References PK_OBJ::i, pk_new(), PK_OBJ::s, and type().
Referenced by pk_run().
|
static |
|
static |
read a tensor's values out of its storage file
| path | the archive |
| dir | the folder data.pkl is in, "archive/" |
| t | the tensor value |
| out | receives the tensor |
Definition at line 644 of file n_pth.c.
References N_PTH_TENSOR::count, N_PTH_TENSOR::data, N_PTH_TENSOR::dims, PK_OBJ::dtype, PK_OBJ::element, element_value(), PK_OBJ::i, PK_OBJ::items, LOG_ERR, PK_OBJ::n, n_log, N_PTH_MAX_DIMS, n_zip_read(), N_PTH_TENSOR::name, PK_INT, PTH_STORAGE_MAX, rest(), PK_OBJ::s, N_PTH_TENSOR::shape, and PK_OBJ::type.
Referenced by collect().