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 18
18
import java .util .ArrayList ;
19
19
import java .util .Arrays ;
20
20
import java .util .List ;
21
+ import org .json .JSONException ;
21
22
import org .json .JSONObject ;
22
23
import org .junit .Test ;
23
24
import org .junit .runner .RunWith ;
@@ -65,7 +66,7 @@ public void onResult(String result) {
65
66
}
66
67
67
68
@ Override
68
- public void onStats (String stats ) {
69
+ public void onStats (String stats ) throws JSONException {
69
70
JSONObject jsonObject = new JSONObject (stats );
70
71
int numGeneratedTokens = jsonObject .getInt ("num_generated_tokens" );
71
72
int inferenceEndMs = jsonObject .getInt ("inference_end_ms" );
Original file line number Diff line number Diff line change 49
49
import java .util .List ;
50
50
import java .util .concurrent .Executor ;
51
51
import java .util .concurrent .Executors ;
52
+ import org .json .JSONException ;
52
53
import org .json .JSONObject ;
53
54
import org .pytorch .executorch .extension .llm .LlmCallback ;
54
55
import org .pytorch .executorch .extension .llm .LlmModule ;
@@ -102,11 +103,16 @@ public void onStats(String result) {
102
103
runOnUiThread (
103
104
() -> {
104
105
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
+ }
110
116
mResultMessage .setTokensPerSecond (tps );
111
117
mMessageAdapter .notifyDataSetChanged ();
112
118
}
Original file line number Diff line number Diff line change 13
13
import android .os .Looper ;
14
14
import android .os .Message ;
15
15
import androidx .annotation .NonNull ;
16
+ import org .json .JSONException ;
16
17
import org .json .JSONObject ;
17
18
import org .pytorch .executorch .extension .llm .LlmCallback ;
18
19
import org .pytorch .executorch .extension .llm .LlmModule ;
@@ -71,11 +72,16 @@ public void onResult(String result) {
71
72
72
73
@ Override
73
74
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
+ }
79
85
mCallback .onStats ("tokens/second: " + tps );
80
86
}
81
87
}
You can’t perform that action at this time.
0 commit comments