Skip to content

Commit 36ea0a3

Browse files
committed
Refactor token handling in MainActivity to use array instead of Atomic types
1 parent 6ecefb5 commit 36ea0a3

File tree

1 file changed

+9
-9
lines changed
  • mobile/examples/phi-3/android/app/src/main/java/ai/onnxruntime/genai/demo

1 file changed

+9
-9
lines changed

mobile/examples/phi-3/android/app/src/main/java/ai/onnxruntime/genai/demo/MainActivity.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.util.List;
2323
import java.util.concurrent.ExecutorService;
2424
import java.util.concurrent.Executors;
25-
import java.util.concurrent.atomic.AtomicInteger;
26-
import java.util.concurrent.atomic.AtomicLong;
2725
import java.util.function.Consumer;
2826

2927
import ai.onnxruntime.genai.SimpleGenAI;
@@ -150,26 +148,28 @@ public void run() {
150148
generatorParams.setSearchOption("length_penalty", (double)lengthPenalty);
151149
generatorParams.setSearchOption("max_length", (double)maxLength);
152150
long startTime = System.currentTimeMillis();
153-
AtomicLong firstTokenTime = new AtomicLong(startTime);
154-
AtomicInteger numTokens = new AtomicInteger(0);
151+
final long[] firstTokenTime = {startTime};
152+
final long[] numTokens = {0};
155153

156154
// Token listener for streaming tokens
157155
Consumer<String> tokenListener = token -> {
158-
firstTokenTime.compareAndSet(startTime, System.currentTimeMillis());
156+
if (numTokens[0] == 0) {
157+
firstTokenTime[0] = System.currentTimeMillis();
159158
}
159+
160160

161161
// Update UI with new token
162162
MainActivity.this.accept(token);
163163

164164
Log.i(TAG, "Generated token: " + token);
165-
numTokens.incrementAndGet();
165+
numTokens[0] += 1;
166166
};
167167

168168
String fullResponse = genAI.generate(generatorParams, promptQuestion_formatted, tokenListener);
169169

170-
long totalTime = System.currentTimeMillis() - firstTokenTime.get();
171-
float promptProcessingTime = (firstTokenTime.get() - startTime) / 1000.0f;
172-
float tokensPerSecond = numTokens.get() > 1 ? (1000.0f * (numTokens.get() - 1)) / totalTime : 0;
170+
long totalTime = System.currentTimeMillis() - firstTokenTime[0];
171+
float promptProcessingTime = (firstTokenTime[0] - startTime) / 1000.0f;
172+
float tokensPerSecond = numTokens[0] > 1 ? (1000.0f * (numTokens[0] - 1)) / totalTime : 0;
173173

174174
runOnUiThread(() -> {
175175
showTokenPopup(promptProcessingTime, tokensPerSecond);

0 commit comments

Comments
 (0)