|
| 1 | +//JAVA 21 |
| 2 | +//PREVIEW |
| 3 | +//DEPS io.github.beehive-lab:gpu-llama3:0.3.2-dev |
| 4 | +//DEPS io.github.beehive-lab:tornado-api:2.2.0 |
| 5 | +//DEPS io.github.beehive-lab:tornado-runtime:2.2.0 |
| 6 | + |
| 7 | +//SOURCES TornadoFlags.java |
| 8 | +// === Set to not get annoying warnings about annotation processing |
| 9 | +//JAVAC_OPTIONS -proc:full |
| 10 | + |
| 11 | +// Compiler options |
| 12 | +//JAVAC_OPTIONS --enable-preview |
| 13 | +//JAVAC_OPTIONS --add-modules=jdk.incubator.vector |
| 14 | + |
| 15 | +// JVM options for basic setup |
| 16 | +//JAVA_OPTIONS --enable-preview |
| 17 | +//JAVA_OPTIONS --add-modules=jdk.incubator.vector |
| 18 | + |
| 19 | +package org.beehive.gpullama3.cli; |
| 20 | + |
| 21 | +import org.beehive.gpullama3.Options; |
| 22 | +import org.beehive.gpullama3.auxiliary.LastRunMetrics; |
| 23 | +import org.beehive.gpullama3.inference.sampler.Sampler; |
| 24 | +import org.beehive.gpullama3.model.Model; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | + |
| 28 | +import static org.beehive.gpullama3.inference.sampler.Sampler.createSampler; |
| 29 | +import static org.beehive.gpullama3.model.loader.ModelLoader.loadModel; |
| 30 | + |
| 31 | +/** |
| 32 | + * LlamaTornadoCli - Pure Java CLI for running llama-tornado models |
| 33 | + * |
| 34 | + * This class provides a standalone command-line interface for running LLaMA models |
| 35 | + * with TornadoVM acceleration. It can be executed directly with JBang or as a |
| 36 | + * compiled Java application. |
| 37 | + * |
| 38 | + * Usage with JBang: |
| 39 | + * jbang LlamaTornadoCli.java --model path/to/model.gguf --prompt "Your prompt here" |
| 40 | + * |
| 41 | + * Usage as compiled application: |
| 42 | + * java --enable-preview --add-modules jdk.incubator.vector \ |
| 43 | + * -cp target/gpu-llama3-0.3.1.jar \ |
| 44 | + * org.beehive.gpullama3.cli.LlamaTornadoCli \ |
| 45 | + * --model path/to/model.gguf --prompt "Your prompt here" |
| 46 | + * |
| 47 | + * Examples: |
| 48 | + * # Interactive chat mode |
| 49 | + * jbang LlamaTornadoCli.java -m model.gguf --interactive |
| 50 | + * |
| 51 | + * # Single instruction mode |
| 52 | + * jbang LlamaTornadoCli.java -m model.gguf -p "Explain quantum computing" |
| 53 | + * |
| 54 | + * # With TornadoVM acceleration |
| 55 | + * jbang LlamaTornadoCli.java -m model.gguf -p "Hello" --use-tornadovm true |
| 56 | + * |
| 57 | + * # Custom temperature and sampling |
| 58 | + * jbang LlamaTornadoCli.java -m model.gguf -p "Tell me a story" \ |
| 59 | + * --temperature 0.7 --top-p 0.9 --max-tokens 512 |
| 60 | + */ |
| 61 | +public class LlamaTornadoCli { |
| 62 | + |
| 63 | + // Configuration flags |
| 64 | + public static final boolean USE_VECTOR_API = Boolean.parseBoolean( |
| 65 | + System.getProperty("llama.VectorAPI", "true")); |
| 66 | + public static final boolean SHOW_PERF_INTERACTIVE = Boolean.parseBoolean( |
| 67 | + System.getProperty("llama.ShowPerfInteractive", "true")); |
| 68 | + |
| 69 | + /** |
| 70 | + * Run a single instruction and display the response |
| 71 | + */ |
| 72 | + private static void runSingleInstruction(Model model, Sampler sampler, Options options) { |
| 73 | + String response = model.runInstructOnce(sampler, options); |
| 74 | + System.out.println(response); |
| 75 | + if (SHOW_PERF_INTERACTIVE) { |
| 76 | + LastRunMetrics.printMetrics(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Main entry point for the CLI application |
| 82 | + * |
| 83 | + * @param args command-line arguments (see Options.parseOptions for details) |
| 84 | + * @throws IOException if model loading fails |
| 85 | + */ |
| 86 | + public static void main(String[] args) throws IOException { |
| 87 | + // Print banner |
| 88 | + printBanner(); |
| 89 | + |
| 90 | + // Check if help requested |
| 91 | + if (args.length == 0 || hasHelpFlag(args)) { |
| 92 | + Options.printUsage(System.out); |
| 93 | + System.exit(0); |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + // Parse options |
| 98 | + Options options = Options.parseOptions(args); |
| 99 | + |
| 100 | + // Load model |
| 101 | + Model model = loadModel(options); |
| 102 | + |
| 103 | + // Create sampler |
| 104 | + Sampler sampler = createSampler(model, options); |
| 105 | + |
| 106 | + // Run in interactive or single-instruction mode |
| 107 | + if (options.interactive()) { |
| 108 | + System.out.println("Starting interactive chat mode..."); |
| 109 | + System.out.println("Type your messages below (Ctrl+C to exit):"); |
| 110 | + System.out.println(); |
| 111 | + model.runInteractive(sampler, options); |
| 112 | + } else { |
| 113 | + runSingleInstruction(model, sampler, options); |
| 114 | + } |
| 115 | + } catch (Exception e) { |
| 116 | + System.err.println("Error: " + e.getMessage()); |
| 117 | + e.printStackTrace(); |
| 118 | + System.exit(1); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Check if help flag is present in arguments |
| 124 | + */ |
| 125 | + private static boolean hasHelpFlag(String[] args) { |
| 126 | + for (String arg : args) { |
| 127 | + if (arg.equals("--help") || arg.equals("-h")) { |
| 128 | + return true; |
| 129 | + } |
| 130 | + } |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Print ASCII banner |
| 136 | + */ |
| 137 | + private static void printBanner() { |
| 138 | + System.out.println(""" |
| 139 | + ╔══════════════════════════════════════════════════════════╗ |
| 140 | + ║ Llama-Tornado CLI - GPU-Accelerated LLM ║ |
| 141 | + ║ Powered by TornadoVM & Java 21 ║ |
| 142 | + ╚══════════════════════════════════════════════════════════╝ |
| 143 | + """); |
| 144 | + } |
| 145 | +} |
0 commit comments