Package ai.djl.engine

Class Engine


  • public abstract class Engine
    extends java.lang.Object
    The Engine interface is the base of the provided implementation for DJL.

    Any engine-specific functionality should be provided through this class. In general, it should contain methods to detect information about the usable machine hardware and to create a new NDManager and Model.

    See Also:
    Engine Guide, EngineProvider, The guide on resource and engine caching
    • Constructor Detail

      • Engine

        public Engine()
    • Method Detail

      • getAlternativeEngine

        public abstract Engine getAlternativeEngine()
        Returns the alternative engine if available.
        Returns:
        the alternative engine
      • getEngineName

        public abstract java.lang.String getEngineName()
        Returns the name of the Engine.
        Returns:
        the name of the engine
      • getRank

        public abstract int getRank()
        Return the rank of the Engine.
        Returns:
        the rank of the engine
      • getDefaultEngineName

        public static java.lang.String getDefaultEngineName()
        Returns the default Engine name.
        Returns:
        the default Engine name
      • getInstance

        public static Engine getInstance()
        Returns the default Engine.
        Returns:
        the instance of Engine
        See Also:
        EngineProvider
      • hasEngine

        public static boolean hasEngine​(java.lang.String engineName)
        Returns if the specified engine is available.
        Parameters:
        engineName - the name of Engine to check
        Returns:
        true if the specified engine is available
        See Also:
        EngineProvider
      • registerEngine

        public static void registerEngine​(EngineProvider provider)
        Registers a EngineProvider if not registered.
        Parameters:
        provider - the EngineProvider to be registered
      • getAllEngines

        public static java.util.Set<java.lang.String> getAllEngines()
        Returns a set of engine names that are loaded.
        Returns:
        a set of engine names that are loaded
      • getEngine

        public static Engine getEngine​(java.lang.String engineName)
        Returns the Engine with the given name.
        Parameters:
        engineName - the name of Engine to retrieve
        Returns:
        the instance of Engine
        See Also:
        EngineProvider
      • getVersion

        public abstract java.lang.String getVersion()
        Returns the version of the deep learning engine.
        Returns:
        the version number of the deep learning engine
      • hasCapability

        public abstract boolean hasCapability​(java.lang.String capability)
        Returns whether the engine has the specified capability.
        Parameters:
        capability - the capability to retrieve
        Returns:
        true if the engine has the specified capability
      • defaultDevice

        public Device defaultDevice()
        Returns the engine's default Device.
        Returns:
        the engine's default Device
      • getDevices

        public Device[] getDevices()
        Returns an array of devices.

        If GPUs are available, it will return an array of Device of size \(min(numAvailable, maxGpus)\). Else, it will return an array with a single CPU device.

        Returns:
        an array of devices
      • getDevices

        public Device[] getDevices​(int maxGpus)
        Returns an array of devices given the maximum number of GPUs to use.

        If GPUs are available, it will return an array of Device of size \(min(numAvailable, maxGpus)\). Else, it will return an array with a single CPU device.

        Parameters:
        maxGpus - the max number of GPUs to use. Use 0 for no GPUs.
        Returns:
        an array of devices
      • getGpuCount

        public int getGpuCount()
        Returns the number of GPUs available in the system.
        Returns:
        the number of GPUs available in the system
      • newSymbolBlock

        public SymbolBlock newSymbolBlock​(NDManager manager)
        Construct an empty SymbolBlock for loading.
        Parameters:
        manager - the manager to manage parameters
        Returns:
        Empty SymbolBlock for static graph
      • newModel

        public abstract Model newModel​(java.lang.String name,
                                       Device device)
        Constructs a new model.
        Parameters:
        name - the model name
        device - the device that the model will be loaded onto
        Returns:
        a new Model instance using the network defined in block
      • newBaseManager

        public abstract NDManager newBaseManager()
        Creates a new top-level NDManager.

        NDManager will inherit default Device.

        Returns:
        a new top-level NDManager
      • newBaseManager

        public abstract NDManager newBaseManager​(Device device)
        Creates a new top-level NDManager with specified Device.
        Parameters:
        device - the default Device
        Returns:
        a new top-level NDManager
      • setRandomSeed

        public void setRandomSeed​(int seed)
        Seeds the random number generator in DJL Engine.

        This will affect all Devices and all operators using Engine's random number generator.

        Parameters:
        seed - the seed to be fixed in Engine
      • getSeed

        public java.lang.Integer getSeed()
        Returns the random seed in DJL Engine.
        Returns:
        seed the seed to be fixed in Engine
      • getDjlVersion

        public static java.lang.String getDjlVersion()
        Returns the DJL API version.
        Returns:
        seed the seed to be fixed in Engine
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • debugEnvironment

        public static void debugEnvironment()
        Prints debug information about the environment for debugging environment issues.