Class AbstractSpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>

java.lang.Object
org.spartan.internal.engine.model.AbstractSpartanModel<SpartanModelConfigType>
All Implemented Interfaces:
AutoCloseable, org.spartan.api.engine.SpartanModel<SpartanModelConfigType>
Direct Known Subclasses:
AutoEncoderCompressorModelImpl, CuriosityDrivenRecurrentSoftActorCriticModelImpl, DoubleDeepQNetworkModelImpl, ProximalPolicyOptimizationSpartanModelImpl, RecurrentSoftActorCriticModelImpl

public abstract class AbstractSpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig> extends Object implements org.spartan.api.engine.SpartanModel<SpartanModelConfigType>
Abstract base for SpartanModel with FFM memory management and Zero-GC tick().

The tick() method contains NO allocations - safe for high-frequency calls. Scalar parameters (agentId, counts) are now passed as primitives directly to C++.

  • Field Details

    • SIMD_PADDING_DOUBLES

      protected static final int SIMD_PADDING_DOUBLES
      SIMD padding in number of doubles.

      The C++ native code uses SIMD operations To prevent SIMD overreads from causing access violations when buffers end at page boundaries, we add significant padding to all buffer allocations. Increased from 4 to 128 to provide maximum safety margin.

      See Also:
    • arena

      protected final Arena arena
    • agentIdentifier

      protected final long agentIdentifier
    • config

      protected final SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig config
    • context

      protected final org.spartan.api.engine.context.SpartanContext context
    • configSegment

      protected final MemorySegment configSegment
    • modelWeightsBuffer

      protected final MemorySegment modelWeightsBuffer
    • actionOutputBuffer

      protected final MemorySegment actionOutputBuffer
    • modelWeightsCount

      protected final int modelWeightsCount
    • actionCount

      protected final int actionCount
    • identifier

      protected final String identifier
  • Constructor Details

    • AbstractSpartanModel

      protected AbstractSpartanModel(@NotNull @NotNull String identifier, long agentIdentifier, @NotNull SpartanModelConfigType config, @NotNull @NotNull org.spartan.api.engine.context.SpartanContext context, @NotNull @NotNull Arena sharedArena, long modelWeightCount, long configSegmentSize, int actionSize)
  • Method Details

    • requireContextSize

      protected static int requireContextSize(@NotNull @NotNull org.spartan.api.engine.context.SpartanContext context)
    • register

      public void register()
      Specified by:
      register in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • tick

      public void tick()
      Specified by:
      tick in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • executeNativeTick

      protected void executeNativeTick(double reward)
    • postTickActions

      protected void postTickActions()
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • getAgentIdentifier

      public long getAgentIdentifier()
      Specified by:
      getAgentIdentifier in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • getIdentifier

      @NotNull public @NotNull String getIdentifier()
      Specified by:
      getIdentifier in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • getSpartanContext

      @NotNull public @NotNull org.spartan.api.engine.context.SpartanContext getSpartanContext()
      Specified by:
      getSpartanContext in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • getSpartanModelConfig

      @NotNull public SpartanModelConfigType getSpartanModelConfig()
      Specified by:
      getSpartanModelConfig in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • getActionOutputBuffer

      @NotNull public @NotNull MemorySegment getActionOutputBuffer()
      Specified by:
      getActionOutputBuffer in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • getModelWeightsBuffer

      @NotNull public @NotNull MemorySegment getModelWeightsBuffer()
      Specified by:
      getModelWeightsBuffer in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • isRegistered

      public boolean isRegistered()
      Specified by:
      isRegistered in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • saveModel

      public void saveModel(@NotNull @NotNull Path filePath) throws org.spartan.api.exception.SpartanPersistenceException
      Specified by:
      saveModel in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
      Throws:
      org.spartan.api.exception.SpartanPersistenceException
    • loadModel

      public void loadModel(@NotNull @NotNull Path filePath) throws org.spartan.api.exception.SpartanPersistenceException
      Specified by:
      loadModel in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
      Throws:
      org.spartan.api.exception.SpartanPersistenceException
    • copy

      @NotNull public @NotNull org.spartan.api.engine.SpartanModel<SpartanModelConfigType> copy(@NotNull @NotNull String newIdentifier)
      Specified by:
      copy in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • getModelType

      protected abstract org.spartan.api.engine.config.SpartanModelType getModelType()
      Returns the model type for this instance. Each subclass must provide its own type.
      Returns:
      the model type
    • setLiveExplorationRate

      public void setLiveExplorationRate(double newEpsilon)
      Specified by:
      setLiveExplorationRate in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • getEpisodeReward

      public double getEpisodeReward()
      Specified by:
      getEpisodeReward in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • resetEpisode

      public void resetEpisode()
      Specified by:
      resetEpisode in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • decayExploration

      public void decayExploration()
      Specified by:
      decayExploration in interface org.spartan.api.engine.SpartanModel<SpartanModelConfigType extends org.spartan.api.engine.config.SpartanModelConfig>
    • release

      protected void release()
      Unregisters from C++ and releases resources.

      This method is called by the framework when the model is no longer needed. Subclasses should not override this method.

    • writeConfigToSegment

      protected abstract void writeConfigToSegment()
    • getCriticWeightsBufferInternal

      @NotNull protected abstract @NotNull MemorySegment getCriticWeightsBufferInternal()
    • getCriticWeightsCount

      protected abstract int getCriticWeightsCount()