Views
No views yet
1import torch
2import numpy as np
3from transformers import AutoModelForCausalLM, AutoTokenizer
4import coremltools as ct
5
6# Load model and convert to TorchScript
7model = AutoModelForCausalLM.from_pretrained("LSX-UniWue/LLaMmlein_1B")
8tokenizer = AutoTokenizer.from_pretrained("LSX-UniWue/LLaMmlein_1B")
9
10# Set model to eval mode
11model.eval()
12
13# Create example input
14text = "Ein Beispieltext"
15inputs = tokenizer(text, return_tensors="pt")
16
17# Create a wrapper class for tracing
18class ModelWrapper(torch.nn.Module):
19 def __init__(self, model):
20 super().__init__()
21 self.model = model
22
23 def forward(self, input_ids):
24 return self.model(input_ids).logits
25
26# Wrap and trace model
27wrapped_model = ModelWrapper(model)
28traced_model = torch.jit.trace(wrapped_model, inputs.input_ids)
29
30# Convert to CoreML
31model_mlpackage = ct.convert(
32 traced_model,
33 inputs=[
34 ct.TensorType(
35 name="input_ids",
36 shape=inputs.input_ids.shape,
37 dtype=np.int32
38 )
39 ],
40 source="pytorch",
41 minimum_deployment_target=ct.target.iOS16,
42 convert_to="mlprogram",
43 compute_precision=ct.precision.FLOAT16,
44 compute_units=ct.ComputeUnit.ALL,
45)
46
47model_mlpackage.save("LLaMmlein_1B.mlpackage")1import CoreML
2
3// Load the model
4let config = MLModelConfiguration()
5let model = try LLaMmlein_1B(configuration: config)
6
7// Prepare input
8let inputIds = // Your tokenized input as [Int32]
9
10// Make prediction
11let prediction = try model.prediction(input_ids: inputIds)1@misc{llammlein2024,
2 title={LLäMmlein: A German Language Model},
3 author={LSX-UniWue},
4 year={2024},
5 publisher={Hugging Face},
6 journal={Hugging Face Hub},
7 howpublished={\url{https://huggingface.co/LSX-UniWue/LLaMmlein_1B}},
8}