File tree Expand file tree Collapse file tree 3 files changed +24
-11
lines changed
examples/demo-apps/android/LlamaDemo/app/src
androidTest/java/com/example/executorchllamademo
main/java/com/example/executorchllamademo Expand file tree Collapse file tree 3 files changed +24
-11
lines changed Original file line number Diff line number Diff line change 1818import java .util .ArrayList ;
1919import java .util .Arrays ;
2020import java .util .List ;
21+ import org .json .JSONException ;
2122import org .json .JSONObject ;
2223import org .junit .Test ;
2324import 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" );
Original file line number Diff line number Diff line change 4949import java .util .List ;
5050import java .util .concurrent .Executor ;
5151import java .util .concurrent .Executors ;
52+ import org .json .JSONException ;
5253import org .json .JSONObject ;
5354import org .pytorch .executorch .extension .llm .LlmCallback ;
5455import 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 }
Original file line number Diff line number Diff line change 1313import android .os .Looper ;
1414import android .os .Message ;
1515import androidx .annotation .NonNull ;
16+ import org .json .JSONException ;
1617import org .json .JSONObject ;
1718import org .pytorch .executorch .extension .llm .LlmCallback ;
1819import 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}
You can’t perform that action at this time.
0 commit comments