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 | |
| from torch import nn | |
| from .attention import CausalSelfAttention | |
| from .bilinear_mlp import BilinearMLP | |
| from .model_config import AdditionModelConfig | |
| class TransformerBlockOutput: | |
| residual_pre_attention: torch.Tensor | |
| attention_out: torch.Tensor | |
| attention_pattern: torch.Tensor | None | |
| residual_after_attention: torch.Tensor | |
| mlp_out: torch.Tensor | |
| residual_after_mlp: torch.Tensor | |
| class TransformerBlock(nn.Module): | |
| def __init__(self, config: AdditionModelConfig) -> None: | |
| super().__init__() | |
| self.residual_alpha = config.residual_alpha | |
| self.attention = CausalSelfAttention(config.d_model, config.n_heads, bias=False) | |
| self.mlp = BilinearMLP(config.d_model, config.d_mlp, bias=False) | |
| def forward(self, x: torch.Tensor, return_pattern: bool = False) -> TransformerBlockOutput: | |
| attention_output = self.attention(x, return_pattern=return_pattern) | |
| residual_after_attention = torch.lerp(x, attention_output.values, self.residual_alpha) | |
| mlp_out = self.mlp(residual_after_attention) | |
| residual_after_mlp = torch.lerp(residual_after_attention, mlp_out, self.residual_alpha) | |
| return TransformerBlockOutput( | |
| residual_pre_attention=x, | |
| attention_out=attention_output.values, | |
| attention_pattern=attention_output.pattern, | |
| residual_after_attention=residual_after_attention, | |
| mlp_out=mlp_out, | |
| residual_after_mlp=residual_after_mlp, | |
| ) | |