Skip to content

Instantly share code, notes, and snippets.

Sure, for running an LLM locally on an iOS device, we can use a smaller, more efficient model like GPT-2 or a distilled version. For simplicity, I will demonstrate using the CoreML framework with a pre-trained GPT-2 model that has been converted to CoreML format. This example will include the necessary steps to integrate the model and measure the response time.

Complete Example for iOS App with Swift, Metal, and a Local GPT-2 Model

1. Convert a Pre-trained GPT-2 Model to CoreML

First, you need to convert a pre-trained GPT-2 model to CoreML format. This is typically done outside Xcode using a Python script. Here is a basic script to convert a GPT-2 model to CoreML:

# Install the required packages

Certainly! Below are the instructions for converting a GPT-2 model to CoreML and the code for a macOS command-line tool to use that model.

Step 1: Convert GPT-2 Model to CoreML

First, use a Python script to convert the GPT-2 model to CoreML.

Python Script to Convert GPT-2 Model to CoreML

  1. Install the required packages: