Skip to main content

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

A default config file is created in the user project directory if not already there

Example Config

project:
  name: SFTrainer
  author: Yuvraj Singh
  version: 1.0

LoRA:
  rank: 4
  alpha: 8
Dataset:
    use_hf_dataset: True
    dataset_path: stanfordnlp/imdb
    max_length: 512
    batch_size:  16
    num_workers:  4
    shuffle:  True
    drop_last: True
    pin_memory:  True
    persistent_workers:  True
    type: "classification" #TODO Add Chat style and Instruction 
   
huggingface:
  hf_token: "..."

Model:
  epochs: 1
  eval_iters: 10

MAP:
  use_bfloat16:  False
  use_float16: False

Optimizations:
  use_compile: False


wandb:
  project_name: "SFTrainer"
  
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()

Project details


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.6.4.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smolhub-0.6.4-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file smolhub-0.6.4.tar.gz.

File metadata

  • Download URL: smolhub-0.6.4.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for smolhub-0.6.4.tar.gz
Algorithm Hash digest
SHA256 c024217767d5920ab1d86133a38fa1196b4f575b351434961ae12806fee404ca
MD5 487969bd61a5f8aaad2ea2aa7506fd73
BLAKE2b-256 11cffb43ea01a3f6e677db3d753a47598b1da267ecb7e8116a4eaca9a5900d3d

See more details on using hashes here.

File details

Details for the file smolhub-0.6.4-py3-none-any.whl.

File metadata

  • Download URL: smolhub-0.6.4-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.11

File hashes

Hashes for smolhub-0.6.4-py3-none-any.whl
Algorithm Hash digest
SHA256 acf90288403dc1c4970c31bb8d9b0cfb707f010cdfcd6051294a49129779c9d8
MD5 06c3b1424e0e15cfeb42ef1a160b152f
BLAKE2b-256 2ccb498b77fae52d8cad21b0d5b046c6d0d94b9e2de5b2540be1346bfe1fa3b1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page