Text Generation
Transformers
Safetensors
deepseek_v3
conversational
custom_code
Eval Results
text-generation-inference
fp8
Instructions to use deepseek-ai/DeepSeek-R1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use deepseek-ai/DeepSeek-R1 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-R1", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-R1", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("deepseek-ai/DeepSeek-R1", trust_remote_code=True) 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=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use deepseek-ai/DeepSeek-R1 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "deepseek-ai/DeepSeek-R1" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "deepseek-ai/DeepSeek-R1", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/deepseek-ai/DeepSeek-R1
- SGLang
How to use deepseek-ai/DeepSeek-R1 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 "deepseek-ai/DeepSeek-R1" \ --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": "deepseek-ai/DeepSeek-R1", "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 "deepseek-ai/DeepSeek-R1" \ --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": "deepseek-ai/DeepSeek-R1", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use deepseek-ai/DeepSeek-R1 with Docker Model Runner:
docker model run hf.co/deepseek-ai/DeepSeek-R1
Update config.json
#160
by jana0010 - opened
- config.json +52 -70
config.json
CHANGED
|
@@ -1,70 +1,52 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
"mscale": 1.0,
|
| 54 |
-
"mscale_all_dim": 1.0,
|
| 55 |
-
"original_max_position_embeddings": 4096,
|
| 56 |
-
"type": "yarn"
|
| 57 |
-
},
|
| 58 |
-
"rope_theta": 10000,
|
| 59 |
-
"routed_scaling_factor": 2.5,
|
| 60 |
-
"scoring_func": "sigmoid",
|
| 61 |
-
"seq_aux": true,
|
| 62 |
-
"tie_word_embeddings": false,
|
| 63 |
-
"topk_group": 4,
|
| 64 |
-
"topk_method": "noaux_tc",
|
| 65 |
-
"torch_dtype": "bfloat16",
|
| 66 |
-
"transformers_version": "4.46.3",
|
| 67 |
-
"use_cache": true,
|
| 68 |
-
"v_head_dim": 128,
|
| 69 |
-
"vocab_size": 129280
|
| 70 |
-
}
|
|
|
|
| 1 |
+
def load_model_with_quantization_fallback(
|
| 2 |
+
model_name: str = "deepseek-ai/DeepSeek-R1",
|
| 3 |
+
trust_remote_code: bool = True,
|
| 4 |
+
device_map: Optional[Union[str, Dict[str, Any]]] = "auto",
|
| 5 |
+
**kwargs
|
| 6 |
+
) -> Tuple[PreTrainedModel, PreTrainedTokenizer]:
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
model = AutoModel.from_pretrained(
|
| 10 |
+
model_name,
|
| 11 |
+
trust_remote_code=trust_remote_code,
|
| 12 |
+
device_map=device_map,
|
| 13 |
+
**kwargs
|
| 14 |
+
)
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 16 |
+
logger.info("Model loaded successfully with original configuration")
|
| 17 |
+
return model, tokenizer
|
| 18 |
+
except ValueError as e:
|
| 19 |
+
if "Unknown quantization type" in str(e):
|
| 20 |
+
logger.warning(
|
| 21 |
+
"Quantization type not supported directly. "
|
| 22 |
+
"Attempting to load without quantization..."
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
config = AutoConfig.from_pretrained(
|
| 26 |
+
model_name,
|
| 27 |
+
trust_remote_code=trust_remote_code
|
| 28 |
+
)
|
| 29 |
+
if hasattr(config, "quantization_config"):
|
| 30 |
+
delattr(config, "quantization_config")
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
model = AutoModel.from_pretrained(
|
| 34 |
+
model_name,
|
| 35 |
+
config=config,
|
| 36 |
+
trust_remote_code=trust_remote_code,
|
| 37 |
+
device_map=device_map,
|
| 38 |
+
**kwargs
|
| 39 |
+
)
|
| 40 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 41 |
+
model_name,
|
| 42 |
+
trust_remote_code=trust_remote_code
|
| 43 |
+
)
|
| 44 |
+
logger.info("Model loaded successfully without quantization")
|
| 45 |
+
return model, tokenizer
|
| 46 |
+
|
| 47 |
+
except Exception as inner_e:
|
| 48 |
+
logger.error(f"Failed to load model without quantization: {str(inner_e)}")
|
| 49 |
+
raise
|
| 50 |
+
else:
|
| 51 |
+
logger.error(f"Unexpected error during model loading: {str(e)}")
|
| 52 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|