Package 

Class LlmModule

  • All Implemented Interfaces:
    java.io.Closeable , java.lang.AutoCloseable

    
    public final class LlmModule
     implements Closeable
                        

    LlmModule is a wrapper around the Executorch LLM. It provides a simple interface to generate text from the model.

    Warning: These APIs are experimental and subject to change without notice

    • Constructor Detail

      • LlmModule

        LlmModule(Integer modelType, String modulePath, String tokenizerPath, Float temperature, List<String> dataFiles, Integer numBos, Integer numEos)
        Constructs a LLM Module for a model with given type, model path, tokenizer, temperature, and dataFiles.
      • LlmModule

        LlmModule(Integer modelType, String modulePath, String tokenizerPath, Float temperature, List<String> dataFiles)
        Constructs a LLM Module for a model with given type, model path, tokenizer, temperature, and dataFiles.
      • LlmModule

        LlmModule(Integer modelType, String modulePath, String tokenizerPath, Float temperature, String dataPath, Integer numBos, Integer numEos)
        Constructs a LLM Module for a model with given type, model path, tokenizer, temperature, and data path.
      • LlmModule

        LlmModule(Integer modelType, String modulePath, String tokenizerPath, Float temperature, String dataPath)
        Constructs a LLM Module for a model with given type, model path, tokenizer, temperature, and data path.
      • LlmModule

        LlmModule(String modulePath, String tokenizerPath, Float temperature)
        Constructs a LLM Module for a model with given model path, tokenizer, temperature.
      • LlmModule

        LlmModule(String modulePath, String tokenizerPath, Float temperature, String dataPath)
        Constructs a LLM Module for a model with given model path, tokenizer, temperature and data path.
      • LlmModule

        LlmModule(Integer modelType, String modulePath, String tokenizerPath, Float temperature)
        Constructs a LLM Module for a model with given path, tokenizer, and temperature.
      • LlmModule

        LlmModule(LlmModuleConfig config)
        Constructs a LLM Module for a model with the given LlmModuleConfig
    • Method Detail

      • close

         Unit close()

        Releases native resources. Callers must ensure no other methods are in-flight. Call stop and wait for generate to return before calling this method.

      • generate

         final Unit generate(String prompt, LlmCallback llmCallback)

        Start generating tokens from the module.

        Parameters:
        prompt - Input prompt
        llmCallback - callback object to receive results.
      • generate

         final Unit generate(String prompt, Integer seqLen, LlmCallback llmCallback)

        Start generating tokens from the module.

        Parameters:
        prompt - Input prompt
        seqLen - sequence length
        llmCallback - callback object to receive results.
      • generate

         final Unit generate(String prompt, LlmCallback llmCallback, Boolean echo)

        Start generating tokens from the module.

        Parameters:
        prompt - Input prompt
        llmCallback - callback object to receive results
        echo - indicate whether to echo the input prompt or not (text completion vs chat)
      • generate

         final Unit generate(String prompt, Integer seqLen, LlmCallback llmCallback, Boolean echo)

        Start generating tokens from the module.

        Parameters:
        prompt - Input prompt
        seqLen - sequence length
        llmCallback - callback object to receive results
        echo - indicate whether to echo the input prompt or not (text completion vs chat)
      • generate

         final Unit generate(String prompt, Integer seqLen, LlmCallback llmCallback, Boolean echo, Float temperature, Integer numBos, Integer numEos)

        Start generating tokens from the module.

        Parameters:
        prompt - Input prompt
        seqLen - sequence length
        llmCallback - callback object to receive results
        echo - indicate whether to echo the input prompt or not (text completion vs chat)
        temperature - temperature for sampling (use negative value to use module default)
        numBos - number of BOS tokens to prepend
        numEos - number of EOS tokens to append
      • generate

         final Unit generate(String prompt, LlmGenerationConfig config, LlmCallback llmCallback)

        Start generating tokens from the module.

        Parameters:
        prompt - Input prompt
        config - the config for generation
        llmCallback - callback object to receive results
      • generate

         final Unit generate(IntArray image, Integer width, Integer height, Integer channels, String prompt, Integer seqLen, LlmCallback llmCallback, Boolean echo)

        Start generating tokens from the module.

        Parameters:
        image - Input image as a byte array
        width - Input image width
        height - Input image height
        channels - Input image number of channels
        prompt - Input prompt
        seqLen - sequence length
        llmCallback - callback object to receive results.
        echo - indicate whether to echo the input prompt or not (text completion vs chat)
      • generate

         final Unit generate(IntArray image, Integer width, Integer height, Integer channels, String prompt, Integer seqLen, LlmCallback llmCallback, Boolean echo, Float temperature)

        Start generating tokens from the module.

        Parameters:
        image - Input image as a byte array
        width - Input image width
        height - Input image height
        channels - Input image number of channels
        prompt - Input prompt
        seqLen - sequence length
        llmCallback - callback object to receive results.
        echo - indicate whether to echo the input prompt or not (text completion vs chat)
        temperature - temperature for sampling (use negative value to use module default)
      • generate

         final Unit generate(IntArray image, Integer width, Integer height, Integer channels, String prompt, Integer seqLen, LlmCallback llmCallback, Boolean echo, Float temperature, Integer numBos, Integer numEos)

        Start generating tokens from the module.

        Parameters:
        image - Input image as a byte array
        width - Input image width
        height - Input image height
        channels - Input image number of channels
        prompt - Input prompt
        seqLen - sequence length
        llmCallback - callback object to receive results.
        echo - indicate whether to echo the input prompt or not (text completion vs chat)
        temperature - temperature for sampling (use negative value to use module default)
        numBos - number of BOS tokens to prepend
        numEos - number of EOS tokens to append
      • prefillImages

         final Unit prefillImages(IntArray image, Integer width, Integer height, Integer channels)

        Prefill the KV cache with the given image input.

        Parameters:
        image - Input image as a byte array
        width - Input image width
        height - Input image height
        channels - Input image number of channels
      • prefillImages

         final Unit prefillImages(ByteBuffer image, Integer width, Integer height, Integer channels)

        Prefill a multimodal Module with the given image input via a direct ByteBuffer. The buffer data is accessed directly without JNI array copies, unlike prefillImages. The ByteBuffer must contain raw uint8 pixel data in CHW format with at least channels * height * width bytes remaining. Only the first channels * height * width bytes from the buffer's current position are read; the position of the original ByteBuffer is not modified.

        Parameters:
        image - Input image as a direct ByteBuffer containing uint8 pixel data
        width - Input image width
        height - Input image height
        channels - Input image number of channels
      • prefillImages

         final Unit prefillImages(FloatArray image, Integer width, Integer height, Integer channels)

        Prefill the KV cache with the given normalized image input.

        Parameters:
        image - Input normalized image as a float array
        width - Input image width
        height - Input image height
        channels - Input image number of channels
      • prefillNormalizedImage

         final Unit prefillNormalizedImage(ByteBuffer image, Integer width, Integer height, Integer channels)

        Prefill a multimodal Module with the given normalized image input via a direct ByteBuffer. The buffer data is accessed directly without JNI array copies, unlike prefillImages. The ByteBuffer must contain normalized float pixel data in CHW format with at least channels * height * width * 4 bytes remaining. Only the first channels * height * width floats from the buffer's current position are consumed. The buffer must use the platform's native byte order (set via buffer.order(ByteOrder.nativeOrder())).

        Parameters:
        image - Input normalized image as a direct ByteBuffer containing float pixel data in native byte order
        width - Input image width
        height - Input image height
        channels - Input image number of channels
      • prefillAudio

         final Unit prefillAudio(ByteArray audio, Integer batchSize, Integer nBins, Integer nFrames)

        Prefill the KV cache with the given preprocessed audio input.

        Parameters:
        audio - Input preprocessed audio as a byte array
        batchSize - Input batch size
        nBins - Input number of bins
        nFrames - Input number of frames
      • prefillAudio

         final Unit prefillAudio(FloatArray audio, Integer batchSize, Integer nBins, Integer nFrames)

        Prefill the KV cache with the given preprocessed audio input.

        Parameters:
        audio - Input preprocessed audio as a float array
        batchSize - Input batch size
        nBins - Input number of bins
        nFrames - Input number of frames
      • prefillRawAudio

         final Unit prefillRawAudio(ByteArray audio, Integer batchSize, Integer nChannels, Integer nSamples)

        Prefill the KV cache with the given raw audio input.

        Parameters:
        audio - Input raw audio as a byte array
        batchSize - Input batch size
        nChannels - Input number of channels
        nSamples - Input number of samples
      • prefillPrompt

         final Unit prefillPrompt(String prompt)

        Prefill the KV cache with the given text prompt.

        Parameters:
        prompt - The text prompt to prefill.
      • resetContext

         final Unit resetContext()

        Reset the context of the LLM. This will clear the KV cache and reset the state of the LLM.

        The startPos will be reset to 0.

      • stop

         final Unit stop()

        Stop current generate() before it finishes.

      • load

         final Unit load()

        Force loading the module. Otherwise the model is loaded during first generate().