huggingface / swift-transformers

Swift Package to implement a transformers-like API in Swift
Apache License 2.0
727 stars 81 forks source link

swift-transformers

Unit Tests

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

Rationale and Overview

Please, check our post.

Modules

import Tokenizers

func testTokenizer() async throws {
    let tokenizer = try await AutoTokenizer.from(pretrained: "pcuenq/Llama-2-7b-chat-coreml")
    let inputIds = tokenizer("Today she took a train to the West")
    assert(inputIds == [1, 20628, 1183, 3614, 263, 7945, 304, 278, 3122])
}

However, you don't usually need to tokenize the input text yourself - the Generation code will take care of it.

Supported Models

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

Encoder-decoder models such as T5 and Flan are currently not supported. They are high up in our priority list.

Other Tools

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.5")
]

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

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

Roadmap / To Do

License

Apache 2.