Use microsoft phi-2

How to use hugging face microsoft phi-2 in google collab
also how to import the token from hugging face to google collab

1 Like

Simply like this.

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

torch.set_default_device("cuda")

model = AutoModelForCausalLM.from_pretrained("microsoft/phi-2", torch_dtype="auto", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-2", trust_remote_code=True)

inputs = tokenizer('''def print_prime(n):
   """
   Print all primes between 1 and n
   """''', return_tensors="pt", return_attention_mask=False)

outputs = model.generate(**inputs, max_length=200)
text = tokenizer.batch_decode(outputs)[0]
print(text)

Using Hugging Face Token on Google Colab

from transformers import AutoTokenizer, Trainer, TrainingArguments,AutoModelForSequenceClassification,AutoModelForCausalLM

from datasets import load_dataset
import torch
from huggingface_hub import login
from peft import LoraConfig, get_peft_model


import os
import time
import gc


# hugging face login

login(token=os.environ.get('huggingfacelogin'))
# model_name='TinyLlama/TinyLlama-1.1B-Chat-v1.0'
model_name='microsoft/phi-1_5'

## Free up memory
gc.collect()

torch.cuda.empty_cache()

os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = ""

#sleep for 6 secs
time.sleep(6)



# Load dataset
train_data = load_dataset('csv', data_files='ivtest-sched-training.csv')





# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name)

if tokenizer.pad_token is None:
    tokenizer.add_special_tokens({'pad_token': '[PAD]'})


# Tokenize data
def tokenize_function(examples):
    text = examples['label']

    return tokenizer(text, padding='max_length', truncation=True,max_length=1024)

tokenized_train_data = train_data.map(tokenize_function, batched=True)
# print(tokenized_train_data)


# Define model
# model = AutoModelForSequenceClassification.from_pretrained(model_name,torch_dtype=torch.bfloat16, device_map={"":"cpu"})
model = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype=torch.bfloat16, device_map={"":"cpu"})



# Define training arguments
training_args = TrainingArguments(
    output_dir='./results',
    run_name='./runs',
    eval_strategy='epoch',
    per_device_train_batch_size=1,
    per_device_eval_batch_size=1,
    num_train_epochs=3,
    weight_decay=0.01,
    fp16=False  # Enable mixed precision training

)
# ✅ Apply LoRA (Low-RAM Training)
lora_config = LoraConfig(
    r=8,
    lora_alpha=16,
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM",
)
model=get_peft_model(model,lora_config)




# Initialize Trainer
trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_train_data['train'],
    eval_dataset=tokenized_train_data['train'],
)

# Train model
trainer.train()


# ✅ Save trained model
model.save_pretrained("./phi1_5-flight-routes-trained")
tokenizer.save_pretrained("./phi1_5-flight-routes-trained")

With this i am getting too many dimensions ‘str’ error .
My csv file contains only one column with data as follows

label
“Flight from AJL to CCU, Departs at 15:00:00, Arrives at 16:10:00 , on flight number 502 .”
“Flight from AJL to GAU, Departs at 11:30:00, Arrives at 12:35:00 , on flight number 459 .”
“Flight from AMD to BBI, Departs at 18:00:00, Arrives at 20:25:00 , on flight number 314 .”
“Flight from AMD to BLR, Departs at 09:05:00, Arrives at 11:00:00 , on flight number 436 .”
“Flight from AMD to BLR, Departs at 11:00:00, Arrives at 13:20:00 , on flight number 6257 .”
“Flight from AMD to BLR, Departs at 12:50:00, Arrives at 14:45:00 , on flight number 6992 .”
“Flight from AMD to BLR, Departs at 20:00:00, Arrives at 22:40:00 , on flight number 157 .”

1 Like

It looks like a PyTorch-related error…
It’s an error that I don’t often see in relation to Trainer.