Instructions to use xiaoyewuz-Ruster/zzjrabbit2.3 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use xiaoyewuz-Ruster/zzjrabbit2.3 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="xiaoyewuz-Ruster/zzjrabbit2.3", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("xiaoyewuz-Ruster/zzjrabbit2.3", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use xiaoyewuz-Ruster/zzjrabbit2.3 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "xiaoyewuz-Ruster/zzjrabbit2.3" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "xiaoyewuz-Ruster/zzjrabbit2.3", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/xiaoyewuz-Ruster/zzjrabbit2.3
- SGLang
How to use xiaoyewuz-Ruster/zzjrabbit2.3 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 "xiaoyewuz-Ruster/zzjrabbit2.3" \ --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": "xiaoyewuz-Ruster/zzjrabbit2.3", "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 "xiaoyewuz-Ruster/zzjrabbit2.3" \ --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": "xiaoyewuz-Ruster/zzjrabbit2.3", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use xiaoyewuz-Ruster/zzjrabbit2.3 with Docker Model Runner:
docker model run hf.co/xiaoyewuz-Ruster/zzjrabbit2.3
Upload zzjrabbit22.py with huggingface_hub
Browse files- zzjrabbit22.py +250 -0
zzjrabbit22.py
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import math
|
| 2 |
+
import os.path
|
| 3 |
+
from typing import Optional, Union
|
| 4 |
+
|
| 5 |
+
import torch
|
| 6 |
+
import torch.nn as nn
|
| 7 |
+
from tokenizers import Tokenizer
|
| 8 |
+
from transformers import (
|
| 9 |
+
GenerationMixin,
|
| 10 |
+
PretrainedConfig,
|
| 11 |
+
PreTrainedModel,
|
| 12 |
+
PreTrainedTokenizer,
|
| 13 |
+
)
|
| 14 |
+
from transformers.modeling_outputs import BaseModelOutput, CausalLMOutput
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class ZZJRabbit22Config(PretrainedConfig):
|
| 18 |
+
model_type = "zzjrabbit22"
|
| 19 |
+
|
| 20 |
+
def __init__(
|
| 21 |
+
self,
|
| 22 |
+
num_layers: int = 12,
|
| 23 |
+
num_attention_heads: int = 8,
|
| 24 |
+
vocab_size: int = 10000,
|
| 25 |
+
hidden_size: int = 1024,
|
| 26 |
+
**kwargs,
|
| 27 |
+
):
|
| 28 |
+
self.num_layers = num_layers
|
| 29 |
+
self.num_attention_heads = num_attention_heads
|
| 30 |
+
self.vocab_size = vocab_size
|
| 31 |
+
self.hidden_size = hidden_size
|
| 32 |
+
assert hidden_size % num_attention_heads == 0
|
| 33 |
+
super().__init__(**kwargs)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class ZZJRabbit22PE(nn.Module):
|
| 37 |
+
def __init__(self, hidden_size: int, max_len: int = 32768):
|
| 38 |
+
super().__init__()
|
| 39 |
+
pe = torch.zeros(max_len, hidden_size)
|
| 40 |
+
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
|
| 41 |
+
div_term = torch.exp(
|
| 42 |
+
torch.arange(0, hidden_size, 2).float() * (-math.log(10000.0) / hidden_size)
|
| 43 |
+
)
|
| 44 |
+
pe[:, 0::2] = torch.sin(position * div_term)
|
| 45 |
+
pe[:, 1::2] = torch.cos(position * div_term)
|
| 46 |
+
pe = pe.unsqueeze(0).transpose(0, 1)
|
| 47 |
+
self.register_buffer("pe", pe)
|
| 48 |
+
|
| 49 |
+
def forward(self, x: torch.Tensor):
|
| 50 |
+
return x + self.pe[: x.size(0), :]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
class ZZJRabbit22Attention(nn.Module):
|
| 54 |
+
def __init__(self, config: ZZJRabbit22Config):
|
| 55 |
+
super().__init__()
|
| 56 |
+
self.config = config
|
| 57 |
+
self.head_dim = config.hidden_size // config.num_attention_heads
|
| 58 |
+
self.q_proj = nn.Linear(config.hidden_size, config.hidden_size)
|
| 59 |
+
self.k_proj = nn.Linear(config.hidden_size, config.hidden_size)
|
| 60 |
+
self.v_proj = nn.Linear(config.hidden_size, config.hidden_size)
|
| 61 |
+
self.out_proj = nn.Linear(config.hidden_size, config.hidden_size)
|
| 62 |
+
self.dropout = nn.Dropout(0.1)
|
| 63 |
+
|
| 64 |
+
def forward(
|
| 65 |
+
self,
|
| 66 |
+
x: torch.Tensor,
|
| 67 |
+
key_padding_mask: Optional[torch.BoolTensor] = None,
|
| 68 |
+
attn_mask: Optional[torch.BoolTensor] = None,
|
| 69 |
+
):
|
| 70 |
+
batch_size = x.size(0)
|
| 71 |
+
Q = (
|
| 72 |
+
self.q_proj(x)
|
| 73 |
+
.view(batch_size, -1, self.config.num_attention_heads, self.head_dim)
|
| 74 |
+
.transpose(1, 2)
|
| 75 |
+
)
|
| 76 |
+
K = (
|
| 77 |
+
self.k_proj(x)
|
| 78 |
+
.view(batch_size, -1, self.config.num_attention_heads, self.head_dim)
|
| 79 |
+
.transpose(1, 2)
|
| 80 |
+
)
|
| 81 |
+
V = (
|
| 82 |
+
self.v_proj(x)
|
| 83 |
+
.view(batch_size, -1, self.config.num_attention_heads, self.head_dim)
|
| 84 |
+
.transpose(1, 2)
|
| 85 |
+
)
|
| 86 |
+
scores = torch.matmul(Q, K.transpose(-2, -1)) / torch.sqrt(
|
| 87 |
+
torch.tensor(self.head_dim, dtype=torch.float32)
|
| 88 |
+
)
|
| 89 |
+
if key_padding_mask is not None:
|
| 90 |
+
scores = scores.masked_fill(
|
| 91 |
+
key_padding_mask.view(batch_size, 1, 1, -1), float("-inf")
|
| 92 |
+
)
|
| 93 |
+
if attn_mask is not None:
|
| 94 |
+
scores = scores.masked_fill(attn_mask, float("-inf"))
|
| 95 |
+
attn_weights = nn.functional.softmax(scores, dim=-1)
|
| 96 |
+
attn_weights = self.dropout(attn_weights)
|
| 97 |
+
context = torch.matmul(attn_weights, V)
|
| 98 |
+
context = context.transpose(1, 2).contiguous()
|
| 99 |
+
context = context.view(batch_size, -1, self.config.hidden_size)
|
| 100 |
+
return self.out_proj(context)
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
class ZZJRabbit22Layer(nn.Module):
|
| 104 |
+
def __init__(self, config: ZZJRabbit22Config):
|
| 105 |
+
super().__init__()
|
| 106 |
+
self.attn = ZZJRabbit22Attention(config)
|
| 107 |
+
self.l1 = nn.Linear(config.hidden_size, config.hidden_size)
|
| 108 |
+
self.l2 = nn.Linear(config.hidden_size, config.hidden_size)
|
| 109 |
+
self.activate = nn.ReLU()
|
| 110 |
+
self.norm = nn.RMSNorm(config.hidden_size)
|
| 111 |
+
|
| 112 |
+
def forward(
|
| 113 |
+
self, x: torch.Tensor, attention_mask: Optional[torch.Tensor] = None
|
| 114 |
+
) -> torch.Tensor:
|
| 115 |
+
key_padding_mask = None
|
| 116 |
+
attn_mask = None
|
| 117 |
+
if self.training:
|
| 118 |
+
attn_mask = torch.gt(
|
| 119 |
+
torch.triu(torch.ones(x.size(-2), x.size(-2), device=x.device), 1), 0
|
| 120 |
+
)
|
| 121 |
+
if attention_mask is not None:
|
| 122 |
+
key_padding_mask = torch.lt(attention_mask, 1)
|
| 123 |
+
attn = self.attn(
|
| 124 |
+
x,
|
| 125 |
+
key_padding_mask=key_padding_mask,
|
| 126 |
+
attn_mask=attn_mask,
|
| 127 |
+
)[0]
|
| 128 |
+
x = self.norm(x + attn)
|
| 129 |
+
o = self.l1(x)
|
| 130 |
+
o = self.activate(o)
|
| 131 |
+
o = self.l2(o)
|
| 132 |
+
return self.norm(x + o)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
class ZZJRabbit22Model(PreTrainedModel):
|
| 136 |
+
config_class = ZZJRabbit22Config
|
| 137 |
+
|
| 138 |
+
def __init__(self, config: ZZJRabbit22Config, **kwargs):
|
| 139 |
+
super().__init__(config, **kwargs)
|
| 140 |
+
self.config = config
|
| 141 |
+
self.emb = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 142 |
+
self.pe = ZZJRabbit22PE(config.hidden_size)
|
| 143 |
+
self.layers = nn.ModuleList(
|
| 144 |
+
[ZZJRabbit22Layer(config) for _ in range(config.num_layers)]
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
def forward(
|
| 148 |
+
self,
|
| 149 |
+
input_ids: torch.Tensor,
|
| 150 |
+
return_dict: Optional[bool] = None,
|
| 151 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 152 |
+
**kwargs,
|
| 153 |
+
):
|
| 154 |
+
res = self.emb(input_ids)
|
| 155 |
+
res = self.pe(res)
|
| 156 |
+
res = res
|
| 157 |
+
for l in self.layers:
|
| 158 |
+
res = l(res, attention_mask)
|
| 159 |
+
if not return_dict:
|
| 160 |
+
return (res,)
|
| 161 |
+
else:
|
| 162 |
+
return BaseModelOutput(res)
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
class ZZJRabbit22ForCausalLM(PreTrainedModel, GenerationMixin):
|
| 166 |
+
config_class = ZZJRabbit22Config
|
| 167 |
+
|
| 168 |
+
def __init__(self, config, **kwargs):
|
| 169 |
+
super().__init__(config, **kwargs)
|
| 170 |
+
self.model = ZZJRabbit22Model(config, **kwargs)
|
| 171 |
+
self.l = nn.Linear(config.hidden_size, config.vocab_size)
|
| 172 |
+
|
| 173 |
+
def forward(
|
| 174 |
+
self,
|
| 175 |
+
input_ids: torch.Tensor,
|
| 176 |
+
return_dict: Optional[bool] = None,
|
| 177 |
+
labels: Optional[torch.Tensor] = None,
|
| 178 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 179 |
+
logits_to_keep: Union[int, torch.Tensor] = 0,
|
| 180 |
+
**kwargs,
|
| 181 |
+
):
|
| 182 |
+
# print(input_ids, return_dict, labels, attention_mask, logits_to_keep, kwargs)
|
| 183 |
+
hidden = self.model(input_ids=input_ids, attention_mask=attention_mask)[0]
|
| 184 |
+
logits = self.l(
|
| 185 |
+
hidden[
|
| 186 |
+
:,
|
| 187 |
+
slice(-logits_to_keep, None)
|
| 188 |
+
if isinstance(logits_to_keep, int)
|
| 189 |
+
else logits_to_keep,
|
| 190 |
+
:,
|
| 191 |
+
]
|
| 192 |
+
)
|
| 193 |
+
if labels is not None:
|
| 194 |
+
loss = self.loss_function(
|
| 195 |
+
logits=logits,
|
| 196 |
+
labels=labels,
|
| 197 |
+
vocab_size=self.config.vocab_size,
|
| 198 |
+
**kwargs,
|
| 199 |
+
)
|
| 200 |
+
print(loss)
|
| 201 |
+
if not return_dict:
|
| 202 |
+
return (loss, logits) if labels is not None else (logits,)
|
| 203 |
+
else:
|
| 204 |
+
return (
|
| 205 |
+
CausalLMOutput(logits=logits, loss=loss)
|
| 206 |
+
if labels is not None
|
| 207 |
+
else CausalLMOutput(logits=logits)
|
| 208 |
+
)
|
| 209 |
+
|
| 210 |
+
@classmethod
|
| 211 |
+
def can_generate(cls):
|
| 212 |
+
return True
|
| 213 |
+
|
| 214 |
+
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 215 |
+
return {"input_ids": input_ids}
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
class ZZJRabbit22Tokenizer(PreTrainedTokenizer):
|
| 219 |
+
vocab_files_names = {"tokenizers_file": "tokenizer.json"}
|
| 220 |
+
|
| 221 |
+
def __init__(self, tokenizers_file, **kwargs):
|
| 222 |
+
self.internal = Tokenizer.from_file(tokenizers_file)
|
| 223 |
+
super().__init__(**kwargs)
|
| 224 |
+
|
| 225 |
+
def get_vocab(self):
|
| 226 |
+
return {self.internal.id_to_token(i): i for i in range(self.vocab_size)}
|
| 227 |
+
|
| 228 |
+
def tokenize(self, text, **kwargs):
|
| 229 |
+
return self.internal.encode(text).tokens
|
| 230 |
+
|
| 231 |
+
def convert_tokens_to_ids(self, tokens):
|
| 232 |
+
return (
|
| 233 |
+
self.internal.token_to_id(tokens)
|
| 234 |
+
if isinstance(tokens, str)
|
| 235 |
+
else [self.internal.token_to_id(t) for t in tokens]
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
def decode(self, tokens, skip_special_tokens=True, **kwargs):
|
| 239 |
+
if isinstance(tokens, torch.Tensor):
|
| 240 |
+
tokens = tokens.tolist()
|
| 241 |
+
return self.internal.decode(tokens, skip_special_tokens=skip_special_tokens)
|
| 242 |
+
|
| 243 |
+
@property
|
| 244 |
+
def vocab_size(self):
|
| 245 |
+
return self.internal.get_vocab_size()
|
| 246 |
+
|
| 247 |
+
def save_vocabulary(self, path, *args, **kwargs) -> tuple[str]:
|
| 248 |
+
p = os.path.join(path, "tokenizer.json")
|
| 249 |
+
self.internal.save(p)
|
| 250 |
+
return (p,)
|