WaveCut commited on
Commit
322d0a8
·
verified ·
1 Parent(s): 7b5cfe3

Add files using upload-large-folder tool

Browse files
lingbot_sdnq_runtime/__init__.py ADDED
@@ -0,0 +1,680 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import os
5
+ import sys
6
+ from contextlib import contextmanager
7
+ from pathlib import Path
8
+ from types import ModuleType
9
+ from typing import Any, Iterable
10
+
11
+ import torch
12
+ from torch import nn
13
+
14
+
15
+ RUNTIME_VERSION = "1"
16
+ EXPERT_MANIFEST_NAME = "sdnq_experts.json"
17
+
18
+
19
+ def _install_sgl_kernel_compat_stub() -> None:
20
+ """Provide the two helpers needed by SGLang's Triton MoE path on Torch 2.8.
21
+
22
+ The upstream sglang-kernel 0.4.4 wheels target the newer Torch/CUDA ABI.
23
+ LingBot only needs token alignment and top-k reduction from that extension;
24
+ its matrix multiplies and activations remain SGLang Triton kernels.
25
+ """
26
+
27
+ module = ModuleType("sgl_kernel")
28
+
29
+ def moe_sum_reduce(
30
+ value: torch.Tensor,
31
+ output: torch.Tensor,
32
+ routed_scaling_factor: float = 1.0,
33
+ ) -> None:
34
+ torch.sum(value, dim=1, out=output)
35
+ if routed_scaling_factor != 1.0:
36
+ output.mul_(routed_scaling_factor)
37
+
38
+ def moe_align_block_size(
39
+ topk_ids: torch.Tensor,
40
+ num_experts_with_sentinel: int,
41
+ block_size: int,
42
+ sorted_ids: torch.Tensor,
43
+ expert_ids: torch.Tensor,
44
+ num_tokens_post_pad: torch.Tensor,
45
+ cumsum_buffer: torch.Tensor,
46
+ _use_int32: bool,
47
+ ) -> None:
48
+ flat = topk_ids.reshape(-1).to(torch.int64)
49
+ num_experts = int(num_experts_with_sentinel) - 1
50
+ valid = (flat >= 0) & (flat < num_experts)
51
+ valid_positions = torch.arange(flat.numel(), device=flat.device, dtype=torch.int64)[valid]
52
+ valid_experts = flat[valid]
53
+ counts = torch.bincount(valid_experts, minlength=num_experts)
54
+ padded_counts = ((counts + block_size - 1) // block_size) * block_size
55
+ padded_offsets = torch.cumsum(padded_counts, dim=0) - padded_counts
56
+ original_offsets = torch.cumsum(counts, dim=0) - counts
57
+ order = torch.argsort(valid_experts, stable=True)
58
+ sorted_experts = valid_experts[order]
59
+ rank_in_expert = torch.arange(
60
+ order.numel(),
61
+ device=flat.device,
62
+ dtype=torch.int64,
63
+ ) - original_offsets[sorted_experts]
64
+ destinations = padded_offsets[sorted_experts] + rank_in_expert
65
+ total_padded = int(padded_counts.sum().item())
66
+ sorted_ids.fill_(flat.numel())
67
+ sorted_ids[destinations] = valid_positions[order].to(sorted_ids.dtype)
68
+ block_experts = torch.repeat_interleave(
69
+ torch.arange(num_experts, device=flat.device, dtype=expert_ids.dtype),
70
+ (padded_counts // block_size).to(torch.int64),
71
+ )
72
+ expert_ids.fill_(-1)
73
+ expert_ids[: block_experts.numel()] = block_experts
74
+ num_tokens_post_pad.fill_(total_padded)
75
+ cumsum_buffer.zero_()
76
+ cumulative = torch.cumsum(counts.to(cumsum_buffer.dtype), dim=0)
77
+ cumsum_buffer[1 : num_experts + 1] = cumulative
78
+
79
+ module.moe_sum_reduce = moe_sum_reduce
80
+ module.moe_align_block_size = moe_align_block_size
81
+ sys.modules["sgl_kernel"] = module
82
+
83
+
84
+ _install_sgl_kernel_compat_stub()
85
+
86
+
87
+ def _dtype_to_name(dtype: torch.dtype) -> str:
88
+ return str(dtype).removeprefix("torch.")
89
+
90
+
91
+ def _dtype_from_name(name: str) -> torch.dtype:
92
+ dtype = getattr(torch, name.removeprefix("torch."), None)
93
+ if not isinstance(dtype, torch.dtype):
94
+ raise ValueError(f"unsupported torch dtype {name!r}")
95
+ return dtype
96
+
97
+
98
+ def _shape(value: torch.Size | Iterable[int] | None) -> list[int] | None:
99
+ return None if value is None else [int(item) for item in value]
100
+
101
+
102
+ def _dequantizer_to_dict(dequantizer: Any) -> dict[str, Any]:
103
+ return {
104
+ "result_dtype": _dtype_to_name(dequantizer.result_dtype),
105
+ "result_shape": _shape(dequantizer.result_shape),
106
+ "original_shape": _shape(dequantizer.original_shape),
107
+ "original_stride": [int(item) for item in dequantizer.original_stride],
108
+ "quantized_weight_shape": _shape(dequantizer.quantized_weight_shape),
109
+ "weights_dtype": dequantizer.weights_dtype,
110
+ "quantized_matmul_dtype": dequantizer.quantized_matmul_dtype,
111
+ "hadamard_group_size": int(dequantizer.hadamard_group_size),
112
+ "group_size": int(dequantizer.group_size),
113
+ "svd_rank": int(dequantizer.svd_rank),
114
+ "svd_steps": int(dequantizer.svd_steps),
115
+ "use_quantized_matmul": bool(dequantizer.use_quantized_matmul),
116
+ "re_quantize_for_matmul": bool(dequantizer.re_quantize_for_matmul),
117
+ "use_stochastic_rounding": bool(dequantizer.use_stochastic_rounding),
118
+ "use_hadamard": bool(dequantizer.use_hadamard),
119
+ "layer_class_name": dequantizer.layer_class_name,
120
+ }
121
+
122
+
123
+ def _dequantizer_from_dict(metadata: dict[str, Any]):
124
+ from sdnq.dequantizer import SDNQDequantizer
125
+
126
+ return SDNQDequantizer(
127
+ result_dtype=_dtype_from_name(metadata["result_dtype"]),
128
+ result_shape=(
129
+ None
130
+ if metadata.get("result_shape") is None
131
+ else torch.Size(metadata["result_shape"])
132
+ ),
133
+ original_shape=torch.Size(metadata["original_shape"]),
134
+ original_stride=list(metadata["original_stride"]),
135
+ quantized_weight_shape=torch.Size(metadata["quantized_weight_shape"]),
136
+ weights_dtype=metadata["weights_dtype"],
137
+ quantized_matmul_dtype=metadata["quantized_matmul_dtype"],
138
+ hadamard_group_size=int(metadata["hadamard_group_size"]),
139
+ group_size=int(metadata["group_size"]),
140
+ svd_rank=int(metadata["svd_rank"]),
141
+ svd_steps=int(metadata["svd_steps"]),
142
+ use_quantized_matmul=bool(metadata["use_quantized_matmul"]),
143
+ re_quantize_for_matmul=bool(metadata["re_quantize_for_matmul"]),
144
+ use_stochastic_rounding=bool(metadata["use_stochastic_rounding"]),
145
+ use_hadamard=bool(metadata["use_hadamard"]),
146
+ layer_class_name=metadata.get("layer_class_name"),
147
+ )
148
+
149
+
150
+ class SDNQExpertWeight(nn.Module):
151
+ """Packed SDNQ storage for one 3-D LingBot expert tensor."""
152
+
153
+ def __init__(
154
+ self,
155
+ metadata: dict[str, Any],
156
+ *,
157
+ weight: torch.Tensor,
158
+ scale: torch.Tensor,
159
+ zero_point: torch.Tensor | None,
160
+ ) -> None:
161
+ super().__init__()
162
+ self.metadata = dict(metadata)
163
+ self.weight = nn.Parameter(weight, requires_grad=False)
164
+ self.scale = nn.Parameter(scale, requires_grad=False)
165
+ self.register_parameter(
166
+ "zero_point",
167
+ None if zero_point is None else nn.Parameter(zero_point, requires_grad=False),
168
+ )
169
+ self._dequantizer = _dequantizer_from_dict(self.metadata)
170
+
171
+ @classmethod
172
+ def from_float(
173
+ cls,
174
+ weight: torch.Tensor,
175
+ *,
176
+ quantization_device: torch.device | str = "cuda",
177
+ return_device: torch.device | str = "cpu",
178
+ result_dtype: torch.dtype = torch.bfloat16,
179
+ ) -> "SDNQExpertWeight":
180
+ from sdnq.quantizer import sdnq_quantize_layer_weight
181
+
182
+ source = weight.detach().to(
183
+ device=quantization_device,
184
+ dtype=result_dtype,
185
+ copy=True,
186
+ )
187
+ dequantizer, tensors = sdnq_quantize_layer_weight(
188
+ source,
189
+ layer_class_name=None,
190
+ weights_dtype="uint4",
191
+ quantized_matmul_dtype=None,
192
+ group_size=0,
193
+ hadamard_group_size=256,
194
+ svd_rank=32,
195
+ svd_steps=8,
196
+ use_svd=False,
197
+ use_hadamard=False,
198
+ use_quantized_matmul=False,
199
+ use_stochastic_rounding=False,
200
+ dequantize_fp32=False,
201
+ torch_dtype=result_dtype,
202
+ )
203
+ del source
204
+ return cls(
205
+ _dequantizer_to_dict(dequantizer),
206
+ weight=tensors["weight"].to(return_device),
207
+ scale=tensors["scale"].to(return_device),
208
+ zero_point=(
209
+ None
210
+ if tensors["zero_point"] is None
211
+ else tensors["zero_point"].to(return_device)
212
+ ),
213
+ )
214
+
215
+ @classmethod
216
+ def empty(cls, metadata: dict[str, Any]) -> "SDNQExpertWeight":
217
+ from sdnq.common import dtype_dict
218
+
219
+ packed_shape = metadata["stored_weight_shape"]
220
+ scale_shape = metadata["scale_shape"]
221
+ zero_point_shape = metadata.get("zero_point_shape")
222
+ storage_dtype = dtype_dict[metadata["weights_dtype"]]["storage_dtype"]
223
+ scale_dtype = _dtype_from_name(metadata["scale_dtype"])
224
+ return cls(
225
+ metadata,
226
+ weight=torch.empty(packed_shape, dtype=storage_dtype),
227
+ scale=torch.empty(scale_shape, dtype=scale_dtype),
228
+ zero_point=(
229
+ None
230
+ if zero_point_shape is None
231
+ else torch.empty(zero_point_shape, dtype=_dtype_from_name(metadata["zero_point_dtype"]))
232
+ ),
233
+ )
234
+
235
+ def manifest(self) -> dict[str, Any]:
236
+ result = dict(self.metadata)
237
+ result.update(
238
+ {
239
+ "stored_weight_shape": _shape(self.weight.shape),
240
+ "stored_weight_dtype": _dtype_to_name(self.weight.dtype),
241
+ "scale_shape": _shape(self.scale.shape),
242
+ "scale_dtype": _dtype_to_name(self.scale.dtype),
243
+ "zero_point_shape": (
244
+ None if self.zero_point is None else _shape(self.zero_point.shape)
245
+ ),
246
+ "zero_point_dtype": (
247
+ None if self.zero_point is None else _dtype_to_name(self.zero_point.dtype)
248
+ ),
249
+ "logical_numel": int(torch.tensor(self.metadata["original_shape"]).prod().item()),
250
+ "stored_bytes": int(
251
+ self.weight.numel() * self.weight.element_size()
252
+ + self.scale.numel() * self.scale.element_size()
253
+ + (
254
+ 0
255
+ if self.zero_point is None
256
+ else self.zero_point.numel() * self.zero_point.element_size()
257
+ )
258
+ ),
259
+ }
260
+ )
261
+ return result
262
+
263
+ def forward(self, dtype: torch.dtype = torch.bfloat16) -> torch.Tensor:
264
+ return self._dequantizer(
265
+ self.weight,
266
+ self.scale,
267
+ zero_point=self.zero_point,
268
+ dtype=dtype,
269
+ )
270
+
271
+
272
+ def _is_grouped_experts(module: nn.Module) -> bool:
273
+ return module.__class__.__name__ == "LingBotVideoGroupedExperts"
274
+
275
+
276
+ def _is_packed_experts(module: nn.Module) -> bool:
277
+ return all(hasattr(module, f"{name}_sdnq") for name in ("w1", "w2", "w3"))
278
+
279
+
280
+ def quantize_moe_experts(
281
+ model: nn.Module,
282
+ *,
283
+ quantization_device: torch.device | str = "cuda",
284
+ return_device: torch.device | str = "cpu",
285
+ ) -> dict[str, Any]:
286
+ entries: list[dict[str, Any]] = []
287
+ for module_path, module in list(model.named_modules()):
288
+ if not _is_grouped_experts(module) or _is_packed_experts(module):
289
+ continue
290
+ entry: dict[str, Any] = {"module": module_path, "weights": {}}
291
+ for name in ("w1", "w2", "w3"):
292
+ original = getattr(module, name)
293
+ original_numel = int(original.numel())
294
+ original_bytes = int(original.numel() * original.element_size())
295
+ packed = SDNQExpertWeight.from_float(
296
+ original,
297
+ quantization_device=quantization_device,
298
+ return_device=return_device,
299
+ )
300
+ delattr(module, name)
301
+ module.add_module(f"{name}_sdnq", packed)
302
+ weight_manifest = packed.manifest()
303
+ weight_manifest["original_numel"] = original_numel
304
+ weight_manifest["original_bytes"] = original_bytes
305
+ entry["weights"][name] = weight_manifest
306
+ del original
307
+ if torch.cuda.is_available():
308
+ torch.cuda.empty_cache()
309
+ entries.append(entry)
310
+ return {
311
+ "format": "lingbot-video-sdnq-experts",
312
+ "version": RUNTIME_VERSION,
313
+ "weights_dtype": "uint4",
314
+ "group_size": 0,
315
+ "use_dynamic_quantization": False,
316
+ "use_svd": False,
317
+ "quant_conv": False,
318
+ "quant_embedding": False,
319
+ "entries": entries,
320
+ }
321
+
322
+
323
+ def install_prequantized_experts(model: nn.Module, manifest: dict[str, Any]) -> None:
324
+ modules = dict(model.named_modules())
325
+ for entry in manifest.get("entries", []):
326
+ module_path = entry["module"]
327
+ module = modules.get(module_path)
328
+ if module is None or not _is_grouped_experts(module):
329
+ raise KeyError(f"LingBot grouped experts module not found: {module_path}")
330
+ for name in ("w1", "w2", "w3"):
331
+ if hasattr(module, name):
332
+ delattr(module, name)
333
+ module.add_module(
334
+ f"{name}_sdnq",
335
+ SDNQExpertWeight.empty(entry["weights"][name]),
336
+ )
337
+
338
+
339
+ _PATCHED = False
340
+
341
+
342
+ def install_runtime_patch() -> None:
343
+ global _PATCHED
344
+ if _PATCHED:
345
+ return
346
+ from lingbot_video.transformer_lingbot_video import (
347
+ LingBotVideoBlock,
348
+ LingBotVideoSparseMoeBlock,
349
+ )
350
+ from lingbot_video.sglang_moe_shim import (
351
+ LightSglangMoeRunnerConfig,
352
+ LightSglangStandardTopKOutput,
353
+ ensure_sglang_moe_ready,
354
+ sglang_fused_experts,
355
+ )
356
+
357
+ original_grouped = LingBotVideoSparseMoeBlock._run_grouped_experts
358
+ original_loop = LingBotVideoSparseMoeBlock._run_experts_for_loop
359
+ original_sglang = LingBotVideoSparseMoeBlock._run_sglang_triton_experts
360
+
361
+ def packed_aware_block_forward(
362
+ self,
363
+ x,
364
+ temb6,
365
+ rotary_emb,
366
+ attention_mask=None,
367
+ moe_padding_mask=None,
368
+ packed_indices=None,
369
+ parallel_config=None,
370
+ ):
371
+ expected_tokens = x.shape[0] * x.shape[1]
372
+ if temb6.ndim != 2 or temb6.shape[0] != expected_tokens:
373
+ raise ValueError(
374
+ "LingBotVideoBlock expects token-level temb6 with shape "
375
+ f"(B*S, 6D); got {tuple(temb6.shape)} for hidden states {tuple(x.shape)}."
376
+ )
377
+ mod = temb6.view(x.shape[0], x.shape[1], -1) + self.scale_shift_table.unsqueeze(0)
378
+ shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp, gate_mlp = mod.chunk(6, dim=-1)
379
+ gate_msa, gate_mlp = gate_msa.tanh(), gate_mlp.tanh()
380
+ scale_msa, scale_mlp = 1.0 + scale_msa, 1.0 + scale_mlp
381
+ to_q_dequantizer = getattr(self.attn.to_q, "sdnq_dequantizer", None)
382
+ bulk_dtype = (
383
+ to_q_dequantizer.result_dtype
384
+ if to_q_dequantizer is not None
385
+ else self.attn.to_q.weight.dtype
386
+ )
387
+ attn_in = (self.norm1(x) * scale_msa + shift_msa).to(bulk_dtype)
388
+ attn_out = self.attn(
389
+ attn_in,
390
+ rotary_emb,
391
+ attention_mask,
392
+ packed_indices=packed_indices,
393
+ parallel_config=parallel_config,
394
+ )
395
+ x = x + (gate_msa * self.norm_post_attn(attn_out)).to(x.dtype)
396
+ ffn_in = (self.norm2(x) * scale_mlp + shift_mlp).to(bulk_dtype)
397
+ if isinstance(self.ffn, LingBotVideoSparseMoeBlock):
398
+ ffn_out = self.ffn(ffn_in, padding_mask=moe_padding_mask)
399
+ else:
400
+ ffn_out = self.ffn(ffn_in)
401
+ ffn_normed = self.norm_post_ffn(ffn_out)
402
+ return x + (gate_mlp * ffn_normed).to(x.dtype)
403
+
404
+ def packed_loop(self, tokens: torch.Tensor, counts: torch.Tensor) -> torch.Tensor:
405
+ if not _is_packed_experts(self.experts):
406
+ return original_loop(self, tokens, counts)
407
+ w1 = self.experts.w1_sdnq(torch.bfloat16)
408
+ w2 = self.experts.w2_sdnq(torch.bfloat16)
409
+ w3 = self.experts.w3_sdnq(torch.bfloat16)
410
+ count_list = counts.tolist()
411
+ splits = torch.split(tokens, count_list, dim=0)
412
+ outputs = []
413
+ for expert_idx, expert_tokens in enumerate(splits):
414
+ if expert_tokens.numel() == 0:
415
+ continue
416
+ h = torch.nn.functional.silu(
417
+ expert_tokens @ w1[expert_idx].transpose(-2, -1)
418
+ )
419
+ h = h * (expert_tokens @ w3[expert_idx].transpose(-2, -1))
420
+ outputs.append(h @ w2[expert_idx].transpose(-2, -1))
421
+ if not outputs:
422
+ return tokens.new_zeros(tokens.shape)
423
+ return torch.cat(outputs, dim=0)
424
+
425
+ def packed_grouped(self, tokens: torch.Tensor, counts: torch.Tensor) -> torch.Tensor:
426
+ if not _is_packed_experts(self.experts):
427
+ return original_grouped(self, tokens, counts)
428
+ if not hasattr(torch, "_grouped_mm"):
429
+ return packed_loop(self, tokens, counts)
430
+ input_shape, padded_tokens, permuted_indices, aligned_counts = self._pad_grouped_tokens(
431
+ tokens,
432
+ counts,
433
+ )
434
+ offsets = torch.cumsum(aligned_counts, dim=0, dtype=torch.int32)
435
+ w1 = self.experts.w1_sdnq(torch.bfloat16)
436
+ h = torch.nn.functional.silu(
437
+ torch._grouped_mm(
438
+ padded_tokens.bfloat16(),
439
+ w1.transpose(-2, -1),
440
+ offs=offsets,
441
+ )
442
+ )
443
+ del w1
444
+ w3 = self.experts.w3_sdnq(torch.bfloat16)
445
+ h = h * torch._grouped_mm(
446
+ padded_tokens.bfloat16(),
447
+ w3.transpose(-2, -1),
448
+ offs=offsets,
449
+ )
450
+ del w3
451
+ w2 = self.experts.w2_sdnq(torch.bfloat16)
452
+ output = torch._grouped_mm(
453
+ h,
454
+ w2.transpose(-2, -1),
455
+ offs=offsets,
456
+ ).type_as(padded_tokens)
457
+ del w2
458
+ return self._unpad_grouped_tokens(output, input_shape, permuted_indices)
459
+
460
+ def packed_sglang(
461
+ self,
462
+ tokens: torch.Tensor,
463
+ top_scores: torch.Tensor,
464
+ top_indices: torch.Tensor,
465
+ ) -> torch.Tensor:
466
+ if not _is_packed_experts(self.experts):
467
+ return original_sglang(self, tokens, top_scores, top_indices)
468
+ ensure_sglang_moe_ready()
469
+ topk_output = LightSglangStandardTopKOutput(
470
+ top_scores.float(),
471
+ top_indices.to(torch.int32),
472
+ torch.empty(0, device=tokens.device),
473
+ )
474
+ runner_config = LightSglangMoeRunnerConfig(
475
+ num_experts=self.num_experts,
476
+ num_local_experts=self.num_experts,
477
+ activation="silu",
478
+ is_gated=True,
479
+ inplace=False,
480
+ )
481
+ w1 = self.experts.w1_sdnq(torch.bfloat16)
482
+ w3 = self.experts.w3_sdnq(torch.bfloat16)
483
+ w13 = torch.cat((w1, w3), dim=1).contiguous()
484
+ del w1, w3
485
+ w2 = self.experts.w2_sdnq(torch.bfloat16).contiguous()
486
+ output = sglang_fused_experts(
487
+ tokens.contiguous().bfloat16(),
488
+ w13,
489
+ w2,
490
+ topk_output,
491
+ runner_config,
492
+ ).type_as(tokens)
493
+ del w13, w2
494
+ return output
495
+
496
+ LingBotVideoSparseMoeBlock._run_experts_for_loop = packed_loop
497
+ LingBotVideoSparseMoeBlock._run_grouped_experts = packed_grouped
498
+ LingBotVideoSparseMoeBlock._run_sglang_triton_experts = packed_sglang
499
+ LingBotVideoBlock.forward = packed_aware_block_forward
500
+ _PATCHED = True
501
+
502
+
503
+ def save_expert_manifest(manifest: dict[str, Any], transformer_dir: str | Path) -> Path:
504
+ path = Path(transformer_dir) / EXPERT_MANIFEST_NAME
505
+ path.write_text(json.dumps(manifest, indent=2, sort_keys=True) + "\n", encoding="utf-8")
506
+ return path
507
+
508
+
509
+ def load_expert_manifest(transformer_dir: str | Path) -> dict[str, Any]:
510
+ path = Path(transformer_dir) / EXPERT_MANIFEST_NAME
511
+ if not path.exists():
512
+ return {"format": "lingbot-video-sdnq-experts", "version": RUNTIME_VERSION, "entries": []}
513
+ return json.loads(path.read_text(encoding="utf-8"))
514
+
515
+
516
+ def _load_state_dict(transformer_dir: Path, device: torch.device | str = "cpu") -> dict[str, torch.Tensor]:
517
+ from sdnq.file_loader import load_files
518
+
519
+ files = sorted(str(path) for path in transformer_dir.glob("*.safetensors"))
520
+ if not files:
521
+ raise FileNotFoundError(f"no safetensors shards in {transformer_dir}")
522
+ return load_files(files, device=device, method="safetensors")
523
+
524
+
525
+ def load_transformer(
526
+ model_root: str | Path,
527
+ *,
528
+ subfolder: str = "transformer",
529
+ torch_dtype: torch.dtype = torch.bfloat16,
530
+ state_device: torch.device | str = "cpu",
531
+ ) -> nn.Module:
532
+ from accelerate import init_empty_weights
533
+ from lingbot_video.transformer_lingbot_video import LingBotVideoTransformer3DModel
534
+ from sdnq import sdnq_post_load_quant
535
+ from sdnq.loader import apply_sdnq_options_to_model, post_process_model
536
+ from sdnq.utils import get_quant_args_from_config
537
+
538
+ install_runtime_patch()
539
+ transformer_dir = Path(model_root) / subfolder
540
+ config = LingBotVideoTransformer3DModel.load_config(str(transformer_dir))
541
+ if hasattr(config, "to_dict"):
542
+ config = config.to_dict()
543
+ config = dict(config)
544
+ config.pop("quantization_config", None)
545
+ quant_config = json.loads(
546
+ (transformer_dir / "quantization_config.json").read_text(encoding="utf-8")
547
+ )
548
+ expert_manifest = load_expert_manifest(transformer_dir)
549
+
550
+ with init_empty_weights():
551
+ model = LingBotVideoTransformer3DModel.from_config(config)
552
+ install_prequantized_experts(model, expert_manifest)
553
+ model = sdnq_post_load_quant(
554
+ model,
555
+ torch_dtype=torch_dtype,
556
+ pre_quantized=True,
557
+ **get_quant_args_from_config(quant_config),
558
+ )
559
+
560
+ state_dict = _load_state_dict(transformer_dir, device=state_device)
561
+ incompatible = model.load_state_dict(state_dict, strict=True, assign=True)
562
+ if incompatible.missing_keys or incompatible.unexpected_keys:
563
+ raise RuntimeError(f"incompatible SDNQ state dict: {incompatible}")
564
+ del state_dict
565
+ model = post_process_model(model)
566
+ model = apply_sdnq_options_to_model(
567
+ model,
568
+ dtype=torch_dtype,
569
+ dequantize_fp32=False,
570
+ use_quantized_matmul=False,
571
+ )
572
+ # LingBot derives autocast/device from its first parameter. Packed SDNQ
573
+ # weights are uint8, so expose a zero-sized floating anchor ahead of child
574
+ # parameters without adding any checkpoint storage.
575
+ model.register_parameter(
576
+ "_sdnq_dtype_anchor",
577
+ nn.Parameter(torch.empty(0, dtype=torch_dtype), requires_grad=False),
578
+ )
579
+ model.eval()
580
+ return model
581
+
582
+
583
+ @contextmanager
584
+ def _patch_qwen_loader():
585
+ from transformers import Qwen3VLForConditionalGeneration
586
+
587
+ original = Qwen3VLForConditionalGeneration.from_pretrained
588
+ attn_implementation = os.environ.get("LINGBOT_QWEN_ATTN_IMPLEMENTATION", "sdpa")
589
+
590
+ @classmethod
591
+ def patched(cls, pretrained_model_name_or_path, *args, **kwargs):
592
+ kwargs.setdefault("attn_implementation", attn_implementation)
593
+ if "torch_dtype" in kwargs and "dtype" not in kwargs:
594
+ kwargs["dtype"] = kwargs.pop("torch_dtype")
595
+ return original(pretrained_model_name_or_path, *args, **kwargs)
596
+
597
+ Qwen3VLForConditionalGeneration.from_pretrained = patched
598
+ try:
599
+ yield
600
+ finally:
601
+ Qwen3VLForConditionalGeneration.from_pretrained = original
602
+
603
+
604
+ def resolve_model_root(repo_id_or_path: str | Path, *, revision: str | None = None) -> Path:
605
+ path = Path(repo_id_or_path)
606
+ if path.exists():
607
+ return path.resolve()
608
+ from huggingface_hub import snapshot_download
609
+
610
+ return Path(snapshot_download(str(repo_id_or_path), revision=revision))
611
+
612
+
613
+ def load_pipeline(
614
+ repo_id_or_path: str | Path,
615
+ *,
616
+ revision: str | None = None,
617
+ transformer_subfolder: str = "transformer",
618
+ device: torch.device | str | None = "cuda",
619
+ torch_dtype: dict[str, torch.dtype] | None = None,
620
+ ):
621
+ from lingbot_video.pipeline_lingbot_video import LingBotVideoPipeline
622
+
623
+ model_root = resolve_model_root(repo_id_or_path, revision=revision)
624
+ dtype_map = torch_dtype or {
625
+ "default": torch.bfloat16,
626
+ "transformer": torch.bfloat16,
627
+ "text_encoder": torch.bfloat16,
628
+ "vae": torch.float32,
629
+ }
630
+ transformer = load_transformer(
631
+ model_root,
632
+ subfolder=transformer_subfolder,
633
+ torch_dtype=dtype_map["transformer"],
634
+ )
635
+ with _patch_qwen_loader():
636
+ pipe = LingBotVideoPipeline.from_pretrained(
637
+ str(model_root),
638
+ transformer=transformer,
639
+ trust_remote_code=True,
640
+ torch_dtype=dtype_map,
641
+ )
642
+ if device is not None:
643
+ pipe = pipe.to(device)
644
+ return pipe
645
+
646
+
647
+ def expert_storage_summary(manifest: dict[str, Any]) -> dict[str, int | float]:
648
+ logical_numel = 0
649
+ original_bytes = 0
650
+ stored_bytes = 0
651
+ tensor_count = 0
652
+ for entry in manifest.get("entries", []):
653
+ for weight in entry["weights"].values():
654
+ logical_numel += int(weight["logical_numel"])
655
+ original_bytes += int(weight["original_bytes"])
656
+ stored_bytes += int(weight["stored_bytes"])
657
+ tensor_count += 1
658
+ return {
659
+ "tensor_count": tensor_count,
660
+ "logical_numel": logical_numel,
661
+ "original_bytes": original_bytes,
662
+ "stored_bytes": stored_bytes,
663
+ "compression_ratio": (original_bytes / stored_bytes if stored_bytes else 0.0),
664
+ }
665
+
666
+
667
+ __all__ = [
668
+ "EXPERT_MANIFEST_NAME",
669
+ "RUNTIME_VERSION",
670
+ "SDNQExpertWeight",
671
+ "expert_storage_summary",
672
+ "install_prequantized_experts",
673
+ "install_runtime_patch",
674
+ "load_expert_manifest",
675
+ "load_pipeline",
676
+ "load_transformer",
677
+ "quantize_moe_experts",
678
+ "resolve_model_root",
679
+ "save_expert_manifest",
680
+ ]
lingbot_sdnq_runtime/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (33 kB). View file
 
refiner/.quantization_complete ADDED
@@ -0,0 +1 @@
 
 
1
+ ok
refiner/component.SHA256SUMS ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ dc51b8c96c2d745df3bd5590d990230a482fd247123599548e0632fdbf97fc22 ./.quantization_complete
2
+ 2effac3d3a8c72dc3938d44b17b25e928b690717cb2e22a061df8aa3ed4a8c9a ./config.json
3
+ b21bc0b086b03cc9a0000cf7256a712dab2695ffcda7026b076c3abb99dd1eb3 ./coverage.json
4
+ f4fcfa25c57404a23e8da451e107b5533af5b579d907aa38b89f0d6a1a2828e7 ./diffusion_pytorch_model-00001-of-00005.safetensors
5
+ 2489df8e6a45a7883fcff10b34faa0209f07d7b97e6c5681439fafe9d4041303 ./diffusion_pytorch_model-00002-of-00005.safetensors
6
+ e0cf002ed56e1e04140afb7137365ea5d8a3497ef7a4c55911b3db2fdf6545f5 ./diffusion_pytorch_model-00003-of-00005.safetensors
7
+ 5bd87a652b9d279dbb5290a02c14ae6ce5987fdb0eb3caa30cea2980b2c63131 ./diffusion_pytorch_model-00004-of-00005.safetensors
8
+ 0e5e9a038b797c68ea42398bbe425707c7b09414c352c6bfbd2923e7750f25ee ./diffusion_pytorch_model-00005-of-00005.safetensors
9
+ 48d8cb723c47742c0e875083a3e699b8a4541d7fc1036b25a1a160746efe051d ./diffusion_pytorch_model.safetensors.index.json
10
+ e5ab2ee967920427f6897837cf55b798d10b0bce96635b92b315d747e8095c21 ./quantization_config.json
11
+ b49557484202c5499185e6af092f850d4b69788fddf2ccda57c06f5493887027 ./sdnq_experts.json
refiner/config.json ADDED
@@ -0,0 +1,260 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_class_name": "LingBotVideoTransformer3DModel",
3
+ "_diffusers_version": "0.39.0",
4
+ "_name_or_path": "models/moe",
5
+ "axes_dims": [
6
+ 32,
7
+ 48,
8
+ 48
9
+ ],
10
+ "axes_lens": [
11
+ 4096,
12
+ 512,
13
+ 512
14
+ ],
15
+ "decoder_sparse_step": 1,
16
+ "depth": 48,
17
+ "freq_dim": 256,
18
+ "hidden_size": 2048,
19
+ "in_channels": 16,
20
+ "intermediate_size": 6144,
21
+ "mlp_only_layers": [],
22
+ "moe_intermediate_size": 768,
23
+ "n_group": 4,
24
+ "n_shared_experts": 1,
25
+ "norm_eps": 1e-06,
26
+ "norm_topk_prob": true,
27
+ "num_attention_heads": 16,
28
+ "num_experts": 128,
29
+ "num_experts_per_tok": 8,
30
+ "out_bias": true,
31
+ "out_channels": 16,
32
+ "patch_embed_bias": true,
33
+ "patch_size": [
34
+ 1,
35
+ 2,
36
+ 2
37
+ ],
38
+ "qkv_bias": false,
39
+ "quantization_config": {
40
+ "add_skip_keys": true,
41
+ "dequantize_fp32": false,
42
+ "dynamic_loss_threshold": null,
43
+ "group_size": 0,
44
+ "hadamard_group_size": 256,
45
+ "is_integer": true,
46
+ "is_training": false,
47
+ "minimum_allowed_numel": 16384,
48
+ "modules_dtype_dict": {},
49
+ "modules_quant_config": {},
50
+ "modules_to_not_convert": [
51
+ "time_embedder",
52
+ ".proj_out",
53
+ "time_modulation",
54
+ ".context_embedder",
55
+ ".t_embedder",
56
+ ".vid_out",
57
+ "norm_post_attn",
58
+ "time_text_embed",
59
+ "norm_out_modulation",
60
+ ".txt_in",
61
+ "patch_emb",
62
+ "patch_embedding",
63
+ ".condition_embedder",
64
+ "wte",
65
+ ".time_embed",
66
+ "scale_shift_table",
67
+ ".txt_out",
68
+ "norm1",
69
+ ".vid_in",
70
+ ".final_layer",
71
+ ".img_in",
72
+ "norm",
73
+ "patch_embed",
74
+ ".x_embedder",
75
+ "norm2",
76
+ ".emb_in",
77
+ "norm_k",
78
+ "multi_modal_projector",
79
+ ".img_out",
80
+ ".emb_out",
81
+ "norm_out",
82
+ "router",
83
+ "lm_head",
84
+ ".norm_out",
85
+ "norm_q",
86
+ ".y_embedder",
87
+ "norm_post_ffn",
88
+ "blocks.0.ffn.experts.w1_sdnq.weight",
89
+ "blocks.0.ffn.experts.w2_sdnq.weight",
90
+ "blocks.0.ffn.experts.w3_sdnq.weight",
91
+ "blocks.1.ffn.experts.w1_sdnq.weight",
92
+ "blocks.1.ffn.experts.w2_sdnq.weight",
93
+ "blocks.1.ffn.experts.w3_sdnq.weight",
94
+ "blocks.2.ffn.experts.w1_sdnq.weight",
95
+ "blocks.2.ffn.experts.w2_sdnq.weight",
96
+ "blocks.2.ffn.experts.w3_sdnq.weight",
97
+ "blocks.3.ffn.experts.w1_sdnq.weight",
98
+ "blocks.3.ffn.experts.w2_sdnq.weight",
99
+ "blocks.3.ffn.experts.w3_sdnq.weight",
100
+ "blocks.4.ffn.experts.w1_sdnq.weight",
101
+ "blocks.4.ffn.experts.w2_sdnq.weight",
102
+ "blocks.4.ffn.experts.w3_sdnq.weight",
103
+ "blocks.5.ffn.experts.w1_sdnq.weight",
104
+ "blocks.5.ffn.experts.w2_sdnq.weight",
105
+ "blocks.5.ffn.experts.w3_sdnq.weight",
106
+ "blocks.6.ffn.experts.w1_sdnq.weight",
107
+ "blocks.6.ffn.experts.w2_sdnq.weight",
108
+ "blocks.6.ffn.experts.w3_sdnq.weight",
109
+ "blocks.7.ffn.experts.w1_sdnq.weight",
110
+ "blocks.7.ffn.experts.w2_sdnq.weight",
111
+ "blocks.7.ffn.experts.w3_sdnq.weight",
112
+ "blocks.8.ffn.experts.w1_sdnq.weight",
113
+ "blocks.8.ffn.experts.w2_sdnq.weight",
114
+ "blocks.8.ffn.experts.w3_sdnq.weight",
115
+ "blocks.9.ffn.experts.w1_sdnq.weight",
116
+ "blocks.9.ffn.experts.w2_sdnq.weight",
117
+ "blocks.9.ffn.experts.w3_sdnq.weight",
118
+ "blocks.10.ffn.experts.w1_sdnq.weight",
119
+ "blocks.10.ffn.experts.w2_sdnq.weight",
120
+ "blocks.10.ffn.experts.w3_sdnq.weight",
121
+ "blocks.11.ffn.experts.w1_sdnq.weight",
122
+ "blocks.11.ffn.experts.w2_sdnq.weight",
123
+ "blocks.11.ffn.experts.w3_sdnq.weight",
124
+ "blocks.12.ffn.experts.w1_sdnq.weight",
125
+ "blocks.12.ffn.experts.w2_sdnq.weight",
126
+ "blocks.12.ffn.experts.w3_sdnq.weight",
127
+ "blocks.13.ffn.experts.w1_sdnq.weight",
128
+ "blocks.13.ffn.experts.w2_sdnq.weight",
129
+ "blocks.13.ffn.experts.w3_sdnq.weight",
130
+ "blocks.14.ffn.experts.w1_sdnq.weight",
131
+ "blocks.14.ffn.experts.w2_sdnq.weight",
132
+ "blocks.14.ffn.experts.w3_sdnq.weight",
133
+ "blocks.15.ffn.experts.w1_sdnq.weight",
134
+ "blocks.15.ffn.experts.w2_sdnq.weight",
135
+ "blocks.15.ffn.experts.w3_sdnq.weight",
136
+ "blocks.16.ffn.experts.w1_sdnq.weight",
137
+ "blocks.16.ffn.experts.w2_sdnq.weight",
138
+ "blocks.16.ffn.experts.w3_sdnq.weight",
139
+ "blocks.17.ffn.experts.w1_sdnq.weight",
140
+ "blocks.17.ffn.experts.w2_sdnq.weight",
141
+ "blocks.17.ffn.experts.w3_sdnq.weight",
142
+ "blocks.18.ffn.experts.w1_sdnq.weight",
143
+ "blocks.18.ffn.experts.w2_sdnq.weight",
144
+ "blocks.18.ffn.experts.w3_sdnq.weight",
145
+ "blocks.19.ffn.experts.w1_sdnq.weight",
146
+ "blocks.19.ffn.experts.w2_sdnq.weight",
147
+ "blocks.19.ffn.experts.w3_sdnq.weight",
148
+ "blocks.20.ffn.experts.w1_sdnq.weight",
149
+ "blocks.20.ffn.experts.w2_sdnq.weight",
150
+ "blocks.20.ffn.experts.w3_sdnq.weight",
151
+ "blocks.21.ffn.experts.w1_sdnq.weight",
152
+ "blocks.21.ffn.experts.w2_sdnq.weight",
153
+ "blocks.21.ffn.experts.w3_sdnq.weight",
154
+ "blocks.22.ffn.experts.w1_sdnq.weight",
155
+ "blocks.22.ffn.experts.w2_sdnq.weight",
156
+ "blocks.22.ffn.experts.w3_sdnq.weight",
157
+ "blocks.23.ffn.experts.w1_sdnq.weight",
158
+ "blocks.23.ffn.experts.w2_sdnq.weight",
159
+ "blocks.23.ffn.experts.w3_sdnq.weight",
160
+ "blocks.24.ffn.experts.w1_sdnq.weight",
161
+ "blocks.24.ffn.experts.w2_sdnq.weight",
162
+ "blocks.24.ffn.experts.w3_sdnq.weight",
163
+ "blocks.25.ffn.experts.w1_sdnq.weight",
164
+ "blocks.25.ffn.experts.w2_sdnq.weight",
165
+ "blocks.25.ffn.experts.w3_sdnq.weight",
166
+ "blocks.26.ffn.experts.w1_sdnq.weight",
167
+ "blocks.26.ffn.experts.w2_sdnq.weight",
168
+ "blocks.26.ffn.experts.w3_sdnq.weight",
169
+ "blocks.27.ffn.experts.w1_sdnq.weight",
170
+ "blocks.27.ffn.experts.w2_sdnq.weight",
171
+ "blocks.27.ffn.experts.w3_sdnq.weight",
172
+ "blocks.28.ffn.experts.w1_sdnq.weight",
173
+ "blocks.28.ffn.experts.w2_sdnq.weight",
174
+ "blocks.28.ffn.experts.w3_sdnq.weight",
175
+ "blocks.29.ffn.experts.w1_sdnq.weight",
176
+ "blocks.29.ffn.experts.w2_sdnq.weight",
177
+ "blocks.29.ffn.experts.w3_sdnq.weight",
178
+ "blocks.30.ffn.experts.w1_sdnq.weight",
179
+ "blocks.30.ffn.experts.w2_sdnq.weight",
180
+ "blocks.30.ffn.experts.w3_sdnq.weight",
181
+ "blocks.31.ffn.experts.w1_sdnq.weight",
182
+ "blocks.31.ffn.experts.w2_sdnq.weight",
183
+ "blocks.31.ffn.experts.w3_sdnq.weight",
184
+ "blocks.32.ffn.experts.w1_sdnq.weight",
185
+ "blocks.32.ffn.experts.w2_sdnq.weight",
186
+ "blocks.32.ffn.experts.w3_sdnq.weight",
187
+ "blocks.33.ffn.experts.w1_sdnq.weight",
188
+ "blocks.33.ffn.experts.w2_sdnq.weight",
189
+ "blocks.33.ffn.experts.w3_sdnq.weight",
190
+ "blocks.34.ffn.experts.w1_sdnq.weight",
191
+ "blocks.34.ffn.experts.w2_sdnq.weight",
192
+ "blocks.34.ffn.experts.w3_sdnq.weight",
193
+ "blocks.35.ffn.experts.w1_sdnq.weight",
194
+ "blocks.35.ffn.experts.w2_sdnq.weight",
195
+ "blocks.35.ffn.experts.w3_sdnq.weight",
196
+ "blocks.36.ffn.experts.w1_sdnq.weight",
197
+ "blocks.36.ffn.experts.w2_sdnq.weight",
198
+ "blocks.36.ffn.experts.w3_sdnq.weight",
199
+ "blocks.37.ffn.experts.w1_sdnq.weight",
200
+ "blocks.37.ffn.experts.w2_sdnq.weight",
201
+ "blocks.37.ffn.experts.w3_sdnq.weight",
202
+ "blocks.38.ffn.experts.w1_sdnq.weight",
203
+ "blocks.38.ffn.experts.w2_sdnq.weight",
204
+ "blocks.38.ffn.experts.w3_sdnq.weight",
205
+ "blocks.39.ffn.experts.w1_sdnq.weight",
206
+ "blocks.39.ffn.experts.w2_sdnq.weight",
207
+ "blocks.39.ffn.experts.w3_sdnq.weight",
208
+ "blocks.40.ffn.experts.w1_sdnq.weight",
209
+ "blocks.40.ffn.experts.w2_sdnq.weight",
210
+ "blocks.40.ffn.experts.w3_sdnq.weight",
211
+ "blocks.41.ffn.experts.w1_sdnq.weight",
212
+ "blocks.41.ffn.experts.w2_sdnq.weight",
213
+ "blocks.41.ffn.experts.w3_sdnq.weight",
214
+ "blocks.42.ffn.experts.w1_sdnq.weight",
215
+ "blocks.42.ffn.experts.w2_sdnq.weight",
216
+ "blocks.42.ffn.experts.w3_sdnq.weight",
217
+ "blocks.43.ffn.experts.w1_sdnq.weight",
218
+ "blocks.43.ffn.experts.w2_sdnq.weight",
219
+ "blocks.43.ffn.experts.w3_sdnq.weight",
220
+ "blocks.44.ffn.experts.w1_sdnq.weight",
221
+ "blocks.44.ffn.experts.w2_sdnq.weight",
222
+ "blocks.44.ffn.experts.w3_sdnq.weight",
223
+ "blocks.45.ffn.experts.w1_sdnq.weight",
224
+ "blocks.45.ffn.experts.w2_sdnq.weight",
225
+ "blocks.45.ffn.experts.w3_sdnq.weight",
226
+ "blocks.46.ffn.experts.w1_sdnq.weight",
227
+ "blocks.46.ffn.experts.w2_sdnq.weight",
228
+ "blocks.46.ffn.experts.w3_sdnq.weight",
229
+ "blocks.47.ffn.experts.w1_sdnq.weight",
230
+ "blocks.47.ffn.experts.w2_sdnq.weight",
231
+ "blocks.47.ffn.experts.w3_sdnq.weight"
232
+ ],
233
+ "modules_to_not_use_matmul": [],
234
+ "non_blocking": false,
235
+ "quant_conv": false,
236
+ "quant_embedding": false,
237
+ "quant_method": "sdnq",
238
+ "quantization_device": "cuda",
239
+ "quantized_matmul_dtype": null,
240
+ "return_device": "cpu",
241
+ "sdnq_version": "0.2.2",
242
+ "svd_rank": 32,
243
+ "svd_steps": 8,
244
+ "use_dynamic_quantization": false,
245
+ "use_grad_ckpt": true,
246
+ "use_hadamard": false,
247
+ "use_quantized_matmul": false,
248
+ "use_quantized_matmul_conv": false,
249
+ "use_static_quantization": true,
250
+ "use_stochastic_rounding": false,
251
+ "use_svd": false,
252
+ "weights_dtype": "uint4"
253
+ },
254
+ "rope_theta": 256.0,
255
+ "routed_scaling_factor": 2.5,
256
+ "score_func": "sigmoid",
257
+ "text_dim": 2560,
258
+ "timestep_mlp_bias": true,
259
+ "topk_group": 2
260
+ }
refiner/coverage.json ADDED
The diff for this file is too large to render. See raw diff
 
refiner/diffusion_pytorch_model-00001-of-00005.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f4fcfa25c57404a23e8da451e107b5533af5b579d907aa38b89f0d6a1a2828e7
3
+ size 3903708600
refiner/diffusion_pytorch_model-00002-of-00005.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2489df8e6a45a7883fcff10b34faa0209f07d7b97e6c5681439fafe9d4041303
3
+ size 3907192200
refiner/diffusion_pytorch_model-00003-of-00005.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e0cf002ed56e1e04140afb7137365ea5d8a3497ef7a4c55911b3db2fdf6545f5
3
+ size 3907192216
refiner/diffusion_pytorch_model-00004-of-00005.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5bd87a652b9d279dbb5290a02c14ae6ce5987fdb0eb3caa30cea2980b2c63131
3
+ size 3907192216
refiner/diffusion_pytorch_model-00005-of-00005.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0e5e9a038b797c68ea42398bbe425707c7b09414c352c6bfbd2923e7750f25ee
3
+ size 3288080256
refiner/diffusion_pytorch_model.safetensors.index.json ADDED
The diff for this file is too large to render. See raw diff
 
refiner/quantization_config.json ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "add_skip_keys": true,
3
+ "dequantize_fp32": false,
4
+ "dynamic_loss_threshold": null,
5
+ "group_size": 0,
6
+ "hadamard_group_size": 256,
7
+ "is_integer": true,
8
+ "is_training": false,
9
+ "minimum_allowed_numel": 16384,
10
+ "modules_dtype_dict": {},
11
+ "modules_quant_config": {},
12
+ "modules_to_not_convert": [
13
+ "time_embedder",
14
+ ".proj_out",
15
+ "time_modulation",
16
+ ".context_embedder",
17
+ ".t_embedder",
18
+ ".vid_out",
19
+ "norm_post_attn",
20
+ "time_text_embed",
21
+ "norm_out_modulation",
22
+ ".txt_in",
23
+ "patch_emb",
24
+ "patch_embedding",
25
+ ".condition_embedder",
26
+ "wte",
27
+ ".time_embed",
28
+ "scale_shift_table",
29
+ ".txt_out",
30
+ "norm1",
31
+ ".vid_in",
32
+ ".final_layer",
33
+ ".img_in",
34
+ "norm",
35
+ "patch_embed",
36
+ ".x_embedder",
37
+ "norm2",
38
+ ".emb_in",
39
+ "norm_k",
40
+ "multi_modal_projector",
41
+ ".img_out",
42
+ ".emb_out",
43
+ "norm_out",
44
+ "router",
45
+ "lm_head",
46
+ ".norm_out",
47
+ "norm_q",
48
+ ".y_embedder",
49
+ "norm_post_ffn",
50
+ "blocks.0.ffn.experts.w1_sdnq.weight",
51
+ "blocks.0.ffn.experts.w2_sdnq.weight",
52
+ "blocks.0.ffn.experts.w3_sdnq.weight",
53
+ "blocks.1.ffn.experts.w1_sdnq.weight",
54
+ "blocks.1.ffn.experts.w2_sdnq.weight",
55
+ "blocks.1.ffn.experts.w3_sdnq.weight",
56
+ "blocks.2.ffn.experts.w1_sdnq.weight",
57
+ "blocks.2.ffn.experts.w2_sdnq.weight",
58
+ "blocks.2.ffn.experts.w3_sdnq.weight",
59
+ "blocks.3.ffn.experts.w1_sdnq.weight",
60
+ "blocks.3.ffn.experts.w2_sdnq.weight",
61
+ "blocks.3.ffn.experts.w3_sdnq.weight",
62
+ "blocks.4.ffn.experts.w1_sdnq.weight",
63
+ "blocks.4.ffn.experts.w2_sdnq.weight",
64
+ "blocks.4.ffn.experts.w3_sdnq.weight",
65
+ "blocks.5.ffn.experts.w1_sdnq.weight",
66
+ "blocks.5.ffn.experts.w2_sdnq.weight",
67
+ "blocks.5.ffn.experts.w3_sdnq.weight",
68
+ "blocks.6.ffn.experts.w1_sdnq.weight",
69
+ "blocks.6.ffn.experts.w2_sdnq.weight",
70
+ "blocks.6.ffn.experts.w3_sdnq.weight",
71
+ "blocks.7.ffn.experts.w1_sdnq.weight",
72
+ "blocks.7.ffn.experts.w2_sdnq.weight",
73
+ "blocks.7.ffn.experts.w3_sdnq.weight",
74
+ "blocks.8.ffn.experts.w1_sdnq.weight",
75
+ "blocks.8.ffn.experts.w2_sdnq.weight",
76
+ "blocks.8.ffn.experts.w3_sdnq.weight",
77
+ "blocks.9.ffn.experts.w1_sdnq.weight",
78
+ "blocks.9.ffn.experts.w2_sdnq.weight",
79
+ "blocks.9.ffn.experts.w3_sdnq.weight",
80
+ "blocks.10.ffn.experts.w1_sdnq.weight",
81
+ "blocks.10.ffn.experts.w2_sdnq.weight",
82
+ "blocks.10.ffn.experts.w3_sdnq.weight",
83
+ "blocks.11.ffn.experts.w1_sdnq.weight",
84
+ "blocks.11.ffn.experts.w2_sdnq.weight",
85
+ "blocks.11.ffn.experts.w3_sdnq.weight",
86
+ "blocks.12.ffn.experts.w1_sdnq.weight",
87
+ "blocks.12.ffn.experts.w2_sdnq.weight",
88
+ "blocks.12.ffn.experts.w3_sdnq.weight",
89
+ "blocks.13.ffn.experts.w1_sdnq.weight",
90
+ "blocks.13.ffn.experts.w2_sdnq.weight",
91
+ "blocks.13.ffn.experts.w3_sdnq.weight",
92
+ "blocks.14.ffn.experts.w1_sdnq.weight",
93
+ "blocks.14.ffn.experts.w2_sdnq.weight",
94
+ "blocks.14.ffn.experts.w3_sdnq.weight",
95
+ "blocks.15.ffn.experts.w1_sdnq.weight",
96
+ "blocks.15.ffn.experts.w2_sdnq.weight",
97
+ "blocks.15.ffn.experts.w3_sdnq.weight",
98
+ "blocks.16.ffn.experts.w1_sdnq.weight",
99
+ "blocks.16.ffn.experts.w2_sdnq.weight",
100
+ "blocks.16.ffn.experts.w3_sdnq.weight",
101
+ "blocks.17.ffn.experts.w1_sdnq.weight",
102
+ "blocks.17.ffn.experts.w2_sdnq.weight",
103
+ "blocks.17.ffn.experts.w3_sdnq.weight",
104
+ "blocks.18.ffn.experts.w1_sdnq.weight",
105
+ "blocks.18.ffn.experts.w2_sdnq.weight",
106
+ "blocks.18.ffn.experts.w3_sdnq.weight",
107
+ "blocks.19.ffn.experts.w1_sdnq.weight",
108
+ "blocks.19.ffn.experts.w2_sdnq.weight",
109
+ "blocks.19.ffn.experts.w3_sdnq.weight",
110
+ "blocks.20.ffn.experts.w1_sdnq.weight",
111
+ "blocks.20.ffn.experts.w2_sdnq.weight",
112
+ "blocks.20.ffn.experts.w3_sdnq.weight",
113
+ "blocks.21.ffn.experts.w1_sdnq.weight",
114
+ "blocks.21.ffn.experts.w2_sdnq.weight",
115
+ "blocks.21.ffn.experts.w3_sdnq.weight",
116
+ "blocks.22.ffn.experts.w1_sdnq.weight",
117
+ "blocks.22.ffn.experts.w2_sdnq.weight",
118
+ "blocks.22.ffn.experts.w3_sdnq.weight",
119
+ "blocks.23.ffn.experts.w1_sdnq.weight",
120
+ "blocks.23.ffn.experts.w2_sdnq.weight",
121
+ "blocks.23.ffn.experts.w3_sdnq.weight",
122
+ "blocks.24.ffn.experts.w1_sdnq.weight",
123
+ "blocks.24.ffn.experts.w2_sdnq.weight",
124
+ "blocks.24.ffn.experts.w3_sdnq.weight",
125
+ "blocks.25.ffn.experts.w1_sdnq.weight",
126
+ "blocks.25.ffn.experts.w2_sdnq.weight",
127
+ "blocks.25.ffn.experts.w3_sdnq.weight",
128
+ "blocks.26.ffn.experts.w1_sdnq.weight",
129
+ "blocks.26.ffn.experts.w2_sdnq.weight",
130
+ "blocks.26.ffn.experts.w3_sdnq.weight",
131
+ "blocks.27.ffn.experts.w1_sdnq.weight",
132
+ "blocks.27.ffn.experts.w2_sdnq.weight",
133
+ "blocks.27.ffn.experts.w3_sdnq.weight",
134
+ "blocks.28.ffn.experts.w1_sdnq.weight",
135
+ "blocks.28.ffn.experts.w2_sdnq.weight",
136
+ "blocks.28.ffn.experts.w3_sdnq.weight",
137
+ "blocks.29.ffn.experts.w1_sdnq.weight",
138
+ "blocks.29.ffn.experts.w2_sdnq.weight",
139
+ "blocks.29.ffn.experts.w3_sdnq.weight",
140
+ "blocks.30.ffn.experts.w1_sdnq.weight",
141
+ "blocks.30.ffn.experts.w2_sdnq.weight",
142
+ "blocks.30.ffn.experts.w3_sdnq.weight",
143
+ "blocks.31.ffn.experts.w1_sdnq.weight",
144
+ "blocks.31.ffn.experts.w2_sdnq.weight",
145
+ "blocks.31.ffn.experts.w3_sdnq.weight",
146
+ "blocks.32.ffn.experts.w1_sdnq.weight",
147
+ "blocks.32.ffn.experts.w2_sdnq.weight",
148
+ "blocks.32.ffn.experts.w3_sdnq.weight",
149
+ "blocks.33.ffn.experts.w1_sdnq.weight",
150
+ "blocks.33.ffn.experts.w2_sdnq.weight",
151
+ "blocks.33.ffn.experts.w3_sdnq.weight",
152
+ "blocks.34.ffn.experts.w1_sdnq.weight",
153
+ "blocks.34.ffn.experts.w2_sdnq.weight",
154
+ "blocks.34.ffn.experts.w3_sdnq.weight",
155
+ "blocks.35.ffn.experts.w1_sdnq.weight",
156
+ "blocks.35.ffn.experts.w2_sdnq.weight",
157
+ "blocks.35.ffn.experts.w3_sdnq.weight",
158
+ "blocks.36.ffn.experts.w1_sdnq.weight",
159
+ "blocks.36.ffn.experts.w2_sdnq.weight",
160
+ "blocks.36.ffn.experts.w3_sdnq.weight",
161
+ "blocks.37.ffn.experts.w1_sdnq.weight",
162
+ "blocks.37.ffn.experts.w2_sdnq.weight",
163
+ "blocks.37.ffn.experts.w3_sdnq.weight",
164
+ "blocks.38.ffn.experts.w1_sdnq.weight",
165
+ "blocks.38.ffn.experts.w2_sdnq.weight",
166
+ "blocks.38.ffn.experts.w3_sdnq.weight",
167
+ "blocks.39.ffn.experts.w1_sdnq.weight",
168
+ "blocks.39.ffn.experts.w2_sdnq.weight",
169
+ "blocks.39.ffn.experts.w3_sdnq.weight",
170
+ "blocks.40.ffn.experts.w1_sdnq.weight",
171
+ "blocks.40.ffn.experts.w2_sdnq.weight",
172
+ "blocks.40.ffn.experts.w3_sdnq.weight",
173
+ "blocks.41.ffn.experts.w1_sdnq.weight",
174
+ "blocks.41.ffn.experts.w2_sdnq.weight",
175
+ "blocks.41.ffn.experts.w3_sdnq.weight",
176
+ "blocks.42.ffn.experts.w1_sdnq.weight",
177
+ "blocks.42.ffn.experts.w2_sdnq.weight",
178
+ "blocks.42.ffn.experts.w3_sdnq.weight",
179
+ "blocks.43.ffn.experts.w1_sdnq.weight",
180
+ "blocks.43.ffn.experts.w2_sdnq.weight",
181
+ "blocks.43.ffn.experts.w3_sdnq.weight",
182
+ "blocks.44.ffn.experts.w1_sdnq.weight",
183
+ "blocks.44.ffn.experts.w2_sdnq.weight",
184
+ "blocks.44.ffn.experts.w3_sdnq.weight",
185
+ "blocks.45.ffn.experts.w1_sdnq.weight",
186
+ "blocks.45.ffn.experts.w2_sdnq.weight",
187
+ "blocks.45.ffn.experts.w3_sdnq.weight",
188
+ "blocks.46.ffn.experts.w1_sdnq.weight",
189
+ "blocks.46.ffn.experts.w2_sdnq.weight",
190
+ "blocks.46.ffn.experts.w3_sdnq.weight",
191
+ "blocks.47.ffn.experts.w1_sdnq.weight",
192
+ "blocks.47.ffn.experts.w2_sdnq.weight",
193
+ "blocks.47.ffn.experts.w3_sdnq.weight"
194
+ ],
195
+ "modules_to_not_use_matmul": [],
196
+ "non_blocking": false,
197
+ "quant_conv": false,
198
+ "quant_embedding": false,
199
+ "quant_method": "sdnq",
200
+ "quantization_device": "cuda",
201
+ "quantized_matmul_dtype": null,
202
+ "return_device": "cpu",
203
+ "sdnq_version": "0.2.2",
204
+ "svd_rank": 32,
205
+ "svd_steps": 8,
206
+ "use_dynamic_quantization": false,
207
+ "use_grad_ckpt": true,
208
+ "use_hadamard": false,
209
+ "use_quantized_matmul": false,
210
+ "use_quantized_matmul_conv": false,
211
+ "use_static_quantization": true,
212
+ "use_stochastic_rounding": false,
213
+ "use_svd": false,
214
+ "weights_dtype": "uint4"
215
+ }
refiner/sdnq_experts.json ADDED
The diff for this file is too large to render. See raw diff