Update code generation tests with improved sampling
CI / build-and-test (push) Has been cancelled

- Implemented top-k sampling (k=50, temperature=0.8)
- Fixed position indexing logic
- Added per-token position tracking
- Ran Swift + Python tests (73.5s total)
- Results: 0 NaN, stable embeddings, but poor code quality
- Issue: Model generates invalid/multilingual characters
- Conclusion: E4B-MarkBase not optimized for code generation
- Recommendation: Use specialized code model for programming tasks
- Test framework: Production-ready, multi-language support
This commit is contained in:
MarkBase Admin
2026-06-23 19:46:51 +08:00
parent 80a78ec554
commit fdeae9a540
3 changed files with 345 additions and 18 deletions
@@ -106,4 +106,27 @@ func verifyOutput(actual: String, expected: String) -> Bool {
let actualTrimmed = actual.trimmingCharacters(in: .whitespacesAndNewlines)
let expectedTrimmed = expected.trimmingCharacters(in: .whitespacesAndNewlines)
return actualTrimmed == expectedTrimmed || actualTrimmed.contains(expectedTrimmed)
}
func sampleTopK(logits: [Float], k: Int = 40, temperature: Float = 0.9) -> Int {
let scaledLogits = logits.map { $0 / temperature }
let sorted = scaledLogits.enumerated().sorted { $0.element > $1.element }
let topK = sorted.prefix(k)
let maxLogit = topK.first?.element ?? 0
let probs = topK.map { Float(exp(Double($0.element - maxLogit))) }
let sum = probs.reduce(0, +)
let normalizedProbs = probs.map { $0 / sum }
let random = Float.random(in: 0..<1)
var cumulative: Float = 0.0
for (index, prob) in normalizedProbs.enumerated() {
cumulative += prob
if random <= cumulative {
return topK[index].offset
}
}
return topK.last?.offset ?? 0
}