Text Generation
Transformers
Safetensors
fixed-width-addition
arithmetic
interpretability
arxiv:2405.14813
custom_code
Instructions to use melephant/1-layer-addition with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use melephant/1-layer-addition with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="melephant/1-layer-addition", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("melephant/1-layer-addition", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use melephant/1-layer-addition with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "melephant/1-layer-addition" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "melephant/1-layer-addition", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/melephant/1-layer-addition
- SGLang
How to use melephant/1-layer-addition with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "melephant/1-layer-addition" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "melephant/1-layer-addition", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "melephant/1-layer-addition" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "melephant/1-layer-addition", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use melephant/1-layer-addition with Docker Model Runner:
docker model run hf.co/melephant/1-layer-addition
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| import torch | |
| import torch.nn.functional as F | |
| from torch import nn | |
| class AttentionOutput: | |
| values: torch.Tensor | |
| pattern: torch.Tensor | None = None | |
| class CausalSelfAttention(nn.Module): | |
| def __init__(self, d_model: int, n_heads: int, bias: bool = False) -> None: | |
| super().__init__() | |
| if d_model % n_heads != 0: | |
| raise ValueError("d_model must be divisible by n_heads.") | |
| self.d_model = d_model | |
| self.n_heads = n_heads | |
| self.d_head = d_model // n_heads | |
| self.W_Q = nn.Linear(d_model, d_model, bias=bias) | |
| self.W_K = nn.Linear(d_model, d_model, bias=bias) | |
| self.W_V = nn.Linear(d_model, d_model, bias=bias) | |
| self.W_O = nn.Linear(d_model, d_model, bias=bias) | |
| self.register_buffer("_causal_mask", torch.empty(0, 0, dtype=torch.bool), persistent=False) | |
| def forward(self, x: torch.Tensor, return_pattern: bool = False) -> AttentionOutput: | |
| batch, seq_len, _ = x.shape | |
| q = self._split_heads(self.W_Q(x)) | |
| k = self._split_heads(self.W_K(x)) | |
| v = self._split_heads(self.W_V(x)) | |
| scores = torch.matmul(q, k.transpose(-1, -2)) / self.d_head | |
| if self._causal_mask.shape[0] < seq_len or self._causal_mask.device != x.device: | |
| self._causal_mask = torch.triu( | |
| torch.ones(seq_len, seq_len, dtype=torch.bool, device=x.device), | |
| diagonal=1, | |
| ) | |
| causal_mask = self._causal_mask[:seq_len, :seq_len] | |
| scores = scores.masked_fill(causal_mask, float("-inf")) | |
| pattern = F.softmax(scores, dim=-1) | |
| attended = torch.matmul(pattern, v) / 3.0 | |
| values = self.W_O(self._merge_heads(attended, batch, seq_len)) | |
| return AttentionOutput(values=values, pattern=pattern if return_pattern else None) | |
| def _split_heads(self, x: torch.Tensor) -> torch.Tensor: | |
| batch, seq_len, _ = x.shape | |
| return x.view(batch, seq_len, self.n_heads, self.d_head).transpose(1, 2) | |
| def _merge_heads(self, x: torch.Tensor, batch: int, seq_len: int) -> torch.Tensor: | |
| return x.transpose(1, 2).contiguous().view(batch, seq_len, self.d_model) | |