Class StateDict
java.lang.Object
ai.beestream.jitnn.io.StateDict
Named tensors and string values: the state of a model, an optimiser or a whole training run,
as PyTorch's
state_dict. Built from live objects (Module.stateDict(),
Optimizer.stateDict()) it refers to their tensors, read when it is saved; loaded from a
file, it reads each tensor only when it is copied in. Nested parts are flattened with dotted
names (model.parameters.0). Files are safetensors (see SafeTensors).
StateDict run = new StateDict().put("model", model.stateDict()).put("optimizer", opt.stateDict());
run.save(Path.of("step-1000.safetensors"));
StateDict in = StateDict.load(Path.of("step-1000.safetensors"));
model.loadStateDict(in.sub("model"));
opt.loadStateDict(in.sub("optimizer"));
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidOverwrites the persistent tensortarget(a parameter, a buffer, optimiser state) with tensorname, in place, so plans compiled withtargetkeep using it.The dtype of tensorname.floatfloatValue(String name) The valuenameas a float; throws when it is missing.booleanWhether the dictionary holds a tensor namedname.intThe valuenameas an int; throws when it is missing.static StateDictOpens a safetensors file; tensors are read when copied in.longThe valuenameas a long; throws when it is missing.Adds a float value (stored as its decimal string).Adds an integer value (stored as its decimal string).Adds every entry ofpartunderprefix..Adds a tensor; its contents are read when the dictionary is saved.Adds a value.voidWrites the dictionary topathas safetensors; reading live tensors waits for them.int[]The shape of tensorname.The entries underprefix., with the prefix removed.Tensornameas a new persistent tensor (a weight that stays on the device).The tensor names, in insertion (or file) order.The valuename; throws when it is missing.The value names.
-
Constructor Details
-
StateDict
public StateDict()An empty dictionary, to fill withputand save.
-
-
Method Details
-
put
-
put
-
put
-
put
-
put
-
sub
-
tensorNames
-
valueNames
-
hasTensor
Whether the dictionary holds a tensor namedname. -
value
-
longValue
The valuenameas a long; throws when it is missing. -
intValue
The valuenameas an int; throws when it is missing. -
floatValue
The valuenameas a float; throws when it is missing. -
dtype
-
shape
The shape of tensorname. -
copyInto
-
check
-
tensor
-
save
Writes the dictionary topathas safetensors; reading live tensors waits for them.- Throws:
IOException
-
load
Opens a safetensors file; tensors are read when copied in.- Throws:
IOException
-