-
Notifications
You must be signed in to change notification settings - Fork 525
[Android] New config API for Llm init and generate #10345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
...torch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmGenerationConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package org.pytorch.executorch.extension.llm; | ||
|
||
/** | ||
* Configuration class for controlling text generation parameters in LLM operations. | ||
* | ||
* <p>This class provides settings for text generation behavior including output formatting, | ||
* generation limits, and sampling parameters. Instances should be created using the {@link | ||
* #create()} method and the fluent builder pattern. | ||
*/ | ||
public class LlmGenerationConfig { | ||
private final boolean echo; | ||
private final int maxNewTokens; | ||
private final boolean warming; | ||
private final int seqLen; | ||
private final float temperature; | ||
|
||
private LlmGenerationConfig(Builder builder) { | ||
this.echo = builder.echo; | ||
this.maxNewTokens = builder.maxNewTokens; | ||
this.warming = builder.warming; | ||
this.seqLen = builder.seqLen; | ||
this.temperature = builder.temperature; | ||
} | ||
|
||
/** | ||
* Creates a new Builder instance for constructing generation configurations. | ||
* | ||
* @return a new Builder with default configuration values | ||
*/ | ||
public static Builder create() { | ||
return new Builder(); | ||
} | ||
|
||
/** | ||
* @return true if input prompt should be included in the output | ||
*/ | ||
public boolean isEcho() { | ||
return echo; | ||
} | ||
|
||
/** | ||
* @return maximum number of tokens to generate (-1 for unlimited) | ||
*/ | ||
public int getMaxNewTokens() { | ||
return maxNewTokens; | ||
} | ||
|
||
/** | ||
* @return true if model warming is enabled | ||
*/ | ||
public boolean isWarming() { | ||
return warming; | ||
} | ||
|
||
/** | ||
* @return maximum sequence length for generation (-1 for default) | ||
*/ | ||
public int getSeqLen() { | ||
return seqLen; | ||
} | ||
|
||
/** | ||
* @return temperature value for sampling (higher = more random) | ||
*/ | ||
public float getTemperature() { | ||
return temperature; | ||
} | ||
|
||
/** | ||
* Builder class for constructing LlmGenerationConfig instances. | ||
* | ||
* <p>Provides a fluent interface for configuring generation parameters with sensible defaults. | ||
* All methods return the builder instance to enable method chaining. | ||
*/ | ||
public static class Builder { | ||
private boolean echo = true; | ||
private int maxNewTokens = -1; | ||
private boolean warming = false; | ||
private int seqLen = -1; | ||
private float temperature = 0.8f; | ||
|
||
Builder() {} | ||
|
||
/** | ||
* Sets whether to include the input prompt in the generated output. | ||
* | ||
* @param echo true to include input prompt, false to return only new tokens | ||
* @return this builder instance | ||
*/ | ||
public Builder echo(boolean echo) { | ||
this.echo = echo; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the maximum number of new tokens to generate. | ||
* | ||
* @param maxNewTokens the token limit (-1 for unlimited generation) | ||
* @return this builder instance | ||
*/ | ||
public Builder maxNewTokens(int maxNewTokens) { | ||
this.maxNewTokens = maxNewTokens; | ||
return this; | ||
} | ||
|
||
/** | ||
* Enables or disables model warming. | ||
* | ||
* @param warming true to generate initial tokens for model warmup | ||
* @return this builder instance | ||
*/ | ||
public Builder warming(boolean warming) { | ||
this.warming = warming; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the maximum sequence length for generation. | ||
* | ||
* @param seqLen maximum sequence length (-1 for default behavior) | ||
* @return this builder instance | ||
*/ | ||
public Builder seqLen(int seqLen) { | ||
this.seqLen = seqLen; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the temperature for random sampling. | ||
* | ||
* @param temperature sampling temperature (typical range 0.0-1.0) | ||
* @return this builder instance | ||
*/ | ||
public Builder temperature(float temperature) { | ||
this.temperature = temperature; | ||
return this; | ||
} | ||
|
||
/** | ||
* Constructs the LlmGenerationConfig instance with the configured parameters. | ||
* | ||
* @return new LlmGenerationConfig instance with current builder settings | ||
*/ | ||
public LlmGenerationConfig build() { | ||
return new LlmGenerationConfig(this); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
...xecutorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmModuleConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package org.pytorch.executorch.extension.llm; | ||
|
||
/** | ||
* Configuration class for initializing a LlmModule. | ||
* | ||
* <p>{@link #create()} method and the fluent builder pattern. | ||
*/ | ||
public class LlmModuleConfig { | ||
private final String modulePath; | ||
private final String tokenizerPath; | ||
private final float temperature; | ||
private final String dataPath; | ||
private final int modelType; | ||
|
||
private LlmModuleConfig(Builder builder) { | ||
this.modulePath = builder.modulePath; | ||
this.tokenizerPath = builder.tokenizerPath; | ||
this.temperature = builder.temperature; | ||
this.dataPath = builder.dataPath; | ||
this.modelType = builder.modelType; | ||
} | ||
|
||
/** Model type constant for text-only models. */ | ||
public static final int MODEL_TYPE_TEXT = 1; | ||
|
||
/** Model type constant for text-and-vision multimodal models. */ | ||
public static final int MODEL_TYPE_TEXT_VISION = 2; | ||
|
||
/** | ||
* Creates a new Builder instance for constructing LlmModuleConfig objects. | ||
* | ||
* @return a new Builder instance with default configuration values | ||
*/ | ||
public static Builder create() { | ||
return new Builder(); | ||
} | ||
|
||
// Getters with documentation | ||
/** | ||
* @return Path to the compiled model module (.pte file) | ||
*/ | ||
public String getModulePath() { | ||
return modulePath; | ||
} | ||
|
||
/** | ||
* @return Path to the tokenizer file or directory | ||
*/ | ||
public String getTokenizerPath() { | ||
return tokenizerPath; | ||
} | ||
|
||
/** | ||
* @return Temperature value for sampling (higher = more random) | ||
*/ | ||
public float getTemperature() { | ||
return temperature; | ||
} | ||
|
||
/** | ||
* @return Optional path to additional data files | ||
*/ | ||
public String getDataPath() { | ||
return dataPath; | ||
} | ||
|
||
/** | ||
* @return Type of model (text-only or text-vision) | ||
*/ | ||
public int getModelType() { | ||
return modelType; | ||
} | ||
|
||
/** | ||
* Builder class for constructing LlmModuleConfig instances with optional parameters. | ||
* | ||
* <p>The builder provides a fluent interface for configuring model parameters and validates | ||
* required fields before construction. | ||
*/ | ||
public static class Builder { | ||
private String modulePath; | ||
private String tokenizerPath; | ||
private float temperature = 0.8f; | ||
private String dataPath = ""; | ||
private int modelType = MODEL_TYPE_TEXT; | ||
|
||
Builder() {} | ||
|
||
/** | ||
* Sets the path to the module. | ||
* | ||
* @param modulePath Path to module | ||
* @return This builder instance for method chaining | ||
*/ | ||
public Builder modulePath(String modulePath) { | ||
this.modulePath = modulePath; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the path to the tokenizer. | ||
* | ||
* @param tokenizerPath Path to tokenizer | ||
* @return This builder instance for method chaining | ||
*/ | ||
public Builder tokenizerPath(String tokenizerPath) { | ||
this.tokenizerPath = tokenizerPath; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the temperature for sampling generation. | ||
* | ||
* @param temperature Temperature value (typical range 0.0-1.0) | ||
* @return This builder instance for method chaining | ||
*/ | ||
public Builder temperature(float temperature) { | ||
this.temperature = temperature; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the path to optional additional data files. | ||
* | ||
* @param dataPath Path to supplementary data resources | ||
* @return This builder instance for method chaining | ||
*/ | ||
public Builder dataPath(String dataPath) { | ||
this.dataPath = dataPath; | ||
return this; | ||
} | ||
|
||
/** | ||
* Sets the model type (text-only or multimodal). | ||
* | ||
* @param modelType One of MODEL_TYPE_TEXT or MODEL_TYPE_TEXT_VISION | ||
* @return This builder instance for method chaining | ||
*/ | ||
public Builder modelType(int modelType) { | ||
this.modelType = modelType; | ||
return this; | ||
} | ||
|
||
/** | ||
* Constructs the LlmModuleConfig instance with validated parameters. | ||
* | ||
* @return New LlmModuleConfig instance with configured values | ||
* @throws IllegalArgumentException if required fields are missing | ||
*/ | ||
public LlmModuleConfig build() { | ||
if (modulePath == null || tokenizerPath == null) { | ||
throw new IllegalArgumentException("Module path and tokenizer path are required"); | ||
} | ||
return new LlmModuleConfig(this); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The builder validates that modulePath is required, but there is no builder method to set modulePath. Consider adding a method like 'public Builder modulePath(String modulePath)' to allow the module path to be set.
Copilot is powered by AI, so mistakes are possible. Review output carefully before use.