Skip to content

Commit 56cfed4

Browse files
committed
Merge branch 'main' into feature/swift-format
2 parents de969f2 + eec56ed commit 56cfed4

File tree

11 files changed

+1051
-245
lines changed

11 files changed

+1051
-245
lines changed

README.md

Lines changed: 78 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,95 @@
1-
# `swift-transformers`
1+
<p align="center">
2+
<picture>
3+
<source media="(prefers-color-scheme: dark)" srcset="media/swift-t-banner.png">
4+
<source media="(prefers-color-scheme: light)" srcset="media/swift-t-banner.png">
5+
<img alt="Swift + Transformers" src="media/swift-t-banner.png" style="max-width: 100%;">
6+
</picture>
7+
<br/>
8+
<br/>
9+
</p>
10+
11+
212
[![Unit Tests](https://github.com/huggingface/swift-transformers/actions/workflows/unit-tests.yml/badge.svg)](https://github.com/huggingface/swift-transformers/actions/workflows/unit-tests.yml)
313
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fhuggingface%2Fswift-transformers%2Fbadge%3Ftype%3Dswift-versions)](https://swiftpackageindex.com/huggingface/swift-transformers)
414
[![](https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fhuggingface%2Fswift-transformers%2Fbadge%3Ftype%3Dplatforms)](https://swiftpackageindex.com/huggingface/swift-transformers)
515

6-
This is a collection of utilities to help adopt language models in Swift apps. It tries to follow the Python `transformers` API and abstractions whenever possible, but it also aims to provide an idiomatic Swift interface and does not assume prior familiarity with [`transformers`](https://github.com/huggingface/transformers) or [`tokenizers`](https://github.com/huggingface/tokenizers).
16+
`swift-transformers` is a collection of utilities to help adopt language models in Swift apps.
717

18+
It tries to follow the Python `transformers` API and abstractions whenever possible, but it also aims to provide an idiomatic Swift interface and does not assume prior familiarity with [`transformers`](https://github.com/huggingface/transformers) or [`tokenizers`](https://github.com/huggingface/tokenizers).
819

9-
## Rationale and Overview
1020

11-
Please, check [our post](https://huggingface.co/blog/swift-coreml-llm).
21+
## Rationale & Overview
1222

13-
## Modules
23+
Check out [our announcement post](https://huggingface.co/blog/swift-coreml-llm).
1424

15-
- `Tokenizers`. Utilities to convert text to tokens and back. Follows the abstractions in [`tokenizers`](https://github.com/huggingface/tokenizers) and [`transformers.js`](https://github.com/xenova/transformers.js). Usage example:
25+
## Modules
1626

27+
- `Tokenizers`: Utilities to convert text to tokens and back, with support for Chat Templates and Tools. Follows the abstractions in [`tokenizers`](https://github.com/huggingface/tokenizers). Usage example:
1728
```swift
1829
import Tokenizers
19-
2030
func testTokenizer() async throws {
21-
let tokenizer = try await AutoTokenizer.from(pretrained: "pcuenq/Llama-2-7b-chat-coreml")
22-
let inputIds = tokenizer("Today she took a train to the West")
23-
assert(inputIds == [1, 20628, 1183, 3614, 263, 7945, 304, 278, 3122])
31+
let tokenizer = try await AutoTokenizer.from(pretrained: "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B")
32+
let messages = [["role": "user", "content": "Describe the Swift programming language."]]
33+
let encoded = try tokenizer.applyChatTemplate(messages: messages)
34+
let decoded = tokenizer.decode(tokens: encoded)
2435
}
2536
```
2637

27-
However, you don't usually need to tokenize the input text yourself - the [`Generation` code](https://github.com/huggingface/swift-transformers/blob/17d4bfae3598482fc7ecf1a621aa77ab586d379a/Sources/Generation/Generation.swift#L82) will take care of it.
38+
- `Hub`: Utilities for interacting with the Hugging Face Hub! Download models, tokenizers and other config files. Usage example:
39+
```swift
40+
import Hub
41+
func testHub() async throws {
42+
let repo = Hub.Repo(id: "mlx-community/Qwen2.5-0.5B-Instruct-2bit-mlx")
43+
let filesToDownload = ["config.json", "*.safetensors"]
44+
let modelDirectory: URL = try await Hub.snapshot(
45+
from: repo,
46+
matching: filesToDownload,
47+
progressHandler: { progress in
48+
print("Download progress: \(progress.fractionCompleted * 100)%")
49+
}
50+
)
51+
print("Files downloaded to: \(modelDirectory.path)")
52+
}
53+
```
2854

29-
- `Hub`. Utilities to download configuration files from the Hub, used to instantiate tokenizers and learn about language model characteristics.
55+
- `Generation`: Algorithms for text generation. Handles tokenization internally. Currently supported ones are: greedy search, top-k sampling, and top-p sampling.
56+
- `Models`: Language model abstraction over a Core ML package.
3057

31-
- `Generation`. Algorithms for text generation. Currently supported ones are greedy search and top-k sampling.
3258

33-
- `Models`. Language model abstraction over a Core ML package.
59+
## Usage via SwiftPM
3460

61+
To use `swift-transformers` with SwiftPM, you can add this to your `Package.swift`:
62+
63+
```swift
64+
dependencies: [
65+
.package(url: "https://github.com/huggingface/swift-transformers", from: "0.1.17")
66+
]
67+
```
68+
69+
And then, add the Transformers library as a dependency to your target:
70+
71+
```swift
72+
targets: [
73+
.target(
74+
name: "YourTargetName",
75+
dependencies: [
76+
.product(name: "Transformers", package: "swift-transformers")
77+
]
78+
)
79+
]
80+
```
81+
82+
## Projects that use swift-transformers ❤️
83+
84+
- [WhisperKit](https://github.com/argmaxinc/WhisperKit): A Swift Package for state-of-the-art speech-to-text systems from [Argmax](https://github.com/argmaxinc)
85+
- [MLX Swift Examples](https://github.com/ml-explore/mlx-swift-examples): A Swift Package for integrating MLX models in Swift apps.
86+
87+
Using `swift-transformers` in your project? Let us know and we'll add you to the list!
3588

3689
## Supported Models
3790

91+
You can run inference on Core ML models with `swift-transformers`. Note that Core ML is not required to use the `Tokenizers` or `Hub` modules.
92+
3893
This package has been tested with autoregressive language models such as:
3994

4095
- GPT, GPT-Neox, GPT-J.
@@ -43,61 +98,25 @@ This package has been tested with autoregressive language models such as:
4398
- Falcon.
4499
- Llama 2.
45100

46-
Encoder-decoder models such as T5 and Flan are currently _not supported_. They are high up in our [priority list](#roadmap).
101+
Encoder-decoder models such as T5 and Flan are currently _not supported_.
47102

48103
## Other Tools
49104

50105
- [`swift-chat`](https://github.com/huggingface/swift-chat), a simple app demonstrating how to use this package.
51106
- [`exporters`](https://github.com/huggingface/exporters), a Core ML conversion package for transformers models, based on Apple's [`coremltools`](https://github.com/apple/coremltools).
52107
- [`transformers-to-coreml`](https://huggingface.co/spaces/coreml-projects/transformers-to-coreml), a no-code Core ML conversion tool built on `exporters`.
53108

54-
## SwiftPM
55-
56-
To use `swift-transformers` with SwiftPM, you can add this to your `Package.swift`:
57-
58-
```swift
59-
dependencies: [
60-
.package(url: "https://github.com/huggingface/swift-transformers", from: "0.1.5")
61-
]
62-
```
63-
64-
And then, add the Transformers library as a dependency to your target:
109+
## Contributing
65110

66-
```
67-
targets: [
68-
.target(
69-
name: "YourTargetName",
70-
dependencies: [
71-
.product(name: "Transformers", package: "swift-transformers")
72-
]
73-
)
74-
]
75-
```
111+
Swift Transformers is a community project and we welcome contributions. Please
112+
check out [Issues](https://github.com/huggingface/swift-transformers/issues)
113+
tagged with `good first issue` if you are looking for a place to start!
76114

77-
## <a name="roadmap"></a> Roadmap / To Do
78-
79-
- [ ] Tokenizers: download from the Hub, port from [`tokenizers`](https://github.com/huggingface/tokenizers)
80-
- [x] BPE family
81-
- [x] Fix Falcon, broken while porting BPE
82-
- [x] Improve tests, add edge cases, see https://github.com/xenova/transformers.js/blob/27920d84831e323275b38f0b5186644b7936e1a2/tests/generate_tests.py#L24
83-
- [x] Include fallback `tokenizer_config.json` for known architectures whose models don't have a configuration in the Hub (GPT2)
84-
- [ ] Port other tokenizer types: Unigram, WordPiece
85-
- [ ] [`exporters`](https://github.com/huggingface/exporters) – Core ML conversion tool.
86-
- [x] Allow max sequence length to be specified.
87-
- [ ] Allow discrete shapes
88-
- [x] Return `logits` from converted Core ML model
89-
- [x] Use `coremltools` @ `main` for latest fixes. In particular, [this merged PR](https://github.com/apple/coremltools/pull/1915) makes it easier to use recent versions of transformers.
90-
- [ ] Generation
91-
- [ ] Nucleus sampling (we currently have greedy and top-k sampling)
92-
- [ ] Use [new `top-k` implementation in `Accelerate`](https://developer.apple.com/documentation/accelerate/bnns#4164142).
93-
- [ ] Support discrete shapes in the underlying Core ML model by selecting the smallest sequence length larger than the input.
94-
- [ ] Optimization: cache past key-values.
95-
- [ ] Encoder-decoder models (T5)
96-
- [ ] [Demo app](https://github.com/huggingface/swift-chat)
97-
- [ ] Allow system prompt to be specified.
98-
- [ ] How to define a system prompt template?
99-
- [ ] Test a code model (to stretch system prompt definition)
115+
Please ensure your code passes the build and test suite before submitting a pull
116+
request. You can run the tests with `swift test`.
100117

101118
## License
102119

103120
[Apache 2](LICENSE).
121+
122+

Sources/Hub/Downloader.swift

Lines changed: 158 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import Combine
1212
class Downloader: NSObject, ObservableObject {
1313
private(set) var destination: URL
1414

15+
private let chunkSize = 10 * 1024 * 1024 // 10MB
16+
1517
enum DownloadState {
1618
case notStarted
1719
case downloading(Double)
@@ -29,7 +31,17 @@ class Downloader: NSObject, ObservableObject {
2931

3032
private var urlSession: URLSession? = nil
3133

32-
init(from url: URL, to destination: URL, using authToken: String? = nil, inBackground: Bool = false) {
34+
init(
35+
from url: URL,
36+
to destination: URL,
37+
using authToken: String? = nil,
38+
inBackground: Bool = false,
39+
resumeSize: Int = 0,
40+
headers: [String: String]? = nil,
41+
expectedSize: Int? = nil,
42+
timeout: TimeInterval = 10,
43+
numRetries: Int = 5
44+
) {
3345
self.destination = destination
3446
super.init()
3547
let sessionIdentifier = "swift-transformers.hub.downloader"
@@ -43,10 +55,28 @@ class Downloader: NSObject, ObservableObject {
4355

4456
self.urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)
4557

46-
setupDownload(from: url, with: authToken)
58+
setupDownload(from: url, with: authToken, resumeSize: resumeSize, headers: headers, expectedSize: expectedSize, timeout: timeout, numRetries: numRetries)
4759
}
4860

49-
private func setupDownload(from url: URL, with authToken: String?) {
61+
/// Sets up and initiates a file download operation
62+
///
63+
/// - Parameters:
64+
/// - url: Source URL to download from
65+
/// - authToken: Bearer token for authentication with Hugging Face
66+
/// - resumeSize: Number of bytes already downloaded for resuming interrupted downloads
67+
/// - headers: Additional HTTP headers to include in the request
68+
/// - expectedSize: Expected file size in bytes for validation
69+
/// - timeout: Time interval before the request times out
70+
/// - numRetries: Number of retry attempts for failed downloads
71+
private func setupDownload(
72+
from url: URL,
73+
with authToken: String?,
74+
resumeSize: Int,
75+
headers: [String: String]?,
76+
expectedSize: Int?,
77+
timeout: TimeInterval,
78+
numRetries: Int
79+
) {
5080
downloadState.value = .downloading(0)
5181
urlSession?.getAllTasks { tasks in
5282
// If there's an existing pending background task with the same URL, let it proceed.
@@ -71,14 +101,137 @@ class Downloader: NSObject, ObservableObject {
71101
}
72102
}
73103
var request = URLRequest(url: url)
104+
105+
// Use headers from argument else create an empty header dictionary
106+
var requestHeaders = headers ?? [:]
107+
108+
// Populate header auth and range fields
74109
if let authToken = authToken {
75-
request.setValue("Bearer \(authToken)", forHTTPHeaderField: "Authorization")
110+
requestHeaders["Authorization"] = "Bearer \(authToken)"
111+
}
112+
if resumeSize > 0 {
113+
requestHeaders["Range"] = "bytes=\(resumeSize)-"
76114
}
115+
116+
117+
request.timeoutInterval = timeout
118+
request.allHTTPHeaderFields = requestHeaders
77119

78-
self.urlSession?.downloadTask(with: request).resume()
120+
Task {
121+
do {
122+
// Create a temp file to write
123+
let tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
124+
FileManager.default.createFile(atPath: tempURL.path, contents: nil)
125+
let tempFile = try FileHandle(forWritingTo: tempURL)
126+
127+
defer { tempFile.closeFile() }
128+
try await self.httpGet(request: request, tempFile: tempFile, resumeSize: resumeSize, numRetries: numRetries, expectedSize: expectedSize)
129+
130+
// Clean up and move the completed download to its final destination
131+
tempFile.closeFile()
132+
try FileManager.default.moveDownloadedFile(from: tempURL, to: self.destination)
133+
134+
self.downloadState.value = .completed(self.destination)
135+
} catch {
136+
self.downloadState.value = .failed(error)
137+
}
138+
}
79139
}
80140
}
81141

142+
/// Downloads a file from given URL using chunked transfer and handles retries.
143+
///
144+
/// Reference: https://github.com/huggingface/huggingface_hub/blob/418a6ffce7881f5c571b2362ed1c23ef8e4d7d20/src/huggingface_hub/file_download.py#L306
145+
///
146+
/// - Parameters:
147+
/// - request: The URLRequest for the file to download
148+
/// - resumeSize: The number of bytes already downloaded. If set to 0 (default), the whole file is download. If set to a positive number, the download will resume at the given position
149+
/// - numRetries: The number of retry attempts remaining for failed downloads
150+
/// - expectedSize: The expected size of the file to download. If set, the download will raise an error if the size of the received content is different from the expected one.
151+
/// - Throws: `DownloadError.unexpectedError` if the response is invalid or file size mismatch occurs
152+
/// `URLError` if the download fails after all retries are exhausted
153+
private func httpGet(
154+
request: URLRequest,
155+
tempFile: FileHandle,
156+
resumeSize: Int,
157+
numRetries: Int,
158+
expectedSize: Int?
159+
) async throws {
160+
guard let session = self.urlSession else {
161+
throw DownloadError.unexpectedError
162+
}
163+
164+
// Create a new request with Range header for resuming
165+
var newRequest = request
166+
if resumeSize > 0 {
167+
newRequest.setValue("bytes=\(resumeSize)-", forHTTPHeaderField: "Range")
168+
}
169+
170+
// Start the download and get the byte stream
171+
let (asyncBytes, response) = try await session.bytes(for: newRequest)
172+
173+
guard let response = response as? HTTPURLResponse else {
174+
throw DownloadError.unexpectedError
175+
}
176+
177+
guard (200..<300).contains(response.statusCode) else {
178+
throw DownloadError.unexpectedError
179+
}
180+
181+
var downloadedSize = resumeSize
182+
183+
// Create a buffer to collect bytes before writing to disk
184+
var buffer = Data(capacity: chunkSize)
185+
186+
var newNumRetries = numRetries
187+
do {
188+
for try await byte in asyncBytes {
189+
buffer.append(byte)
190+
// When buffer is full, write to disk
191+
if buffer.count == chunkSize {
192+
if !buffer.isEmpty { // Filter out keep-alive chunks
193+
try tempFile.write(contentsOf: buffer)
194+
buffer.removeAll(keepingCapacity: true)
195+
downloadedSize += chunkSize
196+
newNumRetries = 5
197+
guard let expectedSize = expectedSize else { continue }
198+
let progress = expectedSize != 0 ? Double(downloadedSize) / Double(expectedSize) : 0
199+
downloadState.value = .downloading(progress)
200+
}
201+
}
202+
}
203+
204+
if !buffer.isEmpty {
205+
try tempFile.write(contentsOf: buffer)
206+
downloadedSize += buffer.count
207+
buffer.removeAll(keepingCapacity: true)
208+
newNumRetries = 5
209+
}
210+
} catch let error as URLError {
211+
if newNumRetries <= 0 {
212+
throw error
213+
}
214+
try await Task.sleep(nanoseconds: 1_000_000_000)
215+
216+
let config = URLSessionConfiguration.default
217+
self.urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)
218+
219+
try await httpGet(
220+
request: request,
221+
tempFile: tempFile,
222+
resumeSize: downloadedSize,
223+
numRetries: newNumRetries - 1,
224+
expectedSize: expectedSize
225+
)
226+
}
227+
228+
// Verify the downloaded file size matches the expected size
229+
let actualSize = try tempFile.seekToEnd()
230+
if let expectedSize = expectedSize, expectedSize != actualSize {
231+
throw DownloadError.unexpectedError
232+
}
233+
}
234+
82235
@discardableResult
83236
func waitUntilDone() throws -> URL {
84237
// It's either this, or stream the bytes ourselves (add to a buffer, save to disk, etc; boring and finicky)

0 commit comments

Comments
 (0)