import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TextStreamer
device = "cuda" if torch.cuda.is_available() else "cpu"
model_id = "DireDreadlord/Dragon-1-0.5B"
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
dtype="auto"
)
model.to(device)
tokenizer = AutoTokenizer.from_pretrained(model_id)
streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=False)
prompt = "Can you reason about the following leetcode question: Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order. Please provide a detailed reasoning and explanation for your answer."
input_ids = tokenizer.apply_chat_template(
[{"role": "user", "content": prompt}],
add_generation_prompt=True,
return_tensors="pt",
tokenize=True,
)["input_ids"].to(device)
output = model.generate(
input_ids,
do_sample=True,
temperature=0.4,
top_k=50,
repetition_penalty=1.05,
max_new_tokens=2048,
streamer=streamer,
)