Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_pth.c File Reference

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"
Include dependency graph for n_pth.c:

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

Detailed Description

Reading the tensors of a PyTorch checkpoint without Python.

Author
Castagnier Mickael
Version
1.0
Date
29/09/2026

Definition in file n_pth.c.


Data Structure Documentation

◆ PK_OBJ

struct PK_OBJ

One pickle value.

Dictionaries keep keys and values alternately in items.

Definition at line 66 of file n_pth.c.

Collaboration diagram for PK_OBJ:
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

◆ PK_VM

struct PK_VM

The interpreter.

Definition at line 89 of file n_pth.c.

Collaboration diagram for PK_VM:
Data Fields
PK_OBJ ** all every value made, freed together
size_t all_cap room for
size_t all_n how many
PK_OBJ ** memo the memo
size_t memo_n its size
size_t sp its depth
PK_OBJ ** stack the stack
size_t stack_cap room for

Macro Definition Documentation

◆ LEFT

#define LEFT   (len - at)

Referenced by pk_run().

◆ PTH_OBJECTS_MAX

#define PTH_OBJECTS_MAX   4000000u

Most values the interpreter makes, and most memo entries: a guard, far above any real checkpoint's.

Definition at line 44 of file n_pth.c.

Referenced by pk_add(), pk_memo_put(), pk_new(), and pk_push().

◆ PTH_PICKLE_MAX

#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().

◆ PTH_STORAGE_MAX

#define PTH_STORAGE_MAX   ((size_t)4 * 1024u * 1024u * 1024u)

Largest single storage read.

Definition at line 41 of file n_pth.c.

Referenced by tensor_read().

Enumeration Type Documentation

◆ PK_TYPE

enum PK_TYPE

What a pickle value is.

Enumerator
PK_NONE 
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 

Definition at line 47 of file n_pth.c.

Function Documentation

◆ collect()

int collect ( const char * path,
const char * dir,
const PK_OBJ * o,
const char * prefix,
N_PTH * pth,
int depth )
static

collect the tensors under a dictionary, depth first

Parameters
paththe archive
dirthe folder data.pkl is in
oa value
prefixits path so far
pthreceives the tensors
depthhow deep, a guard against a dictionary inside itself
Returns
0, or -1
Examples
ex_wrap.c.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ element_value()

float element_value ( const unsigned char * p,
int element,
const char * dtype )
static

convert one stored element to a float

Parameters
pthe element's bytes
elementits size
dtypeits type name
Returns
the value

Definition at line 599 of file n_pth.c.

References le().

Referenced by tensor_read().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ le()

uint64_t le ( const unsigned char * p,
int n )
static

read a little endian unsigned value

Parameters
pthe bytes
nhow many, up to 8
Returns
the value

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().

Here is the caller graph for this function:

◆ pk_add()

int pk_add ( PK_OBJ * o,
PK_OBJ * item )
static

add an item to a tuple, list or dictionary

Parameters
othe container
itemthe item
Returns
0, or -1

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().

Here is the caller graph for this function:

◆ pk_from_mark()

PK_OBJ * pk_from_mark ( PK_VM * vm,
int type )
static

pop the values above the top mark into a new container

Parameters
vmthe interpreter
typePK_TUPLE or PK_LIST
Returns
the container, or NULL

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pk_mark()

long pk_mark ( const PK_VM * vm)
static

where the top mark is on the stack

Parameters
vmthe interpreter
Returns
its index, or -1 when there is none

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().

Here is the caller graph for this function:

◆ pk_memo_put()

int pk_memo_put ( PK_VM * vm,
size_t index,
PK_OBJ * o )
static

store a value in the memo

Parameters
vmthe interpreter
indexwhere
othe value
Returns
0, or -1

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().

Here is the caller graph for this function:

◆ pk_new()

PK_OBJ * pk_new ( PK_VM * vm,
int type )
static

make a value, owned by the interpreter

Parameters
vmthe interpreter
typeits PK_TYPE
Returns
the value, or NULL

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pk_pop()

PK_OBJ * pk_pop ( PK_VM * vm)
static

pop a value

Parameters
vmthe interpreter
Returns
the value, or NULL on an empty stack or at a mark

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().

Here is the caller graph for this function:

◆ pk_push()

int pk_push ( PK_VM * vm,
PK_OBJ * o )
static

push a value

Parameters
vmthe interpreter
othe value; NULL fails
Returns
0, or -1

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().

Here is the caller graph for this function:

◆ pk_reduce()

PK_OBJ * pk_reduce ( PK_VM * vm,
const PK_OBJ * callable,
PK_OBJ * args )
static

the call a REDUCE or NEWOBJ makes, done as data

Parameters
vmthe interpreter
callablewhat is called
argsits arguments, a tuple
Returns
the result, or NULL

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pk_run()

PK_OBJ * pk_run ( PK_VM * vm,
const unsigned char * p,
size_t len )
static

run a pickle

Parameters
vmthe interpreter
pthe pickle
lenits length
Returns
the value it leaves, or NULL

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ pk_string()

PK_OBJ * pk_string ( PK_VM * vm,
int type,
const unsigned char * p,
size_t len )
static

make a string value from bytes

Parameters
vmthe interpreter
typePK_STR or PK_BYTES
pthe bytes
lenhow many
Returns
the value, or NULL

Definition at line 197 of file n_pth.c.

References PK_OBJ::i, pk_new(), PK_OBJ::s, and type().

Referenced by pk_run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ storage_element()

int storage_element ( const char * name,
const char ** dtype )
static

a storage type's element size, from its global's name

Parameters
namethe global, "torch FloatStorage"
dtypereceives a short name
Returns
4, 2 or 8, or 0 for a type not read

Definition at line 245 of file n_pth.c.

Referenced by pk_run().

Here is the caller graph for this function:

◆ tensor_read()

int tensor_read ( const char * path,
const char * dir,
const PK_OBJ * t,
N_PTH_TENSOR * out )
static

read a tensor's values out of its storage file

Parameters
paththe archive
dirthe folder data.pkl is in, "archive/"
tthe tensor value
outreceives the tensor
Returns
0, or -1

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().

Here is the call graph for this function:
Here is the caller graph for this function: