-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathEvaluate.swift
556 lines (466 loc) · 17.8 KB
/
Evaluate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// Copyright © 2024 Apple Inc.
import Foundation
import MLX
import MLXRandom
import Tokenizers
/// A `LogitSampler` is responsible for sampling `logits` produced by
/// a ``LanguageModel`` to produce a token.
///
/// See also: ``LogitProcessor``
public protocol LogitSampler: Sendable {
/// Given `logits` produce a new `MLXArray` with the token.
func sample(logits: MLXArray) -> MLXArray
}
/// A `LogitProcessor` is an optional visitor of `logits`.
///
/// The ``LogitProcessor`` is called with the input (prompt) before generating tokens:
///
/// ```swift
/// processor?.prompt(input.text.tokens)
/// ```
///
/// Then for each token generated it has a chance to adjust the logits:
///
/// ```swift
/// logits = processor?.process(logits: logits) ?? logits
/// let y = sampler.sample(logits: logits)
/// processor?.didSample(token: y)
/// ```
///
/// See also: ``LogitSampler``
public protocol LogitProcessor: Sendable {
/// called before token generation starts with the text tokens of the prompt
mutating func prompt(_ prompt: MLXArray)
/// called to visit ad possibly modify the logits
func process(logits: MLXArray) -> MLXArray
/// called to provide the sampled token
mutating func didSample(token: MLXArray)
}
/// Parameters for text generation, see ``TokenIterator``.
///
/// This produces:
///
/// - ``LogitSampler``
/// - ``LogitProcessor``
///
/// for the `TokenIterator`.
public struct GenerateParameters: Sendable {
/// Step size for processing the prompt
public var prefillStepSize = 512
/// sampling temperature
public var temperature: Float = 0.6
/// top p sampling
public var topP: Float = 1.0
/// penalty factor for repeating tokens
public var repetitionPenalty: Float?
/// number of tokens to consider for repetition penalty
public var repetitionContextSize: Int = 20
public init(
temperature: Float = 0.6, topP: Float = 1.0, repetitionPenalty: Float? = nil,
repetitionContextSize: Int = 20
) {
self.temperature = temperature
self.topP = topP
self.repetitionPenalty = repetitionPenalty
self.repetitionContextSize = repetitionContextSize
}
func sampler() -> LogitSampler {
if temperature == 0 {
return ArgMaxSampler()
} else if topP > 0 && topP < 1 {
return TopPSampler(temperature: temperature, topP: topP)
} else {
return CategoricalSampler(temperature: temperature)
}
}
func processor() -> LogitProcessor? {
if let repetitionPenalty, repetitionContextSize > 0 {
return RepetitionContext(
repetitionPenalty: repetitionPenalty, repetitionContextSize: repetitionContextSize)
} else {
return nil
}
}
}
/// Sampler that uses `argMax` (most likely) to sample the logits.
public struct ArgMaxSampler: LogitSampler {
public func sample(logits: MLX.MLXArray) -> MLX.MLXArray {
argMax(logits, axis: -1)
}
}
/// Sampler that uses `topP` and `temperature` to sample the logits.
public struct TopPSampler: LogitSampler {
let temp: MLXArray
let topP: MLXArray
init(temperature: Float, topP: Float) {
self.temp = MLXArray(temperature)
self.topP = MLXArray(topP)
}
private let compiledTopPSampling: (MLXArray, MLXArray, MLXArray) -> MLXArray = {
compile(inputs: [MLXRandom.globalState], outputs: [MLXRandom.globalState]) {
logits, topP, temp in
let probs = softmax(logits / temp, axis: -1)
let sortedIndices = argSort(probs, axis: -1)
// probs shape is [B,V] and after take it will be [1, B, V], so we squeeze it back to [B, V]
let sortedProbs = take(probs, sortedIndices, axis: -1).squeezed(axis: 0)
let cumulativeProbs = cumsum(sortedProbs, axis: -1)
let topProbs = MLX.where(
cumulativeProbs .> (1 - topP), sortedProbs, zeros(like: sortedProbs))
let sortedToken = categorical(log(topProbs))
return sortedIndices.squeezed(axis: 0)[sortedToken]
}
}()
public func sample(logits: MLXArray) -> MLXArray {
var logits = logits
if logits.dtype == .bfloat16 {
logits = logits.asType(.float32)
}
return compiledTopPSampling(logits, topP, temp)
}
}
/// Processor that uses `temperature` to sample the logits
public struct CategoricalSampler: LogitSampler {
let temp: MLXArray
init(temperature: Float) {
self.temp = MLXArray(temperature)
}
private let compiledCategorical: (MLXArray, MLXArray) -> MLXArray = {
compile(inputs: [MLXRandom.globalState], outputs: [MLXRandom.globalState]) { logits, temp in
categorical(logits * (1 / temp))
}
}()
public func sample(logits: MLXArray) -> MLXArray {
compiledCategorical(logits, temp)
}
}
/// Processor that implements a `repetitionPenalty`
public struct RepetitionContext: LogitProcessor {
/// tokens in the repetition context sliding window
var tokens = [Int]()
/// current write into into the tokens circular array
var index = 0
/// penalty factor for repeating tokens
let repetitionPenalty: Float
/// number of tokens to consider for repetition penalty
let repetitionContextSize: Int
init(repetitionPenalty: Float, repetitionContextSize: Int) {
precondition(repetitionContextSize > 0)
self.repetitionPenalty = repetitionPenalty
self.repetitionContextSize = repetitionContextSize
}
mutating public func prompt(_ prompt: MLXArray) {
if prompt.shape[0] <= repetitionContextSize {
self.tokens = prompt.asArray(Int.self)
} else {
self.tokens = prompt[(-repetitionContextSize)...].asArray(Int.self)
}
}
public func process(logits: MLXArray) -> MLXArray {
if tokens.count > 0 {
let indices = MLXArray(tokens.map { UInt32($0) })
var selectedLogits = logits[0..., indices]
selectedLogits = MLX.where(
selectedLogits .< 0, selectedLogits * repetitionPenalty,
selectedLogits / repetitionPenalty)
logits[0..., indices] = selectedLogits
return logits
}
return logits
}
mutating public func didSample(token: MLXArray) {
if tokens.count >= repetitionContextSize {
tokens[index] = token.item(Int.self)
index = (index + 1) % repetitionContextSize
} else {
tokens.append(token.item(Int.self))
}
}
}
/// Generator of tokens.
///
/// This is typically used via a call to ``generate(input:parameters:context:didGenerate:)``.
///
/// To use it directly:
///
/// ```swift
/// let generateParameters: GenerateParameters
/// let input: LMInput
/// let model: LanguageModel
///
/// let iterator = try TokenIterator(input: input, model: model, parameters: parameters)
///
/// for token in iterator {
/// ...
/// }
/// ```
///
/// Tokens are integers that can be passed through a `Tokenizer` or ``StreamingDetokenizer`` to produce Strings.
///
/// Port of `generate_step()` from https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/utils.py
///
/// Note: this uses `asyncEval()` and there may be an async evaluation running after a call to `next()`.
public struct TokenIterator: Sequence, IteratorProtocol {
let model: any LanguageModel
var state: LMOutput.State?
var y: LMInput.Text
var cache: [KVCache]
var processor: LogitProcessor?
let sampler: LogitSampler
/// Initialize a `TokenIterator` with the given tokens. Note: this has been
/// replaced with ``init(input:model:cache:parameters:)``.
///
/// - Parameters:
/// - prompt: the prompt tokens
/// - model: the ``LanguageModel``
/// - cache: optional ``KVCache``
/// - parameters: the generation parameters
@available(*, deprecated, message: "please use init(input:model:cache:parameters:)")
public init(
prompt: MLXArray, model: any LanguageModel, cache: [KVCache]? = nil,
parameters: GenerateParameters
) throws {
self.model = model
self.y = .init(tokens: prompt)
self.cache = cache ?? model.newCache(parameters: parameters)
self.processor = parameters.processor()
self.sampler = parameters.sampler()
try prepare(input: .init(text: y), windowSize: parameters.prefillStepSize)
}
/// Initialize a `TokenIterator` with the given input.
///
/// If more control is needed over the generation,
/// ``init(input:model:cache:processor:sampler:prefillStepSize:)``
/// allows a caller to specify ``LogitProcessor`` and ``LogitSampler``
/// directly.
///
/// - Parameters:
/// - input: language model input
/// - model: the ``LanguageModel``
/// - cache: optional ``KVCache``
/// - parameters: the generation parameters
public init(
input: LMInput, model: any LanguageModel, cache: [KVCache]? = nil,
parameters: GenerateParameters
) throws {
self.model = model
self.y = input.text
self.cache = cache ?? model.newCache(parameters: parameters)
self.processor = parameters.processor()
self.sampler = parameters.sampler()
try prepare(input: input, windowSize: parameters.prefillStepSize)
}
/// Initialize a `TokenIterator` with the given input and logit handling.
///
/// - Parameters:
/// - input: language model input
/// - model: the ``LanguageModel``
/// - cache: optional ``KVCache``
/// - processor: the logit processor
/// - sampler: the logit sampler
/// - prefillStepSize: optional prefill step size
public init(
input: LMInput, model: any LanguageModel, cache: [KVCache]? = nil,
processor: LogitProcessor?, sampler: LogitSampler, prefillStepSize: Int = 512
) throws {
self.model = model
self.y = input.text
self.cache = cache ?? model.newCache(parameters: nil)
self.processor = processor
self.sampler = sampler
try prepare(input: input, windowSize: prefillStepSize)
}
mutating func prepare(input: LMInput, windowSize: Int? = nil) throws {
processor?.prompt(input.text.tokens)
switch try model.prepare(input, cache: cache, windowSize: windowSize) {
case .tokens(let tokens):
y = tokens
// evaluate the remainder of the prompt -- this primes the pump
let token = step(previous: y)
y = .init(tokens: token)
asyncEval(y.tokens)
case .logits(let result):
y = .init(tokens: convertToToken(logits: result.logits))
asyncEval(y.tokens)
break
}
}
mutating func convertToToken(logits: MLXArray) -> MLXArray {
// process the logits (one hot array of possible tokens)
var logits = logits[0..., -1, 0...]
logits = processor?.process(logits: logits) ?? logits
// transform logits back to a token
let y = sampler.sample(logits: logits)
processor?.didSample(token: y)
return y
}
/// Evaluate the next token and return the new token (y), updating cache state
mutating func step(previous: LMInput.Text) -> MLXArray {
let result = model(
previous[text: .newAxis], cache: cache.isEmpty ? nil : cache, state: state)
self.state = result.state
return convertToToken(logits: result.logits)
}
mutating public func next() -> Int? {
// save current value -- this will be returned
let previousY = y
// compute the next state and async eval the next token
let token = step(previous: previousY)
y = .init(tokens: token)
asyncEval(token)
return previousY.tokens.item(Int.self)
}
}
/// Result of a call to ``generate(input:parameters:context:didGenerate:)``.
public struct GenerateResult: Sendable {
/// input (prompt, images, etc.)
public let inputText: LMInput.Text
@available(*, deprecated, message: "use inputText")
public var promptTokens: [Int] {
inputText.tokens.asArray(Int.self)
}
/// output tokens
public let tokens: [Int]
/// output text
public let output: String
/// time to process the prompt / generate the first token
public let promptTime: TimeInterval
/// time to generate the remaining tokens
public let generateTime: TimeInterval
public var promptTokensPerSecond: Double {
Double(inputText.tokens.size) / promptTime
}
public var tokensPerSecond: Double {
Double(tokens.count) / generateTime
}
public func summary() -> String {
"""
Prompt: \(inputText.tokens.size) tokens, \(promptTokensPerSecond.formatted()) tokens/s
Generation: \(tokens.count) tokens, \(tokensPerSecond.formatted()) tokens/s, \(generateTime.formatted())s
"""
}
}
/// Action from token visitor callback in ``generate(input:parameters:context:didGenerate:)``.
public enum GenerateDisposition: Sendable {
/// keep producing tokens until an EOS token is produced
case more
/// stop producing tokens, e.g. a token limit has been hit
case stop
}
/// Given prompt tokens generate text using the given model and parameters.
///
/// ``generate(input:parameters:context:didGenerate:)`` is the preferred call.
///
/// - Parameters:
/// - promptTokens: tokenized prompt
/// - parameters: generation parameters
/// - model: model to evaluate
/// - tokenizer: tokenizer to convert tokens back into strings and recognizer special tokens
/// - extraEOSTokens: any additional stop tokens
/// - didGenerate: visitor for the tokens as they are generated
@available(*, deprecated, message: "please use generate(input:parameters:context:didGenerate:)")
public func generate(
promptTokens: [Int], parameters: GenerateParameters, model: any LanguageModel,
tokenizer: Tokenizer,
extraEOSTokens: Set<String>? = nil,
didGenerate: ([Int]) -> GenerateDisposition
) throws -> GenerateResult {
let tokens = MLXArray(promptTokens)
let iterator = try TokenIterator(
prompt: tokens, model: model, parameters: parameters)
// this is a compatibility cover -- create the required values
// for the iteration
let input = LMInput(tokens: tokens)
let configuration = ModelConfiguration(id: "stand-in", extraEOSTokens: extraEOSTokens ?? [])
let context = ModelContext(
configuration: configuration, model: model, processor: StandInUserInputProcessor(),
tokenizer: tokenizer)
return generate(
input: input, context: context, iterator: iterator, didGenerate: didGenerate)
}
/// Generate tokens from an ``LMInput`` and a ``ModelContext``.
///
/// For example:
///
/// ```swift
/// let generateParameters: GenerateParameters
/// let input: UserInput
/// let context: ModelContext
///
/// let lmInput = try context.processor.prepare(input: input)
/// let result = generate(input: lmInput,
/// parameters: generateParameters,
/// context: context) { tokens in
/// .more
/// }
/// ```
///
/// Internally this constructs a ``TokenIterator`` and calls
/// ``generate(input:context:iterator:didGenerate:)``
///
/// - Parameters:
/// - input: prepared language model input
/// - parameters: parameters controlling the token generation
/// - context: model context (model and tokenizer)
/// - didGenerate: token visitor that can output tokens as they are generated and indicate early stop
/// - Returns: the generated output
public func generate(
input: LMInput, parameters: GenerateParameters, context: ModelContext,
didGenerate: ([Int]) -> GenerateDisposition
) throws -> GenerateResult {
let iterator = try TokenIterator(
input: input, model: context.model, parameters: parameters)
return generate(
input: input, context: context, iterator: iterator, didGenerate: didGenerate)
}
/// Low level token generation using a ``TokenIterator``.
///
/// ``generate(input:parameters:context:didGenerate:)`` is the preferred call.
///
/// - Parameters:
/// - input: prepared language model input
/// - context: model context (model and tokenizer)
/// - iterator: token iterator
/// - didGenerate: token visitor that can output tokens as they are generated and indicate early stop
/// - Returns: the generated output
public func generate(
input: LMInput, context: ModelContext,
iterator: TokenIterator,
didGenerate: ([Int]) -> GenerateDisposition
) -> GenerateResult {
var start = Date.timeIntervalSinceReferenceDate
var promptTime: TimeInterval = 0
let additionalEOSTokenIds = Set(
(context.configuration.extraEOSTokens ?? [])
.compactMap {
context.tokenizer.convertTokenToId($0)
})
var tokens = [Int]()
for token in iterator {
// compute the timing for the prompt
if tokens.isEmpty {
let now = Date.timeIntervalSinceReferenceDate
promptTime = now - start
start = now
}
if token == context.tokenizer.unknownTokenId || token == context.tokenizer.eosTokenId
|| additionalEOSTokenIds.contains(token)
{
break
}
tokens.append(token)
if didGenerate(tokens) == .stop {
break
}
}
let now = Date.timeIntervalSinceReferenceDate
let generateTime = now - start
// TokenIterator uses `asyncEval()` to keep the pipeline full. If the caller
// exits the program right away, those tasks will still be executing and will
// hit assertions as the mlx scheduler is torn down. Synchronize with the stream
// to make sure it is complete.
Stream().synchronize()
return GenerateResult(
inputText: input.text, tokens: tokens,
output: context.tokenizer.decode(tokens: tokens),
promptTime: promptTime, generateTime: generateTime)
}