Instructions to use moondream/moondream3-preview with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use moondream/moondream3-preview with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="moondream/moondream3-preview", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("moondream/moondream3-preview", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use moondream/moondream3-preview with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "moondream/moondream3-preview" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "moondream/moondream3-preview", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/moondream/moondream3-preview
- SGLang
How to use moondream/moondream3-preview 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 "moondream/moondream3-preview" \ --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": "moondream/moondream3-preview", "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 "moondream/moondream3-preview" \ --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": "moondream/moondream3-preview", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use moondream/moondream3-preview with Docker Model Runner:
docker model run hf.co/moondream/moondream3-preview
Use nn.Linear instead of a custom linear function
#27
by Disty0 - opened
layers.py
CHANGED
|
@@ -31,10 +31,6 @@ class LinearWeights:
|
|
| 31 |
bias: torch.Tensor
|
| 32 |
|
| 33 |
|
| 34 |
-
def linear(x: torch.Tensor, w: LinearWeights) -> torch.Tensor:
|
| 35 |
-
return F.linear(x, w.weight, w.bias)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
def dequantize_tensor(W_q, scale, zero, orig_shape, dtype=torch.bfloat16):
|
| 39 |
_step = W_q.shape[0]
|
| 40 |
W_r = torch.empty([2 * _step, W_q.shape[1]], dtype=dtype, device=W_q.device)
|
|
@@ -226,9 +222,9 @@ def attn(x: torch.Tensor, w: AttentionWeights, n_heads: int) -> torch.Tensor:
|
|
| 226 |
|
| 227 |
q, k, v = [
|
| 228 |
t.view(bsz, q_len, n_heads, head_dim).transpose(1, 2)
|
| 229 |
-
for t in
|
| 230 |
]
|
| 231 |
out = F.scaled_dot_product_attention(q, k, v)
|
| 232 |
out = out.transpose(1, 2).reshape(bsz, q_len, d_model)
|
| 233 |
-
out =
|
| 234 |
return out
|
|
|
|
| 31 |
bias: torch.Tensor
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def dequantize_tensor(W_q, scale, zero, orig_shape, dtype=torch.bfloat16):
|
| 35 |
_step = W_q.shape[0]
|
| 36 |
W_r = torch.empty([2 * _step, W_q.shape[1]], dtype=dtype, device=W_q.device)
|
|
|
|
| 222 |
|
| 223 |
q, k, v = [
|
| 224 |
t.view(bsz, q_len, n_heads, head_dim).transpose(1, 2)
|
| 225 |
+
for t in w.qkv(x).chunk(3, dim=-1)
|
| 226 |
]
|
| 227 |
out = F.scaled_dot_product_attention(q, k, v)
|
| 228 |
out = out.transpose(1, 2).reshape(bsz, q_len, d_model)
|
| 229 |
+
out = w.proj(out)
|
| 230 |
return out
|