harbor run -t openthoughts/tasktrove-mix-h10-reward-staged-v2-mix-h10-staged-1789harbor download openthoughts/tasktrove-mix-h10-reward-staged-v2-mix-h10-staged-1789solution file
from src.ml_ane_transformers.ane_gpt2 import GPT as AneGPT
from src.utils.model_proxy import MLModelProxy
from transformers import AutoTokenizer
import torch
import torch.nn.functional as F
import numpy as np
import coremltools as ct
from stopwatch import Stopwatch
from models.gpt2 import GPT as GPT2
from models.pythia import GPT as Pythia
import argparse
import sys
import os
import glob
from collections import OrderedDict
import subprocess
"""
Load a CoreML model and use it to generate text.
"""
os.environ["TOKENIZERS_PARALLELISM"] = "true"
compute_unit_by_name = OrderedDict([
("All", ct.ComputeUnit.ALL),
("CPUOnly", ct.ComputeUnit.CPU_ONLY),
("CPUAndGPU", ct.ComputeUnit.CPU_AND_GPU),
("CPUAndANE", ct.ComputeUnit.CPU_AND_NE),
])
parser = argparse.ArgumentParser(description='Load a CoreML modelpackage and generate some text.')
parser.add_argument('--model_path', help='path to .mlpackage file', default="gpt2.mlpackage", type=str)
parser.add_argument('--input_prompt', help='input prompt for the model', default="Before boarding your rocket to Mars, remember to pack these items:", type=str)
parser.add_argument('--compute_unit', help='compute unit', type=str, choices=list(compute_unit_by_name.keys()), default="All")
parser.add_argument('--length', help='number of new tokens to generate', type=int, default=40)
parser.add_argument('--verbose', help='print verbose logs', type=bool, default=False)
parser.add_argument('--wait', help='wait for confirmation before loading the model (ie to attach a debugger)', action="store_true")
parser.add_argument('--use-mlpackage', help='don\'t automatically generate a mlmodelc and use it. dramatically slower but useful for debugging this script.', action="store_true")
args = parser.parse_args()
if not args.model_path.endswith('.mlpackage') and not args.model_path.endswith('.mlmodelc') :
print('Error: Model path must end in .mlpackage (or .mlmodelc if you know what you\'re doing)')
sys.exit(1)
# Special handling for first-time run.
if not os.path.exists(args.model_path) and args.model_path == "gpt2.mlpackage":
files = glob.glob('gpt2*.mlpackage')
files = sorted(files, key=lambda x: os.path.getmtime(x))
if len(files) == 0:
print(f"Couldn't find {args.model_path}. Either use the --model_path argument or run convert.py to generate one.")
sys.exit(1)
args.model_path = files[-1]
compute_unit = compute_unit_by_name[args.compute_unit]
def vprint(*pargs, **kwargs):
if args.verbose:
print(*pargs, **kwargs)
def get_tokenizer_name(model_path):
names = GPT2.model_names() + Pythia.model_names()
tokenizer_lookup = {**GPT2.tokenizer_by_name(), **Pythia.tokenizer_by_name()}
for n in sorted(names, key=len):
if model_path.startswith(n):
return tokenizer_lookup[n]
print(f"No tokenizer found for {model_path}")
print(f"Model name must start with one of:")
print(names)
return None
tokenizer_name = get_tokenizer_name(args.model_path)
if tokenizer_name is None:
sys.exit(1)
vprint("Loading tokenizer...")
tok = AutoTokenizer.from_pretrained(tokenizer_name)
tok.pad_token_id = tok.eos_token_id
vprint("Loaded tokenizer.")
if args.wait:
print(f"Current PID: {os.getpid()}")
input("Waiting. Press Enter to continue.")
# Compile to make generations 2-n much much faster.
base_path = args.model_path.replace(".mlpackage/", "").replace(".mlmodelc/", "").replace(".mlpackage", "").replace(".mlmodelc", "")
mlpackage_path = base_path + ".mlpackage"
mlmodelc_path = base_path + ".mlmodelc"
has_compiled_model = os.path.exists(mlmodelc_path)
if not has_compiled_model:
# Looking to turn this off? As far as I know it's not worth it.
# Generating text from a mlpackage does this same compilation every time (slow) and
# it doesn't get cached so you will actually use _more_ disk space without this.
# It's also much faster to load the model this way. For the xl model this will
# take model loading from 1.5 minutes to 2.5 seconds.
print("Compiling model. This first run will be slow but all subsequent runs will be significantly faster.")
cmd = f"xcrun coremlcompiler compile {mlpackage_path} ."
compile_result = subprocess.run(cmd, shell=True)
has_compiled_model = compile_result.returncode == 0
if not has_compiled_model:
print("Failed to compile. Please open an issue (https://github.com/smpanaro/more-ane-transformers/issues) and include the following:")
print(f"code: {compile_result.returncode}\nstdout: {compile_result.stdout}\nstderr: {compile_result.stderr}")
print("Predicting using the (slow) mlpackage method.")
if has_compiled_model and not os.path.exists(mlpackage_path):
# TODO: Dump metadata to disk instead so you can keep just the compiled model.
print(f"No matching mlpackage found for {mlmodelc_path}. Can't predict without that.")
print(f"It should be at: {mlpackage_path}")
sys.exit(1)
# nano = NanoGPT.from_pretrained("gpt2").eval()
print(f"Loading model from path {mlmodelc_path if has_compiled_model else mlpackage_path} using {compute_unit}...")
load_stopwatch = Stopwatch(3)
model, model_with_metadata = None, None
if has_compiled_model:
model = MLModelProxy(mlmodelc_path, compute_unit)
# So we can inspect and see what the inputs are.
model_with_metadata = ct.models.model.MLModel(mlpackage_path, compute_units=compute_unit, skip_model_load=True)
else:
model = ct.models.model.MLModel(mlpackage_path, compute_units=compute_unit)
model_with_metadata = model
load_stopwatch.stop()
print(f"Loaded model in {load_stopwatch}.")
# print(model)
def sample(logits, temperature=0.85, top_k=80):
if isinstance(logits, np.ndarray):
logits = torch.from_numpy(logits)
# pluck the logits at the final step and scale by desired temperature
logits = logits[:, -1, :] / temperature
# optionally crop the logits to only the top k options
if top_k is not None:
v, _ = torch.topk(logits, min(top_k, logits.size(-1)))
logits[logits < v[:, [-1]]] = -float('Inf')
probs = torch.nn.functional.softmax(logits, dim=-1)
return torch.multinomial(probs.squeeze(), num_samples=1)
text = args.input_prompt
inputs = tok(text, return_tensors="pt")
vprint("Tokenized initial inputs:", inputs["input_ids"].shape)
ane_inputs = AneGPT.
Complete the code above, taking into account the cross-file context provided. Your solution must work correctly with the related files in the project.
Place your complete solution in /app/solution.py