Text Generation
Transformers
Safetensors
French
ptit_slm
ptit-slm
from-scratch
post-norm
custom_code
experimental
Instructions to use MisterAI/Ptit_SLM_Poc03 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use MisterAI/Ptit_SLM_Poc03 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="MisterAI/Ptit_SLM_Poc03", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("MisterAI/Ptit_SLM_Poc03", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use MisterAI/Ptit_SLM_Poc03 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "MisterAI/Ptit_SLM_Poc03" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "MisterAI/Ptit_SLM_Poc03", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/MisterAI/Ptit_SLM_Poc03
- SGLang
How to use MisterAI/Ptit_SLM_Poc03 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 "MisterAI/Ptit_SLM_Poc03" \ --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": "MisterAI/Ptit_SLM_Poc03", "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 "MisterAI/Ptit_SLM_Poc03" \ --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": "MisterAI/Ptit_SLM_Poc03", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use MisterAI/Ptit_SLM_Poc03 with Docker Model Runner:
docker model run hf.co/MisterAI/Ptit_SLM_Poc03
Download modeling_ptit_slm.py from MisterAI/Ptit_SLM_Poc03: direct link, hf CLI and curl.
- Browser
- Download file 3.29 kB
-
https://huggingface.co/MisterAI/Ptit_SLM_Poc03/resolve/main/modeling_ptit_slm.py
- Command line
-
hf download hf://MisterAI/Ptit_SLM_Poc03/modeling_ptit_slm.py
-
curl -L -o modeling_ptit_slm.py https://huggingface.co/MisterAI/Ptit_SLM_Poc03/resolve/main/modeling_ptit_slm.py
3.29 kB
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from transformers import PreTrainedModel, GenerationMixin | |
| from transformers.modeling_outputs import CausalLMOutput | |
| from .configuration_ptit_slm import PtitSLMConfig | |
| class TransformerBlock(nn.Module): | |
| """Bloc POST-norm : x = norm1(x + attn(x)) ; x = norm2(x + ffn(x)).""" | |
| def __init__(self, d, n_heads, d_ff, p, eps): | |
| super().__init__() | |
| self.attention = nn.MultiheadAttention(d, n_heads, dropout=p, batch_first=True) | |
| self.feed_forward = nn.Sequential( | |
| nn.Linear(d, d_ff), nn.GELU(), nn.Linear(d_ff, d), nn.Dropout(p) | |
| ) | |
| self.norm1 = nn.LayerNorm(d, eps=eps) | |
| self.norm2 = nn.LayerNorm(d, eps=eps) | |
| self.dropout = nn.Dropout(p) | |
| def forward(self, x): | |
| T = x.size(1) | |
| interdit = torch.triu(torch.ones(T, T, dtype=torch.bool, device=x.device), diagonal=1) | |
| a, _ = self.attention(x, x, x, attn_mask=interdit, need_weights=False) | |
| x = self.norm1(x + self.dropout(a)) | |
| x = self.norm2(x + self.dropout(self.feed_forward(x))) | |
| return x | |
| class PtitSLMForCausalLM(PreTrainedModel, GenerationMixin): | |
| config_class = PtitSLMConfig | |
| def __init__(self, config): | |
| super().__init__(config) | |
| d = config.hidden_size | |
| self.embed = nn.Embedding(config.vocab_size, d) | |
| self.pos_embed = nn.Embedding(config.max_position_embeddings, d) | |
| self.layers = nn.ModuleList([ | |
| TransformerBlock(d, config.num_attention_heads, config.intermediate_size, | |
| config.dropout, config.layer_norm_eps) | |
| for _ in range(config.num_hidden_layers) | |
| ]) | |
| self.dropout = nn.Dropout(config.dropout) | |
| self.ln_f = nn.LayerNorm(d, eps=config.layer_norm_eps) | |
| self.head = nn.Linear(d, config.vocab_size) # AVEC biais, comme l'original | |
| self.post_init() | |
| def get_input_embeddings(self): | |
| return self.embed | |
| def set_input_embeddings(self, value): | |
| self.embed = value | |
| def get_output_embeddings(self): | |
| return self.head | |
| def set_output_embeddings(self, value): | |
| self.head = value | |
| def forward(self, input_ids, attention_mask=None, labels=None, **kwargs): | |
| # attention_mask ignoré : usage prévu = un seul prompt, sans padding | |
| T = input_ids.size(1) | |
| if T > self.config.max_position_embeddings: | |
| raise ValueError(f"Séquence trop longue : {T} > {self.config.max_position_embeddings}") | |
| pos = torch.arange(T, device=input_ids.device).unsqueeze(0) | |
| x = self.dropout(self.embed(input_ids) + self.pos_embed(pos)) | |
| for layer in self.layers: | |
| x = layer(x) | |
| logits = self.head(self.ln_f(x)) | |
| loss = None | |
| if labels is not None: | |
| loss = F.cross_entropy( | |
| logits[:, :-1].reshape(-1, logits.size(-1)), | |
| labels[:, 1:].reshape(-1), | |
| ignore_index=-100, | |
| ) | |
| return CausalLMOutput(loss=loss, logits=logits) | |
| def prepare_inputs_for_generation(self, input_ids, **kwargs): | |
| # pas de KV-cache (comme l'original) + fenêtre glissante sur le contexte entraîné | |
| return {"input_ids": input_ids[:, -self.config.max_context:]} | |