Skip to content

Commit 48d2df8

Browse files
committed
use ml-explore/mlx-swift-lm#190 for LoRA example
1 parent 31e568f commit 48d2df8

3 files changed

Lines changed: 23 additions & 17 deletions

File tree

Applications/LoRATrainingExample/ContentView.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ class LoRAEvaluator {
193193
let modelContainer = try await loadModel()
194194

195195
// apply LoRA adapters and train
196-
let modelAdapter = try await modelContainer.perform { context in
196+
let _ = try await modelContainer.perform { context in
197197
try LoRAContainer.from(
198198
model: context.model,
199199
configuration: LoRAConfiguration(numLayers: loraLayers)
@@ -270,22 +270,28 @@ class LoRAEvaluator {
270270
let modelContainer = try await loadModel()
271271

272272
// evaluate
273-
let result = try await modelContainer.perform { context in
274-
let input = try await context.processor.prepare(input: .init(prompt: prompt))
275-
return try MLXLMCommon.generate(
276-
input: input, parameters: generateParameters, context: context
277-
) { tokens in
278-
if tokens.count % evaluateShowEvery == 0 {
279-
let fullOutput = context.tokenizer.decode(tokens: tokens)
280-
Task { @MainActor in
281-
self.output = fullOutput
282-
}
273+
let input = try await modelContainer.processor.prepare(input: .init(prompt: prompt))
274+
275+
var count = 0
276+
var output = ""
277+
for try await item in try await modelContainer.generate(
278+
input: input, parameters: generateParameters
279+
) {
280+
switch item {
281+
case .chunk(let string):
282+
count += 1
283+
output += string
284+
285+
if count % evaluateShowEvery == 0 {
286+
self.output = output
283287
}
284-
return tokens.count >= maxTokens ? .stop : .more
288+
289+
default:
290+
break
285291
}
286292
}
287293

288-
self.output = result.output
294+
self.output = output
289295
self.progress = nil
290296
}
291297
}

mlx-swift-examples.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3287,7 +3287,7 @@
32873287
isa = XCRemoteSwiftPackageReference;
32883288
repositoryURL = "https://github.com/ml-explore/mlx-swift-lm";
32893289
requirement = {
3290-
branch = main;
3290+
branch = "v3-api-p1";
32913291
kind = branch;
32923292
};
32933293
};

mlx-swift-examples.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)