Image-Text-to-Text
Transformers
Safetensors
lfm3_vl
feature-extraction
liquid
lfm3
edge
decision
classification
extraction
calibration
system-one
multimodal
decision-model
custom_code
conversational
Eval Results
Instructions to use LiquidAI/d1-2B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use LiquidAI/d1-2B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="LiquidAI/d1-2B", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModel processor = AutoProcessor.from_pretrained("LiquidAI/d1-2B", trust_remote_code=True) model = AutoModel.from_pretrained("LiquidAI/d1-2B", trust_remote_code=True, device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use LiquidAI/d1-2B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "LiquidAI/d1-2B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "LiquidAI/d1-2B", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/LiquidAI/d1-2B
- SGLang
How to use LiquidAI/d1-2B 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 "LiquidAI/d1-2B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "LiquidAI/d1-2B", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'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 "LiquidAI/d1-2B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "LiquidAI/d1-2B", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use LiquidAI/d1-2B with Docker Model Runner:
docker model run hf.co/LiquidAI/d1-2B
Commit ·
a6bce77
1
Parent(s): 652f920
Load the repo's own LFM3 classes on transformers 5.23 (#8)
Browse files- Load the repo's own LFM3 classes on transformers 5.23 (dcd5c0cc4c1327b4d5554fcdb5889e9da19d4a95)
- native_lfm3.py +14 -14
- native_lfm3_vl.py +15 -17
- runner.py +9 -4
native_lfm3.py
CHANGED
|
@@ -1,24 +1,24 @@
|
|
| 1 |
"""LFM3's transformers classes, one module a model type: `native_lfm3` (text), `native_lfm3_vl` (pictures),
|
| 2 |
`native_lfm3_omni` (pictures and audio), each importing the one before it, so a Hub repository serves only its
|
| 3 |
-
model's files.
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
from __future__ import annotations
|
| 11 |
|
| 12 |
-
|
| 13 |
-
from transformers import Lfm3Config, Lfm3ForCausalLM, Lfm3Model
|
| 14 |
-
except ImportError:
|
| 15 |
-
from transformers import AutoConfig, AutoModel
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
__all__ = ["Lfm3Config", "Lfm3ForCausalLM", "Lfm3Model"]
|
|
|
|
| 1 |
"""LFM3's transformers classes, one module a model type: `native_lfm3` (text), `native_lfm3_vl` (pictures),
|
| 2 |
`native_lfm3_omni` (pictures and audio), each importing the one before it, so a Hub repository serves only its
|
| 3 |
+
model's files. The classes are the copies of transformers' LFM3 code beside these (each names its commit), on every
|
| 4 |
+
transformers: the released weights, the recorded losses and `lfm3.lfm3_conv` were made on them. transformers ships
|
| 5 |
+
LFM3 from 5.23 (`lfm3`, `lfm3_vl`) as other code: a torch-only convolution, another picture processor and
|
| 6 |
+
placeholder. So the copies are registered with the Auto classes over transformers' own: a checkpoint's config and
|
| 7 |
+
processor load as these, and the composite models build their parts from these.
|
| 8 |
|
| 9 |
+
Moving to transformers' classes is a migration of its own, checked for parity on the released weights; until then
|
| 10 |
+
the copies (the files headed "Vendored from") stay."""
|
| 11 |
|
| 12 |
from __future__ import annotations
|
| 13 |
|
| 14 |
+
from transformers import AutoConfig, AutoModel
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
from .configuration_lfm3 import Lfm3Config
|
| 17 |
+
from .modeling_lfm3 import Lfm3ForCausalLM, Lfm3Model
|
| 18 |
|
| 19 |
+
# exist_ok: over transformers' class of the model type, and a second copy of these files in one process (two
|
| 20 |
+
# snapshots) registers again. A model mapping is read by config class, so the copies' configs give the copies' models.
|
| 21 |
+
AutoConfig.register(Lfm3Config.model_type, Lfm3Config, exist_ok=True)
|
| 22 |
+
AutoModel.register(Lfm3Config, Lfm3Model, exist_ok=True) # the composite models build their text model by config
|
| 23 |
|
| 24 |
__all__ = ["Lfm3Config", "Lfm3ForCausalLM", "Lfm3Model"]
|
native_lfm3_vl.py
CHANGED
|
@@ -2,24 +2,22 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
try:
|
| 8 |
-
from transformers import Lfm3VlConfig, Lfm3VlForConditionalGeneration, Lfm3VlProcessor
|
| 9 |
-
except ImportError:
|
| 10 |
-
from transformers import AutoConfig, AutoImageProcessor, AutoModel, AutoProcessor, AutoVideoProcessor
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
|
| 25 |
__all__ = ["Lfm3VlConfig", "Lfm3VlForConditionalGeneration", "Lfm3VlProcessor"]
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
+
from transformers import AutoConfig, AutoImageProcessor, AutoModel, AutoProcessor, AutoVideoProcessor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
from .configuration_lfm3_vl import Lfm3VlConfig, Lfm3VlVisionConfig
|
| 8 |
+
from .image_processing_lfm3_vl import Lfm3VlImageProcessor
|
| 9 |
+
from .modeling_lfm3_vl import Lfm3VlForConditionalGeneration, Lfm3VlVisionModel
|
| 10 |
+
from .native_lfm3 import Lfm3Model # noqa: F401 (registered: the VL builds its text model by config)
|
| 11 |
+
from .processing_lfm3_vl import Lfm3VlProcessor
|
| 12 |
+
from .video_processing_lfm3_vl import Lfm3VlVideoProcessor
|
| 13 |
|
| 14 |
+
for config in (Lfm3VlConfig, Lfm3VlVisionConfig):
|
| 15 |
+
AutoConfig.register(config.model_type, config, exist_ok=True)
|
| 16 |
+
# the tower, built by config. Never the VL itself: a class registered for its config would load in place of the
|
| 17 |
+
# Hub class a repository's `auto_map` names
|
| 18 |
+
AutoModel.register(Lfm3VlVisionConfig, Lfm3VlVisionModel, exist_ok=True)
|
| 19 |
+
AutoImageProcessor.register(Lfm3VlConfig, fast_image_processor_class=Lfm3VlImageProcessor, exist_ok=True)
|
| 20 |
+
AutoVideoProcessor.register(Lfm3VlConfig, Lfm3VlVideoProcessor, exist_ok=True)
|
| 21 |
+
AutoProcessor.register(Lfm3VlConfig, Lfm3VlProcessor, exist_ok=True) # read by `runner.auto_processor`
|
| 22 |
|
| 23 |
__all__ = ["Lfm3VlConfig", "Lfm3VlForConditionalGeneration", "Lfm3VlProcessor"]
|
runner.py
CHANGED
|
@@ -70,10 +70,15 @@ def remote_ok(model_id) -> bool:
|
|
| 70 |
|
| 71 |
|
| 72 |
def auto_processor(path: str, revision: str | None = None):
|
| 73 |
-
"""The media processor a checkpoint holds.
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
def _under(config: dict | None, compiled):
|
|
|
|
| 70 |
|
| 71 |
|
| 72 |
def auto_processor(path: str, revision: str | None = None):
|
| 73 |
+
"""The media processor a checkpoint holds. A model type whose classes are registered from outside transformers
|
| 74 |
+
(copies served beside this file) takes the processor registered with them: by name, `AutoProcessor` finds
|
| 75 |
+
transformers' class of that name first."""
|
| 76 |
+
from transformers import CONFIG_MAPPING, PROCESSOR_MAPPING, AutoProcessor, PretrainedConfig
|
| 77 |
+
|
| 78 |
+
kind = PretrainedConfig.get_config_dict(path, revision=revision)[0].get("model_type")
|
| 79 |
+
own = PROCESSOR_MAPPING.get(CONFIG_MAPPING[kind], None) if kind in CONFIG_MAPPING else None
|
| 80 |
+
cls = AutoProcessor if own is None or own.__module__.startswith("transformers.") else own
|
| 81 |
+
return cls.from_pretrained(path, revision=revision, trust_remote_code=remote_ok(path))
|
| 82 |
|
| 83 |
|
| 84 |
def _under(config: dict | None, compiled):
|