Downloads · 30 days
4
20% of all-time downloads
aloksingh2130/Bloom-3B-Squad-v2
Bloom-3B-Squad-v2 is a machine learning model from aloksingh2130. Use it for the machine learning task on the model card, and read the license before you ship it in a product. It is set up for peft.
- PEFT 0.6.0.dev0 Using LoRA to fine tune Bloom-3B model on Squad v2 dataset
Downloads · 30 days
4
20% of all-time downloads
All-time downloads
20
Public
Repo size
9.9 MB
Likes
0
Public
Click a slice to open those files.
.bin9.9 MB · 100%
From the Hugging Face model README
How to use: Code below
!pip install -q bitsandbytes datasets accelerate loralib !pip install -q git+https://github.com/huggingface/peft.git git+https://github.com/huggingface/transformers.git
import torch torch.cuda.is_available()
import os os.environ["CUDA_VISIBLE_DEVICES"]="0" import torch import torch.nn as nn import bitsandbytes as bnb from transformers import AutoTokenizer, AutoConfig, AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained( "bigscience/bloom-3b", torch_dtype=torch.float16, device_map='auto', )
tokenizer = AutoTokenizer.from_pretrained("bigscience/tokenizer")
print(model)
for param in model.parameters(): param.requires_grad = False # freeze the model - train adapters later if param.ndim == 1: # cast the small parameters (e.g. layernorm) to fp32 for stability param.data = param.data.to(torch.float32)
model.gradient_checkpointing_enable() # reduce number of stored activations model.enable_input_require_grads()
class CastOutputToFloat(nn.Sequential): def forward(self, x): return super().forward(x).to(torch.float32) model.lm_head = CastOutputToFloat(model.lm_head)
def print_trainable_parameters(model): """ Prints the number of trainable parameters in the model. """ trainable_params = 0 all_param = 0 for _, param in model.named_parameters(): all_param += param.numel() if param.requires_grad: trainable_params += param.numel() print( f"trainable params: {trainable_params} || all params: {all_param} || trainable%: {100 * trainable_params / all_param}" )
from peft import LoraConfig, get_peft_model
config = LoraConfig( r=8, lora_alpha=16, target_modules=["query_key_value"], lora_dropout=0.05, bias="none", task_type="CAUSAL_LM" )
model = get_peft_model(model, config) print_trainable_parameters(model)
from datasets import load_dataset qa_dataset = load_dataset("squad_v2")
def create_prompt(context, question, answer): if len(answer["text"]) < 1: answer = "Cannot Find Answer" else: answer = answer["text"][0] prompt_template = f"### CONTEXT\n{context}\n\n### QUESTION\n{question}\n\n### ANSWER\n{answer}</s>" return prompt_template
mapped_qa_dataset = qa_dataset.map(lambda samples: tokenizer(create_prompt(samples['context'], samples['question'], samples['answers'])))
torch.cuda.empty_cache()
import transformers
trainer = transformers.Trainer( # doc for parameter https://huggingface.co/docs/transformers/main_classes/trainer model=model, train_dataset=mapped_qa_dataset["train"], eval_dataset=mapped_qa_dataset["validation"], args=transformers.TrainingArguments( per_device_train_batch_size=4, gradient_accumulation_steps=4, # evaluation_strategy="steps", # eval_steps=1000, warmup_steps=100, max_steps=100, learning_rate=2e-3, fp16=True, logging_steps=1, output_dir='outputs', ), data_collator=transformers.DataCollatorForLanguageModeling(tokenizer, mlm=False) )
model.config.use_cache = False # silence the warnings. Please re-enable for inference! trainer.train()
HUGGING_FACE_USER_NAME = "aloksingh2130" model_name = "Bloom-3B-Squad-v2" model.push_to_hub(f"{HUGGING_FACE_USER_NAME}/{model_name}", use_auth_token=True)