Skip to main content

Super Lazy Language Model

Project description

Alt text

LicenseLinkedIn Twitter

Author: Henry Ndubuaku

Overview

I mean, do not train or fine-tune LLMs on your laptop, traing is done at much higher precision than inference (float32 or bfload16). Also, additional memory is often used for the gradients, optimizer states, and batch size. So, 4 - 6x the model size. For simplicity, around 8-24G of RAM per 1B params.

HOWEVER, if you must do so on a laptop for whatever weird reason, this library implements most language models such that only the weights for each layer is loaded to the RAM, it implements LoRA fine-tuning such that frozen params are memory-mapped rather than loaded.

Note the following:

  1. Compute intensity = computation time / communication time, and maximisin this means maximising GPU utilisation.
  2. Many computations in transformer models can be parallelised, QKV projections for example.
  3. Most operations in transformers follow the signature A @ B * Scale, A.K.A scaled dot-product.
  4. Q @ K.T / sqrt(dimK) is obiously equivalent to Q @ K.T * dimK^(-1/2)
  5. But Lora_A @ Lora_B = Lora_A @ Lora_B * 1, also A * B = I @ A * B, and so on.

We expressed the transformer forward pass and the backward vector-jacobian products for each layer as a bunch of scaled matmuls, which are bundled together and executed in parallel across different CPU cores as C++ extensions to bypass GIL. This concept makes it easy for an upcoming feature, where each bundle could be distributed across your friends' laptops, such that they only execute one operation called Bundled Scaled Matmul. You're welcome.

Limitations

  1. Gradient accumulation, gradient checkpointing and lazy execution trade time-complexity for memory-efficiency, but you have no choice, do you?
  2. Yeah...your laptop will definitley heat up, GPUs burn up at data centers and cost so much to cool, your laptop is not special.

Supported Models

  • deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
  • Qwen/Qwen2.5-0.5B
  • Qwen/Qwen2.5-0.5B-Instruct
  • Qwen/Qwen2.5-1.5B
  • Qwen/Qwen2.5-1.5B-Instruct
  • Qwen/Qwen2.5-3B
  • Qwen/Qwen2.5-3B-Instruct

Getting Started

  1. pip install sllm-lib
    
  2. Initialize the model:
    from sllm.nn import SuperLazyLanguageModel
    from sllm.config import Config
    
    config = Config(
        model_name="deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B",
        lora_alpha=32,
        lora_r=8,
        lora_dropout=0.1,
    )
    
    model = SuperLazyLanguageModel(config)
    
    # Train like a normal pytorch model
    
  3. You can use SLLM functionalities:
    import torch
    from datasets import load_dataset
    
    from sllm.nn import SuperLazyLanguageModel
    from sllm.train import sft, prepare_dataset
    
    torch.manual_seed(42)
    
    name = "Qwen/Qwen2-0.5B-Instruct"
    dataset = load_dataset("yahma/alpaca-cleaned", split="train[:200]")
    
    dataset = prepare_dataset(
       model_name=name, 
       instructions=dataset["instruction"], 
       responses=dataset["output"], 
       inputs=dataset["input"],
       max_seq_len=256,
    )
    
    model = SuperLazyLanguageModel(
       name=name, 
       lora_alpha=32, 
       lora_r=8, 
       lora_dropout=0.1,
    )
    
    optimizer = torch.optim.AdamW(model.parameters(), lr=1e-5)
    sft(model=model, dataset=dataset, optimizer=optimizer, batch_size=8, epochs=3)
    

Contributing

Whether you’re improving documentation, optimizing kernels, or adding new features, your help is invaluable.

  1. Create a feature branch (git checkout -b feature/awesome-improvement).
  2. Commit your changes (git commit -m 'Add awesome feature').
  3. Push to the branch (git push origin feature/awesome-improvement).
  4. Open a Pull Request.

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

sllm_lib-0.0.1.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

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

sllm_lib-0.0.1-cp311-cp311-macosx_10_9_universal2.whl (147.8 kB view details)

Uploaded CPython 3.11macOS 10.9+ universal2 (ARM64, x86-64)

File details

Details for the file sllm_lib-0.0.1.tar.gz.

File metadata

  • Download URL: sllm_lib-0.0.1.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for sllm_lib-0.0.1.tar.gz
Algorithm Hash digest
SHA256 cab3320c0d01708776e611d9f703459d614ff6c8d5bf90aeffd69ff978568c53
MD5 66a50db6cf3a8e234dac14e38498198c
BLAKE2b-256 e7f4e573e52f2e28fc935633b1b7fca073a9c81bac07ac5c7a103bedf52e38ec

See more details on using hashes here.

File details

Details for the file sllm_lib-0.0.1-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for sllm_lib-0.0.1-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 a5dc84dee30d38a211c37519ec5e5a044fde99d237d7c9eba49eb59a3ea0fc1c
MD5 358ef43ce0a911a60d6cde11f9049c66
BLAKE2b-256 4770b8552fd2e4497e1355c9bfc9f1563c11e181a3c66b266dfbb1601e6e4e2d

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