Skip to content
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
24 changes: 20 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,17 @@ jobs:
HF_HUB_READ_TOKEN: ${{ secrets.HF_HUB_READ_TOKEN }}

test-linux:
name: Test (Linux)
name: Test (Linux)${{ matrix.traits != '' && format(' with --traits {0}', matrix.traits) || '' }}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh nice

runs-on: ubuntu-latest
container: swift:6.2.3
container: swift:${{ matrix.swift }}
strategy:
fail-fast: false
matrix:
include:
- swift: "6.2.3"
traits: ""
- swift: "6.2.3"
traits: "Xet"
steps:
- name: Checkout PR merge ref if called from PR
if: ${{ github.event.pull_request.number }}
Expand All @@ -39,8 +47,16 @@ jobs:
if: ${{ !github.event.pull_request.number }}
uses: actions/checkout@v6

- name: Cache Swift Package Manager dependencies
uses: actions/cache@v5
with:
path: ~/.cache/org.swift.swiftpm
key: ${{ runner.os }}-swift-${{ matrix.swift }}-${{ matrix.traits == '' && 'xet-off' || 'xet-on' }}-spm-${{ hashFiles('Package*.swift') }}-${{ hashFiles('Package.resolved') }}
restore-keys: |
${{ runner.os }}-swift-${{ matrix.swift }}-${{ matrix.traits == '' && 'xet-off' || 'xet-on' }}-spm-

- name: Build
run: swift build
run: swift build${{ matrix.traits != '' && format(' --traits {0}', matrix.traits) || '' }}

- name: Run tests
env:
Expand All @@ -49,7 +65,7 @@ jobs:
# FoundationNetworking race condition that crashes during teardown.
# See: https://github.com/swiftlang/swift-corelibs-foundation/issues/3675
run: |
swift test --skip HubTests --skip TokenizersTests
swift test --skip HubTests --skip TokenizersTests${{ matrix.traits != '' && format(' --traits {0}', matrix.traits) || '' }}
lint:
name: Lint
runs-on: macos-latest
Expand Down
67 changes: 67 additions & 0 deletions Package@swift-6.1.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// swift-tools-version: 6.1
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

/// Define the strict concurrency settings to be applied to all targets.
let swiftSettings: [SwiftSetting] = [
.enableExperimentalFeature("StrictConcurrency")
]

let package = Package(
name: "swift-transformers",
platforms: [.iOS(.v16), .macOS(.v13)],
products: [
.library(name: "Hub", targets: ["Hub"]),
.library(name: "Tokenizers", targets: ["Tokenizers"]),
.library(name: "Transformers", targets: ["Tokenizers", "Generation", "Models"]),
],
traits: [
.trait(
name: "Xet",
description: "Enable Xet transport support in swift-huggingface."
)
],
dependencies: [
.package(url: "https://github.com/huggingface/swift-jinja.git", from: "2.0.0"),
// Work around SwiftPM trait resolution issue for transitive Xet dependency.
.package(url: "https://github.com/huggingface/swift-xet.git", from: "0.2.0"),
.package(
url: "https://github.com/huggingface/swift-huggingface.git",
from: "0.9.0",
traits: [
.defaults,
.trait(name: "Xet", condition: .when(traits: ["Xet"])),
]
),
.package(url: "https://github.com/apple/swift-collections.git", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-crypto.git", "3.0.0"..<"5.0.0"),
.package(url: "https://github.com/ibireme/yyjson.git", exact: "0.12.0"),
],
targets: [
.target(name: "Generation", dependencies: ["Tokenizers"]),
.target(
name: "Hub",
dependencies: [
.product(name: "Jinja", package: "swift-jinja"),
.product(name: "HuggingFace", package: "swift-huggingface"),
.product(name: "Xet", package: "swift-xet", condition: .when(traits: ["Xet"])),
.product(name: "OrderedCollections", package: "swift-collections"),
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "yyjson", package: "yyjson"),
],
resources: [
.process("Resources")
],
swiftSettings: swiftSettings
),
.target(name: "Models", dependencies: ["Tokenizers", "Generation"]),
.target(name: "Tokenizers", dependencies: ["Hub", .product(name: "Jinja", package: "swift-jinja")]),
.testTarget(name: "Benchmarks", dependencies: ["Hub", "Tokenizers", .product(name: "yyjson", package: "yyjson")]),
.testTarget(name: "GenerationTests", dependencies: ["Generation"]),
.testTarget(name: "HubTests", dependencies: ["Hub", .product(name: "Jinja", package: "swift-jinja")], swiftSettings: swiftSettings),
.testTarget(name: "ModelsTests", dependencies: ["Models", "Hub"], resources: [.process("Resources")]),
.testTarget(name: "TokenizersTests", dependencies: ["Tokenizers", "Models", "Hub"], resources: [.process("Resources")]),
],
swiftLanguageModes: [.v5]
)
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ To use `swift-transformers` with SwiftPM, you can add this to your `Package.swif

```swift
dependencies: [
.package(url: "https://github.com/huggingface/swift-transformers", from: "0.1.17")
.package(url: "https://github.com/huggingface/swift-transformers", from: "1.3.0")
]
```

Expand All @@ -141,6 +141,46 @@ targets: [
]
```

### Optional Xet trait

`swift-transformers` includes an `Xet` [package trait](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0450-swiftpm-package-traits.md)
that enables fast, parallel downloads from the Hugging Face Hub
via [swift-xet](https://github.com/huggingface/swift-xet).
Because Xet introduces additional transitive dependencies
(including [AsyncHTTPClient](https://github.com/swift-server/async-http-client)),
it is opt-in.

On Swift 6.1+, enable it in your `Package.swift`:

```swift
dependencies: [
.package(
url: "https://github.com/huggingface/swift-transformers",
from: "1.3.0",
traits: ["Xet"]
)
]
```

Or from the command line:

```bash
swift build --traits Xet
swift test --traits Xet
```

When the trait is not enabled,
Hub downloads use the default `URLSession`-based transport.

On Swift versions earlier than 6.1,
package traits are unavailable and Xet transport cannot be enabled.

> [!NOTE]
> Xcode doesn't yet provide a built-in way to declare package dependencies with traits.
> As a workaround, you can create an internal Swift package
> that re-exports `swift-transformers` with the `Xet` trait enabled,
> then add that package as a local dependency in your Xcode project.

## Projects that use swift-transformers ❤️

- [WhisperKit](https://github.com/argmaxinc/WhisperKit): A Swift Package for state-of-the-art speech-to-text systems from [Argmax](https://github.com/argmaxinc)
Expand Down
Loading