No project description provided
Project description
SmolHub
A lightweight package for fine-tuning language models using LoRA (Low-Rank Adaptation).
Installation
pip install smolhub
Usage
import torch
import smolhub
# from smolhub.helper.dataset.load_config import Config
from smolhub.scripts.finetune import SFTTrainer
from transformers import AutoTokenizer, AutoModelForCausalLM
from smolhub.helper.scheduler import CustomLRScheduler
from smolhub.scripts.lora import LoRAModel
from smolhub.helper.dataset.dataset_main import PreprocessDataset
from load_config import Config #Needs to be created
model_id = "openai-community/gpt2"
config = Config().get_config()
dataset_path = config["Dataset"]["dataset_path"]
tokenizer = AutoTokenizer.from_pretrained(model_id, token=config['huggingface']['hf_token'])
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", token=config['huggingface']['hf_token'])
if tokenizer.pad_token is None:
# Set the pad token to the eos token if it doesn't exist
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
# tokenizer.pad_token = tokenizer.eos_token
print("Setting pad token as PAD token ")
model.resize_token_embeddings(len(tokenizer))
lora_model = LoRAModel(model)
optimizer = torch.optim.Adam(lora_model.parameters(), lr=2e-3)
scheduler = CustomLRScheduler(optimizer, warmup_iters=100, lr_decay_iters=2000, min_lr=2e-5, max_lr=2e-3, _type="cosine")
#Loading the dataset
preprocess_dataset = PreprocessDataset(dataset_path=dataset_path, tokenizer=tokenizer)
train_dataloader, val_dataloader, test_dataloader = preprocess_dataset.prepare_dataset()
#Initialize the Trainer
sft_trainer = SFTTrainer(lora_model, train_dataloader, val_dataloader, test_dataloader, optimizer, None, scheduler)
#Train
sft_trainer.train()
Config File
import yaml
class Config:
def __init__(self, config_path='/mnt/c/Users/yuvra/OneDrive/Desktop/Work/pytorch/SmolHub/tests/config.yaml'):
self.config_path = config_path
self.config = self.load_config()
def load_config(self):
with open(self.config_path, "r") as f:
config = yaml.safe_load(f)
return config
def get_config(self):
return self.config
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
smolhub-0.5.3.tar.gz
(10.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
smolhub-0.5.3-py3-none-any.whl
(12.4 kB
view details)
File details
Details for the file smolhub-0.5.3.tar.gz.
File metadata
- Download URL: smolhub-0.5.3.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3e5d65331a5b3b25c713c0ef13fa4ce884416f8b846a0383ce6ff9a779b4bebc
|
|
| MD5 |
dc83bb3dbbca00bef4090c4edc9e3a8e
|
|
| BLAKE2b-256 |
f1745305ea7e7e63e9d12e030818e699a27255258c830429e74441dcefeda557
|
File details
Details for the file smolhub-0.5.3-py3-none-any.whl.
File metadata
- Download URL: smolhub-0.5.3-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bb44fff2ff99c966a167801d27900f4aa4f44c34767bae58dc60bcb9771bb4e4
|
|
| MD5 |
f5f2228fa919ac7202c8881fe3b28735
|
|
| BLAKE2b-256 |
1464f11650446f649c44e326cf12dc89fe0f91390048b6cd07408cb88b42f3a4
|