Text Generation
Transformers
Safetensors
llada2_moe
dllm
diffusion
llm
text_generation
conversational
custom_code
Instructions to use inclusionAI/LLaDA2.1-mini with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use inclusionAI/LLaDA2.1-mini with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="inclusionAI/LLaDA2.1-mini", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("inclusionAI/LLaDA2.1-mini", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use inclusionAI/LLaDA2.1-mini with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "inclusionAI/LLaDA2.1-mini" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "inclusionAI/LLaDA2.1-mini", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/inclusionAI/LLaDA2.1-mini
- SGLang
How to use inclusionAI/LLaDA2.1-mini 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 "inclusionAI/LLaDA2.1-mini" \ --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": "inclusionAI/LLaDA2.1-mini", "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 "inclusionAI/LLaDA2.1-mini" \ --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": "inclusionAI/LLaDA2.1-mini", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use inclusionAI/LLaDA2.1-mini with Docker Model Runner:
docker model run hf.co/inclusionAI/LLaDA2.1-mini
fixes for transforemrs v5
#4
by kashif HF Staff - opened
27 unmodified lines28293031323334353655 unmodified lines9293949596979899100101102103104105106107108109110111112113114115553 unmodified lines669670671672673674675676677678679680681682683684178 unmodified lines86386486586686786886987087187287387487587687787887927 unmodified linesfrom transformers.activations import ACT2FNfrom transformers.cache_utils import Cache, DynamicCachefrom transformers.modeling_attn_mask_utils import (_prepare_4d_causal_attention_mask_for_sdpa,)from transformers.modeling_outputs import (MoeModelOutputWithPast,MoeCausalLMOutputWithPast,55 unmodified linesclass LLaDA2MoeRotaryEmbedding(nn.Module):def __init__(self, config: LLaDA2MoeConfig, device=None):super().__init__()# BC: "rope_type" was originally "type"if hasattr(config, "rope_scaling") and config.rope_scaling is not None:self.rope_type = config.rope_scaling.get("rope_type", config.rope_scaling.get("type"))else:self.rope_type = "default"self.max_seq_len_cached = config.max_position_embeddingsself.original_max_seq_len = config.max_position_embeddingsself.config = configself.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device)self.register_buffer("inv_freq", inv_freq, persistent=False)self.original_inv_freq = self.inv_freq@torch.no_grad()@dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope)553 unmodified lines_supports_flex_attn = True_supports_cache_class = Truedef _init_weights(self, module):std = self.config.initializer_rangeif isinstance(module, nn.Linear):module.weight.data.normal_(mean=0.0, std=std)if module.bias is not None:module.bias.data.zero_()elif isinstance(module, nn.Embedding):module.weight.data.normal_(mean=0.0, std=std)if module.padding_idx is not None:module.weight.data[module.padding_idx].zero_()LLADA2MOE_INPUTS_DOCSTRING = r"""178 unmodified linesdevice=inputs_embeds.device,)position_ids = position_ids.unsqueeze(0)if attention_mask.size() == (batch_size, 1, seq_length, seq_length):attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(attention_mask,(batch_size, seq_length),inputs_embeds,past_seen_tokens,)else:raise ValueError(f"LLaDA2.0 only support block attention mask with shape: {(batch_size, 1, seq_length, seq_length)}, the input attention with shape {attention_mask.size()=}!")# embed positionshidden_states = inputs_embeds27 unmodified lines2829303132333455 unmodified lines90919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130553 unmodified lines684685686687688689690691692693694695178 unmodified lines87487587687787887988088188288388427 unmodified linesfrom transformers.activations import ACT2FNfrom transformers.cache_utils import Cache, DynamicCachefrom transformers.masking_utils import create_bidirectional_maskfrom transformers.modeling_outputs import (MoeModelOutputWithPast,MoeCausalLMOutputWithPast,55 unmodified linesclass LLaDA2MoeRotaryEmbedding(nn.Module):inv_freq: torch.Tensor # fix linting for register_bufferdef __init__(self, config: LLaDA2MoeConfig, device=None):super().__init__()self.max_seq_len_cached = config.max_position_embeddingsself.original_max_seq_len = config.max_position_embeddingsself.config = configself.rope_type = self.config.rope_parameters["rope_type"]rope_init_fn: Callable = self.compute_default_rope_parametersif self.rope_type != "default":rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]inv_freq, self.attention_scaling = rope_init_fn(self.config, device)self.register_buffer("inv_freq", inv_freq, persistent=False)self.register_buffer("original_inv_freq", inv_freq.clone(), persistent=False)@staticmethoddef compute_default_rope_parameters(config: LLaDA2MoeConfig = None,device=None,seq_len: int = None,):base = config.rope_parameters["rope_theta"]partial_rotary_factor = config.rope_parameters.get("partial_rotary_factor", 1.0)head_dim = getattr(config, "head_dim", None) or config.hidden_size // config.num_attention_headsdim = int(head_dim * partial_rotary_factor)attention_factor = 1.0 # Unused in this type of RoPEinv_freq = 1.0 / (base ** (torch.arange(0, dim, 2, dtype=torch.int64).to(device=device, dtype=torch.float) / dim))return inv_freq, attention_factor@torch.no_grad()@dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope)553 unmodified lines_supports_flex_attn = True_supports_cache_class = True@torch.no_grad()def _init_weights(self, module):super()._init_weights(module)std = self.config.initializer_rangeif isinstance(module, LLaDA2MoeGate):nn.init.normal_(module.weight, mean=0.0, std=std)LLADA2MOE_INPUTS_DOCSTRING = r"""178 unmodified linesdevice=inputs_embeds.device,)position_ids = position_ids.unsqueeze(0)attention_mask = create_bidirectional_mask(config=self.config,inputs_embeds=inputs_embeds,attention_mask=attention_mask,)# embed positionshidden_states = inputs_embeds
modeling_llada2_moe.py+37-32