Text Generation
Transformers
Safetensors
qwen3
dllm
diffusion
llm
text_generation
conversational
custom_code
text-generation-inference
Instructions to use GSAI-ML/ReFusion with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use GSAI-ML/ReFusion with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="GSAI-ML/ReFusion", 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("GSAI-ML/ReFusion", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("GSAI-ML/ReFusion", 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=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Inference
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use GSAI-ML/ReFusion with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "GSAI-ML/ReFusion" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "GSAI-ML/ReFusion", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/GSAI-ML/ReFusion
- SGLang
How to use GSAI-ML/ReFusion 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 "GSAI-ML/ReFusion" \ --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": "GSAI-ML/ReFusion", "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 "GSAI-ML/ReFusion" \ --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": "GSAI-ML/ReFusion", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use GSAI-ML/ReFusion with Docker Model Runner:
docker model run hf.co/GSAI-ML/ReFusion
Download modular_qwen3.py from GSAI-ML/ReFusion: direct link, hf CLI and curl.
- Browser
- Download file 7.4 kB
-
https://huggingface.co/GSAI-ML/ReFusion/resolve/main/modular_qwen3.py
- Command line
-
hf download hf://GSAI-ML/ReFusion/modular_qwen3.py
-
curl -L -o modular_qwen3.py https://huggingface.co/GSAI-ML/ReFusion/resolve/main/modular_qwen3.py
7.4 kB
| # coding=utf-8 | |
| # Copyright 2025 The Qwen team, Alibaba Group and the HuggingFace Inc. team. All rights reserved. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| """PyTorch Qwen3 model.""" | |
| from typing import Callable, Optional, Tuple | |
| import torch | |
| import torch.utils.checkpoint | |
| from transformers.cache_utils import Cache | |
| from transformers.modeling_flash_attention_utils import FlashAttentionKwargs | |
| from transformers.modeling_outputs import CausalLMOutputWithPast | |
| from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS | |
| from transformers.processing_utils import Unpack | |
| from transformers.utils import LossKwargs, logging | |
| from ..gemma.modeling_gemma import GemmaMLP | |
| from ..llama.modeling_llama import ( | |
| LlamaAttention, | |
| LlamaDecoderLayer, | |
| LlamaForCausalLM, | |
| LlamaForQuestionAnswering, | |
| LlamaForSequenceClassification, | |
| LlamaForTokenClassification, | |
| LlamaRMSNorm, | |
| apply_rotary_pos_emb, | |
| eager_attention_forward, | |
| ) | |
| from ..mistral.modeling_mistral import MistralModel | |
| from .configuration_qwen3 import Qwen3Config | |
| logger = logging.get_logger(__name__) | |
| _CHECKPOINT_FOR_DOC = "Qwen/Qwen3-8B" | |
| class Qwen3RMSNorm(LlamaRMSNorm): | |
| pass | |
| class Qwen3MLP(GemmaMLP): | |
| pass | |
| class Qwen3Attention(LlamaAttention): | |
| def __init__(self, config: Qwen3Config, layer_idx: int): | |
| super().__init__(config, layer_idx) | |
| self.q_norm = Qwen3RMSNorm(self.head_dim, eps=config.rms_norm_eps) # unlike olmo, only on the head dim! | |
| self.k_norm = Qwen3RMSNorm(self.head_dim, eps=config.rms_norm_eps) # thus post q_norm does not need reshape | |
| self.sliding_window = config.sliding_window | |
| if not ( | |
| self.config.use_sliding_window | |
| and getattr(self.config, "sliding_window", None) is not None | |
| and self.layer_idx >= self.config.max_window_layers | |
| ): | |
| self.sliding_window = None | |
| def forward( | |
| self, | |
| hidden_states: torch.Tensor, | |
| position_embeddings: Tuple[torch.Tensor, torch.Tensor], | |
| attention_mask: Optional[torch.Tensor], | |
| past_key_value: Optional[Cache] = None, | |
| cache_position: Optional[torch.LongTensor] = None, | |
| **kwargs: Unpack[FlashAttentionKwargs], | |
| ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: | |
| input_shape = hidden_states.shape[:-1] | |
| hidden_shape = (*input_shape, -1, self.head_dim) | |
| query_states = self.q_norm(self.q_proj(hidden_states).view(hidden_shape)).transpose(1, 2) | |
| key_states = self.k_norm(self.k_proj(hidden_states).view(hidden_shape)).transpose(1, 2) | |
| value_states = self.v_proj(hidden_states).view(hidden_shape).transpose(1, 2) | |
| cos, sin = position_embeddings | |
| query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin) | |
| if past_key_value is not None: | |
| # sin and cos are specific to RoPE models; cache_position needed for the static cache | |
| cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position} | |
| key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs) | |
| attention_interface: Callable = eager_attention_forward | |
| if self.config._attn_implementation != "eager": | |
| if self.config._attn_implementation == "sdpa" and kwargs.get("output_attentions", False): | |
| logger.warning_once( | |
| "`torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to " | |
| 'eager attention. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.' | |
| ) | |
| else: | |
| attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation] | |
| attn_output, attn_weights = attention_interface( | |
| self, | |
| query_states, | |
| key_states, | |
| value_states, | |
| attention_mask, | |
| dropout=0.0 if not self.training else self.attention_dropout, | |
| scaling=self.scaling, | |
| sliding_window=self.sliding_window, # diff with Llama | |
| **kwargs, | |
| ) | |
| attn_output = attn_output.reshape(*input_shape, -1).contiguous() | |
| attn_output = self.o_proj(attn_output) | |
| return attn_output, attn_weights | |
| class Qwen3DecoderLayer(LlamaDecoderLayer): | |
| def __init__(self, config: Qwen3Config, layer_idx: int): | |
| super().__init__() | |
| self.self_attn = Qwen3Attention(config=config, layer_idx=layer_idx) | |
| self.mlp = Qwen3MLP(config) | |
| if ( | |
| config.sliding_window and config._attn_implementation != "flash_attention_2" | |
| ): # diff with Llama is this warning | |
| logger.warning_once( | |
| f"Sliding Window Attention is enabled but not implemented for `{config._attn_implementation}`; " | |
| "unexpected results may be encountered." | |
| ) | |
| class Qwen3Model(MistralModel): # mistral model creates sliding window | |
| pass | |
| class KwargsForCausalLM(FlashAttentionKwargs, LossKwargs): ... | |
| class Qwen3ForCausalLM(LlamaForCausalLM): | |
| def forward( | |
| self, | |
| **super_kwargs: Unpack[KwargsForCausalLM], | |
| ) -> CausalLMOutputWithPast: | |
| r""" | |
| labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): | |
| Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., | |
| config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored | |
| (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`. | |
| Example: | |
| ```python | |
| >>> from transformers import AutoTokenizer, Qwen3ForCausalLM | |
| >>> model = Qwen3ForCausalLM.from_pretrained("Qwen/Qwen3-8B") | |
| >>> tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-8B") | |
| >>> prompt = "Hey, are you conscious? Can you talk to me?" | |
| >>> inputs = tokenizer(prompt, return_tensors="pt") | |
| >>> # Generate | |
| >>> generate_ids = model.generate(inputs.input_ids, max_length=30) | |
| >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] | |
| "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you." | |
| ```""" | |
| return super().forward(**super_kwargs) | |
| class Qwen3ForSequenceClassification(LlamaForSequenceClassification): | |
| pass | |
| class Qwen3ForTokenClassification(LlamaForTokenClassification): | |
| pass | |
| class Qwen3ForQuestionAnswering(LlamaForQuestionAnswering): | |
| pass | |
| __all__ = [ | |
| "Qwen3ForCausalLM", | |
| "Qwen3ForQuestionAnswering", | |
| "Qwen3Model", | |
| "Qwen3PreTrainedModel", # noqa: F822 | |
| "Qwen3ForSequenceClassification", | |
| "Qwen3ForTokenClassification", | |
| ] | |