Text Generation
Transformers
Safetensors
lafa
feature-extraction
qwen2
flow-matching
speculative-decoding
custom_code
conversational
Instructions to use lafa-hf/lafa-7B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use lafa-hf/lafa-7B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="lafa-hf/lafa-7B", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("lafa-hf/lafa-7B", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use lafa-hf/lafa-7B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "lafa-hf/lafa-7B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "lafa-hf/lafa-7B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/lafa-hf/lafa-7B
- SGLang
How to use lafa-hf/lafa-7B 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 "lafa-hf/lafa-7B" \ --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": "lafa-hf/lafa-7B", "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 "lafa-hf/lafa-7B" \ --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": "lafa-hf/lafa-7B", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use lafa-hf/lafa-7B with Docker Model Runner:
docker model run hf.co/lafa-hf/lafa-7B
Download cache.py from lafa-hf/lafa-7B: direct link, hf CLI and curl.
- Browser
- Download file 3.23 kB
-
https://huggingface.co/lafa-hf/lafa-7B/resolve/main/cache.py
- Command line
-
hf download hf://lafa-hf/lafa-7B/cache.py
-
curl -L -o cache.py https://huggingface.co/lafa-hf/lafa-7B/resolve/main/cache.py
3.23 kB
| """KV cache utilities for inference.""" | |
| def trim_kv_cache(past_kv, n_trim: int): | |
| """Remove the last n_trim entries from a KV cache. | |
| Supports DynamicCache (.crop), older DynamicCache (.key_cache), | |
| and legacy tuple-of-tuples format. | |
| """ | |
| if n_trim <= 0: | |
| return past_kv | |
| if hasattr(past_kv, "crop"): | |
| past_kv.crop(past_kv.get_seq_length() - n_trim) | |
| return past_kv | |
| elif hasattr(past_kv, "key_cache"): | |
| for i in range(len(past_kv.key_cache)): | |
| past_kv.key_cache[i] = past_kv.key_cache[i][:, :, :-n_trim, :] | |
| past_kv.value_cache[i] = past_kv.value_cache[i][:, :, :-n_trim, :] | |
| if hasattr(past_kv, "_seen_tokens"): | |
| past_kv._seen_tokens -= n_trim | |
| return past_kv | |
| else: | |
| return tuple( | |
| (kc[:, :, :-n_trim, :], vc[:, :, :-n_trim, :]) for kc, vc in past_kv | |
| ) | |
| def gather_kv_cache(past_kv, keep_index): | |
| """Keep only the sequence positions in `keep_index`, dropping all others. | |
| `keep_index` is a 1-D LongTensor of cache positions to retain, in the order | |
| they should appear afterwards. Unlike trim_kv_cache (which only drops a | |
| contiguous tail), this index-selects along the sequence dim, so it can drop | |
| *interior* entries. Self-speculative decoding uses it to compact a fused | |
| [draft | noise-block] cache down to prefix+accepted tokens without paying for | |
| a re-encode forward: the accepted draft tokens attended only to the prefix | |
| and earlier accepted tokens, so their cached K/V are byte-identical to a | |
| plain causal decode and can be kept verbatim. | |
| Supports DynamicCache (.key_cache/.value_cache lists or newer .layers) and | |
| the legacy tuple-of-tuples format. | |
| """ | |
| n_keep = int(keep_index.numel()) | |
| # DynamicCache with key_cache / value_cache lists. | |
| if hasattr(past_kv, "key_cache") and getattr(past_kv, "key_cache"): | |
| for i in range(len(past_kv.key_cache)): | |
| k = past_kv.key_cache[i] | |
| if k is None or k.numel() == 0: | |
| continue | |
| idx = keep_index.to(k.device) | |
| past_kv.key_cache[i] = k.index_select(2, idx).contiguous() | |
| past_kv.value_cache[i] = ( | |
| past_kv.value_cache[i].index_select(2, idx).contiguous() | |
| ) | |
| if hasattr(past_kv, "_seen_tokens"): | |
| past_kv._seen_tokens = n_keep | |
| return past_kv | |
| # Newer DynamicCache: per-layer objects exposing .keys / .values. | |
| if hasattr(past_kv, "layers") and getattr(past_kv, "layers"): | |
| for layer in past_kv.layers: | |
| k = getattr(layer, "keys", None) | |
| if k is None or k.numel() == 0: | |
| continue | |
| idx = keep_index.to(k.device) | |
| layer.keys = k.index_select(2, idx).contiguous() | |
| layer.values = layer.values.index_select(2, idx).contiguous() | |
| if hasattr(past_kv, "_seen_tokens"): | |
| past_kv._seen_tokens = n_keep | |
| return past_kv | |
| # Legacy tuple-of-tuples. | |
| return tuple( | |
| ( | |
| kc.index_select(2, keep_index.to(kc.device)).contiguous(), | |
| vc.index_select(2, keep_index.to(vc.device)).contiguous(), | |
| ) | |
| for kc, vc in past_kv | |
| ) | |