Instructions to use tiny-random/lfm3 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use tiny-random/lfm3 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="tiny-random/lfm3", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("tiny-random/lfm3", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("tiny-random/lfm3", trust_remote_code=True, device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.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(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use tiny-random/lfm3 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "tiny-random/lfm3" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tiny-random/lfm3", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/tiny-random/lfm3
- SGLang
How to use tiny-random/lfm3 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 "tiny-random/lfm3" \ --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": "tiny-random/lfm3", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'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 "tiny-random/lfm3" \ --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": "tiny-random/lfm3", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use tiny-random/lfm3 with Docker Model Runner:
docker model run hf.co/tiny-random/lfm3
Download .meta/create_model.py from tiny-random/lfm3: direct link, hf CLI and curl.
- Browser
- Download file 2.41 kB
-
https://huggingface.co/tiny-random/lfm3/resolve/main/.meta/create_model.py
- Command line
-
hf download hf://tiny-random/lfm3/.meta/create_model.py
-
curl -L -o create_model.py https://huggingface.co/tiny-random/lfm3/resolve/main/.meta/create_model.py
2.41 kB
| def create_model(source_model_id: str, source_revision: str, save_folder: str) -> str: | |
| import json | |
| from pathlib import Path | |
| import torch | |
| from huggingface_hub import hf_hub_download | |
| from transformers import AutoConfig, AutoModelForCausalLM, set_seed | |
| folder = Path(save_folder) | |
| folder.mkdir(parents=True, exist_ok=True) | |
| # LFM3 is not registered in the installed Transformers release; ship its pinned | |
| # remote code so the checkpoint loads without a newer package. | |
| for name in ( | |
| "configuration_lfm3.py", | |
| "modeling_lfm3.py", | |
| "lfm3_conv.py", | |
| "chat_template.jinja", | |
| "generation_config.json", | |
| "special_tokens_map.json", | |
| "tokenizer.json", | |
| "tokenizer_config.json", | |
| "LICENSE", | |
| ): | |
| (folder / name).write_bytes(Path(hf_hub_download(source_model_id, name, revision=source_revision)).read_bytes()) | |
| config = json.loads(Path(hf_hub_download(source_model_id, "config.json", revision=source_revision)).read_text()) | |
| # Keep the two-conv prefix and two complete full/conv/sliding/conv cycles. | |
| # Match tiny-random/lfm2's residual/MLP widths; retain LFM3's 4:1 Q/KV ratio. | |
| config.update( | |
| hidden_size=64, | |
| intermediate_size=128, | |
| num_hidden_layers=10, | |
| num_attention_heads=4, | |
| num_key_value_heads=1, | |
| head_dim=32, | |
| max_position_embeddings=128000, | |
| ) | |
| config["layer_types"] = config["layer_types"][:10] | |
| (folder / "config.json").write_text(json.dumps(config, indent=2) + "\n") | |
| set_seed(42) | |
| model = AutoModelForCausalLM.from_config( | |
| AutoConfig.from_pretrained(folder, trust_remote_code=True), trust_remote_code=True, dtype=torch.bfloat16 | |
| ) | |
| model.save_pretrained(folder) | |
| # `save_pretrained()` rewrites these two files with this environment's | |
| # versions; restore the pinned source bytes plus the deliberate config edits | |
| # so only the intended fields differ from the source. | |
| (folder / "config.json").write_text(json.dumps(config, indent=2) + "\n") | |
| (folder / "generation_config.json").write_bytes( | |
| Path(hf_hub_download(source_model_id, "generation_config.json", revision=source_revision)).read_bytes() | |
| ) | |
| return str(model) | |
| create_model( | |
| source_model_id="LiquidAI/LFM3-1.7B", | |
| source_revision="ac2586a34f1a1c03329dbdcdefabd8b6478ddaa7", | |
| save_folder="/tmp/lfm3", | |
| ) | |