Instructions to use Lanni-ni/alibi_2_4_256_pile_seed44 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Lanni-ni/alibi_2_4_256_pile_seed44 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Lanni-ni/alibi_2_4_256_pile_seed44", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("Lanni-ni/alibi_2_4_256_pile_seed44", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Lanni-ni/alibi_2_4_256_pile_seed44 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Lanni-ni/alibi_2_4_256_pile_seed44" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Lanni-ni/alibi_2_4_256_pile_seed44", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Lanni-ni/alibi_2_4_256_pile_seed44
- SGLang
How to use Lanni-ni/alibi_2_4_256_pile_seed44 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 "Lanni-ni/alibi_2_4_256_pile_seed44" \ --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": "Lanni-ni/alibi_2_4_256_pile_seed44", "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 "Lanni-ni/alibi_2_4_256_pile_seed44" \ --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": "Lanni-ni/alibi_2_4_256_pile_seed44", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Lanni-ni/alibi_2_4_256_pile_seed44 with Docker Model Runner:
docker model run hf.co/Lanni-ni/alibi_2_4_256_pile_seed44
Download ops/sliding_window_attention_std.py from Lanni-ni/alibi_2_4_256_pile_seed44: direct link, hf CLI and curl.
- Browser
- Download file 2.39 kB
-
https://huggingface.co/Lanni-ni/alibi_2_4_256_pile_seed44/resolve/main/ops/sliding_window_attention_std.py
- Command line
-
hf download hf://Lanni-ni/alibi_2_4_256_pile_seed44/ops/sliding_window_attention_std.py
-
curl -L -o sliding_window_attention_std.py https://huggingface.co/Lanni-ni/alibi_2_4_256_pile_seed44/resolve/main/ops/sliding_window_attention_std.py
2.39 kB
| """ | |
| Sliding Window / Hard Attention | |
| Based on "Context Limitations Make Neural Language Models More Human-Like" | |
| (Kuribayashi et al., 2022) | |
| """ | |
| import math | |
| import torch | |
| import torch.nn.functional as F | |
| from einops import rearrange | |
| from typing import Optional | |
| def sliding_window_attention_std( | |
| q: torch.Tensor, | |
| k: torch.Tensor, | |
| v: torch.Tensor, | |
| *, | |
| head_first: bool = False, | |
| seq_start: Optional[torch.Tensor] = None, | |
| sm_scale: Optional[float] = None, | |
| window_size: int = 2, # 默认2-gram(看前1个token) | |
| ) -> torch.Tensor: | |
| """ | |
| Sliding Window Attention | |
| 硬截断:只能attend到最近window_size个token | |
| """ | |
| if not head_first: | |
| q = rearrange(q, "b t h d -> b h t d") | |
| k = rearrange(k, "b t h d -> b h t d") | |
| v = rearrange(v, "b t h d -> b h t d") | |
| B, H, T_q, D = q.shape | |
| T_k = k.shape[2] | |
| if sm_scale is None: | |
| sm_scale = 1.0 / math.sqrt(D) | |
| # Compute logits | |
| logits = torch.matmul(q.float(), k.float().transpose(-2, -1)) * sm_scale | |
| # Create sliding window mask | |
| mask = create_sliding_window_mask(T_q, T_k, window_size, device=q.device) | |
| logits = logits.masked_fill(~mask, float('-inf')) | |
| # Seq start mask | |
| if seq_start is not None: | |
| seq_mask = torch.arange(T_k, device=q.device)[None, None, None, :] < seq_start[None, :, None, None] | |
| logits = logits.masked_fill(seq_mask, float('-inf')) | |
| # Standard softmax | |
| weights = F.softmax(logits, dim=-1) | |
| # Apply to values | |
| out = torch.matmul(weights, v) | |
| if not head_first: | |
| out = rearrange(out, "b h t d -> b t h d") | |
| return out | |
| def create_sliding_window_mask( | |
| T_q: int, | |
| T_k: int, | |
| window_size: int, | |
| device: torch.device | |
| ) -> torch.Tensor: | |
| """ | |
| 创建sliding window mask | |
| window_size=1: 只看前1个token (2-gram) | |
| window_size=2: 只看前2个token (3-gram) | |
| """ | |
| # 基础causal mask | |
| mask = torch.tril(torch.ones(T_q, T_k, dtype=torch.bool, device=device)) | |
| # 应用window限制 | |
| if window_size > 0 and window_size < T_k: | |
| for i in range(T_q): | |
| # 只保留 [i-window_size+1, i] 范围 | |
| start = max(0, i - window_size + 1) | |
| if start > 0: | |
| mask[i, :start] = False | |
| return mask[None, None, :, :] # [1, 1, T_q, T_k] |