Skip to content
Draft
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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,8 @@ if(EXECUTORCH_BUILD_EXTENSION_APPLE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/apple)
endif()



if(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/extension/data_loader)
install(
Expand Down
120 changes: 97 additions & 23 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
{
"name": "android-arm64-v8a",
"displayName": "Build executorch core and JNI bindings on android arm64-v8a",
"inherits": ["common"],
"inherits": [
"common"
],
"binaryDir": "${sourceDir}/cmake-out-android-arm64-v8a",
"cacheVariables": {
"EXECUTORCH_BUILD_PRESET_FILE": "${sourceDir}/tools/cmake/preset/android.cmake",
Expand All @@ -18,13 +20,19 @@
"condition": {
"type": "inList",
"string": "${hostSystemName}",
"list": ["Darwin", "Linux", "Windows"]
"list": [
"Darwin",
"Linux",
"Windows"
]
}
},
{
"name": "android-x86_64",
"displayName": "Build executorch core and JNI bindings on android x86_64",
"inherits": ["common"],
"inherits": [
"common"
],
"binaryDir": "${sourceDir}/cmake-out-android-x86_64",
"cacheVariables": {
"EXECUTORCH_BUILD_PRESET_FILE": "${sourceDir}/tools/cmake/preset/android.cmake",
Expand All @@ -33,13 +41,19 @@
"condition": {
"type": "inList",
"string": "${hostSystemName}",
"list": ["Darwin", "Linux", "Windows"]
"list": [
"Darwin",
"Linux",
"Windows"
]
}
},
{
"name": "macos",
"displayName": "Build ExecuTorch for macOS",
"inherits": ["common"],
"inherits": [
"common"
],
"generator": "Xcode",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/third-party/ios-cmake/ios.toolchain.cmake",
Expand All @@ -57,7 +71,9 @@
{
"name": "ios",
"displayName": "Build ExecuTorch for iOS",
"inherits": ["common"],
"inherits": [
"common"
],
"generator": "Xcode",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/third-party/ios-cmake/ios.toolchain.cmake",
Expand All @@ -75,7 +91,9 @@
{
"name": "ios-simulator",
"displayName": "Build ExecuTorch for iOS Simulator",
"inherits": ["common"],
"inherits": [
"common"
],
"generator": "Xcode",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/third-party/ios-cmake/ios.toolchain.cmake",
Expand All @@ -93,7 +111,9 @@
{
"name": "linux",
"displayName": "Build ExecuTorch for Linux",
"inherits": ["common"],
"inherits": [
"common"
],
"cacheVariables": {
"CMAKE_SYSTEM_NAME": "Linux",
"EXECUTORCH_BUILD_PRESET_FILE": "${sourceDir}/tools/cmake/preset/linux.cmake"
Expand All @@ -104,18 +124,55 @@
"rhs": "Linux"
}
},
{
"name": "jni",
"displayName": "Build ExecuTorch with JNI support",
"inherits": [
"common"
],
"cacheVariables": {
"EXECUTORCH_BUILD_EXECUTOR_RUNNER": "OFF",
"EXECUTORCH_BUILD_EXAMPLES": "OFF",
"EXECUTORCH_BUILD_HOST_JAVA": "ON",
"EXECUTORCH_BUILD_KERNELS_OPTIMIZED": "OFF",
"EXECUTORCH_BUILD_KERNELS_QUANTIZED": "OFF",
"EXECUTORCH_BUILD_DEVTOOLS": "OFF",
"EXECUTORCH_BUILD_EXTENSION_MODULE": "ON",
"EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR": "ON",
"EXECUTORCH_BUILD_EXTENSION_NAMED_DATA_MAP": "ON",
"EXECUTORCH_BUILD_EXTENSION_TENSOR": "ON",
"EXECUTORCH_BUILD_EXTENSION_DATA_LOADER": "ON",
"EXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL": "ON",
"EXECUTORCH_BUILD_ANDROID_JNI": "ON",
"JAVA_HOME": "$env{JAVA_HOME}"
},
"condition": {
"type": "inList",
"string": "${hostSystemName}",
"list": [
"Linux",
"Darwin"
]
}
},
{
"name": "pybind",
"displayName": "Build pybindings exported in the wheel",
"inherits": ["common"],
"inherits": [
"common"
],
"cacheVariables": {
"EXECUTORCH_BUILD_PRESET_FILE": "${sourceDir}/tools/cmake/preset/pybind.cmake",
"CMAKE_OSX_DEPLOYMENT_TARGET": "12.0"
},
"condition": {
"type": "inList",
"string": "${hostSystemName}",
"list": ["Darwin", "Linux", "Windows"]
"list": [
"Darwin",
"Linux",
"Windows"
]
}
},
{
Expand All @@ -131,7 +188,11 @@
"condition": {
"type": "inList",
"string": "${hostSystemName}",
"list": ["Darwin", "Linux", "Windows"]
"list": [
"Darwin",
"Linux",
"Windows"
]
}
},
{
Expand All @@ -157,7 +218,10 @@
"condition": {
"type": "inList",
"string": "${hostSystemName}",
"list": ["Linux", "Windows"]
"list": [
"Linux",
"Windows"
]
}
},
{
Expand Down Expand Up @@ -247,13 +311,19 @@
"condition": {
"type": "inList",
"string": "${hostSystemName}",
"list": ["Darwin", "Linux", "Windows"]
"list": [
"Darwin",
"Linux",
"Windows"
]
}
},
{
"name": "windows",
"displayName": "Build ExecuTorch for Windows",
"inherits": ["common"],
"inherits": [
"common"
],
"cacheVariables": {
"CMAKE_SYSTEM_NAME": "Windows",
"EXECUTORCH_BUILD_PRESET_FILE": "${sourceDir}/tools/cmake/preset/windows.cmake"
Expand All @@ -266,18 +336,22 @@
}
},
{
"name": "zephyr",
"displayName": "Build ExecuTorch for Zephyr RTOS",
"inherits": ["common"],
"cacheVariables": {
"EXECUTORCH_BUILD_PRESET_FILE": "${sourceDir}/tools/cmake/preset/zephyr.cmake",
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/examples/zephyr/x86_64-linux-arm-zephyr-eabi-gcc.cmake"
}
"name": "zephyr",
"displayName": "Build ExecuTorch for Zephyr RTOS",
"inherits": [
"common"
],
"cacheVariables": {
"EXECUTORCH_BUILD_PRESET_FILE": "${sourceDir}/tools/cmake/preset/zephyr.cmake",
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/examples/zephyr/x86_64-linux-arm-zephyr-eabi-gcc.cmake"
}
},
{
"name": "arm-baremetal",
"displayName": "Build ExecuTorch for Arm baremetal",
"inherits": ["common"],
"inherits": [
"common"
],
"cacheVariables": {
"EXECUTORCH_BUILD_PRESET_FILE": "${sourceDir}/tools/cmake/preset/arm_baremetal.cmake",
"CMAKE_TOOLCHAIN_FILE": "${sourceDir}/examples/arm/ethos-u-setup/arm-none-eabi-gcc.cmake"
Expand Down Expand Up @@ -449,4 +523,4 @@
]
}
]
}
}
116 changes: 116 additions & 0 deletions examples/java/LlamaChat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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 com.example.executorch;

import java.util.Scanner;
import org.pytorch.executorch.extension.llm.LlmCallback;
import org.pytorch.executorch.extension.llm.LlmModule;

/**
* Interactive chat application using LlmModule for text generation.
*
* Usage: LlamaChat <path_to_model.pte> <path_to_tokenizer>
*/
public class LlamaChat {
private static final int SEQ_LEN = 512;
private static final boolean ECHO = false;
private static final float TEMPERATURE = 0.7f;

public static void main(String[] args) {
System.out.println("LlamaChat: Starting...");

if (args.length < 2) {
System.out.println("Usage: LlamaChat <path_to_model.pte> <path_to_tokenizer>");
System.exit(1);
}

String ptePath = args[0];
String tokenizerPath = args[1];

try {
System.out.println("Loading model: " + ptePath);
System.out.println("Loading tokenizer: " + tokenizerPath);

// Create the LlmModule
LlmModule module = new LlmModule(LlmModule.MODEL_TYPE_TEXT, ptePath, tokenizerPath, TEMPERATURE);

// Load the model
int loadResult = module.load();
if (loadResult != 0) {
System.err.println("Failed to load model, error code: " + loadResult);
System.exit(1);
}
System.out.println("Model loaded successfully.");
System.out.println();

// Start interactive chat loop
Scanner scanner = new Scanner(System.in);
System.out.println("=== LlamaChat ===");
System.out.println("Type your message and press Enter. Type 'quit' or 'exit' to end.");
System.out.println();

while (true) {
System.out.print("You: ");
System.out.flush();

String input = scanner.nextLine();

if (input == null || input.trim().isEmpty()) {
continue;
}

String trimmedInput = input.trim().toLowerCase();
if (trimmedInput.equals("quit") || trimmedInput.equals("exit")) {
System.out.println("Goodbye!");
break;
}

System.out.print("Assistant: ");
System.out.flush();

StringBuilder response = new StringBuilder();

// Create callback to print tokens as they are generated
LlmCallback callback = new LlmCallback() {
@Override
public void onResult(String result) {
response.append(result);

}

@Override
public void onStats(String stats) {
// Optionally print stats for debugging
// System.out.println("\n[Stats: " + stats + "]");
}
};

// Generate response
int result = module.generate(input, SEQ_LEN, callback, ECHO, TEMPERATURE);

if (result != 0) {
System.out.println("\n[Generation ended with code: " + result + "]");
}

System.out.println(response.toString());
System.out.println();
}

// Clean up
scanner.close();
module.destroy();
System.out.println("LlamaChat: Finished.");

} catch (Exception e) {
System.err.println("Error occurred:");
e.printStackTrace();
System.exit(1);
}
}
}
Loading