-
- 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classLlmModule.Companion
-
Constructor Summary
Constructors Constructor Description 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(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(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(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(String modulePath, String tokenizerPath, Float temperature)Constructs a LLM Module for a model with given model path, tokenizer, temperature. 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(Integer modelType, String modulePath, String tokenizerPath, Float temperature)Constructs a LLM Module for a model with given path, tokenizer, and temperature. LlmModule(LlmModuleConfig config)Constructs a LLM Module for a model with the given LlmModuleConfig
-
Method Summary
Modifier and Type Method Description Unitclose()Releases native resources. final UnitresetNative()final Unitgenerate(String prompt, LlmCallback llmCallback)Start generating tokens from the module. final Unitgenerate(String prompt, Integer seqLen, LlmCallback llmCallback)Start generating tokens from the module. final Unitgenerate(String prompt, LlmCallback llmCallback, Boolean echo)Start generating tokens from the module. final Unitgenerate(String prompt, Integer seqLen, LlmCallback llmCallback, Boolean echo)Start generating tokens from the module. final Unitgenerate(String prompt, Integer seqLen, LlmCallback llmCallback, Boolean echo, Float temperature, Integer numBos, Integer numEos)Start generating tokens from the module. final Unitgenerate(String prompt, LlmGenerationConfig config, LlmCallback llmCallback)Start generating tokens from the module. final Unitgenerate(IntArray image, Integer width, Integer height, Integer channels, String prompt, Integer seqLen, LlmCallback llmCallback, Boolean echo)Start generating tokens from the module. final Unitgenerate(IntArray image, Integer width, Integer height, Integer channels, String prompt, Integer seqLen, LlmCallback llmCallback, Boolean echo, Float temperature)Start generating tokens from the module. final Unitgenerate(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. final UnitprefillImages(IntArray image, Integer width, Integer height, Integer channels)Prefill the KV cache with the given image input. final UnitprefillImages(ByteBuffer image, Integer width, Integer height, Integer channels)Prefill a multimodal Module with the given image input via a direct ByteBuffer. final UnitprefillImages(FloatArray image, Integer width, Integer height, Integer channels)Prefill the KV cache with the given normalized image input. final UnitprefillNormalizedImage(ByteBuffer image, Integer width, Integer height, Integer channels)Prefill a multimodal Module with the given normalized image input via a direct ByteBuffer. final UnitprefillAudio(ByteArray audio, Integer batchSize, Integer nBins, Integer nFrames)Prefill the KV cache with the given preprocessed audio input. final UnitprefillAudio(FloatArray audio, Integer batchSize, Integer nBins, Integer nFrames)Prefill the KV cache with the given preprocessed audio input. final UnitprefillRawAudio(ByteArray audio, Integer batchSize, Integer nChannels, Integer nSamples)Prefill the KV cache with the given raw audio input. final UnitprefillPrompt(String prompt)Prefill the KV cache with the given text prompt. final UnitresetContext()Reset the context of the LLM. final Unitstop()Stop current generate() before it finishes. final Unitload()Force loading the module. -
-
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
-
resetNative
@Deprecated(message = Use close() instead, replaceWith = @ReplaceWith(imports = {}, expression = close())) final Unit resetNative()
-
generate
final Unit generate(String prompt, LlmCallback llmCallback)
Start generating tokens from the module.
- Parameters:
prompt- Input promptllmCallback- callback object to receive results.
-
generate
final Unit generate(String prompt, Integer seqLen, LlmCallback llmCallback)
Start generating tokens from the module.
- Parameters:
prompt- Input promptseqLen- sequence lengthllmCallback- callback object to receive results.
-
generate
final Unit generate(String prompt, LlmCallback llmCallback, Boolean echo)
Start generating tokens from the module.
- Parameters:
prompt- Input promptllmCallback- callback object to receive resultsecho- 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 promptseqLen- sequence lengthllmCallback- callback object to receive resultsecho- 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 promptseqLen- sequence lengthllmCallback- callback object to receive resultsecho- 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 prependnumEos- 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 promptconfig- the config for generationllmCallback- 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 arraywidth- Input image widthheight- Input image heightchannels- Input image number of channelsprompt- Input promptseqLen- sequence lengthllmCallback- 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 arraywidth- Input image widthheight- Input image heightchannels- Input image number of channelsprompt- Input promptseqLen- sequence lengthllmCallback- 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 arraywidth- Input image widthheight- Input image heightchannels- Input image number of channelsprompt- Input promptseqLen- sequence lengthllmCallback- 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 prependnumEos- 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 arraywidth- Input image widthheight- Input image heightchannels- 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 datawidth- Input image widthheight- Input image heightchannels- 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 arraywidth- Input image widthheight- Input image heightchannels- 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 orderwidth- Input image widthheight- Input image heightchannels- 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 arraybatchSize- Input batch sizenBins- Input number of binsnFrames- 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 arraybatchSize- Input batch sizenBins- Input number of binsnFrames- 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 arraybatchSize- Input batch sizenChannels- Input number of channelsnSamples- 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.
-
-
-
-