Instructions to use Qwen/Qwen-VL with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Qwen/Qwen-VL with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="Qwen/Qwen-VL", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-VL", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Qwen/Qwen-VL with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Qwen/Qwen-VL" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Qwen/Qwen-VL", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Qwen/Qwen-VL
- SGLang
How to use Qwen/Qwen-VL 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 "Qwen/Qwen-VL" \ --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": "Qwen/Qwen-VL", "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 "Qwen/Qwen-VL" \ --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": "Qwen/Qwen-VL", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Qwen/Qwen-VL with Docker Model Runner:
docker model run hf.co/Qwen/Qwen-VL
Update modeling_qwen.py
Browse files- modeling_qwen.py +7 -7
modeling_qwen.py
CHANGED
|
@@ -154,7 +154,7 @@ class QWenAttention(nn.Module):
|
|
| 154 |
if self.rotary_ndims is not None
|
| 155 |
else self.hidden_size_per_attention_head
|
| 156 |
)
|
| 157 |
-
self.rotary_emb =
|
| 158 |
|
| 159 |
self.use_dynamic_ntk = config.use_dynamic_ntk
|
| 160 |
self.use_logn_attn = config.use_logn_attn
|
|
@@ -386,12 +386,12 @@ class QWenBlock(nn.Module):
|
|
| 386 |
hidden_size = config.hidden_size
|
| 387 |
self.bf16 = config.bf16
|
| 388 |
|
| 389 |
-
self.ln_1 =
|
| 390 |
hidden_size,
|
| 391 |
eps=config.layer_norm_epsilon,
|
| 392 |
)
|
| 393 |
self.attn = QWenAttention(config)
|
| 394 |
-
self.ln_2 =
|
| 395 |
hidden_size,
|
| 396 |
eps=config.layer_norm_epsilon,
|
| 397 |
)
|
|
@@ -460,7 +460,7 @@ class QWenPreTrainedModel(PreTrainedModel):
|
|
| 460 |
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
| 461 |
if module.padding_idx is not None:
|
| 462 |
module.weight.data[module.padding_idx].zero_()
|
| 463 |
-
elif isinstance(module,
|
| 464 |
module.weight.data.fill_(1.0)
|
| 465 |
|
| 466 |
for name, p in module.named_parameters():
|
|
@@ -500,7 +500,7 @@ class QWenModel(QWenPreTrainedModel):
|
|
| 500 |
for i in range(config.num_hidden_layers)
|
| 501 |
]
|
| 502 |
)
|
| 503 |
-
self.ln_f =
|
| 504 |
self.embed_dim,
|
| 505 |
eps=config.layer_norm_epsilon,
|
| 506 |
)
|
|
@@ -1041,7 +1041,7 @@ class QWenLMHeadModel(QWenPreTrainedModel):
|
|
| 1041 |
)
|
| 1042 |
|
| 1043 |
|
| 1044 |
-
class
|
| 1045 |
def __init__(self, dim, base=10000):
|
| 1046 |
super().__init__()
|
| 1047 |
self.dim = dim
|
|
@@ -1104,7 +1104,7 @@ def apply_rotary_pos_emb(t, freqs):
|
|
| 1104 |
return torch.cat((t_, t_pass_), dim=-1).type_as(t)
|
| 1105 |
|
| 1106 |
|
| 1107 |
-
class
|
| 1108 |
def __init__(self, dim: int, eps: float = 1e-6):
|
| 1109 |
super().__init__()
|
| 1110 |
self.eps = eps
|
|
|
|
| 154 |
if self.rotary_ndims is not None
|
| 155 |
else self.hidden_size_per_attention_head
|
| 156 |
)
|
| 157 |
+
self.rotary_emb = QWenRotaryEmbedding(dim, base=config.rotary_emb_base)
|
| 158 |
|
| 159 |
self.use_dynamic_ntk = config.use_dynamic_ntk
|
| 160 |
self.use_logn_attn = config.use_logn_attn
|
|
|
|
| 386 |
hidden_size = config.hidden_size
|
| 387 |
self.bf16 = config.bf16
|
| 388 |
|
| 389 |
+
self.ln_1 = QWenRMSNorm(
|
| 390 |
hidden_size,
|
| 391 |
eps=config.layer_norm_epsilon,
|
| 392 |
)
|
| 393 |
self.attn = QWenAttention(config)
|
| 394 |
+
self.ln_2 = QWenRMSNorm(
|
| 395 |
hidden_size,
|
| 396 |
eps=config.layer_norm_epsilon,
|
| 397 |
)
|
|
|
|
| 460 |
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
| 461 |
if module.padding_idx is not None:
|
| 462 |
module.weight.data[module.padding_idx].zero_()
|
| 463 |
+
elif isinstance(module, QWenRMSNorm):
|
| 464 |
module.weight.data.fill_(1.0)
|
| 465 |
|
| 466 |
for name, p in module.named_parameters():
|
|
|
|
| 500 |
for i in range(config.num_hidden_layers)
|
| 501 |
]
|
| 502 |
)
|
| 503 |
+
self.ln_f = QWenRMSNorm(
|
| 504 |
self.embed_dim,
|
| 505 |
eps=config.layer_norm_epsilon,
|
| 506 |
)
|
|
|
|
| 1041 |
)
|
| 1042 |
|
| 1043 |
|
| 1044 |
+
class QWenRotaryEmbedding(torch.nn.Module):
|
| 1045 |
def __init__(self, dim, base=10000):
|
| 1046 |
super().__init__()
|
| 1047 |
self.dim = dim
|
|
|
|
| 1104 |
return torch.cat((t_, t_pass_), dim=-1).type_as(t)
|
| 1105 |
|
| 1106 |
|
| 1107 |
+
class QWenRMSNorm(torch.nn.Module):
|
| 1108 |
def __init__(self, dim: int, eps: float = 1e-6):
|
| 1109 |
super().__init__()
|
| 1110 |
self.eps = eps
|