Text Generation
Transformers
PyTorch
Safetensors
mistral
custom_code
Eval Results (legacy)
text-generation-inference
Instructions to use d-matrix/Mistral-7B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use d-matrix/Mistral-7B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="d-matrix/Mistral-7B", trust_remote_code=True)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("d-matrix/Mistral-7B", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("d-matrix/Mistral-7B", trust_remote_code=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use d-matrix/Mistral-7B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "d-matrix/Mistral-7B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "d-matrix/Mistral-7B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/d-matrix/Mistral-7B
- SGLang
How to use d-matrix/Mistral-7B 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 "d-matrix/Mistral-7B" \ --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": "d-matrix/Mistral-7B", "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 "d-matrix/Mistral-7B" \ --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": "d-matrix/Mistral-7B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use d-matrix/Mistral-7B with Docker Model Runner:
docker model run hf.co/d-matrix/Mistral-7B
Update modeling_mistral.py
Browse files- modeling_mistral.py +1 -35
modeling_mistral.py
CHANGED
|
@@ -34,7 +34,7 @@ from transformers.utils import (
|
|
| 34 |
)
|
| 35 |
from .configuration_mistral import MistralConfig
|
| 36 |
from transformers.models.mistral.modeling_mistral import MistralRMSNorm
|
| 37 |
-
|
| 38 |
|
| 39 |
logger = logging.get_logger(__name__)
|
| 40 |
|
|
@@ -58,40 +58,6 @@ class MistralMLP(nn.Module):
|
|
| 58 |
return down_proj
|
| 59 |
|
| 60 |
|
| 61 |
-
def rotate_half(x):
|
| 62 |
-
"""Rotates half the hidden dims of the input."""
|
| 63 |
-
x1 = x[..., : x.shape[-1] // 2]
|
| 64 |
-
x2 = x[..., x.shape[-1] // 2 :]
|
| 65 |
-
return torch.cat((-x2, x1), dim=-1)
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):
|
| 69 |
-
"""Applies Rotary Position Embedding to the query and key tensors.
|
| 70 |
-
|
| 71 |
-
Args:
|
| 72 |
-
q (`torch.Tensor`): The query tensor.
|
| 73 |
-
k (`torch.Tensor`): The key tensor.
|
| 74 |
-
cos (`torch.Tensor`): The cosine part of the rotary embedding.
|
| 75 |
-
sin (`torch.Tensor`): The sine part of the rotary embedding.
|
| 76 |
-
position_ids (`torch.Tensor`, *optional*):
|
| 77 |
-
Deprecated and unused.
|
| 78 |
-
unsqueeze_dim (`int`, *optional*, defaults to 1):
|
| 79 |
-
The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
|
| 80 |
-
sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
|
| 81 |
-
that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
|
| 82 |
-
k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
|
| 83 |
-
cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
|
| 84 |
-
the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
|
| 85 |
-
Returns:
|
| 86 |
-
`tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
|
| 87 |
-
"""
|
| 88 |
-
cos = cos.unsqueeze(unsqueeze_dim)
|
| 89 |
-
sin = sin.unsqueeze(unsqueeze_dim)
|
| 90 |
-
q_embed = (q * cos) + (rotate_half(q) * sin)
|
| 91 |
-
k_embed = (k * cos) + (rotate_half(k) * sin)
|
| 92 |
-
return q_embed, k_embed
|
| 93 |
-
|
| 94 |
-
|
| 95 |
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
|
| 96 |
"""
|
| 97 |
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
|
|
|
|
| 34 |
)
|
| 35 |
from .configuration_mistral import MistralConfig
|
| 36 |
from transformers.models.mistral.modeling_mistral import MistralRMSNorm
|
| 37 |
+
from transformers.models.mistral.modeling_mistral import apply_rotary_pos_emb
|
| 38 |
|
| 39 |
logger = logging.get_logger(__name__)
|
| 40 |
|
|
|
|
| 58 |
return down_proj
|
| 59 |
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
|
| 62 |
"""
|
| 63 |
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
|