Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/bump mlx #75

Merged
merged 29 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ let package = Package(
.library(name: "SpeziLLMFog", targets: ["SpeziLLMFog"])
],
dependencies: [
.package(url: "https://github.com/ml-explore/mlx-swift", branch: "0.21.2"), // Pin MLX library as it doesn't follow semantic versioning
.package(url: "https://github.com/ml-explore/mlx-swift-examples", branch: "1.16.0"), // Pin MLX library as it doesn't follow semantic versioning
.package(url: "https://github.com/huggingface/swift-transformers", .upToNextMinor(from: "0.1.12")),
.package(url: "https://github.com/ml-explore/mlx-swift", .upToNextMinor(from: "0.21.2")),
.package(url: "https://github.com/ml-explore/mlx-swift-examples", exact: "1.18.1"), // Pin MLX Swift Examples as it doesn't follow semantic versioning
.package(url: "https://github.com/huggingface/swift-transformers", .upToNextMinor(from: "0.1.14")),
.package(url: "https://github.com/StanfordBDHG/OpenAI", .upToNextMinor(from: "0.2.9")),
.package(url: "https://github.com/StanfordSpezi/Spezi", from: "1.2.1"),
.package(url: "https://github.com/StanfordSpezi/SpeziFoundation", from: "2.0.0-beta.3"),
.package(url: "https://github.com/StanfordSpezi/SpeziFoundation", from: "2.0.0"),
.package(url: "https://github.com/StanfordSpezi/SpeziStorage", from: "1.0.2"),
.package(url: "https://github.com/StanfordSpezi/SpeziOnboarding", from: "1.1.1"),
.package(url: "https://github.com/StanfordSpezi/SpeziChat", .upToNextMinor(from: "0.2.1")),
Expand Down
2 changes: 1 addition & 1 deletion Sources/SpeziLLM/Models/LLMContextEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
case tool(id: String, name: String)


var rawValue: String {
package var rawValue: String {

Check warning on line 49 in Sources/SpeziLLM/Models/LLMContextEntity.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziLLM/Models/LLMContextEntity.swift#L49

Added line #L49 was not covered by tests
switch self {
case .user: "user"
case .assistant: "assistant"
Expand Down

This file was deleted.

15 changes: 13 additions & 2 deletions Sources/SpeziLLMLocal/Configuration/LLMLocalParameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@
let systemPrompt: String?
/// Indicates the maximum output length generated by the LLM.
let maxOutputLength: Int

/// Additional End of Sequence tokens at which the generation will be stoped.
let extraEOSTokens: Set<String>
/// Interval for displaying output after every N tokens generated.
let displayEveryNTokens: Int
/// RNG seed of the LLM
let seed: UInt64?
/// The chat template to use for the model in the Jinja format
let chatTemplate: String?


/// Creates the ``LLMLocalParameters`` which wrap the underlying llama.cpp `llama_model_params` C struct.
/// Is passed to the underlying llama.cpp model in order to configure the LLM.
Expand All @@ -36,15 +41,21 @@
/// - maxOutputLength: The maximum output length generated by the Spezi LLM, defaults to `512`.
/// - extraEOSTokens: Additional tokens to use for end of string
/// - displayEveryNTokens: Interval for displaying output after every N tokens generated, defaults to `4`.
/// - seed: RNG seed of the LLM, defaults to a random seed.
/// - chatTemplate: Can be set to manually overwrite the chatTemplate within the `swift-transformers` package.
public init(
systemPrompt: String? = Defaults.defaultSystemPrompt,
maxOutputLength: Int = 512,
extraEOSTokens: Set<String> = [],
displayEveryNTokens: Int = 4
displayEveryNTokens: Int = 4,
seed: UInt64? = nil,
chatTemplate: String? = nil
) {
self.systemPrompt = systemPrompt
self.maxOutputLength = maxOutputLength
self.extraEOSTokens = extraEOSTokens
self.displayEveryNTokens = displayEveryNTokens
self.seed = seed
self.chatTemplate = chatTemplate

Check warning on line 59 in Sources/SpeziLLMLocal/Configuration/LLMLocalParameters.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziLLMLocal/Configuration/LLMLocalParameters.swift#L58-L59

Added lines #L58 - L59 were not covered by tests
}
}
25 changes: 25 additions & 0 deletions Sources/SpeziLLMLocal/Helpers/LLMContext+FormattedChat.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// This source file is part of the Stanford Spezi open source project
//
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md)
//
// SPDX-License-Identifier: MIT
//

import SpeziLLM

extension LLMContext {
/// Formats the current ``LLMContext`` for compatibility with Transformers-based chat models.
///
/// - Returns: An array of dictionaries where each dictionary represents a message in the format:
/// - `role`: The role of the message (e.g., "user", "assistant"), derived from the `rawValue` of the entry's `role`.
/// - `content`: The textual content of the message.
package var formattedChat: [[String: String]] {
self.map { entry in
[
"role": entry.role.rawValue,
"content": entry.content
]
}
}

Check warning on line 24 in Sources/SpeziLLMLocal/Helpers/LLMContext+FormattedChat.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziLLMLocal/Helpers/LLMContext+FormattedChat.swift#L17-L24

Added lines #L17 - L24 were not covered by tests
}
18 changes: 15 additions & 3 deletions Sources/SpeziLLMLocal/LLMLocalPlatform.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import Spezi
import SpeziFoundation
import SpeziLLM

#if targetEnvironment(simulator)
import OSLog
#endif

/// LLM execution platform of an ``LLMLocalSchema``.
///
Expand Down Expand Up @@ -58,19 +60,29 @@

public nonisolated func configure() {
#if targetEnvironment(simulator)
assertionFailure("SpeziLLMLocal: Code cannot be run on simulator.")
#endif
Logger(
subsystem: "Spezi",
category: "LLMLocalPlatform"
).warning("SpeziLLMLocal is only supported on physical devices. Use `LLMMockPlatform` instead.")

Check warning on line 66 in Sources/SpeziLLMLocal/LLMLocalPlatform.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziLLMLocal/LLMLocalPlatform.swift#L63-L66

Added lines #L63 - L66 were not covered by tests
LeonNissen marked this conversation as resolved.
Show resolved Hide resolved
#else
if let cacheLimit = configuration.cacheLimit {
MLX.GPU.set(cacheLimit: cacheLimit * 1024 * 1024)
}
if let memoryLimit = configuration.memoryLimit {
MLX.GPU.set(memoryLimit: memoryLimit.limit, relaxed: memoryLimit.relaxed)
}
#endif
}

#if targetEnvironment(simulator)
public nonisolated func callAsFunction(with llmSchema: LLMLocalSchema) -> LLMLocalMockSession {
LLMLocalMockSession(self, schema: llmSchema)
}

Check warning on line 80 in Sources/SpeziLLMLocal/LLMLocalPlatform.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SpeziLLMLocal/LLMLocalPlatform.swift#L78-L80

Added lines #L78 - L80 were not covered by tests
#else
public nonisolated func callAsFunction(with llmSchema: LLMLocalSchema) -> LLMLocalSession {
LLMLocalSession(self, schema: llmSchema)
}
#endif

deinit {
MLX.GPU.clearCache()
Expand Down
Loading
Loading