Text Generation
Transformers
Safetensors
English
qwen2
sdlm
diffusion language model
custom_code
conversational
text-generation-inference
Instructions to use OpenGVLab/SDLM-32B-D4 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use OpenGVLab/SDLM-32B-D4 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="OpenGVLab/SDLM-32B-D4", 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("OpenGVLab/SDLM-32B-D4", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("OpenGVLab/SDLM-32B-D4", 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 OpenGVLab/SDLM-32B-D4 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "OpenGVLab/SDLM-32B-D4" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "OpenGVLab/SDLM-32B-D4", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/OpenGVLab/SDLM-32B-D4
- SGLang
How to use OpenGVLab/SDLM-32B-D4 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 "OpenGVLab/SDLM-32B-D4" \ --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": "OpenGVLab/SDLM-32B-D4", "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 "OpenGVLab/SDLM-32B-D4" \ --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": "OpenGVLab/SDLM-32B-D4", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use OpenGVLab/SDLM-32B-D4 with Docker Model Runner:
docker model run hf.co/OpenGVLab/SDLM-32B-D4
Update attn_mask_utils.py
Browse files29 unmodified lines3031323334353659 unmodified lines9697989910010129 unmodified linesreturn seq_lendef update_causal_mask_with_pad_non_visible_2d(input_ids: torch.Tensor,attn_mask_2d: torch.Tensor,59 unmodified linesreturn attn_mask_2ddef update_causal_mask_for_one_gen_window_2d(input_ids: torch.Tensor,attn_mask_2d: torch.Tensor,29 unmodified lines30313233343559 unmodified lines959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015129 unmodified linesreturn seq_lendef update_causal_mask_with_pad_non_visible_2d(input_ids: torch.Tensor,attn_mask_2d: torch.Tensor,59 unmodified linesreturn attn_mask_2ddef update_causal_mask_with_pad_non_visible_2d_for_ssd_cache(input_ids: torch.Tensor,attn_mask_2d: torch.Tensor,block_size: int = 4,use_cache: bool = True,causal_attn: bool = False) -> torch.Tensor:"""Updates a 2D attention mask for Self-Speculative Decoding generateDetails is avaliabe in Appendix B Figure 5.Args:input_ids: Input token IDs (unused in current implementation)attn_mask_2d: 2D attention mask matrix of shape [seq_len, seq_len] where:- 0.0 indicates allowed attention- -inf indicates masked attentionblock_size: Size of the diffusion windowuse_cache: Whether key-value cache is being usedcausal_attn: If True, maintains strict causal masking throughoutReturns:Modified attention mask with updated visibility patterns"""q_len, kv_len = attn_mask_2d.shapeif q_len == kv_len:# prefillreturn update_causal_mask_for_one_gen_window_2d(input_ids = input_ids,attn_mask_2d = attn_mask_2d,block_size = block_size,use_cache = use_cache,causal_attn = causal_attn)start_ix = q_len - block_sizestart_jx = kv_len - block_sizefor ix in range(block_size-1, -1, -1):attn_mask_2d[start_ix:start_ix+block_size, start_jx:start_jx+block_size] = 0.0attn_mask_2d[start_ix+block_size:, start_jx-ix:start_jx+block_size] = -float('inf')start_ix = start_ix - ix - block_sizestart_jx = start_jx - ix - block_sizeattn_mask_2d[start_ix+block_size:, start_jx+block_size-1] = -float('inf')return attn_mask_2ddef update_causal_mask_for_one_gen_window_2d(input_ids: torch.Tensor,attn_mask_2d: torch.Tensor,
attn_mask_utils.py+51-1