- Notifications
You must be signed in to change notification settings - Fork130
Swift Package to implement a transformers-like API in Swift
License
huggingface/swift-transformers
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
swift-transformers
is a collection of utilities to help adopt language models in Swift apps.
It tries to follow the Pythontransformers
API and abstractions whenever possible, but it also aims to provide an idiomatic Swift interface and does not assume prior familiarity withtransformers
ortokenizers
.
Check outour announcement post.
Tokenizers
: Utilities to convert text to tokens and back, with support for Chat Templates and Tools. Follows the abstractions intokenizers
. Usage example:
import Tokenizersfunc testTokenizer()asyncthrows{lettokenizer=tryawaitAutoTokenizer.from(pretrained:"deepseek-ai/DeepSeek-R1-Distill-Qwen-7B")letmessages=[["role":"user","content":"Describe the Swift programming language."]]letencoded=try tokenizer.applyChatTemplate(messages: messages)letdecoded= tokenizer.decode(tokens: encoded)}
Hub
: Utilities for interacting with the Hugging Face Hub! Download models, tokenizers and other config files. Usage example:
import Hubfunc testHub()asyncthrows{letrepo=Hub.Repo(id:"mlx-community/Qwen2.5-0.5B-Instruct-2bit-mlx")letfilesToDownload=["config.json","*.safetensors"]letmodelDirectory:URL=tryawaitHub.snapshot( from: repo, matching: filesToDownload, progressHandler:{ progressinprint("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.
To useswift-transformers
with SwiftPM, you can add this to yourPackage.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")])]
- WhisperKit: A Swift Package for state-of-the-art speech-to-text systems fromArgmax
- MLX Swift Examples: A Swift Package for integrating MLX models in Swift apps.
Usingswift-transformers
in your project? Let us know and we'll add you to the list!
You can run inference on Core ML models withswift-transformers
. Note that Core ML is not required to use theTokenizers
orHub
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 currentlynot supported.
swift-chat
, a simple app demonstrating how to use this package.exporters
, a Core ML conversion package for transformers models, based on Apple'scoremltools
.transformers-to-coreml
, a no-code Core ML conversion tool built onexporters
.
Swift Transformers is a community project and we welcome contributions. Pleasecheck outIssuestagged withgood first issue
if you are looking for a place to start!
Please ensure your code passes the build and test suite before submitting a pullrequest. You can run the tests withswift test
.
About
Swift Package to implement a transformers-like API in Swift
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.