Image-Text-to-Text
Transformers
Safetensors
deepseek_v41
text-generation
nvfp4
fp4
deepseek
Mixture of Experts
multimodal
libertai
8-bit precision
fp8
Instructions to use LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4")# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4
- SGLang
How to use LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4 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 "LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4" \ --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": "LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4", "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 "LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4" \ --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": "LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4 with Docker Model Runner:
docker model run hf.co/LibertAIDAI/DeepSeek-V4.1-Flash-NVFP4
Final card and build scripts: measured numbers, Engram caveat, pruning analysis
Browse files- test_engram_shard.py +70 -0
test_engram_shard.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""Check the hand-written safetensors container round-trips before it meets a 94.6 GiB table.
|
| 3 |
+
|
| 4 |
+
Builds a shard shaped like the real ones (one Engram table plus the small q/k/wkv tensors),
|
| 5 |
+
runs write_engram_shard, then reopens the result with safetensors and verifies:
|
| 6 |
+
* it parses at all (the header length, padding and offsets are right)
|
| 7 |
+
* every tensor is present with the right dtype and shape
|
| 8 |
+
* pass-through tensors are byte-identical
|
| 9 |
+
* the packed table dequantizes to the same values the in-RAM path produced
|
| 10 |
+
"""
|
| 11 |
+
import os, sys, tempfile
|
| 12 |
+
import torch
|
| 13 |
+
from safetensors.torch import save_file
|
| 14 |
+
from safetensors import safe_open
|
| 15 |
+
|
| 16 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 17 |
+
import dsv41_fp4_stream as Q
|
| 18 |
+
|
| 19 |
+
ROWS, COLS = 250_000, 256
|
| 20 |
+
NBLK = COLS // Q.SRC_BLOCK
|
| 21 |
+
torch.manual_seed(1)
|
| 22 |
+
|
| 23 |
+
raw = (torch.randn(ROWS, COLS) * torch.empty(ROWS, 1).uniform_(0.05, 4.0)).clamp(-448, 448)
|
| 24 |
+
tensors = {
|
| 25 |
+
"layers.1.engram.embed.weight": raw.to(torch.float8_e4m3fn),
|
| 26 |
+
"layers.1.engram.embed.scale": torch.randint(110, 140, (ROWS, NBLK), dtype=torch.uint8).view(torch.float8_e8m0fnu),
|
| 27 |
+
"layers.1.engram.q_weight": torch.randn(4, 5120).bfloat16(),
|
| 28 |
+
"layers.1.engram.k_weight": torch.randn(4, 5120).bfloat16(),
|
| 29 |
+
"layers.1.engram.wkv.weight": torch.randn(256, 6144).to(torch.float8_e4m3fn),
|
| 30 |
+
"layers.1.engram.wkv.scale": torch.randint(120, 132, (8, 192), dtype=torch.uint8).view(torch.float8_e8m0fnu),
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
DEVS = ["cpu"] + (["cuda"] if torch.cuda.is_available() else [])
|
| 34 |
+
with tempfile.TemporaryDirectory() as td:
|
| 35 |
+
src = os.path.join(td, "s.safetensors")
|
| 36 |
+
save_file(tensors, src, metadata={"format": "pt"})
|
| 37 |
+
|
| 38 |
+
for dev in DEVS:
|
| 39 |
+
dst = os.path.join(td, f"o-{dev}.safetensors")
|
| 40 |
+
st = Q.write_engram_shard(src, dst, device=dev, chunk_rows=100_000)
|
| 41 |
+
print(f"\n--- device={dev} --- engram={st['engram']} pass={st['pass']} "
|
| 42 |
+
f"cos={st['cos_sum']/st['cos_n']:.6f}")
|
| 43 |
+
|
| 44 |
+
with safe_open(dst, framework="pt") as g:
|
| 45 |
+
got = set(g.keys())
|
| 46 |
+
want = set(tensors)
|
| 47 |
+
assert got == want, f"key mismatch\n got {sorted(got)}\n want {sorted(want)}"
|
| 48 |
+
w = g.get_tensor("layers.1.engram.embed.weight")
|
| 49 |
+
sc = g.get_tensor("layers.1.engram.embed.scale")
|
| 50 |
+
assert w.dtype == torch.uint8 and tuple(w.shape) == (ROWS, COLS // 2), (w.dtype, w.shape)
|
| 51 |
+
assert sc.dtype == torch.float8_e8m0fnu and tuple(sc.shape) == (ROWS, NBLK)
|
| 52 |
+
for k in ["layers.1.engram.q_weight", "layers.1.engram.k_weight",
|
| 53 |
+
"layers.1.engram.wkv.weight", "layers.1.engram.wkv.scale"]:
|
| 54 |
+
a, b = tensors[k], g.get_tensor(k)
|
| 55 |
+
assert a.dtype == b.dtype and a.shape == b.shape, (k, a.dtype, b.dtype)
|
| 56 |
+
assert bool((a.view(torch.uint8) == b.view(torch.uint8)).all()), f"{k} changed"
|
| 57 |
+
print(f" keys/dtypes/shapes OK; pass-through tensors byte-identical")
|
| 58 |
+
|
| 59 |
+
# compare against the in-RAM path
|
| 60 |
+
with safe_open(src, framework="pt") as f2:
|
| 61 |
+
p2, s2, _, _ = Q.quantize_engram_to_fp4(
|
| 62 |
+
f2, "layers.1.engram.embed.weight", "layers.1.engram.embed.scale",
|
| 63 |
+
ROWS, COLS, chunk_rows=100_000, device=dev)
|
| 64 |
+
assert bool((p2 == w).all()), "streamed weights differ from the in-RAM path"
|
| 65 |
+
assert bool((s2.view(torch.uint8) == sc.view(torch.uint8)).all()), "scales differ"
|
| 66 |
+
print(f" matches the in-RAM path bit-for-bit")
|
| 67 |
+
|
| 68 |
+
print(f" file {os.path.getsize(dst):,} B (source {os.path.getsize(src):,} B)")
|
| 69 |
+
|
| 70 |
+
print("\nSTREAMED ENGRAM SHARD OK")
|