Instructions to use chris320211/flatquant-phi3-mini-4k-g52xlarge with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use chris320211/flatquant-phi3-mini-4k-g52xlarge with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="chris320211/flatquant-phi3-mini-4k-g52xlarge", 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("chris320211/flatquant-phi3-mini-4k-g52xlarge", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("chris320211/flatquant-phi3-mini-4k-g52xlarge", 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 chris320211/flatquant-phi3-mini-4k-g52xlarge with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "chris320211/flatquant-phi3-mini-4k-g52xlarge" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "chris320211/flatquant-phi3-mini-4k-g52xlarge", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/chris320211/flatquant-phi3-mini-4k-g52xlarge
- SGLang
How to use chris320211/flatquant-phi3-mini-4k-g52xlarge 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 "chris320211/flatquant-phi3-mini-4k-g52xlarge" \ --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": "chris320211/flatquant-phi3-mini-4k-g52xlarge", "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 "chris320211/flatquant-phi3-mini-4k-g52xlarge" \ --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": "chris320211/flatquant-phi3-mini-4k-g52xlarge", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use chris320211/flatquant-phi3-mini-4k-g52xlarge with Docker Model Runner:
docker model run hf.co/chris320211/flatquant-phi3-mini-4k-g52xlarge
Download quant_agent_inference_adapter.py from chris320211/flatquant-phi3-mini-4k-g52xlarge: direct link, hf CLI and curl.
- Browser
- Download file 2.74 kB
-
https://huggingface.co/chris320211/flatquant-phi3-mini-4k-g52xlarge/resolve/main/quant_agent_inference_adapter.py
- Command line
-
hf download hf://chris320211/flatquant-phi3-mini-4k-g52xlarge/quant_agent_inference_adapter.py
-
curl -L -o quant_agent_inference_adapter.py https://huggingface.co/chris320211/flatquant-phi3-mini-4k-g52xlarge/resolve/main/quant_agent_inference_adapter.py
2.74 kB
| """Reload packed FlatQuant W4A4 weights (Linear4bit + transforms) from the artifact.""" | |
| from __future__ import annotations | |
| import json | |
| import os | |
| import sys | |
| from argparse import Namespace | |
| import torch | |
| from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer | |
| QUANT_AGENT_ADAPTER_API = 1 | |
| def load_model_and_tokenizer(model_path, model_id, dtype, device, trust_remote_code): | |
| method_repo = os.environ.get("QUANT_AGENT_METHOD_REPO") | |
| if method_repo: | |
| sys.path.insert(0, method_repo) | |
| import flatquant.utils as utils | |
| from flatquant.model_tools.phi3_utils import apply_flatquant_to_phi3 | |
| import flatquant.pack_utils as pack_utils | |
| packed_path = os.path.join(model_path, "packed_w4a4.pt") | |
| if not os.path.isfile(packed_path): | |
| raise FileNotFoundError("packed W4A4 artifact missing: %s" % packed_path) | |
| dtypes = {"float16": torch.float16, "bfloat16": torch.bfloat16, "auto": "auto"} | |
| torch_dtype = dtypes.get(dtype, torch.float16) | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| model_path, trust_remote_code=trust_remote_code, local_files_only=True | |
| ) | |
| if tokenizer.pad_token is None: | |
| tokenizer.pad_token = tokenizer.eos_token | |
| config = AutoConfig.from_pretrained( | |
| model_path, trust_remote_code=trust_remote_code, local_files_only=True | |
| ) | |
| config._attn_implementation = "eager" | |
| config._attn_implementation_internal = "eager" | |
| utils.skip_initialization() | |
| model = AutoModelForCausalLM.from_config( | |
| config, | |
| torch_dtype=torch_dtype if device == "cuda" else torch.float32, | |
| trust_remote_code=trust_remote_code, | |
| attn_implementation="eager", | |
| ) | |
| model.seqlen = 2048 | |
| with open(os.path.join(model_path, "flatquant_args.json"), "r", encoding="utf-8") as handle: | |
| args = Namespace(**json.load(handle)) | |
| model = apply_flatquant_to_phi3(args, model) | |
| for layer in model.model.layers: | |
| layer.self_attn.rep_matrix_only() | |
| layer.mlp.rep_matrix_only() | |
| pack_utils.prepare_packed_slots(model) | |
| payload = torch.load(packed_path, map_location="cpu", weights_only=False) | |
| missing, unexpected = model.load_state_dict(payload, strict=False) | |
| packed_missing = [key for key in missing if "linear.weight" in key or key.endswith("a4.clip_factor_a_max")] | |
| if packed_missing: | |
| raise RuntimeError( | |
| "packed_w4a4.pt did not restore Linear4bit weights: missing=%s unexpected=%s" | |
| % (packed_missing[:12], list(unexpected)[:12]) | |
| ) | |
| pack_utils.cast_linear4bit_kernel_dtypes(model) | |
| pack_utils.restore_packed_eval_runtime(model) | |
| model.to(device) | |
| pack_utils.cast_linear4bit_kernel_dtypes(model) | |
| model.eval() | |
| return model, tokenizer | |