Skip to main content

A tool for loading and computing on parts of LLM models.

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

LLM Layer Collector

PyPI - Version

A practical Python package for working with Huggingface models at the layer level. Designed to help developers and researchers load specific model components when working with large, sharded checkpoints.

What It Does

  • Easily load layers, embedding, head, and norm and run partial computation of language models.
  • Uses Huggingface file format to find the appropriate parts of the model.
  • Uses the transformers and pytorch libraries to load data and run computations.
  • Useful for research, development, and memory-constrained environments

Getting Started

Installation

pip install llm-layer-collector

Essential Components

The LlmLayerCollector class serves as your central interface to the package's functionality.

Required Parameters:

  • model_dir: Path to your model directory containing shards and configuration
  • cache_file: Location for storing shard metadata

Optional Parameters:

  • shard_pattern: Custom regex for matching shard files
  • layer_prefix: Prefix for identifying decoder layers (default: "model.layers.")
  • input_embedding_layer_name: Name for the embedding layer (default: 'model.embed_tokens.weight')
  • norm_layer_name: Name for the norm weight (default: 'momdel.norm.weight')
  • lm_head_name: Name for the head weight (default: 'lm_head.weight')
  • device: Target device for tensor operations ("cpu" or "cuda") (default: "cpu")
  • dtype: Desired numerical precision (default: torch.float16)

Example

This example uses all of the parts of the package to generate a token prediction

from llm_layer_collector import LlmLayerCollector
from llm_layer_collector.compute import compute_embedding, compute_layer, compute_head
from transformers import AutoTokenizer
import torch

# Initialize core components
collector = LlmLayerCollector(
    model_dir="/path/to/model",
    cache_file="cache.json",
    device="cuda",
    dtype=torch.float16
)

# Set up tokenization
tokenizer = AutoTokenizer.from_pretrained("/path/to/model")
input_text = "The quick brown fox"
input_ids = tokenizer(input_text, return_tensors='pt')['input_ids']

# Load model components
embedding = collector.load_input_embedding()
norm = collector.load_norm()
head = collector.load_head()
layers = collector.load_layer_set(0, collector.num_layers - 1)

# Execute forward pass
state = compute_embedding(embedding, input_ids, collector.config)
for layer in layers:
    state.state = compute_layer(layer, state)

# Generate predictions
predictions = compute_head(head, norm(state.state), topk=1)

Computation Pipeline

Our helper functions provide a streamlined approach to model operations:

  • compute_embedding: Handles input embedding and causal mask setup
  • compute_layer: Manages state transitions through decoder layers
  • compute_head: Processes final linear projections and token prediction

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

llm_layer_collector-0.1.8.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

llm_layer_collector-0.1.8-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file llm_layer_collector-0.1.8.tar.gz.

File metadata

  • Download URL: llm_layer_collector-0.1.8.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for llm_layer_collector-0.1.8.tar.gz
Algorithm Hash digest
SHA256 4c8bb71010bf9cc9d4f7de2dc401af625cb0654d2d8412806ba265919ec103d7
MD5 e8db66b3a8b42b3f4db62126ec37ff31
BLAKE2b-256 259c4690da096d2eabee9ec053c23c8eda764bef2930b9a97528042d735f1221

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_layer_collector-0.1.8.tar.gz:

Publisher: publish.yml on erinclemmer/llm-layer-collector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llm_layer_collector-0.1.8-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_layer_collector-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 d1575583a689bcf3c12fe2dcfabce821d0da3ca9a0fe3f923500c9a757219b98
MD5 98a67fc0dee8d5f543c666dc08579e9a
BLAKE2b-256 f1e653bb4a88309d43a2b860b3eb86cbaf0cf024e75e9f41dc8fb91cde521b80

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_layer_collector-0.1.8-py3-none-any.whl:

Publisher: publish.yml on erinclemmer/llm-layer-collector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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