Text Generation
Transformers
Safetensors
English
expivme_diffusion
feature-extraction
language-model
transformer
rope
swiglu
diffusion
masked-diffusion
discrete-diffusion
instruction-tuned
conversational
tiny
small
experimental
custom_code
Instructions to use IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct", trust_remote_code=True) messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct
- SGLang
How to use IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct 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 "IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct" \ --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": "IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct", "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 "IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct" \ --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": "IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct with Docker Model Runner:
docker model run hf.co/IvmeLabs/ExpIvme-DiffusionConversate-v1-Instruct
File size: 7,151 Bytes
3fcf7dd | 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | """HuggingFace Transformers model for ExpIvme-DiffusionConversate-v1-Instruct (SFT)."""
from dataclasses import dataclass
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.attention import SDPBackend, sdpa_kernel
from transformers import PreTrainedModel
from transformers.modeling_outputs import ModelOutput
try:
from .configuration_expivme_diffusion import ExpIvmeDiffusionConfig
except ImportError:
from configuration_expivme_diffusion import ExpIvmeDiffusionConfig
if torch.cuda.is_available() and torch.cuda.get_device_capability(0)[0] >= 8:
PREFERRED_SDPA_BACKENDS = [SDPBackend.CUDNN_ATTENTION, SDPBackend.FLASH_ATTENTION, SDPBackend.EFFICIENT_ATTENTION, SDPBackend.MATH]
else:
PREFERRED_SDPA_BACKENDS = [SDPBackend.EFFICIENT_ATTENTION, SDPBackend.MATH]
def _precompute_rope_freqs(head_dim, max_seq_len, theta, device=None):
freqs = 1.0 / (theta ** (torch.arange(0, head_dim, 2, device=device).float() / head_dim))
positions = torch.arange(max_seq_len, device=device).float()
angles = torch.outer(positions, freqs)
return torch.cos(angles), torch.sin(angles)
def _apply_rope(x, rope_cos_sin):
cos, sin = rope_cos_sin
B, H, T, D = x.shape
x1 = x[..., 0::2]
x2 = x[..., 1::2]
cos = cos.view(1, 1, T, D // 2).to(x.dtype)
sin = sin.view(1, 1, T, D // 2).to(x.dtype)
out1 = x1 * cos - x2 * sin
out2 = x1 * sin + x2 * cos
return torch.stack([out1, out2], dim=-1).reshape(B, H, T, D).type_as(x)
class ExpIvmeRMSNorm(nn.Module):
def __init__(self, dim, eps=1e-5):
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
def forward(self, x):
dtype = x.dtype
x = x.float()
rms = torch.rsqrt(x.pow(2).mean(dim=-1, keepdim=True) + self.eps)
return (x * rms).to(dtype) * self.weight
class ExpIvmeSelfAttention(nn.Module):
def __init__(self, hidden_dim, n_heads, dropout=0.0):
super().__init__()
self.n_heads = n_heads
self.head_dim = hidden_dim // n_heads
self.dropout = dropout
self.q_proj = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.k_proj = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.v_proj = nn.Linear(hidden_dim, hidden_dim, bias=False)
self.out_proj = nn.Linear(hidden_dim, hidden_dim, bias=False)
def forward(self, x, rope_freqs, attn_mask=None):
B, T, C = x.shape
q = self.q_proj(x).view(B, T, self.n_heads, self.head_dim).transpose(1, 2)
k = self.k_proj(x).view(B, T, self.n_heads, self.head_dim).transpose(1, 2)
v = self.v_proj(x).view(B, T, self.n_heads, self.head_dim).transpose(1, 2)
q = _apply_rope(q, rope_freqs)
k = _apply_rope(k, rope_freqs)
with sdpa_kernel(PREFERRED_SDPA_BACKENDS):
out = F.scaled_dot_product_attention(
q, k, v, attn_mask=attn_mask, is_causal=False,
dropout_p=self.dropout if self.training else 0.0,
)
out = out.transpose(1, 2).contiguous().view(B, T, C)
return self.out_proj(out)
class ExpIvmeSwiGLU(nn.Module):
def __init__(self, hidden_dim, ffn_mult):
super().__init__()
inner_dim = int(hidden_dim * ffn_mult * 2 / 3)
inner_dim = ((inner_dim + 7) // 8) * 8
self.gate_proj = nn.Linear(hidden_dim, inner_dim, bias=False)
self.up_proj = nn.Linear(hidden_dim, inner_dim, bias=False)
self.down_proj = nn.Linear(inner_dim, hidden_dim, bias=False)
def forward(self, x):
return self.down_proj(F.silu(self.gate_proj(x)) * self.up_proj(x))
class ExpIvmeBlock(nn.Module):
def __init__(self, hidden_dim, n_heads, ffn_mult, norm_eps, dropout=0.0):
super().__init__()
self.attn_norm = ExpIvmeRMSNorm(hidden_dim, eps=norm_eps)
self.attn = ExpIvmeSelfAttention(hidden_dim, n_heads, dropout)
self.ffn_norm = ExpIvmeRMSNorm(hidden_dim, eps=norm_eps)
self.ffn = ExpIvmeSwiGLU(hidden_dim, ffn_mult)
def forward(self, x, rope_freqs, attn_mask=None):
x = x + self.attn(self.attn_norm(x), rope_freqs, attn_mask=attn_mask)
x = x + self.ffn(self.ffn_norm(x))
return x
@dataclass
class DiffusionLMOutput(ModelOutput):
loss: torch.FloatTensor = None
logits: torch.FloatTensor = None
class ExpIvmeForDiffusionLMHub(PreTrainedModel):
config_class = ExpIvmeDiffusionConfig
base_model_prefix = "model"
_tied_weights_keys = {"lm_head.weight": "model.tok_embed.weight"}
def __init__(self, config):
super().__init__(config)
self.model = nn.Module()
self.model.tok_embed = nn.Embedding(config.vocab_size, config.hidden_dim)
self.model.blocks = nn.ModuleList([
ExpIvmeBlock(config.hidden_dim, config.n_heads, config.ffn_mult, config.norm_eps, config.dropout)
for _ in range(config.n_layers)
])
self.model.final_norm = ExpIvmeRMSNorm(config.hidden_dim, eps=config.norm_eps)
self.lm_head = nn.Linear(config.hidden_dim, config.vocab_size, bias=False)
self.head_dim = config.hidden_dim // config.n_heads
self.rope_theta = config.rope_theta
self.post_init()
if config.tie_word_embeddings:
self.tie_weights()
def get_input_embeddings(self):
return self.model.tok_embed
def set_input_embeddings(self, value):
self.model.tok_embed = value
def get_output_embeddings(self):
return self.lm_head
def forward(self, input_ids, attention_mask=None, labels=None, mask_positions=None, t=None, return_dict=True, **kw):
B, T = input_ids.shape
rope = _precompute_rope_freqs(self.head_dim, T, self.rope_theta, device=input_ids.device)
sdpa_mask = None
if attention_mask is not None:
sdpa_mask = torch.zeros(B, 1, 1, T, dtype=torch.float32, device=input_ids.device)
sdpa_mask.masked_fill_(attention_mask[:, None, None, :] == 0, float("-inf"))
sdpa_mask = sdpa_mask.to(dtype=self.model.tok_embed.weight.dtype)
x = self.model.tok_embed(input_ids)
for block in self.model.blocks:
x = block(x, rope, attn_mask=sdpa_mask)
x = self.model.final_norm(x)
logits = self.lm_head(x)
loss = None
if labels is not None and mask_positions is not None:
ce = F.cross_entropy(
logits.view(-1, self.config.vocab_size), labels.view(-1), ignore_index=-100, reduction="none",
).view(B, T)
ce = ce * mask_positions.float()
per_example_loss = ce.sum(dim=1)
if t is not None:
weight = 1.0 / t.clamp(min=1e-3)
per_example_loss = per_example_loss * weight
n_masked = mask_positions.float().sum(dim=1).clamp(min=1.0)
loss = (per_example_loss / n_masked).mean()
if not return_dict:
return (loss, logits) if loss is not None else (logits,)
return DiffusionLMOutput(loss=loss, logits=logits)
__all__ = ["ExpIvmeDiffusionConfig", "ExpIvmeForDiffusionLMHub"]
|