Skip to content

Commit f3384b1

Browse files
committed
exception
1 parent 40b3f02 commit f3384b1

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

examples/demo-apps/android/LlamaDemo/app/src/androidTest/java/com/example/executorchllamademo/PerfTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.ArrayList;
1919
import java.util.Arrays;
2020
import java.util.List;
21+
import org.json.JSONException;
2122
import org.json.JSONObject;
2223
import org.junit.Test;
2324
import org.junit.runner.RunWith;
@@ -65,7 +66,7 @@ public void onResult(String result) {
6566
}
6667

6768
@Override
68-
public void onStats(String stats) {
69+
public void onStats(String stats) throws JSONException {
6970
JSONObject jsonObject = new JSONObject(stats);
7071
int numGeneratedTokens = jsonObject.getInt("num_generated_tokens");
7172
int inferenceEndMs = jsonObject.getInt("inference_end_ms");

examples/demo-apps/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/MainActivity.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.List;
5050
import java.util.concurrent.Executor;
5151
import java.util.concurrent.Executors;
52+
import org.json.JSONException;
5253
import org.json.JSONObject;
5354
import org.pytorch.executorch.extension.llm.LlmCallback;
5455
import org.pytorch.executorch.extension.llm.LlmModule;
@@ -102,11 +103,16 @@ public void onStats(String result) {
102103
runOnUiThread(
103104
() -> {
104105
if (mResultMessage != null) {
105-
JSONObject jsonObject = new JSONObject(stats);
106-
int numGeneratedTokens = jsonObject.getInt("num_generated_tokens");
107-
int inferenceEndMs = jsonObject.getInt("inference_end_ms");
108-
int promptEvalEndMs = jsonObject.getInt("prompt_eval_end_ms");
109-
float tps = (float) numGeneratedTokens / (inferenceEndMs - promptEvalEndMs) * 1000;
106+
float tps = 0;
107+
try {
108+
JSONObject jsonObject = new JSONObject(stats);
109+
int numGeneratedTokens = jsonObject.getInt("num_generated_tokens");
110+
int inferenceEndMs = jsonObject.getInt("inference_end_ms");
111+
int promptEvalEndMs = jsonObject.getInt("prompt_eval_end_ms");
112+
tps = (float) numGeneratedTokens / (inferenceEndMs - promptEvalEndMs) * 1000;
113+
} catch (JSONException e) {
114+
Log.e("LLM", "Error parsing JSON: " + e.getMessage());
115+
}
110116
mResultMessage.setTokensPerSecond(tps);
111117
mMessageAdapter.notifyDataSetChanged();
112118
}

examples/demo-apps/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/ModelRunner.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.os.Looper;
1414
import android.os.Message;
1515
import androidx.annotation.NonNull;
16+
import org.json.JSONException;
1617
import org.json.JSONObject;
1718
import org.pytorch.executorch.extension.llm.LlmCallback;
1819
import org.pytorch.executorch.extension.llm.LlmModule;
@@ -71,11 +72,16 @@ public void onResult(String result) {
7172

7273
@Override
7374
public void onStats(String stats) {
74-
JSONObject jsonObject = new JSONObject(stats);
75-
int numGeneratedTokens = jsonObject.getInt("num_generated_tokens");
76-
int inferenceEndMs = jsonObject.getInt("inference_end_ms");
77-
int promptEvalEndMs = jsonObject.getInt("prompt_eval_end_ms");
78-
float tps = (float) numGeneratedTokens / (inferenceEndMs - promptEvalEndMs) * 1000;
75+
float tps = 0;
76+
try {
77+
JSONObject jsonObject = new JSONObject(stats);
78+
int numGeneratedTokens = jsonObject.getInt("num_generated_tokens");
79+
int inferenceEndMs = jsonObject.getInt("inference_end_ms");
80+
int promptEvalEndMs = jsonObject.getInt("prompt_eval_end_ms");
81+
tps = (float) numGeneratedTokens / (inferenceEndMs - promptEvalEndMs) * 1000;
82+
} catch (JSONException e) {
83+
Log.e("LLM", "Error parsing JSON: " + e.getMessage());
84+
}
7985
mCallback.onStats("tokens/second: " + tps);
8086
}
8187
}

0 commit comments

Comments
 (0)