File size: 44,168 Bytes
d178c84 | 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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 | """Portable selected-block FP4 image-MLP bridge runtime for the ComfyUI plugin."""
from __future__ import annotations
import ctypes
import math
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Iterable, Mapping
RUNTIME_ROOT = Path(__file__).resolve().parent
ABI_VERSION = 1
UP_IN_FEATURES = 3072
UP_OUT_FEATURES = 12288
DOWN_OUT_FEATURES = 3072
TRANSFORMER_BLOCK_COUNT = 12
BLOCK0_IMG_MLP_MODULE = "transformer_blocks.0.img_mlp"
UP_MODULE = f"{BLOCK0_IMG_MLP_MODULE}.net.0.proj"
DOWN_MODULE = f"{BLOCK0_IMG_MLP_MODULE}.net.2"
def _tensor_bytes(tensor: Any) -> int:
return int(tensor.numel() * tensor.element_size())
def resolve_existing_runtime_file(
path: str | Path,
label: str,
*,
base_dir: str | Path | None = None,
) -> Path:
root = RUNTIME_ROOT if base_dir is None else Path(base_dir).expanduser().resolve()
candidate = Path(path).expanduser()
resolved = (root / candidate).resolve() if not candidate.is_absolute() else candidate.resolve()
if not resolved.is_file():
raise RuntimeError(f"{label} must resolve to an existing file: {resolved}")
return resolved
def _require_positive_finite(value: float, label: str) -> float:
if not math.isfinite(value) or value <= 0.0:
raise RuntimeError(f"{label} must be finite and positive; got {value!r}")
return float(value)
def _require_block_index(block_index: int) -> int:
value = int(block_index)
if value < 0 or value >= TRANSFORMER_BLOCK_COUNT:
raise RuntimeError(
f"bridge block index must be in [0, {TRANSFORMER_BLOCK_COUNT - 1}], got {value}"
)
return value
def _module_names_for_block(block_index: int) -> tuple[str, str, str]:
base = f"transformer_blocks.{block_index}.img_mlp"
return (
base,
f"{base}.net.0.proj",
f"{base}.net.2",
)
def normalize_block_tensor_scales(
block_tensor_scales: str | Mapping[int | str, float | str],
) -> dict[int, float]:
if isinstance(block_tensor_scales, str):
text = block_tensor_scales.strip()
if not text:
raise RuntimeError("bridge block scale map must not be empty")
parsed: dict[int | str, float | str] = {}
for entry in text.split(","):
item = entry.strip()
if not item:
raise RuntimeError("bridge block scale map contains an empty entry")
if "=" not in item:
raise RuntimeError(
"bridge block scale map entries must look like block=scale"
)
block_text, scale_text = item.split("=", 1)
block_key = block_text.strip()
if block_key in parsed:
raise RuntimeError(f"duplicate bridge block index {block_key}")
parsed[block_key] = scale_text.strip()
block_tensor_scales = parsed
if not block_tensor_scales:
raise RuntimeError("bridge candidate requires at least one selected block")
normalized: dict[int, float] = {}
for raw_block_index, raw_scale in block_tensor_scales.items():
try:
block_index = _require_block_index(int(raw_block_index))
except (TypeError, ValueError) as error:
raise RuntimeError(
f"invalid bridge block index {raw_block_index!r}"
) from error
if block_index in normalized:
raise RuntimeError(f"duplicate bridge block index {block_index}")
try:
scale_value = float(raw_scale)
except (TypeError, ValueError) as error:
raise RuntimeError(
f"invalid bridge fixed tensor scale {raw_scale!r} for block {block_index}"
) from error
normalized[block_index] = _require_positive_finite(
scale_value,
f"bridge fixed tensor scale for block {block_index}",
)
return dict(sorted(normalized.items()))
def normalize_selected_blocks(
selected_blocks: None | int | str | Iterable[int],
*,
allowed_blocks: Iterable[int] | None = None,
) -> set[int]:
if selected_blocks is None:
return set()
if isinstance(selected_blocks, int):
items: list[int | str] = [selected_blocks]
elif isinstance(selected_blocks, str):
text = selected_blocks.strip()
if not text:
raise RuntimeError("selected_blocks must not be empty")
items = [item.strip() for item in text.split(",")]
else:
items = list(selected_blocks)
normalized = {_require_block_index(int(item)) for item in items}
if allowed_blocks is not None:
allowed = {_require_block_index(int(item)) for item in allowed_blocks}
unknown = sorted(normalized - allowed)
if unknown:
raise RuntimeError(f"selected blocks were not installed: {unknown}")
return normalized
def _require_packed_projection(
projection: Any,
*,
label: str,
in_features: int,
out_features: int,
torch: Any,
) -> None:
required = (
"packed_weight",
"weight_scales",
"weight_scale",
"bias",
"in_features",
"out_features",
)
if projection is None or any(not hasattr(projection, name) for name in required):
raise RuntimeError(f"{label} is not a packed NVFP4 projection")
if int(projection.in_features) != in_features:
raise RuntimeError(
f"{label} in_features changed: {int(projection.in_features)} != {in_features}"
)
if int(projection.out_features) != out_features:
raise RuntimeError(
f"{label} out_features changed: {int(projection.out_features)} != {out_features}"
)
if projection.bias is None:
raise RuntimeError(f"{label} requires a resident BF16 bias")
tensors = (
("packed_weight", projection.packed_weight, torch.uint8),
("weight_scales", projection.weight_scales, torch.uint8),
("weight_scale", projection.weight_scale, torch.float32),
("bias", projection.bias, torch.bfloat16),
)
for tensor_label, tensor, dtype in tensors:
if tensor.device.type != "cuda":
raise RuntimeError(f"{label}.{tensor_label} must be CUDA-resident")
if tensor.dtype != dtype:
raise RuntimeError(
f"{label}.{tensor_label} dtype changed: {tensor.dtype} != {dtype}"
)
if not tensor.is_contiguous():
raise RuntimeError(f"{label}.{tensor_label} must be contiguous")
def _require_exact_numel(
tensor: Any,
*,
expected: int,
label: str,
) -> None:
if int(tensor.numel()) != int(expected):
raise RuntimeError(
f"{label} size changed: {int(tensor.numel())} != {int(expected)}"
)
def _record_stream_for_tensors(current_stream: Any, *tensors: Any) -> None:
for tensor in tensors:
if tensor is not None:
tensor.record_stream(current_stream)
def _resolve_up_projection(activation: Any, module_name: str) -> tuple[Any, str]:
projection = getattr(activation, "proj", None)
if projection is None:
projection = getattr(activation, "projection", None)
if projection is None:
raise RuntimeError(f"{module_name} is missing a .proj/.projection projection")
if (
getattr(activation, "is_mage_fused_gelu_up_wrapper", False)
or getattr(activation, "_xpo3_fused_gelu_up", False)
):
return projection, "fused_gelu_up_wrapper"
if getattr(activation, "approximate", None) == "tanh":
return projection, "tanh_gelu"
raise RuntimeError(
f"{module_name} activation changed; expected tanh GELU or _xpo3_fused_gelu_up wrapper"
)
class BridgeUpLibrary:
def __init__(self, path: Path, device: int):
self.path = path
self.library = ctypes.CDLL(str(path), mode=ctypes.RTLD_LOCAL)
self.context = ctypes.c_void_p()
self._bind()
if int(self.library.mage_nvfp4_bridge_up_abi_version()) != ABI_VERSION:
raise RuntimeError("bridge-up ABI mismatch")
status = self.library.mage_nvfp4_bridge_up_create_context(
int(device), ctypes.byref(self.context)
)
if status:
raise self.error("creating bridge-up context")
def _bind(self) -> None:
library = self.library
library.mage_nvfp4_bridge_up_abi_version.argtypes = []
library.mage_nvfp4_bridge_up_abi_version.restype = ctypes.c_int
library.mage_nvfp4_bridge_up_last_error.argtypes = []
library.mage_nvfp4_bridge_up_last_error.restype = ctypes.c_char_p
for suffix in (
"input_bytes",
"weight_bytes",
"weight_scale_bytes",
"output_fp4_bytes",
"output_scale_bytes",
):
function = getattr(library, f"mage_nvfp4_bridge_up_{suffix}")
function.argtypes = [ctypes.c_int, ctypes.c_int]
function.restype = ctypes.c_size_t
library.mage_nvfp4_bridge_up_bias_bytes.argtypes = [ctypes.c_int]
library.mage_nvfp4_bridge_up_bias_bytes.restype = ctypes.c_size_t
library.mage_nvfp4_bridge_up_scalar_bytes.argtypes = []
library.mage_nvfp4_bridge_up_scalar_bytes.restype = ctypes.c_size_t
library.mage_nvfp4_bridge_up_create_context.argtypes = [
ctypes.c_int,
ctypes.POINTER(ctypes.c_void_p),
]
library.mage_nvfp4_bridge_up_create_context.restype = ctypes.c_int
library.mage_nvfp4_bridge_up_destroy_context.argtypes = [ctypes.c_void_p]
library.mage_nvfp4_bridge_up_destroy_context.restype = ctypes.c_int
library.mage_nvfp4_bridge_up_context_reserved_bytes.argtypes = [
ctypes.c_void_p
]
library.mage_nvfp4_bridge_up_context_reserved_bytes.restype = ctypes.c_size_t
forward = library.mage_nvfp4_bridge_up_forward
forward.argtypes = [
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_int,
ctypes.c_int,
ctypes.c_int,
ctypes.c_size_t,
]
forward.restype = ctypes.c_int
self.forward_function = forward
def error(self, operation: str) -> RuntimeError:
raw = self.library.mage_nvfp4_bridge_up_last_error()
message = raw.decode("utf-8", errors="replace") if raw else "unknown native error"
return RuntimeError(f"{operation}: {message}")
def helper(self, suffix: str, *dimensions: int) -> int:
function = getattr(self.library, f"mage_nvfp4_bridge_up_{suffix}")
value = int(function(*dimensions))
if value == 0:
raise self.error(f"querying bridge-up {suffix}")
return value
def scalar_bytes(self) -> int:
value = int(self.library.mage_nvfp4_bridge_up_scalar_bytes())
if value == 0:
raise self.error("querying bridge-up scalar bytes")
return value
@property
def reserved_bytes(self) -> int:
return int(
self.library.mage_nvfp4_bridge_up_context_reserved_bytes(self.context)
)
def forward(
self,
input_bf16: Any,
packed_weight: Any,
packed_weight_scales: Any,
weight_tensor_scale: Any,
bias_bf16: Any,
bridge_norm_constant: Any,
output_fp4: Any,
output_scales: Any,
logical_m: int,
stream: int,
) -> None:
status = self.forward_function(
self.context,
ctypes.c_void_p(input_bf16.data_ptr()),
_tensor_bytes(input_bf16),
ctypes.c_void_p(packed_weight.data_ptr()),
_tensor_bytes(packed_weight),
ctypes.c_void_p(packed_weight_scales.data_ptr()),
_tensor_bytes(packed_weight_scales),
ctypes.c_void_p(weight_tensor_scale.data_ptr()),
_tensor_bytes(weight_tensor_scale),
ctypes.c_void_p(bias_bf16.data_ptr()),
_tensor_bytes(bias_bf16),
ctypes.c_void_p(bridge_norm_constant.data_ptr()),
_tensor_bytes(bridge_norm_constant),
ctypes.c_void_p(output_fp4.data_ptr()),
_tensor_bytes(output_fp4),
ctypes.c_void_p(output_scales.data_ptr()),
_tensor_bytes(output_scales),
int(logical_m),
UP_IN_FEATURES,
UP_OUT_FEATURES,
int(stream),
)
if status:
raise self.error("running bridge-up")
def close(self) -> None:
if not self.context:
return
status = self.library.mage_nvfp4_bridge_up_destroy_context(self.context)
self.context = ctypes.c_void_p()
if status:
raise self.error("destroying bridge-up context")
class PrequantizedDownLibrary:
def __init__(self, path: Path, device: int):
self.path = path
self.library = ctypes.CDLL(str(path), mode=ctypes.RTLD_LOCAL)
self.context = ctypes.c_void_p()
self._bind()
if (
int(self.library.mage_nvfp4_prequantized_down_abi_version())
!= ABI_VERSION
):
raise RuntimeError("bridge-down ABI mismatch")
status = self.library.mage_nvfp4_prequantized_down_create_context(
int(device), ctypes.byref(self.context)
)
if status:
raise self.error("creating bridge-down context")
def _bind(self) -> None:
library = self.library
prefix = "mage_nvfp4_prequantized_down_"
abi = getattr(library, f"{prefix}abi_version")
abi.argtypes = []
abi.restype = ctypes.c_int
last_error = getattr(library, f"{prefix}last_error")
last_error.argtypes = []
last_error.restype = ctypes.c_char_p
for suffix in (
"activation_bytes",
"activation_scale_bytes",
"weight_bytes",
"weight_scale_bytes",
"output_bytes",
):
function = getattr(library, f"{prefix}{suffix}")
function.argtypes = [ctypes.c_int, ctypes.c_int]
function.restype = ctypes.c_size_t
bias_bytes = getattr(library, f"{prefix}bias_bytes")
bias_bytes.argtypes = [ctypes.c_int]
bias_bytes.restype = ctypes.c_size_t
create = getattr(library, f"{prefix}create_context")
create.argtypes = [ctypes.c_int, ctypes.POINTER(ctypes.c_void_p)]
create.restype = ctypes.c_int
destroy = getattr(library, f"{prefix}destroy_context")
destroy.argtypes = [ctypes.c_void_p]
destroy.restype = ctypes.c_int
reserved = getattr(library, f"{prefix}context_reserved_bytes")
reserved.argtypes = [ctypes.c_void_p]
reserved.restype = ctypes.c_size_t
forward = getattr(library, f"{prefix}forward")
forward.argtypes = [
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_void_p,
ctypes.c_size_t,
ctypes.c_int,
ctypes.c_int,
ctypes.c_int,
ctypes.c_size_t,
]
forward.restype = ctypes.c_int
self.forward_function = forward
def error(self, operation: str) -> RuntimeError:
raw = self.library.mage_nvfp4_prequantized_down_last_error()
message = raw.decode("utf-8", errors="replace") if raw else "unknown native error"
return RuntimeError(f"{operation}: {message}")
def helper(self, suffix: str, *dimensions: int) -> int:
function = getattr(self.library, f"mage_nvfp4_prequantized_down_{suffix}")
value = int(function(*dimensions))
if value == 0:
raise self.error(f"querying bridge-down {suffix}")
return value
@property
def reserved_bytes(self) -> int:
return int(
self.library.mage_nvfp4_prequantized_down_context_reserved_bytes(
self.context
)
)
def forward(
self,
activation_fp4: Any,
activation_scales: Any,
activation_tensor_scale: Any,
packed_weight: Any,
packed_weight_scales: Any,
weight_tensor_scale: Any,
bias_bf16: Any,
output_bf16: Any,
logical_m: int,
stream: int,
) -> None:
status = self.forward_function(
self.context,
ctypes.c_void_p(activation_fp4.data_ptr()),
_tensor_bytes(activation_fp4),
ctypes.c_void_p(activation_scales.data_ptr()),
_tensor_bytes(activation_scales),
ctypes.c_void_p(activation_tensor_scale.data_ptr()),
_tensor_bytes(activation_tensor_scale),
ctypes.c_void_p(packed_weight.data_ptr()),
_tensor_bytes(packed_weight),
ctypes.c_void_p(packed_weight_scales.data_ptr()),
_tensor_bytes(packed_weight_scales),
ctypes.c_void_p(weight_tensor_scale.data_ptr()),
_tensor_bytes(weight_tensor_scale),
ctypes.c_void_p(bias_bf16.data_ptr()),
_tensor_bytes(bias_bf16),
ctypes.c_void_p(output_bf16.data_ptr()),
_tensor_bytes(output_bf16),
int(logical_m),
UP_OUT_FEATURES,
DOWN_OUT_FEATURES,
int(stream),
)
if status:
raise self.error("running bridge-down")
def close(self) -> None:
if not self.context:
return
status = self.library.mage_nvfp4_prequantized_down_destroy_context(
self.context
)
self.context = ctypes.c_void_p()
if status:
raise self.error("destroying bridge-down context")
@dataclass
class _BridgeBuffers:
payload: Any
scales: Any
output: Any
@dataclass(frozen=True)
class _BridgeBlockBinding:
block_index: int
module: str
up_module: str
down_module: str
up_projection: Any
down_projection: Any
fixed_tensor_scale: float
bridge_tensor_scale: Any
bridge_norm_constant: Any
activation_mode: str
@dataclass
class _BlockTelemetry:
native_calls: int = 0
fallback_calls: int = 0
fallback_reasons: dict[str, int] = field(default_factory=dict)
last_route: str | None = None
last_logical_m: int | None = None
last_stream: int | None = None
def record(self, *, native: bool, reason: str, logical_m: int | None, stream: int | None) -> None:
self.last_route = reason
self.last_logical_m = logical_m
self.last_stream = stream
if native:
self.native_calls += 1
return
self.fallback_calls += 1
self.fallback_reasons[reason] = self.fallback_reasons.get(reason, 0) + 1
def snapshot(self) -> dict[str, Any]:
return {
"native_calls": self.native_calls,
"fallback_calls": self.fallback_calls,
"fallback_reasons": dict(sorted(self.fallback_reasons.items())),
"last_route": self.last_route,
"last_logical_m": self.last_logical_m,
"last_stream": self.last_stream,
}
class BlockImgMlpBridgeRuntime:
def __init__(
self,
*,
bridge_up_library_path: Path,
bridge_down_library_path: Path,
torch: Any,
device_index: int | None = None,
bridge_up_factory: Any = BridgeUpLibrary,
bridge_down_factory: Any = PrequantizedDownLibrary,
) -> None:
self.torch = torch
self.bridge_up_library_path = Path(bridge_up_library_path).resolve()
self.bridge_down_library_path = Path(bridge_down_library_path).resolve()
self.device_index = (
int(device_index)
if device_index is not None
else int(torch.cuda.current_device())
)
self.bridge_up = bridge_up_factory(
self.bridge_up_library_path,
self.device_index,
)
self.bridge_down = bridge_down_factory(
self.bridge_down_library_path,
self.device_index,
)
scalar_bytes = self.bridge_up.scalar_bytes()
if scalar_bytes != 4:
raise RuntimeError(
f"bridge scalar ABI changed: expected 4 bytes, got {scalar_bytes}"
)
self._buffers_by_m: dict[int, _BridgeBuffers] = {}
self._bindings_by_block: dict[int, _BridgeBlockBinding] = {}
self._telemetry_by_block: dict[int, _BlockTelemetry] = {}
self._enabled = True
self._enabled_blocks: set[int] = set()
self._bound_stream: int | None = None
self._closed = False
def _telemetry(self, block_index: int) -> _BlockTelemetry:
return self._telemetry_by_block.setdefault(block_index, _BlockTelemetry())
def _logical_m_reason(self, logical_m: int) -> str | None:
if logical_m <= 0:
return "logical_m_non_positive"
if logical_m % 8:
return "logical_m_not_divisible_by_8"
if logical_m > 6400:
return "logical_m_above_max"
return None
def _require_logical_m(self, logical_m: int) -> None:
reason = self._logical_m_reason(logical_m)
if reason == "logical_m_non_positive":
raise RuntimeError("bridge candidate requires a positive logical M")
if reason == "logical_m_not_divisible_by_8":
raise RuntimeError(
"bridge candidate only supports logical M divisible by 8"
)
if reason == "logical_m_above_max":
raise RuntimeError(
"bridge candidate only supports logical M up to 6400"
)
def _buffers(self, logical_m: int, device: Any) -> _BridgeBuffers:
self._require_logical_m(logical_m)
cached = self._buffers_by_m.get(int(logical_m))
if cached is not None:
return cached
payload_bytes = self.bridge_up.helper("output_fp4_bytes", logical_m, UP_OUT_FEATURES)
scale_bytes = self.bridge_up.helper("output_scale_bytes", logical_m, UP_OUT_FEATURES)
output_bytes = self.bridge_down.helper("output_bytes", logical_m, DOWN_OUT_FEATURES)
output_elements = output_bytes // self.torch.tensor(
[],
dtype=self.torch.bfloat16,
).element_size()
expected_elements = int(logical_m) * DOWN_OUT_FEATURES
if output_elements != expected_elements:
raise RuntimeError(
"bridge-down output byte contract changed: "
f"{output_elements} elements != {expected_elements}"
)
buffers = _BridgeBuffers(
payload=self.torch.empty(payload_bytes, dtype=self.torch.uint8, device=device),
scales=self.torch.empty(scale_bytes, dtype=self.torch.uint8, device=device),
output=self.torch.empty(
expected_elements,
dtype=self.torch.bfloat16,
device=device,
).view(int(logical_m), DOWN_OUT_FEATURES),
)
self._buffers_by_m[int(logical_m)] = buffers
return buffers
def bind_block(
self,
*,
block_index: int,
module: str,
up_module: str,
down_module: str,
up_projection: Any,
down_projection: Any,
fixed_tensor_scale: float,
activation_mode: str,
) -> _BridgeBlockBinding:
checked_block_index = _require_block_index(block_index)
if checked_block_index in self._bindings_by_block:
raise RuntimeError(f"bridge block {checked_block_index} already installed")
scale_value = _require_positive_finite(
fixed_tensor_scale,
f"bridge fixed tensor scale for block {checked_block_index}",
)
binding = _BridgeBlockBinding(
block_index=checked_block_index,
module=module,
up_module=up_module,
down_module=down_module,
up_projection=up_projection,
down_projection=down_projection,
fixed_tensor_scale=scale_value,
bridge_tensor_scale=self.torch.tensor(
[scale_value],
device=f"cuda:{self.device_index}",
dtype=self.torch.float32,
),
bridge_norm_constant=self.torch.tensor(
[1.0 / scale_value],
device=f"cuda:{self.device_index}",
dtype=self.torch.float32,
),
activation_mode=activation_mode,
)
self._bindings_by_block[checked_block_index] = binding
self._enabled_blocks.add(checked_block_index)
self._telemetry(checked_block_index)
return binding
def installed_block_indices(self) -> list[int]:
return sorted(self._bindings_by_block)
@property
def enabled(self) -> bool:
return self._enabled and not self._closed
@property
def enabled_block_indices(self) -> list[int]:
return sorted(self._enabled_blocks)
def set_enabled(
self,
enabled: bool,
selected_blocks: None | int | str | Iterable[int] = None,
) -> None:
if enabled and self._closed:
raise RuntimeError("FP4 bridge runtime is closed")
if selected_blocks is None:
self._enabled = bool(enabled)
return
blocks = normalize_selected_blocks(
selected_blocks,
allowed_blocks=self._bindings_by_block,
)
if enabled:
self._enabled_blocks.update(blocks)
else:
self._enabled_blocks.difference_update(blocks)
def set_active_blocks(
self,
selected_blocks: None | int | str | Iterable[int],
) -> None:
if self._closed:
raise RuntimeError("FP4 bridge runtime is closed")
self._enabled_blocks = normalize_selected_blocks(
selected_blocks,
allowed_blocks=self._bindings_by_block,
)
def reset_telemetry(self) -> None:
self._telemetry_by_block = {
block_index: _BlockTelemetry()
for block_index in self._bindings_by_block
}
def _logical_m_from_shape(self, hidden_states: Any) -> int | None:
shape = getattr(hidden_states, "shape", None)
if shape is None:
return None
if len(shape) < 1:
return None
return int(math.prod(int(value) for value in shape[:-1])) if len(shape) > 1 else 1
def _current_stream_handle(self, hidden_states: Any) -> int:
current_stream = self.torch.cuda.current_stream(hidden_states.device)
return int(current_stream.cuda_stream)
def _route_reason(
self,
hidden_states: Any,
binding: _BridgeBlockBinding,
) -> tuple[bool, str, int | None, int | None]:
logical_m = self._logical_m_from_shape(hidden_states)
if self._closed:
return False, "runtime_closed", logical_m, None
if not self._enabled:
return False, "runtime_disabled", logical_m, None
if binding.block_index not in self._enabled_blocks:
return False, "block_disabled", logical_m, None
if getattr(hidden_states, "device", None) is None:
return False, "missing_device", logical_m, None
if hidden_states.device.type != "cuda":
return False, f"device_{hidden_states.device.type}", logical_m, None
if getattr(hidden_states, "dtype", None) != self.torch.bfloat16:
return False, f"dtype_{hidden_states.dtype}", logical_m, None
if getattr(hidden_states, "ndim", 0) < 1:
return False, "shape_rank", logical_m, None
if int(hidden_states.shape[-1]) != UP_IN_FEATURES:
return False, "shape_last_dim", logical_m, None
if logical_m is None:
return False, "logical_m_unknown", logical_m, None
logical_reason = self._logical_m_reason(int(logical_m))
if logical_reason is not None:
return False, logical_reason, int(logical_m), None
try:
stream = self._current_stream_handle(hidden_states)
except Exception:
return False, "stream_query_failed", int(logical_m), None
if self._bound_stream is not None and self._bound_stream != stream:
return False, "stream_mismatch", int(logical_m), stream
return True, "native", int(logical_m), stream
def _native_forward(
self,
hidden_states: Any,
binding: _BridgeBlockBinding,
*,
logical_m: int,
stream: int,
) -> Any:
flattened = hidden_states.reshape(-1, UP_IN_FEATURES).contiguous()
buffers = self._buffers(logical_m, flattened.device)
current_stream = self.torch.cuda.current_stream(hidden_states.device)
if self._bound_stream is None:
self._bound_stream = stream
self.bridge_up.forward(
flattened,
binding.up_projection.packed_weight,
binding.up_projection.weight_scales,
binding.up_projection.weight_scale,
binding.up_projection.bias,
binding.bridge_norm_constant,
buffers.payload,
buffers.scales,
logical_m,
stream,
)
self.bridge_down.forward(
buffers.payload,
buffers.scales,
binding.bridge_tensor_scale,
binding.down_projection.packed_weight,
binding.down_projection.weight_scales,
binding.down_projection.weight_scale,
binding.down_projection.bias,
buffers.output,
logical_m,
stream,
)
_record_stream_for_tensors(
current_stream,
flattened,
binding.up_projection.packed_weight,
binding.up_projection.weight_scales,
binding.up_projection.weight_scale,
binding.up_projection.bias,
binding.bridge_norm_constant,
buffers.payload,
buffers.scales,
binding.bridge_tensor_scale,
binding.down_projection.packed_weight,
binding.down_projection.weight_scales,
binding.down_projection.weight_scale,
binding.down_projection.bias,
buffers.output,
)
return buffers.output.view(*hidden_states.shape[:-1], DOWN_OUT_FEATURES)
def forward_or_fallback(
self,
hidden_states: Any,
binding: _BridgeBlockBinding,
fallback_module: Any,
) -> Any:
can_launch, reason, logical_m, stream = self._route_reason(
hidden_states,
binding,
)
telemetry = self._telemetry(binding.block_index)
if not can_launch:
telemetry.record(
native=False,
reason=reason,
logical_m=logical_m,
stream=stream,
)
return fallback_module(hidden_states)
try:
output = self._native_forward(
hidden_states,
binding,
logical_m=int(logical_m),
stream=int(stream),
)
except Exception:
telemetry.record(
native=False,
reason="native_error",
logical_m=logical_m,
stream=stream,
)
# A native error can occur after work was enqueued. Do not mix an
# ordinary fallback launch into that same stream.
raise
telemetry.record(
native=True,
reason="native",
logical_m=logical_m,
stream=stream,
)
return output
def telemetry_snapshot(self) -> dict[str, Any]:
return {
str(block_index): self._telemetry(block_index).snapshot()
for block_index in sorted(self._bindings_by_block)
}
def report(self) -> dict[str, Any]:
return {
"bridge_up_library": str(self.bridge_up_library_path),
"bridge_down_library": str(self.bridge_down_library_path),
"block_indices": self.installed_block_indices(),
"enabled": self.enabled,
"enabled_block_indices": self.enabled_block_indices,
"logical_m_contract": {
"divisible_by_8": True,
"max_supported": 6400,
"fallback_on_unsupported": True,
},
"toggle_contract": {
"default_enabled": True,
"global_gate": "set_enabled(bool)",
"block_gate": "set_enabled(bool, selected_blocks=...)",
"native_route_requires_global_and_block_enable": True,
},
"telemetry_by_block": self.telemetry_snapshot(),
"closed": self._closed,
}
def close(self) -> None:
if self._closed:
return
self._closed = True
self._enabled = False
if self.torch.cuda.is_available():
self.torch.cuda.synchronize(self.device_index)
close_error = None
for library in (self.bridge_down, self.bridge_up):
try:
library.close()
except Exception as error: # noqa: BLE001
if close_error is None:
close_error = error
self._bindings_by_block.clear()
self._enabled_blocks.clear()
self._buffers_by_m.clear()
if close_error is not None:
raise close_error
def install_selected_img_mlp_bridges(
transformer: Any,
*,
bridge_up_library_path: str | Path,
bridge_down_library_path: str | Path,
block_tensor_scales: str | Mapping[int | str, float | str],
torch: Any,
enabled: bool = True,
device_index: int | None = None,
bridge_up_factory: Any = BridgeUpLibrary,
bridge_down_factory: Any = PrequantizedDownLibrary,
) -> tuple[BlockImgMlpBridgeRuntime, dict[str, Any]]:
"""Replace selected image MLP blocks with the chained FP4 bridge pair."""
import torch.nn as nn
if not torch.cuda.is_available():
raise RuntimeError("bridge candidate installation requires CUDA")
if transformer.training:
raise RuntimeError("bridge candidate expects an eval-mode transformer")
normalized_scales = normalize_block_tensor_scales(block_tensor_scales)
runtime = BlockImgMlpBridgeRuntime(
bridge_up_library_path=resolve_existing_runtime_file(
bridge_up_library_path,
"bridge-up library",
),
bridge_down_library_path=resolve_existing_runtime_file(
bridge_down_library_path,
"bridge-down library",
),
torch=torch,
device_index=device_index,
bridge_up_factory=bridge_up_factory,
bridge_down_factory=bridge_down_factory,
)
installed: list[dict[str, Any]] = []
try:
for block_index in sorted(normalized_scales):
module_name, up_module_name, down_module_name = _module_names_for_block(
block_index
)
block = transformer.transformer_blocks[block_index]
feed_forward = block.img_mlp
net = getattr(feed_forward, "net", None)
if net is None or len(net) != 3:
raise RuntimeError(
f"{module_name} layout changed; expected 3 net entries"
)
activation = net[0]
up_projection, activation_mode = _resolve_up_projection(
activation,
up_module_name,
)
dropout = net[1]
if not isinstance(dropout, nn.Dropout):
raise RuntimeError(f"{module_name}.net.1 changed; expected Dropout")
down_projection = net[2]
try:
_require_packed_projection(
up_projection,
label=up_module_name,
in_features=UP_IN_FEATURES,
out_features=UP_OUT_FEATURES,
torch=torch,
)
_require_packed_projection(
down_projection,
label=down_module_name,
in_features=UP_OUT_FEATURES,
out_features=DOWN_OUT_FEATURES,
torch=torch,
)
except RuntimeError as error:
raise RuntimeError(
f"{module_name} cannot use the bridge candidate because it is not a packed NVFP4 image MLP block: {error}"
) from error
if up_projection.packed_weight.device != down_projection.packed_weight.device:
raise RuntimeError(
f"{module_name} requires up/down projections on one device"
)
_require_exact_numel(
up_projection.packed_weight,
expected=runtime.bridge_up.helper(
"weight_bytes",
UP_OUT_FEATURES,
UP_IN_FEATURES,
),
label=f"{up_module_name}.packed_weight",
)
_require_exact_numel(
up_projection.weight_scales,
expected=runtime.bridge_up.helper(
"weight_scale_bytes",
UP_OUT_FEATURES,
UP_IN_FEATURES,
),
label=f"{up_module_name}.weight_scales",
)
_require_exact_numel(
up_projection.bias,
expected=runtime.bridge_up.helper("bias_bytes", UP_OUT_FEATURES)
// up_projection.bias.element_size(),
label=f"{up_module_name}.bias",
)
_require_exact_numel(
down_projection.packed_weight,
expected=runtime.bridge_down.helper(
"weight_bytes",
DOWN_OUT_FEATURES,
UP_OUT_FEATURES,
),
label=f"{down_module_name}.packed_weight",
)
_require_exact_numel(
down_projection.weight_scales,
expected=runtime.bridge_down.helper(
"weight_scale_bytes",
DOWN_OUT_FEATURES,
UP_OUT_FEATURES,
),
label=f"{down_module_name}.weight_scales",
)
_require_exact_numel(
down_projection.bias,
expected=runtime.bridge_down.helper("bias_bytes", DOWN_OUT_FEATURES)
// down_projection.bias.element_size(),
label=f"{down_module_name}.bias",
)
binding = runtime.bind_block(
block_index=block_index,
module=module_name,
up_module=up_module_name,
down_module=down_module_name,
up_projection=up_projection,
down_projection=down_projection,
fixed_tensor_scale=normalized_scales[block_index],
activation_mode=activation_mode,
)
class BlockImgMlpBridge(nn.Module):
def __init__(
self,
installed_binding: _BridgeBlockBinding,
original_feed_forward: Any,
) -> None:
super().__init__()
self.binding = installed_binding
self.fallback_module = original_feed_forward
self._xpo3_fp4_bridge = True
self._xpo3_fp4_bridge_block_index = int(
installed_binding.block_index
)
def forward(self, hidden_states: Any) -> Any:
return runtime.forward_or_fallback(
hidden_states,
self.binding,
self.fallback_module,
)
block.img_mlp = BlockImgMlpBridge(binding, feed_forward)
installed.append(
{
"block_index": block_index,
"module": module_name,
"up_module": up_module_name,
"down_module": down_module_name,
"fixed_tensor_scale": float(binding.fixed_tensor_scale),
"bridge_norm_constant": float(binding.bridge_norm_constant.item()),
"activation_mode": activation_mode,
}
)
runtime.set_enabled(bool(enabled))
except Exception:
runtime.close()
raise
metadata = runtime.report()
metadata.update(
{
"mode": "selected_img_mlp",
"block_count": len(installed),
"blocks": installed,
"fixed_tensor_scales_by_block": {
str(entry["block_index"]): entry["fixed_tensor_scale"]
for entry in installed
},
"bridge_up_context_reserved_bytes": runtime.bridge_up.reserved_bytes,
"bridge_down_context_reserved_bytes": runtime.bridge_down.reserved_bytes,
"resident_scalar_bytes": (
sum(
_tensor_bytes(binding.bridge_tensor_scale)
for binding in runtime._bindings_by_block.values()
)
+ sum(
_tensor_bytes(binding.bridge_norm_constant)
for binding in runtime._bindings_by_block.values()
)
),
"reuses_existing_projection_buffers": True,
"validation": {
"no_bf16_post_gelu_intermediate_materialized": True,
"bf16_input_required": True,
"bf16_output_preserved": True,
"dropout_bypassed_in_eval_only": True,
"single_cuda_stream_required_for_native_route": True,
"unsupported_conditions_fallback_before_native_launch": True,
},
}
)
return runtime, metadata
def install_block0_img_mlp_bridge(
transformer: Any,
*,
bridge_up_library_path: str | Path,
bridge_down_library_path: str | Path,
fixed_tensor_scale: float,
torch: Any,
enabled: bool = True,
device_index: int | None = None,
bridge_up_factory: Any = BridgeUpLibrary,
bridge_down_factory: Any = PrequantizedDownLibrary,
) -> tuple[BlockImgMlpBridgeRuntime, dict[str, Any]]:
runtime, metadata = install_selected_img_mlp_bridges(
transformer,
bridge_up_library_path=bridge_up_library_path,
bridge_down_library_path=bridge_down_library_path,
block_tensor_scales={0: fixed_tensor_scale},
torch=torch,
enabled=enabled,
device_index=device_index,
bridge_up_factory=bridge_up_factory,
bridge_down_factory=bridge_down_factory,
)
metadata["mode"] = "block0_img_mlp"
return runtime, metadata
__all__ = [
"BLOCK0_IMG_MLP_MODULE",
"DOWN_MODULE",
"TRANSFORMER_BLOCK_COUNT",
"UP_MODULE",
"BlockImgMlpBridgeRuntime",
"install_block0_img_mlp_bridge",
"install_selected_img_mlp_bridges",
"normalize_block_tensor_scales",
"normalize_selected_blocks",
"resolve_existing_runtime_file",
]
|