Skip to content

Instantly share code, notes, and snippets.

@szmeku
Created July 11, 2024 22:17
Show Gist options
  • Save szmeku/7e8e8487d903723c0dc1b2f44c2d73c2 to your computer and use it in GitHub Desktop.
Save szmeku/7e8e8487d903723c0dc1b2f44c2d73c2 to your computer and use it in GitHub Desktop.

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:

    pip install transformers coremltools torch
  2. Create the conversion script (convert_gpt2_to_coreml.py):

    import coremltools as ct
    from transformers import GPT2LMHeadModel, GPT2Tokenizer
    import torch
    
    # Load the pre-trained GPT-2 model and tokenizer
    model_name = "gpt2"
    tokenizer = GPT2Tokenizer.from_pretrained(model_name)
    model = GPT2LMHeadModel.from_pretrained(model_name)
    
    # Trace the model with a dummy input
    input_text = "Convert this text"
    input_ids = tokenizer.encode(input_text, return_tensors="pt")
    
    traced_model = torch.jit.trace(model, input_ids)
    coreml_model = ct.convert(
        traced_model,
        inputs=[ct.TensorType(name="input_ids", shape=input_ids.shape)],
    )
    
    # Save the CoreML model
    coreml_model.save("GPT2.mlmodel")
  3. Run the conversion script:

    python convert_gpt2_to_coreml.py

This will generate a GPT2.mlmodel file in your current directory.

Step 2: Create a macOS Command-Line Tool

Create a new Command-Line Tool in Xcode

  1. Open Xcode and create a new project.
  2. Choose "macOS" and then "Command Line Tool".
  3. Name your project, ensure the language is set to Swift.

Add the Model to Your Xcode Project

  1. Drag and drop the GPT2.mlmodel file into your Xcode project.

Replace main.swift with the following code

import Foundation
import CoreML

// Define the LLM Manager
class LLMManager {
    static let shared = LLMManager()
    
    private init() {}
    
    func runLLMTest() {
        let input = "Fixed prompt for the language model"
        
        // Measure the time of LLM response
        let startTime = CFAbsoluteTimeGetCurrent()
        
        // Perform LLM inference
        let output = processLLM(input: input)
        
        let endTime = CFAbsoluteTimeGetCurrent()
        let responseTime = endTime - startTime
        
        print("LLM Response: \(output)")
        print("LLM Response Time: \(responseTime) seconds")
    }
    
    private func processLLM(input: String) -> String {
        guard let model = try? GPT2(configuration: MLModelConfiguration()) else {
            return "Failed to load model"
        }
        
        guard let input_ids = try? GPT2Input(text: input) else {
            return "Failed to create input"
        }
        
        guard let output = try? model.prediction(input: input_ids) else {
            return "Failed to make prediction"
        }
        
        return output.text
    }
}

// Run the LLM Test
LLMManager.shared.runLLMTest()

Additional Steps

  1. Add the Model to the Target:

    • Ensure the GPT2.mlmodel file is added to the target settings of your project.
  2. Run the Project:

    • Select the scheme for your command-line tool and run the project in Xcode. The output will be printed to the console.

Summary

  • Convert GPT-2 Model to CoreML: Use the provided Python script to convert a GPT-2 model to CoreML.
  • macOS Command-Line Tool: Create a new command-line tool in Xcode, add the GPT2.mlmodel file, and use the provided Swift code to perform inference and measure the response time.

This setup will allow you to run a local CoreML model in a macOS command-line tool and measure the inference time without any user interface.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment