Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions extension/android/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ non_fbcode_target(_kind = fb_android_library,
name = "executorch_llama",
warnings_as_errors = False,
srcs = [
"executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmCallback.java",
"executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmGenerationConfig.java",
"executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmModule.java",
"executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmModuleConfig.java",
"executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmCallback.kt",
"executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmGenerationConfig.kt",
"executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmModule.kt",
"executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmModuleConfig.kt",
],
autoglob = False,
language = "JAVA",
language = "KOTLIN",
Comment on lines 49 to +56
Copy link

Copilot AI Apr 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description says package-info.java was removed, but org/pytorch/executorch/extension/llm/package-info.java still exists in this package. Either remove it as described, or update the PR description to avoid confusion about what changed.

Copilot uses AI. Check for mistakes.
deps = [
":executorch",
"//fbandroid/java/com/facebook/jni:jni",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,42 @@
* LICENSE file in the root directory of this source tree.
*/

package org.pytorch.executorch.extension.llm;
package org.pytorch.executorch.extension.llm

import com.facebook.jni.annotations.DoNotStrip;
import org.pytorch.executorch.annotations.Experimental;
import com.facebook.jni.annotations.DoNotStrip
import org.pytorch.executorch.annotations.Experimental

/**
* Callback interface for Llama model. Users can implement this interface to receive the generated
* Callback interface for Llm model. Users can implement this interface to receive the generated
* tokens and statistics.
*
* <p>Warning: These APIs are experimental and subject to change without notice
* Warning: These APIs are experimental and subject to change without notice
*/
@Experimental
public interface LlmCallback {
interface LlmCallback {
/**
* Called when a new result is available from JNI. Users will keep getting onResult() invocations
* until generate() finishes.
*
* @param result Last generated token
*/
@DoNotStrip
public void onResult(String result);
@DoNotStrip fun onResult(result: String)

/**
* Called when the statistics for the generate() is available.
*
* <p>The result will be a JSON string. See extension/llm/stats.h for the field definitions.
* The result will be a JSON string. See extension/llm/stats.h for the field definitions.
*
* @param stats JSON string containing the statistics for the generate()
*/
@DoNotStrip
default void onStats(String stats) {}
@DoNotStrip fun onStats(stats: String) {}

/**
* Called when an error occurs during generate().
*
* @param errorCode Error code from the ExecuTorch runtime (see {@link
* org.pytorch.executorch.ExecutorchRuntimeException})
* @param errorCode Error code from the ExecuTorch runtime (see
* [org.pytorch.executorch.ExecutorchRuntimeException])
* @param message Human-readable error description
*/
@DoNotStrip
default void onError(int errorCode, String message) {}
@DoNotStrip fun onError(errorCode: Int, message: String) {}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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.
*
* This class provides settings for text generation behavior including output formatting, generation
* limits, and sampling parameters. Instances should be created using the [create] method and the
* fluent builder pattern.
*/
class LlmGenerationConfig
private constructor(
@get:JvmName("isEcho") val echo: Boolean,
val maxNewTokens: Int,
@get:JvmName("isWarming") val warming: Boolean,
val seqLen: Int,
val temperature: Float,
val numBos: Int,
val numEos: Int,
) {

companion object {
/**
* Creates a new Builder instance for constructing generation configurations.
*
* @return a new Builder with default configuration values
*/
@JvmStatic fun create(): Builder = Builder()
}

/**
* Builder class for constructing LlmGenerationConfig instances.
*
* Provides a fluent interface for configuring generation parameters with sensible defaults. All
* methods return the builder instance to enable method chaining.
*/
class Builder internal constructor() {
private var echo: Boolean = true
private var maxNewTokens: Int = -1
private var warming: Boolean = false
private var seqLen: Int = -1
private var temperature: Float = 0.8f
private var numBos: Int = 0
private var numEos: Int = 0

/** Sets whether to include the input prompt in the generated output. */
fun echo(echo: Boolean): Builder = apply { this.echo = echo }

/** Sets the maximum number of new tokens to generate. */
fun maxNewTokens(maxNewTokens: Int): Builder = apply { this.maxNewTokens = maxNewTokens }

/** Enables or disables model warming. */
fun warming(warming: Boolean): Builder = apply { this.warming = warming }

/** Sets the maximum sequence length for generation. */
fun seqLen(seqLen: Int): Builder = apply { this.seqLen = seqLen }

/** Sets the temperature for random sampling. */
fun temperature(temperature: Float): Builder = apply { this.temperature = temperature }

/** Sets the number of BOS tokens to prepend. */
fun numBos(numBos: Int): Builder = apply { this.numBos = numBos }

/** Sets the number of EOS tokens to append. */
fun numEos(numEos: Int): Builder = apply { this.numEos = numEos }

/** Constructs the LlmGenerationConfig instance with the configured parameters. */
fun build(): LlmGenerationConfig =
LlmGenerationConfig(echo, maxNewTokens, warming, seqLen, temperature, numBos, numEos)
}
}
Loading
Loading