Skip to content

Commit 982cdc6

Browse files
committed
lab: float issues
1 parent 09cac43 commit 982cdc6

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

internal/adapter/metrics/extractor.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func (e *Extractor) mapFieldToMetrics(field string, result gjson.Result, metrics
237237
metrics.Model = result.String()
238238
case "finish_reason":
239239
metrics.FinishReason = result.String()
240-
case "done":
240+
case "done", "is_complete":
241241
metrics.IsComplete = result.Bool()
242242
case "prompt_duration_ns", "prompt_eval_duration":
243243
ms := result.Int() / 1_000_000
@@ -290,7 +290,7 @@ func (e *Extractor) runCalculations(profileName string, values map[string]interf
290290
if val, ok := result.(float64); ok {
291291
switch field {
292292
case "tokens_per_second":
293-
metrics.TokensPerSecond = float32(val)
293+
metrics.TokensPerSecond = util.SafeFloat32(val)
294294
case "ttft_ms":
295295
metrics.TTFTMs = util.SafeInt32(int64(val))
296296
case "total_ms":
@@ -305,7 +305,8 @@ func (e *Extractor) runCalculations(profileName string, values map[string]interf
305305

306306
// Calculate tokens per second if we have the data and no calculation provided
307307
if metrics.TokensPerSecond == 0 && metrics.OutputTokens > 0 && metrics.GenerationMs > 0 {
308-
metrics.TokensPerSecond = float32(metrics.OutputTokens) / (float32(metrics.GenerationMs) / 1000.0)
308+
tps := float64(metrics.OutputTokens) * 1000.0 / float64(metrics.GenerationMs)
309+
metrics.TokensPerSecond = util.SafeFloat32(tps)
309310
}
310311

311312
// Ensure total tokens is set if we have input and output

internal/util/safe.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,17 @@ func SafeInt32(value int64) int32 {
3030
}
3131
return int32(value)
3232
}
33+
34+
// SafeFloat32 safely converts float64 to float32, handling special values
35+
func SafeFloat32(value float64) float32 {
36+
if math.IsNaN(value) || math.IsInf(value, 0) {
37+
return 0
38+
}
39+
if value > math.MaxFloat32 {
40+
return math.MaxFloat32
41+
}
42+
if value < -math.MaxFloat32 {
43+
return -math.MaxFloat32
44+
}
45+
return float32(value)
46+
}

0 commit comments

Comments
 (0)