Text Generation
Transformers
Safetensors
PyTorch
Russian
transformer
fill-mask
chat
russian
easyformer
custom_code
conversational
Eval Results (legacy)
Instructions to use OpenRussianAI/andrey with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use OpenRussianAI/andrey with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="OpenRussianAI/andrey", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelWithLMHead model = AutoModelWithLMHead.from_pretrained("OpenRussianAI/andrey", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use OpenRussianAI/andrey with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "OpenRussianAI/andrey" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenRussianAI/andrey", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/OpenRussianAI/andrey
- SGLang
How to use OpenRussianAI/andrey 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 "OpenRussianAI/andrey" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenRussianAI/andrey", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'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 "OpenRussianAI/andrey" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenRussianAI/andrey", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use OpenRussianAI/andrey with Docker Model Runner:
docker model run hf.co/OpenRussianAI/andrey
Create modeling_easyformer.py
Browse files- modeling_easyformer.py +81 -0
modeling_easyformer.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import math, torch, torch.nn as nn, torch.nn.functional as F
|
| 3 |
+
from transformers import PreTrainedModel, GenerationMixin
|
| 4 |
+
from configuration_easyformer import EasyFormerConfig
|
| 5 |
+
|
| 6 |
+
class RMSNorm(nn.Module):
|
| 7 |
+
def __init__(self, d, eps=1e-5):
|
| 8 |
+
super().__init__()
|
| 9 |
+
self.w = nn.Parameter(torch.ones(d)); self.eps = eps
|
| 10 |
+
def forward(self, x):
|
| 11 |
+
return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps) * self.w
|
| 12 |
+
|
| 13 |
+
class EasyFormerAttention(nn.Module):
|
| 14 |
+
def __init__(self, cfg):
|
| 15 |
+
super().__init__()
|
| 16 |
+
self.qkv = nn.Linear(cfg.d_model, 3 * cfg.d_model, bias=False)
|
| 17 |
+
self.proj = nn.Linear(cfg.d_model, cfg.d_model, bias=False)
|
| 18 |
+
self.drop = nn.Dropout(cfg.dropout)
|
| 19 |
+
self.register_buffer("mask", torch.tril(torch.ones(cfg.ctx, cfg.ctx)).bool())
|
| 20 |
+
def forward(self, x):
|
| 21 |
+
B, T, C = x.shape
|
| 22 |
+
q, k, v = self.qkv(x).chunk(3, dim=-1)
|
| 23 |
+
att = (q @ k.transpose(-2, -1)) / math.sqrt(C)
|
| 24 |
+
att = att.masked_fill(~self.mask[:T, :T], float("-inf"))
|
| 25 |
+
att = self.drop(F.softmax(att, dim=-1))
|
| 26 |
+
return self.proj(att @ v)
|
| 27 |
+
|
| 28 |
+
class EasyFormerFFN(nn.Module):
|
| 29 |
+
def __init__(self, cfg):
|
| 30 |
+
super().__init__()
|
| 31 |
+
self.fc1 = nn.Linear(cfg.d_model, 2 * cfg.d_model)
|
| 32 |
+
self.fc2 = nn.Linear(2 * cfg.d_model, cfg.d_model)
|
| 33 |
+
self.drop = nn.Dropout(cfg.dropout)
|
| 34 |
+
def forward(self, x):
|
| 35 |
+
return self.drop(self.fc2(F.relu(self.fc1(x))))
|
| 36 |
+
|
| 37 |
+
class EasyFormerBlock(nn.Module):
|
| 38 |
+
def __init__(self, cfg):
|
| 39 |
+
super().__init__()
|
| 40 |
+
self.ln1 = RMSNorm(cfg.d_model); self.attn = EasyFormerAttention(cfg)
|
| 41 |
+
self.ln2 = RMSNorm(cfg.d_model); self.ffn = EasyFormerFFN(cfg)
|
| 42 |
+
def forward(self, x):
|
| 43 |
+
x = x + self.attn(self.ln1(x))
|
| 44 |
+
x = x + self.ffn(self.ln2(x))
|
| 45 |
+
return x
|
| 46 |
+
|
| 47 |
+
class EasyFormerPreTrainedModel(PreTrainedModel):
|
| 48 |
+
config_class = EasyFormerConfig
|
| 49 |
+
base_model_prefix = "easyformer"
|
| 50 |
+
supports_gradient_checkpointing = False
|
| 51 |
+
|
| 52 |
+
class EasyFormerLMHeadModel(EasyFormerPreTrainedModel, GenerationMixin):
|
| 53 |
+
def __init__(self, config):
|
| 54 |
+
super().__init__(config)
|
| 55 |
+
self.cfg = config
|
| 56 |
+
self.tok_emb = nn.Embedding(config.vocab_size, config.d_model)
|
| 57 |
+
self.pos_emb = nn.Embedding(config.ctx, config.d_model)
|
| 58 |
+
self.drop = nn.Dropout(config.dropout)
|
| 59 |
+
self.blocks = nn.ModuleList(
|
| 60 |
+
[EasyFormerBlock(config) for _ in range(config.n_layer)]
|
| 61 |
+
)
|
| 62 |
+
self.ln_f = RMSNorm(config.d_model)
|
| 63 |
+
self.lm_head = nn.Linear(config.d_model, config.vocab_size, bias=False)
|
| 64 |
+
self.lm_head.weight = self.tok_emb.weight
|
| 65 |
+
|
| 66 |
+
def forward(self, input_ids, attention_mask=None, labels=None, **kwargs):
|
| 67 |
+
B, T = input_ids.shape
|
| 68 |
+
pos = torch.arange(T, device=input_ids.device)
|
| 69 |
+
x = self.drop(self.tok_emb(input_ids) + self.pos_emb(pos))
|
| 70 |
+
for b in self.blocks: x = b(x)
|
| 71 |
+
logits = self.lm_head(self.ln_f(x))
|
| 72 |
+
if labels is not None:
|
| 73 |
+
loss = F.cross_entropy(
|
| 74 |
+
logits.view(-1, self.cfg.vocab_size),
|
| 75 |
+
labels.view(-1), ignore_index=-100,
|
| 76 |
+
)
|
| 77 |
+
return {"loss": loss, "logits": logits}
|
| 78 |
+
return {"logits": logits}
|
| 79 |
+
|
| 80 |
+
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 81 |
+
return {"input_ids": input_ids}
|