GuppyLM Documentation

repository·main·Indexed 25 days ago

https://github.com/arman-bd/guppylm

GuppyLM is a tiny (~9M parameter) language model designed to simulate the personality of a small fish. Serving as an educational tool, it demonstrates the full pipeline of training a functional LLM from scratch, including data generation, tokenizer training, and architecture implementation. The project includes a vanilla transformer architecture, the GuppyLM-60k-generic synthetic dataset, and tools for local inference, browser-based execution via WebAssembly, and training via Google Colab.

Tokens
2.6K
Snippets
11
Records
19
Agent score
83%

What's inside GuppyLM

  1. Load the GuppyLM Chat Dataset

    main

    You can load the GuppyLM Chat training dataset (60K single-turn conversations) using the Hugging Face datasets library. The dataset contains conversations between a human and Guppy, a small fish character that speaks in short, lowercase sentences about its aquatic environment.

    from datasets import load_dataset
    ds = load_dataset("arman-bd/guppylm-60k-generic")
    print(ds["train"][0])
    # {'input': 'hi guppy', 'output': 'hello. the water is nice today.', 'category': 'greeting'}
  2. Chat with GuppyLM locally

    main

    To run GuppyLM in an interactive chat mode on your local machine, install the required dependencies and use the guppylm module. Note that in interactive mode, the conversation grows until it hits the 128-token limit, which may reduce output quality.

    pip install torch tokenizers
    python -m guppylm chat
  3. Train GuppyLM in Google Colab

    main

    You can train the model from scratch using a Google Colab notebook.

    1. Open the training notebook.
    2. Set the runtime to T4 GPU.
    3. Run all cells to download the dataset, train the tokenizer, train the model, and run tests.
    4. Once complete, you can upload the resulting model to HuggingFace or download it locally.
  4. Install dependencies and download GuppyLM weights

    main

    To use GuppyLM, you need to install torch, tokenizers, and huggingface_hub. You must also download the model weights from Hugging Face (specifically the arman-bd/guppylm-9M repository) to your local directory.

    # Setup + Download
    !pip install -q torch tokenizers huggingface_hub
    import os, shutil
    if os.path.exists('/content/guppy'): shutil.rmtree('/content/guppy')
    os.makedirs('/content/guppy'); os.chdir('/content/guppy')
    
    from huggingface_hub import snapshot_download
    snapshot_download(repo_id='arman-bd/guppylm-9M', local_dir='.')
    print('Model downloaded.')
  5. Export GuppyLM to HuggingFace

    main
    To upload the model to HuggingFace, you can export it in PyTorch (pytorch_model.bin) and quantized ONNX (model.onnx) formats. The export script automatically generates a config.json compatible with standard HuggingFace keys (e.g., mapping d_model to hidden_size).
  6. Train GuppyLM model

    main

    Training is performed using the train() function from train.py. The process involves downloading the fish conversation dataset, training a BPE tokenizer, and running the training loop with a cosine learning rate schedule. The training loop supports mixed-precision training (AMP) on CUDA devices.

    from train import train
    train()
  7. Use GuppyInference for chat completions

    main

    To interact with the GuppyLM model, use the GuppyInference class. You must provide the path to the model checkpoint (.pt file) and the tokenizer configuration (tokenizer.json). The chat_completion method accepts a list of message dictionaries following the standard {'role': '...', 'content': '...'} format and returns a response object containing the model's generated text.

    from inference import GuppyInference
    
    engine = GuppyInference('checkpoints/best_model.pt', 'data/tokenizer.json')
    r = engine.chat_completion([{'role': 'user', 'content': 'hi guppy'}])
    print(r['choices'][0]['message']['content'])
    # hi there. i just found a nice spot near the rock.
  8. Configure GuppyLM hyperparameters

    main

    GuppyLM uses two main configuration classes defined in config.py: GuppyConfig for model architecture and TrainConfig for training hyperparameters.

    GuppyConfig

    • vocab_size: Vocabulary size (default: 4096)
    • max_seq_len: Maximum sequence length (default: 128)
    • d_model: Model dimension (default: 384)
    • n_layers: Number of transformer layers (default: 6)
    • n_heads: Number of attention heads (default: 6)
    • ffn_hidden: Hidden dimension of the FFN (default: 768)
    • dropout: Dropout probability (default: 0.1)
    • pad_id: Padding token ID (default: 0)
    • bos_id: Beginning of sequence token ID (default: 1, <|im_start|>)
    • eos_id: End of sequence token ID (default: 2, <|im_end|>)