Skip to content

huggingface/swift-transformers

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swift + Transformers

Unit Tests

swift-transformers 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 or tokenizers.

Rationale & Overview

Check out our announcement post.

Modules

  • Tokenizers: Utilities to convert text to tokens and back, with support for Chat Templates and Tools. Follows the abstractions in tokenizers. Usage example:
import Tokenizers
func testTokenizer() async throws {
    let tokenizer = try await AutoTokenizer.from(pretrained: "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B")
    let messages = [["role": "user", "content": "Describe the Swift programming language."]]
    let encoded = try tokenizer.applyChatTemplate(messages: messages)
    let decoded = tokenizer.decode(tokens: encoded)
}
  • Hub: Utilities for interacting with the Hugging Face Hub! Download models, tokenizers and other config files. Usage example:
import Hub
func testHub() async throws {
    let repo = Hub.Repo(id: "mlx-community/Qwen2.5-0.5B-Instruct-2bit-mlx")
    let filesToDownload = ["config.json", "*.safetensors"]
    let modelDirectory: URL = try await Hub.snapshot(
        from: repo,
        matching: filesToDownload,
        progressHandler: { progress in
            print("Download progress: \(progress.fractionCompleted * 100)%")
        }
    )
    print("Files downloaded to: \(modelDirectory.path)")
}
  • Generation: Algorithms for text generation. Handles tokenization internally. Currently supported ones are: greedy search, top-k sampling, and top-p sampling.
  • Models: Language model abstraction over a Core ML package.

Usage via SwiftPM

To use swift-transformers with SwiftPM, you can add this to your Package.swift:

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

And then, add the Transformers library as a dependency to your target:

targets: [
    .target(
        name: "YourTargetName",
        dependencies: [
            .product(name: "Transformers", package: "swift-transformers")
        ]
    )
]

Projects that use swift-transformers ❤️

Using swift-transformers in your project? Let us know and we'll add you to the list!

Supported Models

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.

This package has been tested with autoregressive language models such as:

  • GPT, GPT-Neox, GPT-J.
  • SantaCoder.
  • StarCoder.
  • Falcon.
  • Llama 2.

Encoder-decoder models such as T5 and Flan are currently not supported.

Other Tools

Contributing

Swift Transformers is a community project and we welcome contributions. Please check out Issues tagged with good first issue if you are looking for a place to start!

Please ensure your code passes the build and test suite before submitting a pull request. You can run the tests with swift test.

License

Apache 2.

About

Swift Package to implement a transformers-like API in Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages