Convert Figma logo to code with AI

huggingface logoswift-transformers

Swift Package to implement a transformers-like API in Swift

1,017
130
1,017
29

Top Related Projects

Stable Diffusion with Core ML on Apple Silicon

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.

6,134

Swift for TensorFlow

Port of OpenAI's Whisper model in C/C++

21,810

A minimal PyTorch re-implementation of the OpenAI GPT (Generative Pretrained Transformer) training

Quick Overview

Swift Transformers is a library that brings Hugging Face's Transformers to Apple platforms using Swift. It allows developers to run state-of-the-art natural language processing models on iOS, macOS, and other Apple devices, leveraging the power of Core ML for efficient on-device inference.

Pros

  • Native Swift implementation for seamless integration with Apple platforms
  • Utilizes Core ML for optimized on-device performance
  • Supports a wide range of pre-trained models from the Hugging Face ecosystem
  • Enables offline NLP capabilities for enhanced privacy and reduced latency

Cons

  • Limited to Apple platforms, not suitable for cross-platform development
  • May have a smaller community and fewer resources compared to the Python version
  • Potential limitations in model customization compared to the original Transformers library
  • Might lag behind the Python version in terms of new model support and features

Code Examples

  1. Loading a pre-trained model:
import SwiftTransformers

let model = try! Pipeline(model: .distilbertBaseUncased, tokenizer: .distilbertBaseUncased)
  1. Performing sentiment analysis:
let text = "I love using Swift Transformers!"
let result = try! model.classify(text)
print(result) // Prints the sentiment classification and confidence score
  1. Generating text:
let prompt = "Once upon a time"
let generatedText = try! model.generate(prompt, maxLength: 50)
print(generatedText) // Prints the generated text continuation

Getting Started

To get started with Swift Transformers:

  1. Add the package to your Xcode project:

  2. Import the library in your Swift file:

import SwiftTransformers
  1. Initialize a pipeline with a pre-trained model:
let pipeline = try! Pipeline(model: .bertBaseUncased, tokenizer: .bertBaseUncased)
  1. Use the pipeline for various NLP tasks, such as classification or text generation.

Competitor Comparisons

Stable Diffusion with Core ML on Apple Silicon

Pros of ml-stable-diffusion

  • Specifically optimized for Apple Silicon, leveraging Metal performance
  • Includes a user-friendly demo app for image generation
  • Provides comprehensive documentation and examples for integration

Cons of ml-stable-diffusion

  • Limited to stable diffusion models, less versatile than swift-transformers
  • Requires macOS and Apple hardware, reducing cross-platform compatibility
  • Less frequent updates compared to swift-transformers

Code Comparison

swift-transformers:

let model = try GPT2(checkpoint: "gpt2-medium")
let input = "Hello, how are"
let output = try model.generate(input)
print(output)

ml-stable-diffusion:

let pipeline = try StableDiffusionPipeline(resourcesAt: resourcesURL)
let image = try pipeline.generateImages(
    prompt: "A beautiful sunset over the ocean",
    imageCount: 1
)[0]

Both repositories offer Swift implementations for machine learning models, but they focus on different areas. swift-transformers provides a broader range of transformer-based models, while ml-stable-diffusion specializes in image generation using stable diffusion models optimized for Apple hardware.

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.

Pros of coremltools

  • Broader scope, supporting conversion of various ML models to Core ML format
  • Extensive documentation and Apple's official support
  • Integration with a wide range of ML frameworks (TensorFlow, PyTorch, scikit-learn, etc.)

Cons of coremltools

  • Focused on Core ML conversion, not specifically optimized for Transformers
  • May require more setup and configuration for Transformer models
  • Less streamlined for Swift developers working directly with Transformer models

Code Comparison

swift-transformers:

let model = TransformerModel(configuration: config)
let tokenizer = Tokenizer(vocabulary: vocab)
let input = tokenizer.encode(text: "Hello, world!")
let output = model(input)

coremltools:

import coremltools as ct

mlmodel = ct.convert(
    'bert_model.pkl',
    source='sklearn',
    convert_to='mlprogram',
    compute_units=ct.ComputeUnit.ALL
)

Summary

swift-transformers is tailored for Swift developers working with Transformer models, offering a more streamlined experience. coremltools, on the other hand, provides a broader toolset for converting various ML models to Core ML format, with extensive documentation and support from Apple. The choice between the two depends on the specific requirements of the project and the developer's familiarity with Swift and Core ML ecosystems.

6,134

Swift for TensorFlow

Pros of swift

  • More mature and established project with a larger community
  • Broader scope, covering general machine learning beyond just transformers
  • Better integration with TensorFlow ecosystem and tools

Cons of swift

  • Steeper learning curve for those not familiar with TensorFlow
  • Less focused on transformer models specifically
  • May have more overhead for simple transformer-based tasks

Code Comparison

swift-transformers:

import Transformers

let model = GPT2.pretrained()
let tokenizer = GPT2Tokenizer.pretrained()

let input = tokenizer.encode("Hello, world!")
let output = model.generate(input)

swift:

import TensorFlow

let model = BERT.pretrained(.bertBase)
let tokenizer = BertTokenizer.pretrained(.bertBase)

let input = tokenizer.encode("Hello, world!")
let output = model(input)

Summary

swift-transformers is a more focused library specifically for transformer models in Swift, while swift is a broader machine learning framework that includes transformer capabilities. swift-transformers may be easier to use for transformer-specific tasks, while swift offers more flexibility and integration with the larger TensorFlow ecosystem. The choice between the two depends on the specific project requirements and the developer's familiarity with TensorFlow.

Port of OpenAI's Whisper model in C/C++

Pros of whisper.cpp

  • Lightweight and efficient C++ implementation for running Whisper models
  • Optimized for CPU usage, making it suitable for devices with limited resources
  • Supports various quantization options for reduced memory footprint

Cons of whisper.cpp

  • Limited to Whisper models, while swift-transformers supports a broader range of transformer-based models
  • Less integration with the broader Hugging Face ecosystem and tools
  • May require more manual setup and configuration compared to swift-transformers

Code Comparison

whisper.cpp:

#include "whisper.h"

int main() {
    struct whisper_context * ctx = whisper_init_from_file("ggml-base.en.bin");
    whisper_full_default(ctx, wparams, pcmf32.data(), pcmf32.size());
    whisper_print_timings(ctx);
    whisper_free(ctx);
}

swift-transformers:

import SwiftTransformers

let tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
let model = AutoModel.from_pretrained("bert-base-uncased")
let input = tokenizer.encode("Hello, world!")
let output = model.forward(input)

The code snippets demonstrate the basic usage of each library. whisper.cpp focuses on audio transcription with Whisper models, while swift-transformers provides a more general-purpose interface for various transformer models in Swift.

21,810

A minimal PyTorch re-implementation of the OpenAI GPT (Generative Pretrained Transformer) training

Pros of minGPT

  • Simplicity and educational focus, making it easier to understand GPT architecture
  • Lightweight implementation with minimal dependencies
  • Flexibility for experimentation and customization

Cons of minGPT

  • Limited features compared to swift-transformers
  • Less optimized for production use
  • Smaller community and fewer updates

Code Comparison

minGPT:

class GPT(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.tok_emb = nn.Embedding(config.vocab_size, config.n_embd)
        self.pos_emb = nn.Parameter(torch.zeros(1, config.block_size, config.n_embd))
        self.drop = nn.Dropout(config.embd_pdrop)
        self.blocks = nn.Sequential(*[Block(config) for _ in range(config.n_layer)])
        self.ln_f = nn.LayerNorm(config.n_embd)
        self.head = nn.Linear(config.n_embd, config.vocab_size, bias=False)

swift-transformers:

public struct TransformerConfig {
    public let vocabularySize: Int
    public let hiddenSize: Int
    public let numHiddenLayers: Int
    public let numAttentionHeads: Int
    public let intermediateSize: Int
    public let hiddenAct: String
    public let hiddenDropoutProb: Float
    public let attentionProbsDropoutProb: Float
    public let maxPositionEmbeddings: Int
    public let typeVocabSize: Int
    public let initializerRange: Float
}

The code snippets show the core structure of each implementation, highlighting the differences in language (Python vs. Swift) and approach to model configuration.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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.