Instructions to use vikhyatk/moondream2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use vikhyatk/moondream2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="vikhyatk/moondream2", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("vikhyatk/moondream2", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use vikhyatk/moondream2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "vikhyatk/moondream2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "vikhyatk/moondream2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/vikhyatk/moondream2
- SGLang
How to use vikhyatk/moondream2 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 "vikhyatk/moondream2" \ --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": "vikhyatk/moondream2", "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 "vikhyatk/moondream2" \ --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": "vikhyatk/moondream2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use vikhyatk/moondream2 with Docker Model Runner:
docker model run hf.co/vikhyatk/moondream2
Support transformers 5: call post_init() and recompute non-persistent buffers
#84
by markbackman - opened
- hf_moondream.py +24 -0
hf_moondream.py
CHANGED
|
@@ -6,6 +6,7 @@ from typing import Union
|
|
| 6 |
|
| 7 |
from .config import MoondreamConfig
|
| 8 |
from .moondream import MoondreamModel
|
|
|
|
| 9 |
|
| 10 |
# Files sometimes don't get loaded without these...
|
| 11 |
from .image_crops import *
|
|
@@ -44,6 +45,29 @@ class HfMoondream(PreTrainedModel):
|
|
| 44 |
MoondreamConfig.from_dict(config.config), setup_caches=False
|
| 45 |
)
|
| 46 |
self._is_kv_cache_setup = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def _setup_caches(self):
|
| 49 |
if not self._is_kv_cache_setup:
|
|
|
|
| 6 |
|
| 7 |
from .config import MoondreamConfig
|
| 8 |
from .moondream import MoondreamModel
|
| 9 |
+
from .rope import precompute_freqs_cis
|
| 10 |
|
| 11 |
# Files sometimes don't get loaded without these...
|
| 12 |
from .image_crops import *
|
|
|
|
| 45 |
MoondreamConfig.from_dict(config.config), setup_caches=False
|
| 46 |
)
|
| 47 |
self._is_kv_cache_setup = False
|
| 48 |
+
self.post_init()
|
| 49 |
+
|
| 50 |
+
@torch.no_grad()
|
| 51 |
+
def _init_weights(self, module):
|
| 52 |
+
# transformers>=5 builds the model on the meta device, so non-persistent
|
| 53 |
+
# buffers are not loaded from the checkpoint and must be recomputed here.
|
| 54 |
+
if module is self.model:
|
| 55 |
+
config = self.model.config
|
| 56 |
+
max_context = config.text.max_context
|
| 57 |
+
patch_w = config.vision.crop_size // config.vision.enc_patch_size
|
| 58 |
+
prefix_attn_len = 1 + patch_w**2
|
| 59 |
+
attn_mask = torch.tril(
|
| 60 |
+
torch.ones(max_context, max_context, dtype=torch.bool)
|
| 61 |
+
)
|
| 62 |
+
attn_mask[:prefix_attn_len, :prefix_attn_len] = 1
|
| 63 |
+
module.attn_mask.copy_(attn_mask)
|
| 64 |
+
elif module is self.model.text:
|
| 65 |
+
config = self.model.config.text
|
| 66 |
+
module.freqs_cis.copy_(
|
| 67 |
+
precompute_freqs_cis(
|
| 68 |
+
config.dim // (2 * config.n_heads), config.max_context
|
| 69 |
+
)
|
| 70 |
+
)
|
| 71 |
|
| 72 |
def _setup_caches(self):
|
| 73 |
if not self._is_kv_cache_setup:
|