Byte Latent Transformer (BLT)

repository·main·Indexed 24 days ago

https://github.com/facebookresearch/blt

A byte-level LLM architecture that utilizes dynamically sized patches instead of traditional tokens to improve inference efficiency and robustness. The library provides tools for loading models via HuggingFace, preparing training data, and executing training jobs using torchrun or SLURM.

Tokens
2.4K
Snippets
10
Records
11
Agent score
35%

What's inside blt

  1. Install BLT via Slurm Job

    main

    If you have access to a SLURM cluster, you can use the provided setup script to build the environment.

    git clone https://github.com/facebookresearch/blt
    cd blt
    
    bash setup/create_env.sh
    # or if you have access to a SLURM cluster
    sbatch setup/create_env.sh

    After completion, activate the environment using the generated name: conda activate blt_<date>

  2. Install BLT using uv (Experimental/Reproducible)

    main

    For a reproducible build using a lock file, use uv. This method installs pre-build groups and compiles xformers before syncing the environment.

    uv pip install --group pre_build --no-build-isolation
    uv pip install --group compile_xformers --no-build-isolation
    uv sync
    uv run python download_blt_weights.py
    uv run python demo.py "A BLT has"
  3. Download and prepare HuggingFace training data

    main

    Use setup/download_prepare_hf_data.py to download and prepare datasets like fineweb_edu, fineweb_edu_10bt, or dclm_baseline_1.0.

    Arguments:

    • <dataset_name>: e.g., fineweb_edu
    • <MEMORY>: Memory allocation for terashuf.
    • --data_dir: Directory to save data (default ./data).
    • --seed: Random seed.
    • --nchunks: Number of chunks. If running on fewer than 32 GPUs, it is recommended to set this to 1 or match the number of GPUs (nchunks = NGPUs).
    python setup/download_prepare_hf_data.py fineweb_edu <MEMORY> --data_dir ./data --seed 42 --nchunks <NCHUNKS>
  4. Run BLT training and debug jobs

    main

    Training can be launched using stool (for SLURM), torchrun, or standard Python. Note that configurations (like dump_dir, data.root_dir, data.tokenizer.path) must be adapted in your .yaml config files.

    Using stool (SLURM):

    python -m bytelatent.stool script=bytelatent.train config=bytelatent/configs/debug.yaml nodes=1 partition=<partition>

    Using torchrun (Local multi-GPU):

    torchrun --nproc-per-node 8 -m bytelatent.train config=bytelatent/configs/debug.yaml

    Using standard Python (Single GPU):

    python -m bytelatent.train  config=bytelatent/configs/debug.yaml
    # stool stands for SLURM tool !
    python -m bytelatent.stool script=bytelatent.train config=bytelatent/configs/debug.yaml nodes=1 partition=<partition>
    
    # or if you want to launch locally you can use torchrun
    torchrun --nproc-per-node 8 -m bytelatent.train config=bytelatent/configs/debug.yaml
    
    # or you can also launch on 1 GPU
    python -m bytelatent.train  config=bytelatent/configs/debug.yaml
  5. Install BLT using Conda and Pip

    main

    To set up a local development environment using Conda, clone the repository and follow these steps to create a Python 3.12 environment with the necessary dependencies, including nightly PyTorch and a specific version of xformers.

    git clone https://github.com/facebookresearch/blt
    cd blt
    conda create -n blt python=3.12
    conda activate blt
    pip install --pre torch --index-url https://download.pytorch.org/whl/nightly/cu121
    pip install ninja
    pip install -v -U git+https://github.com/facebookresearch/xformers.git@de742ec3d64bd83b1184cc043e541f15d270c148
    pip install -r requirements.txt
  6. Understand the TrainState lifecycle

    main

    The TrainState class (implementing Stateful) tracks the progress of the training loop and is used for checkpointing. It ensures that training can be resumed from the exact point of interruption.

    Key fields:

    • step: The current number of optimizer steps taken.
    • acc_step: The current number of accumulation steps completed since the last optimizer step.
    • scheduler: The current state of the learning rate scheduler.
    • data_loader_state: The state of the data loader (e.g., PackTokensState) to ensure data continuity upon restart.
    @dataclass
    class TrainState(Stateful):
        step: int  # Nb of steps taken by the optimizer
        acc_step: int  # Nb of accumulation steps done since last optimizer step
        scheduler: lr_scheduler.LambdaLR
        data_loader_state: PackTokensState
  7. Run training via CLI with OmegaConf

    main

    The training script uses OmegaConf to allow hierarchical configuration via the command line. You can provide a base configuration file and override specific nested attributes using dot notation.

    Configuration Merging Order:

    1. Default values in TrainArgs dataclass.
    2. Values from the provided configuration file (--config <path>).
    3. Values provided via command line arguments.

    Example Usage: If your config file defines model.dim: 128, you can override it to 64 by passing model.dim=64 as a CLI argument.

  8. Load BLT weights via HF Hub in Python

    main

    To use BLT models in your own code, you can load the LMTransformer, ByteLatentTransformer, and BltTokenizerAndPatcher from their respective HuggingFace repositories. Note that you must first request access to the weights on HuggingFace and log in via huggingface-cli login.

    from bytelatent.transformer import LMTransformer
    from bytelatent.model.blt import ByteLatentTransformer
    from bytelatent.hf import BltTokenizerAndPatcher
    
    entropy_repo = "facebook/blt-entropy"
    blt_repo = "facebook/blt-1b"
    
    # Load components
    entropy_model = LMTransformer.from_pretrained(entropy_repo)
    blt_model = ByteLatentTransformer.from_pretrained(blt_repo)
    tok_and_patcher = BltTokenizerAndPatcher.from_pretrained(blt_repo)
    
    # Build tokenizer and patcher
    tokenizer = tok_and_patcher.tokenizer_args.build()
    patcher = tok_and_patcher.patcher_args.build()
    from bytelatent.transformer import LMTransformer
    from bytelatent.model.blt import ByteLatentTransformer
    from bytelatent.hf import BltTokenizerAndPatcher
    
    entropy_repo = "facebook/blt-entropy"
    blt_repo = "facebook/blt-1b"
    entropy_model = LMTransformer.from_pretrained(entropy_repo)
    blt_model = ByteLatentTransformer.from_pretrained(blt_repo)
    tok_and_patcher = BltTokenizerAndPatcher.from_pretrained(blt_repo)
    tokenizer = tok_and_patcher.tokenizer_args.build()
    patcher = tok_and_patcher.patcher_args.build()
  9. Configure training via TrainArgs

    main

    The lingua_train.py entrypoint uses a TrainArgs configuration object (based on Pydantic) to define the training setup. This object aggregates several sub-configurations:

    • name: Name of the training run.
    • dump_dir: Directory where logs, configs, and checkpoints are saved.
    • grad_acc_steps: Number of gradient accumulation steps. Total batch size is batch_size * grad_acc_steps.
    • steps: Total number of optimizer steps to take.
    • data: DataArgs for dataset configuration.
    • optim: OptimArgs for optimizer settings.
    • model: LMTransformerArgs for model architecture.
    • distributed: DistributedArgs for parallelism settings.
    • env: EnvironmentArgs for runtime environment.
    • checkpoint: CheckpointArgs for saving/loading.
    • profiling: ProfilerArgs for performance profiling.
    • logging: LoggingArgs for metrics and WandB.
    • async_eval_gpus: If set, launches evaluation on a separate set of GPUs instead of running locally.
    • eval: Configuration for evaluation runs.
    class TrainArgs(BaseModel):
        name: str = "lingua"
        dump_dir: str = ""
        grad_acc_steps: int = 1
        steps: int = 1000
        data: DataArgs
        optim: OptimArgs
        model: LMTransformerArgs
        distributed: DistributedArgs
        env: EnvironmentArgs
        checkpoint: CheckpointArgs
        profiling: ProfilerArgs
        logging: LoggingArgs
        async_eval_gpus: int | None = None
        eval: Any | None = None
  10. Load BLT weights via CLI

    main

    You can load weights directly from the HuggingFace hub using the bytelatent.hf module via the command line.

    python -m bytelatent.hf load-transformers --entropy-repo facebook/blt-entropy --blt-repo facebook/blt-1b --prompt "My test prompt" hub