Skip to content

Commit 553257b

Browse files
committed
Use same as llm:Stats
1 parent 2ff9abd commit 553257b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

extension/android/executorch_android/src/main/java/org/pytorch/executorch/extension/llm/LlmCallback.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,22 @@ public interface LlmCallback {
3131
/**
3232
* Called when the statistics for the generate() is available.
3333
*
34+
* Note: This is a deprecated API and will be removed in the future. Please use onStats(String stats)
35+
*
3436
* @param tps Tokens/second for generated tokens.
3537
*/
38+
@Deprecated
3639
@DoNotStrip
3740
public void onStats(float tps);
41+
42+
/**
43+
* Called when the statistics for the generate() is available.
44+
*
45+
* The result will be a JSON string. See extension/llm/stats.h for the field
46+
* definitions.
47+
*
48+
* @param stats JSON string containing the statistics for the generate()
49+
*/
50+
@DoNotStrip
51+
default public void onStats(String stats) {}
3852
}

extension/android/jni/jni_layer_llama.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,20 @@ class ExecuTorchLlmCallbackJni
100100

101101
void onStats(const llm::Stats& result) const {
102102
static auto cls = ExecuTorchLlmCallbackJni::javaClassStatic();
103-
static const auto method = cls->getMethod<void(jfloat)>("onStats");
103+
static const auto tps_method = cls->getMethod<void(jfloat)>("onStats");
104104
double eval_time =
105105
(double)(result.inference_end_ms - result.prompt_eval_end_ms);
106106

107107
float tps = result.num_generated_tokens / eval_time *
108108
result.SCALING_FACTOR_UNITS_PER_SECOND;
109-
110-
method(self(), tps);
109+
tps_method(self(), tps);
110+
111+
static const auto on_stats_method =
112+
cls->getMethod<void(facebook::jni::local_ref<jstring>)>("onStats");
113+
on_stats_method(
114+
self(),
115+
facebook::jni::make_jstring(
116+
executorch::extension::llm::stats_to_json_string(result)));
111117
}
112118
};
113119

0 commit comments

Comments
 (0)