Class StateDict

java.lang.Object
ai.beestream.jitnn.io.StateDict

public final class StateDict extends Object
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 Details

    • StateDict

      public StateDict()
      An empty dictionary, to fill with put and save.
  • Method Details

    • put

      public StateDict put(String name, Tensor t)
      Adds a tensor; its contents are read when the dictionary is saved.
    • put

      public StateDict put(String name, String value)
      Adds a value.
    • put

      public StateDict put(String name, long value)
      Adds an integer value (stored as its decimal string).
    • put

      public StateDict put(String name, float value)
      Adds a float value (stored as its decimal string).
    • put

      public StateDict put(String prefix, StateDict part)
      Adds every entry of part under prefix..
    • sub

      public StateDict sub(String prefix)
      The entries under prefix., with the prefix removed.
    • tensorNames

      public Set<String> tensorNames()
      The tensor names, in insertion (or file) order.
    • valueNames

      public Set<String> valueNames()
      The value names.
    • hasTensor

      public boolean hasTensor(String name)
      Whether the dictionary holds a tensor named name.
    • value

      public String value(String name)
      The value name; throws when it is missing.
    • longValue

      public long longValue(String name)
      The value name as a long; throws when it is missing.
    • intValue

      public int intValue(String name)
      The value name as an int; throws when it is missing.
    • floatValue

      public float floatValue(String name)
      The value name as a float; throws when it is missing.
    • dtype

      public DType dtype(String name)
      The dtype of tensor name.
    • shape

      public int[] shape(String name)
      The shape of tensor name.
    • copyInto

      public void copyInto(String name, Tensor target)
      Overwrites the persistent tensor target (a parameter, a buffer, optimiser state) with tensor name, in place, so plans compiled with target keep using it. Dtype and shape must match.
    • check

      public void check(String name, Tensor target)
      Throws unless tensor name can be copied into target (see copyInto(String, Tensor)).
    • tensor

      public Tensor tensor(String name)
      Tensor name as a new persistent tensor (a weight that stays on the device).
    • save

      public void save(Path path) throws IOException
      Writes the dictionary to path as safetensors; reading live tensors waits for them.
      Throws:
      IOException
    • load

      public static StateDict load(Path path) throws IOException
      Opens a safetensors file; tensors are read when copied in.
      Throws:
      IOException