Skip to content

Commit e1d3222

Browse files
authored
Decode token in generate() (#260)
* Decode token in generate() * Use naiveStramingDetokenizer
1 parent cb66b4b commit e1d3222

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

Libraries/MLXLMCommon/Evaluate.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,13 @@ public func generate(
723723
var promptTime: TimeInterval = 0
724724

725725
let additionalEOSTokenIds = Set(
726-
(context.configuration.extraEOSTokens ?? [])
726+
context.configuration.extraEOSTokens
727727
.compactMap {
728728
context.tokenizer.convertTokenToId($0)
729729
})
730730

731731
var tokenCount = 0
732+
var detokenizer = NaiveStreamingDetokenizer(tokenizer: context.tokenizer)
732733

733734
for token in iterator {
734735

@@ -748,8 +749,11 @@ public func generate(
748749
break
749750
}
750751

751-
tokenCount += 1
752-
continuation.yield(.token(token))
752+
detokenizer.append(token: token)
753+
if let chunk = detokenizer.next() {
754+
tokenCount += 1
755+
continuation.yield(.chunk(chunk))
756+
}
753757
}
754758

755759
let now = Date.timeIntervalSinceReferenceDate
@@ -819,11 +823,11 @@ public struct GenerateCompletionInfo: Sendable {
819823
/// Represents the different stages or outputs of the token generation process.
820824
///
821825
/// This enum distinguishes between the following:
822-
/// - `.token`: An individual token generated by the language model.
826+
/// - `.chunk`: A decoded string from one or more tokens generated by the language model.
823827
/// - `.info`: Metadata and performance statistics about the generation process.
824828
public enum Generation {
825829
/// A generated token represented as an integer.
826-
case token(Int)
830+
case chunk(String)
827831
/// Completion information summarizing token counts and performance metrics.
828832
case info(GenerateCompletionInfo)
829833
}

0 commit comments

Comments
 (0)