Class DeepSeekCheckpoint

java.lang.Object
smile.llm.model.deepseek.DeepSeekCheckpoint
All Implemented Interfaces:
AutoCloseable

public final class DeepSeekCheckpoint extends Object implements AutoCloseable
Reads tensors of a DeepSeek-V4.1 checkpoint (sharded safetensors) one at a time, so a slice of the model can be loaded without touching the other shards.

Dtypes: BF16, F32, I8 (packed fp4 experts) and F8_E4M3 load as such; F8_E8M0 scales load as raw uint8 exponent bytes (see DeepSeekOps.e8m0ToFloat(Tensor)). Tensors that live in a huge shard but were range-fetched into engram_small.safetensors (the engram projection, not its table) are found there.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Opens a checkpoint directory containing model.safetensors.index.json.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    dev(String name)
    Loads a tensor to the model's device (Dsv.device()).
    get(String name)
    Loads a tensor to CPU.
    getAs(String name, ScalarType dtype)
    Loads a tensor reinterpreted without conversion (view(dtype)), e.g.
    boolean
    has(String name)
    Whether a tensor is available locally.
    boolean
    isInt8(String name)
    Whether a tensor is stored as int8 (packed fp4 pairs), from the shard header.
    long
    Faults a tensor's whole mapping into the process (page-table entries), so that random row reads are plain memory accesses instead of one page fault each.
    long
    Reads a tensor's bytes sequentially so that the operating system caches the whole of it.
    readRows(String name, long[] rows)
    Reads rows of a 2-D tensor straight from its shard file (for the huge engram tables).
    long[]
    shape(String name)
    Shape of a tensor, from the shard header (nothing is read).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DeepSeekCheckpoint

      public DeepSeekCheckpoint(Path dir) throws IOException
      Opens a checkpoint directory containing model.safetensors.index.json.
      Parameters:
      dir - the checkpoint directory.
      Throws:
      IOException - if the index cannot be read.
  • Method Details

    • has

      public boolean has(String name)
      Whether a tensor is available locally.
      Parameters:
      name - the tensor name.
      Returns:
      true if the index lists it and its shard file exists.
    • get

      public Tensor get(String name)
      Loads a tensor to CPU.
      Parameters:
      name - the tensor name, e.g. layers.0.attn.wq_a.weight.
      Returns:
      the tensor (see the class comment for dtypes).
    • shape

      public long[] shape(String name)
      Shape of a tensor, from the shard header (nothing is read).
      Parameters:
      name - the tensor name.
      Returns:
      the shape.
    • isInt8

      public boolean isInt8(String name)
      Whether a tensor is stored as int8 (packed fp4 pairs), from the shard header.
      Parameters:
      name - the tensor name.
      Returns:
      true for I8.
    • readRows

      public Tensor readRows(String name, long[] rows)
      Reads rows of a 2-D tensor straight from its shard file (for the huge engram tables).
      Parameters:
      name - the tensor name.
      rows - the row ids.
      Returns:
      a CPU tensor [rows.length, width] of the tensor's dtype.
    • prefetch

      public long prefetch(String name)
      Reads a tensor's bytes sequentially so that the operating system caches the whole of it. Engram tables are read at random (one row per hashed n-gram), and every first touch of a page on a network file system costs a round trip; one sequential pass at start-up turns that into memory copies.
      Parameters:
      name - the tensor name.
      Returns:
      the number of bytes read.
    • populate

      public long populate(String name)
      Faults a tensor's whole mapping into the process (page-table entries), so that random row reads are plain memory accesses instead of one page fault each. Run after prefetch(String) has put the pages in the page cache.
      Parameters:
      name - the tensor name.
      Returns:
      the number of bytes mapped, 0 if the tensor cannot be mapped.
    • dev

      public Tensor dev(String name)
      Loads a tensor to the model's device (Dsv.device()).
      Parameters:
      name - the tensor name.
      Returns:
      the tensor.
    • getAs

      public Tensor getAs(String name, ScalarType dtype)
      Loads a tensor reinterpreted without conversion (view(dtype)), e.g. int8 bytes as fp4 pairs.
      Parameters:
      name - the tensor name.
      dtype - the dtype to view the bytes as.
      Returns:
      the viewed tensor.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException