|
| 1 | +package com.example.modeltest; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.util.Scanner; |
| 5 | + |
| 6 | +import ai.onnxruntime.genai.GenAIException; |
| 7 | +import ai.onnxruntime.genai.Generator; |
| 8 | +import ai.onnxruntime.genai.GeneratorParams; |
| 9 | +import ai.onnxruntime.genai.Model; |
| 10 | +import ai.onnxruntime.genai.Sequences; |
| 11 | +import ai.onnxruntime.genai.Tokenizer; |
| 12 | +import ai.onnxruntime.genai.TokenizerStream; |
| 13 | + |
| 14 | +/** |
| 15 | + * Standalone Java application to test the Phi-3 model locally |
| 16 | + * |
| 17 | + * Usage: |
| 18 | + * 1. Make sure you have the model files in the directory specified by MODEL_PATH |
| 19 | + * 2. Compile and run this application |
| 20 | + * 3. Enter prompts to test the model |
| 21 | + * 4. Type 'exit' to quit |
| 22 | + */ |
| 23 | +public class ModelTest { |
| 24 | + |
| 25 | + // Change this to the path where you have the model files locally |
| 26 | + private static final String MODEL_PATH = "C:\\Users\\shekadam\\.aitk\\models\\Microsoft\\Phi-3.5-mini-instruct-generic-cpu\\cpu-int4-rtn-block-32-acc-level-4"; |
| 27 | + |
| 28 | + public static void main(String[] args) { |
| 29 | + System.out.println("Phi-3 Model Test Application"); |
| 30 | + System.out.println("============================"); |
| 31 | + |
| 32 | + // Check if model directory exists |
| 33 | + File modelDir = new File(MODEL_PATH); |
| 34 | + if (!modelDir.exists() || !modelDir.isDirectory()) { |
| 35 | + System.err.println("Model directory not found at: " + MODEL_PATH); |
| 36 | + System.err.println("Please update the MODEL_PATH in the source code."); |
| 37 | + return; |
| 38 | + } |
| 39 | + |
| 40 | + // Check for model files |
| 41 | + File[] onnxFiles = modelDir.listFiles((dir, name) -> name.endsWith(".onnx")); |
| 42 | + if (onnxFiles == null || onnxFiles.length == 0) { |
| 43 | + System.err.println("No ONNX files found in directory: " + MODEL_PATH); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + System.out.println("Found " + onnxFiles.length + " ONNX files in model directory"); |
| 48 | + System.out.println("Initializing model, please wait..."); |
| 49 | + |
| 50 | + Model model = null; |
| 51 | + Tokenizer tokenizer = null; |
| 52 | + |
| 53 | + try { |
| 54 | + // Initialize model and tokenizer |
| 55 | + long startTime = System.currentTimeMillis(); |
| 56 | + model = new Model(MODEL_PATH); |
| 57 | + tokenizer = model.createTokenizer(); |
| 58 | + long initTime = System.currentTimeMillis() - startTime; |
| 59 | + |
| 60 | + System.out.println("Model initialized successfully in " + initTime + "ms"); |
| 61 | + System.out.println("Enter your prompts, or type 'exit' to quit:"); |
| 62 | + |
| 63 | + // Process user prompts |
| 64 | + Scanner scanner = new Scanner(System.in); |
| 65 | + while (true) { |
| 66 | + System.out.print("\nPrompt> "); |
| 67 | + String input = scanner.nextLine().trim(); |
| 68 | + |
| 69 | + if (input.equalsIgnoreCase("exit")) { |
| 70 | + break; |
| 71 | + } |
| 72 | + |
| 73 | + if (input.isEmpty()) { |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + generateResponse(model, tokenizer, input); |
| 78 | + } |
| 79 | + |
| 80 | + scanner.close(); |
| 81 | + |
| 82 | + } catch (GenAIException e) { |
| 83 | + System.err.println("Error initializing model: " + e.getMessage()); |
| 84 | + e.printStackTrace(); |
| 85 | + } finally { |
| 86 | + // Clean up resources |
| 87 | + if (tokenizer != null) tokenizer.close(); |
| 88 | + if (model != null) model.close(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private static void generateResponse(Model model, Tokenizer tokenizer, String prompt) { |
| 93 | + TokenizerStream stream = null; |
| 94 | + GeneratorParams generatorParams = null; |
| 95 | + Sequences encodedPrompt = null; |
| 96 | + Generator generator = null; |
| 97 | + |
| 98 | + try { |
| 99 | + long startTime = System.currentTimeMillis(); |
| 100 | + |
| 101 | + // Format prompt for Phi-3 model |
| 102 | + String promptFormatted = "<s>You are a helpful AI assistant. Answer in two paragraphs or less<|end|><|user|>" + |
| 103 | + prompt + "<|end|>\n<assistant|>"; |
| 104 | + |
| 105 | + // Create tokenizer stream |
| 106 | + stream = tokenizer.createStream(); |
| 107 | + |
| 108 | + // Create generator parameters |
| 109 | + generatorParams = model.createGeneratorParams(); |
| 110 | + generatorParams.setSearchOption("max_length", 100L); |
| 111 | + generatorParams.setSearchOption("temperature", 0.7); |
| 112 | + generatorParams.setSearchOption("top_p", 0.9); |
| 113 | + |
| 114 | + // Encode the prompt |
| 115 | + encodedPrompt = tokenizer.encode(promptFormatted); |
| 116 | + generatorParams.setInput(encodedPrompt); |
| 117 | + |
| 118 | + // Create generator |
| 119 | + generator = new Generator(model, generatorParams); |
| 120 | + |
| 121 | + StringBuilder result = new StringBuilder(); |
| 122 | + System.out.print("\nGenerating: "); |
| 123 | + |
| 124 | + // Generate tokens until done |
| 125 | + int tokenCount = 0; |
| 126 | + while (!generator.isDone()) { |
| 127 | + generator.computeLogits(); |
| 128 | + generator.generateNextToken(); |
| 129 | + |
| 130 | + int token = generator.getLastTokenInSequence(0); |
| 131 | + String decodedToken = stream.decode(token); |
| 132 | + result.append(decodedToken); |
| 133 | + |
| 134 | + // Print progress |
| 135 | + System.out.print(decodedToken); |
| 136 | + tokenCount++; |
| 137 | + } |
| 138 | + |
| 139 | + long totalTime = System.currentTimeMillis() - startTime; |
| 140 | + double tokensPerSecond = tokenCount * 1000.0 / totalTime; |
| 141 | + |
| 142 | + System.out.println("\n\nGeneration complete!"); |
| 143 | + System.out.println("Time: " + totalTime + "ms for " + tokenCount + " tokens (" + |
| 144 | + String.format("%.2f", tokensPerSecond) + " tokens/sec)"); |
| 145 | + |
| 146 | + } catch (Exception e) { |
| 147 | + System.err.println("\nError generating response: " + e.getMessage()); |
| 148 | + e.printStackTrace(); |
| 149 | + } finally { |
| 150 | + // Clean up resources |
| 151 | + if (generator != null) generator.close(); |
| 152 | + if (encodedPrompt != null) encodedPrompt.close(); |
| 153 | + if (stream != null) stream.close(); |
| 154 | + if (generatorParams != null) generatorParams.close(); |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments