Class Module

java.lang.Object
ai.beestream.jitnn.nn.Module
All Implemented Interfaces:
AutoCloseable
Direct Known Subclasses:
AdaptiveAvgPool, AdaptiveMaxPool, Add, AvgPool1d, AvgPool2d, AvgPool3d, BatchNorm2d, Bert, CELU, CharRnn, Conv1d, Conv2d, Conv3d, ConvTranspose2d, Cube, Dcgan.Discriminator, Dcgan.Generator, Doc2Vec, Dropout, ELU, Embedding, EmbeddingBag, Flatten, GlobalAvgPool2d, GloVe, GPT2, GraphConv, GroupNorm, GRU, HardSigmoid, HardSwish, HardTanh, InstanceNorm, LayerNorm, LeakyReLU, Linear, Llama, LocalResponseNorm, LSTM, MaxPool1d, MaxPool2d, MaxPool3d, Mish, MultiheadAttention, Pad, PReLU, ReLU, ReLU6, RMSNorm, RReLU, SELU, Sequential, Sigmoid, Softplus, Softsign, SyncBatchNorm2d, Tanh, ThresholdedReLU, UNet, Upsample, Vae, Word2Vec

public abstract class Module extends Object implements AutoCloseable
A differentiable building block with parameters and child modules.

A module owns its parameters and buffers, and its children's. close() releases them on the device. Training a module (forward and backward, zeroGrad, its optimiser) is for one thread at a time; inference in evaluation mode (Grad.inference) may run from several threads at once, sharing the weights.

  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    protected Tensor
    buffer(Tensor init)
    Registers persistent state that is not trained (running statistics).
    This module's buffers and its children's.
    protected <M extends Module>
    M
    child(M module)
    Adds a child module; returns it.
    void
    Releases the parameters and buffers of this module and its children, their gradients, and the weights jitNN derived from them (BatchNorm-folded and channels-last copies).
    abstract Tensor
    Applies the module to x.
    boolean
    Whether close() was called.
    boolean
    Whether the module is in training mode (the default).
    void
    Overwrites the parameters and buffers with those of d, in place, so compiled plans keep using them.
    protected Tensor
    Registers a trainable parameter initialised from init.
    long
    The number of trainable values: the elements of every parameter.
    This module's parameters and its children's, in registration order.
    The parameters (parameters.<i>) and buffers (buffers.<i>) in registration order, referring to the live tensors: save it with StateDict.save(Path).
    train(boolean on)
    Switches this module and its children to training mode (on) or evaluation mode; returns this module.
    void
    Clears the gradients of every parameter.

    Methods inherited from class Object

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

    • Module

      public Module()
  • Method Details

    • forward

      public abstract Tensor forward(Tensor x)
      Applies the module to x.
    • parameter

      protected Tensor parameter(Tensor init)
      Registers a trainable parameter initialised from init.
    • buffer

      protected Tensor buffer(Tensor init)
      Registers persistent state that is not trained (running statistics).
    • buffers

      public List<Tensor> buffers()
      This module's buffers and its children's.
    • child

      protected <M extends Module> M child(M module)
      Adds a child module; returns it.
    • parameters

      public List<Tensor> parameters()
      This module's parameters and its children's, in registration order.
    • zeroGrad

      public void zeroGrad()
      Clears the gradients of every parameter.
    • train

      public Module train(boolean on)
      Switches this module and its children to training mode (on) or evaluation mode; returns this module.
    • isTraining

      public boolean isTraining()
      Whether the module is in training mode (the default).
    • parameterCount

      public long parameterCount()
      The number of trainable values: the elements of every parameter.
    • stateDict

      public StateDict stateDict()
      The parameters (parameters.<i>) and buffers (buffers.<i>) in registration order, referring to the live tensors: save it with StateDict.save(Path). Names are positions, so a checkpoint loads into a model built the same way.
    • loadStateDict

      public void loadStateDict(StateDict d)
      Overwrites the parameters and buffers with those of d, in place, so compiled plans keep using them. Every name, dtype and shape must match.
    • close

      public void close()
      Releases the parameters and buffers of this module and its children, their gradients, and the weights jitNN derived from them (BatchNorm-folded and channels-last copies). Using the module afterwards is an error. Plans compiled for it stay cached for a model of the same structure, which reuses them without compiling, and are closed once idle (jitnn.plan.idleExecutions) or by JitnnRuntime.reset(). Closing twice does nothing.
      Specified by:
      close in interface AutoCloseable
    • isClosed

      public boolean isClosed()
      Whether close() was called.