Instructions to use ewre324/moondream2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- llama.cpp
How to use ewre324/moondream2 with llama.cpp:
Install (macOS, Linux)
curl -LsSf https://llama.app/install.sh | sh # Start a local OpenAI-compatible server with a web UI: llama serve -hf ewre324/moondream2:F16 # Run inference directly in the terminal: llama cli -hf ewre324/moondream2:F16
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama serve -hf ewre324/moondream2:F16 # Run inference directly in the terminal: llama cli -hf ewre324/moondream2:F16
Use pre-built binary
# Download pre-built binary from: # https://github.com/ggerganov/llama.cpp/releases # Start a local OpenAI-compatible server with a web UI: ./llama-server -hf ewre324/moondream2:F16 # Run inference directly in the terminal: ./llama-cli -hf ewre324/moondream2:F16
Build from source code
git clone https://github.com/ggerganov/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-server llama-cli # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama-server -hf ewre324/moondream2:F16 # Run inference directly in the terminal: ./build/bin/llama-cli -hf ewre324/moondream2:F16
Use Docker
docker model run hf.co/ewre324/moondream2:F16
- LM Studio
- Jan
- vLLM
How to use ewre324/moondream2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "ewre324/moondream2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "ewre324/moondream2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/ewre324/moondream2:F16
- Ollama
How to use ewre324/moondream2 with Ollama:
ollama run hf.co/ewre324/moondream2:F16
- Unsloth Studio
How to use ewre324/moondream2 with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for ewre324/moondream2 to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for ewre324/moondream2 to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for ewre324/moondream2 to start chatting
- Docker Model Runner
How to use ewre324/moondream2 with Docker Model Runner:
docker model run hf.co/ewre324/moondream2:F16
- Lemonade
How to use ewre324/moondream2 with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull ewre324/moondream2:F16
Run and chat with the model
lemonade run user.moondream2-F16
List all available models
lemonade list
- Atomic Chat
| import torch | |
| import torch.nn as nn | |
| from torch.nn import functional as F | |
| from .layers import layer_norm, linear, mlp | |
| from .rope import apply_rotary_emb, precompute_freqs_cis | |
| from .weights import AttentionWeights | |
| from .config import TextConfig | |
| def text_encoder(input_ids: torch.Tensor, w: nn.Module): | |
| return F.embedding(input_ids, w.wte) | |
| def attn( | |
| x: torch.Tensor, | |
| w: AttentionWeights, | |
| freqs_cis: torch.Tensor, | |
| layer_kv_cache: torch.Tensor, | |
| attn_mask: torch.Tensor, | |
| n_heads: int, | |
| pos: int, | |
| ): | |
| bsz, q_len, d_model = x.shape | |
| head_dim = d_model // n_heads | |
| q, k, v = [ | |
| t.view(bsz, q_len, n_heads, head_dim).transpose(1, 2) | |
| for t in linear(x, w.qkv).chunk(3, dim=-1) | |
| ] | |
| position_ids = torch.arange(pos, pos + q_len, dtype=torch.long) | |
| q = apply_rotary_emb(q, freqs_cis, position_ids, n_heads) | |
| k = apply_rotary_emb(k, freqs_cis, position_ids, n_heads) | |
| k_, v_ = k, v | |
| if layer_kv_cache is not None: | |
| k = torch.cat([layer_kv_cache[0, :, :, :pos, :], k], dim=2) | |
| v = torch.cat([layer_kv_cache[1, :, :, :pos, :], v], dim=2) | |
| out = F.scaled_dot_product_attention(q, k, v, attn_mask=attn_mask).to( | |
| # This type conversion isn't needed when running in PyTorch directly, but the | |
| # ONNX export runs attention in float32 because the attention mask is cast to | |
| # float32. | |
| x.dtype | |
| ) | |
| out = out.transpose(1, 2).reshape(bsz, q_len, d_model) | |
| out = linear(out, w.proj) | |
| return out, torch.stack([k_, v_]) | |
| def text_decoder( | |
| inputs_embeds: torch.Tensor, | |
| w: nn.Module, | |
| kv_cache: torch.Tensor, | |
| pos: int, | |
| config: TextConfig, | |
| ): | |
| hidden_BTC = inputs_embeds | |
| new_kv_cache = [torch.empty(0)] * len(w.blocks) | |
| attn_mask = w.attn_mask[ | |
| :, :, pos : pos + hidden_BTC.size(1), : pos + hidden_BTC.size(1) | |
| ] | |
| for i, block in enumerate(w.blocks): | |
| l_in = layer_norm(hidden_BTC, block.ln) | |
| l_attn, new_kv_cache[i] = attn( | |
| l_in, | |
| block.attn, | |
| freqs_cis=w.freqs_cis, | |
| layer_kv_cache=kv_cache[i], | |
| attn_mask=attn_mask, | |
| n_heads=config.n_heads, | |
| pos=pos, | |
| ) | |
| l_mlp = mlp(l_in, block.mlp) | |
| hidden_BTC = hidden_BTC + l_attn + l_mlp | |
| return hidden_BTC, torch.stack(new_kv_cache) | |
| def lm_head(hidden_BTC: torch.Tensor, w: nn.Module): | |
| hidden_BC = hidden_BTC[:, -1, :] | |
| hidden_BC = layer_norm(hidden_BC, w.post_ln) | |
| logits = linear(hidden_BC, w.lm_head) | |
| return logits | |
| def prefill( | |
| inputs_embeds: torch.Tensor, | |
| kv_cache: torch.Tensor, | |
| pos: int, | |
| w: nn.Module, | |
| config: TextConfig, | |
| ): | |
| # Updates kv_cache in-place | |
| hidden, kv_cache[:, :, :, :, pos : pos + inputs_embeds.size(1), :] = text_decoder( | |
| inputs_embeds, w, kv_cache, pos, config | |
| ) | |
| return hidden | |
| def decode_one_token( | |
| token_emb: torch.Tensor, | |
| kv_cache: torch.Tensor, | |
| pos: int, | |
| w: nn.Module, | |
| config: TextConfig, | |
| ): | |
| hidden, kv_cache_update = text_decoder(token_emb[None], w, kv_cache, pos, config) | |
| logits = lm_head(hidden, w) | |
| return logits, hidden, kv_cache_update | |
| def build_text_model(config: TextConfig, dtype: torch.dtype) -> nn.Module: | |
| text = nn.ModuleDict( | |
| { | |
| "blocks": nn.ModuleList( | |
| [ | |
| nn.ModuleDict( | |
| { | |
| "ln": nn.LayerNorm(config.dim, dtype=dtype), | |
| "attn": nn.ModuleDict( | |
| { | |
| "qkv": nn.Linear( | |
| config.dim, 3 * config.dim, dtype=dtype | |
| ), | |
| "proj": nn.Linear( | |
| config.dim, config.dim, dtype=dtype | |
| ), | |
| } | |
| ), | |
| "mlp": nn.ModuleDict( | |
| { | |
| "fc1": nn.Linear( | |
| config.dim, 4 * config.dim, dtype=dtype | |
| ), | |
| "fc2": nn.Linear( | |
| 4 * config.dim, config.dim, dtype=dtype | |
| ), | |
| } | |
| ), | |
| } | |
| ) | |
| for _ in range(config.n_layers) | |
| ] | |
| ), | |
| "post_ln": nn.LayerNorm(config.dim, dtype=dtype), | |
| "lm_head": nn.Linear(config.dim, config.vocab_size, dtype=dtype), | |
| } | |
| ) | |
| text.wte = nn.Parameter(torch.empty(config.vocab_size, config.dim, dtype=dtype)) | |
| text.register_buffer( | |
| "freqs_cis", | |
| precompute_freqs_cis(config.dim // (2 * config.n_heads), config.max_context), | |
| persistent=False, | |
| ) | |
| attn_mask = torch.tril( | |
| torch.ones(1, 1, config.max_context, config.max_context, dtype=torch.bool) | |
| ) | |
| if config.prefix_attn != 0: | |
| attn_mask[..., : config.prefix_attn, : config.prefix_attn] = 1 | |
| text.register_buffer("attn_mask", attn_mask, persistent=False) | |
| return text | |