Skip to content

Commit 4d3fc1b

Browse files
authored
fix: always populate mot.usage in HuggingFace backend (#694) (#697)
Token count extraction in _post_process_async was gated behind `span is not None or metrics_enabled`, so mot.usage was never populated in plain (non-telemetry) runs. Now extracted unconditionally — usage is a standard mot field, not a telemetry concern.
1 parent 224d14f commit 4d3fc1b

1 file changed

Lines changed: 2 additions & 10 deletions

File tree

mellea/backends/huggingface.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,26 +1133,18 @@ class used during generation, if any.
11331133
)
11341134

11351135
span = mot._meta.get("_telemetry_span")
1136-
from ..telemetry.metrics import is_metrics_enabled
11371136

1138-
metrics_enabled = is_metrics_enabled()
1139-
1140-
# Extract token counts only if needed
1137+
# Derive token counts from the output sequences (HF models have no usage object).
11411138
hf_output = mot._meta.get("hf_output")
11421139
n_prompt, n_completion = None, None
1143-
if (span is not None or metrics_enabled) and isinstance(
1144-
hf_output, GenerateDecoderOnlyOutput
1145-
):
1146-
# HuggingFace local models don't provide usage objects, but we can
1147-
# calculate token counts from sequences
1140+
if isinstance(hf_output, GenerateDecoderOnlyOutput):
11481141
try:
11491142
if input_ids is not None and hf_output.sequences is not None:
11501143
n_prompt = input_ids.shape[1]
11511144
n_completion = hf_output.sequences[0].shape[0] - n_prompt
11521145
except Exception:
11531146
pass
11541147

1155-
# Populate standardized usage field (convert to OpenAI format)
11561148
if n_prompt is not None and n_completion is not None:
11571149
mot.usage = {
11581150
"prompt_tokens": n_prompt,

0 commit comments

Comments
 (0)