File size: 43,293 Bytes
b3a3b15 |
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 |
from functools import partial
from typing import Optional
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import Tensor
from torchvision.ops import StochasticDepth
try:
from flash_attn.ops.triton.layer_norm import layer_norm_fn, RMSNorm
except ImportError:
layer_norm_fn, RMSNorm = None, None
class SelectBlock(nn.Module):
def __init__(
self,
dim,
mixer_cls=None,
mlp_cls=None,
mlp_router=None,
mha_router=None,
norm_cls=nn.LayerNorm,
dropout_cls=nn.Dropout,
prenorm=True,
resid_dropout1=0.0,
resid_dropout2=0.0,
drop_path1=0.0,
drop_path2=0.0,
fused_dropout_add_ln=False,
return_residual=False,
residual_in_fp32=False,
sequence_parallel=False,
mark_shared_params=False,
):
"""
For prenorm=True, this Block has a slightly different structure compared to a regular
prenorm Transformer block.
The standard block is: LN -> MHA -> Dropout -> Add -> LN -> MLP -> Dropout -> Add.
Here we do: Dropout -> Add -> LN -> MHA -> Dropout -> Add -> LN -> MLP, etc.
If you want to do concurrency with CUDA graphs, your shapes must remain fixed
(batch_size, seq_len, etc.) across captures and replays. Also avoid any operations
that cause dynamic shape changes or memory allocations.
"""
super().__init__()
self.prenorm = prenorm
self.fused_dropout_add_ln = fused_dropout_add_ln
self.return_residual = return_residual
self.residual_in_fp32 = residual_in_fp32
if self.residual_in_fp32:
assert self.prenorm, "residual_in_fp32 is only compatible with prenorm=True"
assert mixer_cls is not None and mlp_cls is not None, (
"mixer_cls and mlp_cls cannot be None in SelectBlock"
)
# MHA & MLP submodules
self.mixer = mixer_cls(dim)
self.dropout1 = dropout_cls(resid_dropout1)
self.drop_path1 = StochasticDepth(drop_path1, mode="row")
self.norm1 = norm_cls(dim)
self.mlp = mlp_cls(dim)
self.total_neurons = self.mlp.fc1.weight.shape[0]
# Routers
if mlp_router is not None:
self.mlp_router = mlp_router(dim)
self.skip_attn_router = False
else:
self.mlp_router = None
self.skip_attn_router = True
if mha_router is not None:
self.mha_router = mha_router(dim)
else:
self.mha_router = None
if not isinstance(self.mlp, nn.Identity):
self.dropout2 = dropout_cls(resid_dropout2)
self.drop_path2 = StochasticDepth(drop_path2, mode="row")
self.norm2 = norm_cls(dim)
if self.fused_dropout_add_ln:
assert layer_norm_fn is not None, "Triton layer_norm_fn not installed"
assert isinstance(self.norm1, (nn.LayerNorm, RMSNorm)) and isinstance(self.dropout1, nn.Dropout)
# Mark the norm parameters for sequence parallel / shared params if needed
if sequence_parallel:
for p in self.norm1.parameters():
p._sequence_parallel = True
if hasattr(self, "norm2"):
for p in self.norm2.parameters():
p._sequence_parallel = True
if mark_shared_params:
for p in self.norm1.parameters():
p._shared_params = True
if hasattr(self, "norm2"):
for p in self.norm2.parameters():
p._shared_params = True
self.mlp_topk = None
self.skip_mlp_router = False
self.skip_attn_router = False
# We'll use an extra stream for concurrency
self.sparse_stream = torch.cuda.Stream(device="cuda", priority=0)
self.main_stream = torch.cuda.Stream(device="cuda", priority=-5)
# We'll record events to coordinate concurrency
self.mha_event = torch.cuda.Event(enable_timing=False, blocking=False)
self.mlp_event = torch.cuda.Event(enable_timing=False, blocking=False)
self.use_tensor_parallel = mark_shared_params
if self.use_tensor_parallel:
# save the stream and events in the mixer and mlp classes
self.mlp.router = self.mlp_router
self.mixer.router = self.mha_router
self.mlp_topk_layers = None # this will be a dictionary of layer_idx -> topk value
self.attn_topk_layers = None # this will be a dictionary of layer_idx -> topk value
def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None, **kwargs):
return self.mixer.allocate_inference_cache(batch_size, max_seqlen, dtype=dtype, **kwargs)
def prefill_forward(self, hidden_states: Tensor, residual: Optional[Tensor] = None, mixer_kwargs=None, mixer_subset=None):
hidden_states = self.mixer(hidden_states, **mixer_kwargs)
if mixer_subset is not None:
residual = residual[:, mixer_subset]
if not isinstance(self.mlp, nn.Identity):
if not self.fused_dropout_add_ln:
dropped = self.drop_path2(self.dropout2(hidden_states))
if dropped.shape != residual.shape:
dropped = dropped.view(residual.shape)
residual = (dropped + residual) if residual is not None else dropped
hidden_states = self.norm2(residual.to(dtype=self.norm2.weight.dtype))
if self.residual_in_fp32:
residual = residual.to(torch.float32)
else:
if self.drop_path2.p == 0 or not self.training:
rowscale2 = None
else:
rowscale2 = self.drop_path2(
torch.ones(
hidden_states.shape[:-1],
device=hidden_states.device,
dtype=hidden_states.dtype,
)
)
if hidden_states.shape != residual.shape:
hidden_states = hidden_states.view(residual.shape)
hidden_states, residual = layer_norm_fn(
hidden_states,
self.norm2.weight,
self.norm2.bias,
residual=residual,
eps=self.norm2.eps,
dropout_p=self.dropout2.p if self.training else 0.0,
rowscale=rowscale2,
prenorm=True,
residual_in_fp32=self.residual_in_fp32,
is_rms_norm=isinstance(self.norm2, RMSNorm),
)
hidden_states = self.mlp(hidden_states)
return hidden_states, residual
def decode_forward(self, hidden_states: Tensor, residual: Optional[Tensor] = None, mixer_subset=None, mixer_kwargs=None):
""" Single GPU Decode Forward
Args:
hidden_states (Tensor): _description_
residual (Optional[Tensor], optional): _description_. Defaults to None.
mixer_subset (_type_, optional): _description_. Defaults to None.
"""
curr_stream = torch.cuda.current_stream()
# We want to run MHA & mlp_router in parallel on different streams
router_inputs = hidden_states.squeeze(1) # shape (batch_size, dim)
self.main_stream.wait_stream(curr_stream)
self.sparse_stream.wait_stream(curr_stream)
main_stream = self.main_stream
# if mlp_topk > th * total_neurons, skip mlp router
# if self.mlp_topk > 0.8 * self.total_neurons:
# self.skip_mlp_router = True
# else:
# self.skip_mlp_router = False
# [Sparse stream] mlp_router
if not self.skip_mlp_router:
with torch.cuda.stream(self.sparse_stream):
index_vec = self.mlp_router._select_neurons_topk(router_inputs, topk = self.mlp_topk)
self.sparse_stream.record_event(self.mlp_event)
# [Main stream] MHA
with torch.cuda.stream(main_stream):
batch_head_idx = self.mha_router._select_heads(router_inputs)
hidden_states = self.mixer(
hidden_states,
batch_head_idx=batch_head_idx,
**mixer_kwargs
)
main_stream.record_event(self.mha_event)
# Now we unify after both are done, then do the next steps
with torch.cuda.stream(main_stream):
# Wait on router & MHA
curr_stream.wait_stream(main_stream)
main_stream.wait_event(self.mha_event)
# normal residual / layernorm
if mixer_subset is not None:
residual = residual[:, mixer_subset]
if not isinstance(self.mlp, nn.Identity):
if not self.fused_dropout_add_ln:
dropped = self.drop_path2(self.dropout2(hidden_states))
residual = (dropped + residual) if residual is not None else dropped
hidden_states = self.norm2(
residual.to(dtype=self.norm2.weight.dtype)
)
if self.residual_in_fp32:
residual = residual.to(torch.float32)
else:
if self.drop_path2.p == 0 or not self.training:
rowscale2 = None
else:
rowscale2 = self.drop_path2(
torch.ones(
hidden_states.shape[:-1],
device=hidden_states.device,
dtype=hidden_states.dtype,
)
)
if hidden_states.shape != residual.shape:
hidden_states = hidden_states.view(residual.shape)
hidden_states, residual = layer_norm_fn(
hidden_states,
self.norm2.weight,
self.norm2.bias,
residual=residual,
eps=self.norm2.eps,
dropout_p=self.dropout2.p if self.training else 0.0,
rowscale=rowscale2,
prenorm=True,
residual_in_fp32=self.residual_in_fp32,
is_rms_norm=isinstance(self.norm2, RMSNorm),
)
# hidden_states = self.mlp(hidden_states, index_vec=test_index_vec, index_size=test_index_size)
if self.skip_mlp_router:
hidden_states = self.mlp(hidden_states, index_vec=None)
else:
curr_stream.wait_stream(self.sparse_stream)
main_stream.wait_event(self.mlp_event)
hidden_states = self.mlp(hidden_states, index_vec=index_vec)
curr_stream.wait_stream(main_stream)
curr_stream.wait_stream(self.sparse_stream)
return hidden_states, residual
def tp_decode_forward(self, hidden_states: Tensor, residual: Optional[Tensor] = None, mixer_subset=None, mixer_kwargs=None):
"""
Tensor Parallel Decode Forward
"""
curr_stream = torch.cuda.current_stream()
self.sparse_stream.wait_stream(curr_stream)
# self.main_stream.wait_stream(curr_stream)
router_inputs = hidden_states.squeeze(1) # shape (batch_size, dim)
if self.mlp_topk > 0.8 * self.total_neurons:
self.skip_mlp_router = True
else:
self.skip_mlp_router = False
# attention router is synchronous
batch_head_idx = self.mha_router._select_heads(router_inputs)
# mlp router is asynchronous
if not self.skip_mlp_router:
with torch.cuda.stream(self.sparse_stream):
index_vec = self.mlp_router._select_neurons_topk(router_inputs, topk = self.mlp_topk)
self.sparse_stream.record_event(self.mlp_event)
hidden_states = self.mixer(hidden_states, **mixer_kwargs, batch_head_idx=batch_head_idx)
if mixer_subset is not None:
residual = residual[:, mixer_subset]
if not isinstance(self.mlp, nn.Identity):
if not self.fused_dropout_add_ln:
dropped = self.drop_path2(self.dropout2(hidden_states))
if dropped.shape != residual.shape:
dropped = dropped.view(residual.shape)
residual = (dropped + residual) if residual is not None else dropped
hidden_states = self.norm2(residual.to(dtype=self.norm2.weight.dtype))
if self.residual_in_fp32:
residual = residual.to(torch.float32)
else:
if self.drop_path2.p == 0 or not self.training:
rowscale2 = None
else:
rowscale2 = self.drop_path2(
torch.ones(
hidden_states.shape[:-1],
device=hidden_states.device,
dtype=hidden_states.dtype,
)
)
if hidden_states.shape != residual.shape:
hidden_states = hidden_states.view(residual.shape)
hidden_states, residual = layer_norm_fn(
hidden_states,
self.norm2.weight,
self.norm2.bias,
residual=residual,
eps=self.norm2.eps,
dropout_p=self.dropout2.p if self.training else 0.0,
rowscale=rowscale2,
prenorm=True,
residual_in_fp32=self.residual_in_fp32,
is_rms_norm=isinstance(self.norm2, RMSNorm),
)
# curr_stream.wait_stream(self.sparse_stream)
if self.skip_mlp_router:
hidden_states = self.mlp(hidden_states, index_vec=None)
else:
curr_stream.wait_event(self.mlp_event)
hidden_states = self.mlp(hidden_states, index_vec=index_vec)
return hidden_states, residual
def attn_sparse_forward(self, hidden_states: Tensor, residual: Optional[Tensor] = None, mixer_subset=None, mixer_kwargs=None):
"""
Decode Forward with Sparse Attention Router
"""
# We want to run MHA & mlp_router in parallel on different streams
router_inputs = hidden_states.squeeze(1) # shape (batch_size, dim)
batch_head_idx = self.mha_router._select_heads(router_inputs)
# print(f"hidden_states shape: {hidden_states.shape}")
# print(f"hidden states: {hidden_states}")
hidden_states = self.mixer(hidden_states, batch_head_idx=batch_head_idx, **mixer_kwargs)
# normal residual / layernorm
if mixer_subset is not None:
residual = residual[:, mixer_subset]
if not isinstance(self.mlp, nn.Identity):
if not self.fused_dropout_add_ln:
dropped = self.drop_path2(self.dropout2(hidden_states))
residual = (dropped + residual) if residual is not None else dropped
hidden_states = self.norm2(
residual.to(dtype=self.norm2.weight.dtype)
)
if self.residual_in_fp32:
residual = residual.to(torch.float32)
else:
if self.drop_path2.p == 0 or not self.training:
rowscale2 = None
else:
rowscale2 = self.drop_path2(
torch.ones(hidden_states.shape[:-1], device=hidden_states.device, dtype=hidden_states.dtype,)
)
if hidden_states.shape != residual.shape:
hidden_states = hidden_states.view(residual.shape)
hidden_states, residual = layer_norm_fn(hidden_states, self.norm2.weight, self.norm2.bias, residual=residual,
eps=self.norm2.eps, dropout_p=self.dropout2.p if self.training else 0.0,
rowscale=rowscale2, prenorm=True, residual_in_fp32=self.residual_in_fp32,
is_rms_norm=isinstance(self.norm2, RMSNorm),)
# hidden_states = self.mlp(hidden_states, index_vec=test_index_vec, index_size=test_index_size)
hidden_states = self.mlp(hidden_states)
return hidden_states, residual
def mlp_sparse_forward(self, hidden_states: Tensor, residual: Optional[Tensor] = None, mixer_subset=None, mixer_kwargs=None):
""" Single GPU Decode Forward
Args:
hidden_states (Tensor): _description_
residual (Optional[Tensor], optional): _description_. Defaults to None.
mixer_subset (_type_, optional): _description_. Defaults to None.
"""
curr_stream = torch.cuda.current_stream()
# We want to run MHA & mlp_router in parallel on different streams
router_inputs = hidden_states.squeeze(1) # shape (batch_size, dim)
self.main_stream.wait_stream(curr_stream)
self.sparse_stream.wait_stream(curr_stream)
main_stream = self.main_stream
# if mlp_topk > th * total_neurons, skip mlp router
if self.mlp_topk > 0.8 * self.total_neurons:
self.skip_mlp_router = True
else:
self.skip_mlp_router = False
# [Sparse stream] mlp_router
if not self.skip_mlp_router:
with torch.cuda.stream(self.sparse_stream):
index_vec = self.mlp_router._select_neurons_topk(router_inputs, topk = self.mlp_topk)
self.sparse_stream.record_event(self.mlp_event)
# [Main stream] MHA
with torch.cuda.stream(main_stream):
# batch_head_idx = self.mha_router._select_heads(router_inputs)
hidden_states = self.mixer(
hidden_states,
batch_head_idx=None,
**mixer_kwargs
)
main_stream.record_event(self.mha_event)
# Now we unify after both are done, then do the next steps
with torch.cuda.stream(main_stream):
# Wait on router & MHA
curr_stream.wait_stream(main_stream)
main_stream.wait_event(self.mha_event)
# normal residual / layernorm
if mixer_subset is not None:
residual = residual[:, mixer_subset]
if not isinstance(self.mlp, nn.Identity):
if not self.fused_dropout_add_ln:
dropped = self.drop_path2(self.dropout2(hidden_states))
residual = (dropped + residual) if residual is not None else dropped
hidden_states = self.norm2(
residual.to(dtype=self.norm2.weight.dtype)
)
if self.residual_in_fp32:
residual = residual.to(torch.float32)
else:
if self.drop_path2.p == 0 or not self.training:
rowscale2 = None
else:
rowscale2 = self.drop_path2(
torch.ones(
hidden_states.shape[:-1],
device=hidden_states.device,
dtype=hidden_states.dtype,
)
)
if hidden_states.shape != residual.shape:
hidden_states = hidden_states.view(residual.shape)
hidden_states, residual = layer_norm_fn(
hidden_states,
self.norm2.weight,
self.norm2.bias,
residual=residual,
eps=self.norm2.eps,
dropout_p=self.dropout2.p if self.training else 0.0,
rowscale=rowscale2,
prenorm=True,
residual_in_fp32=self.residual_in_fp32,
is_rms_norm=isinstance(self.norm2, RMSNorm),
)
# hidden_states = self.mlp(hidden_states, index_vec=test_index_vec, index_size=test_index_size)
if self.skip_mlp_router:
hidden_states = self.mlp(hidden_states, index_vec=None)
else:
curr_stream.wait_stream(self.sparse_stream)
main_stream.wait_event(self.mlp_event)
hidden_states = self.mlp(hidden_states, index_vec=index_vec)
curr_stream.wait_stream(main_stream)
curr_stream.wait_stream(self.sparse_stream)
return hidden_states, residual
def forward(
self,
hidden_states: Tensor,
residual: Optional[Tensor] = None,
mixer_subset=None,
mixer_kwargs=None,
mlp_topk=None,
attn_topk=None,
):
"""
This forward pass includes concurrency logic in the decode branch.
If you're capturing with a CUDA graph, the concurrency (two-stream usage) must be
inside the captured region so that the replay reproduces the parallel streams.
"""
# simulation values
if mlp_topk is not None:
self.mlp_topk = mlp_topk
if attn_topk is not None:
self.mha_router.topk = attn_topk
if mixer_kwargs is None:
mixer_kwargs = {"inference_params": None}
else:
# Ensure 'inference_params' key exists
if "inference_params" not in mixer_kwargs:
mixer_kwargs["inference_params"] = None
if self.prenorm:
# --- 1) Prenorm’s dropout/add/layernorm
if not self.fused_dropout_add_ln:
dropped = self.drop_path1(self.dropout1(hidden_states))
residual = (dropped + residual) if residual is not None else dropped
hidden_states = self.norm1(residual.to(dtype=self.norm1.weight.dtype))
if self.residual_in_fp32:
residual = residual.to(torch.float32)
else:
# fused dropout + add + layernorm
if self.drop_path1.p == 0 or not self.training:
rowscale1 = None
else:
rowscale1 = self.drop_path1(
torch.ones(
hidden_states.shape[:-1],
device=hidden_states.device,
dtype=hidden_states.dtype,
)
)
if residual is not None and hidden_states.shape != residual.shape:
hidden_states = hidden_states.view(residual.shape)
hidden_states, residual = layer_norm_fn(
hidden_states,
self.norm1.weight,
self.norm1.bias,
residual=residual,
eps=self.norm1.eps,
dropout_p=self.dropout1.p if self.training else 0.0,
rowscale=rowscale1,
prenorm=True,
residual_in_fp32=self.residual_in_fp32,
is_rms_norm=isinstance(self.norm1, RMSNorm),
)
if mixer_subset is not None:
mixer_kwargs["mixer_subset"] = mixer_subset
# Check if we are in the prefill or decode stage
prefill_stage = (
mixer_kwargs["inference_params"] is None
or mixer_kwargs["inference_params"].seqlen_offset == 0
)
if prefill_stage:
# --- 2) Prefill stage (no concurrency): just do normal forward
hidden_states, residual = self.prefill_forward(hidden_states, residual, mixer_kwargs, mixer_subset)
else:
# --- 3) Decode stage:
if self.mlp_router is None:
# decode stage with only attention router, works with both single gpu and tensor parallel
hidden_states, residual = self.attn_sparse_forward(hidden_states, residual, mixer_subset, mixer_kwargs)
else:
if not self.use_tensor_parallel:
if self.mha_router is None:
# decode stage with mlp routers (opt models and single gpu)
hidden_states, residual = self.mlp_sparse_forward(hidden_states, residual, mixer_subset, mixer_kwargs)
else:
# decode stage with mlp and attention routers (opt models and single gpu)
hidden_states, residual = self.decode_forward(hidden_states, residual, mixer_subset, mixer_kwargs)
else:
# uses both mlp and attention routers in tensor parallel
hidden_states, residual = self.tp_decode_forward(hidden_states, residual, mixer_subset, mixer_kwargs)
return hidden_states, residual
else:
# post-norm architecture not implemented here
raise NotImplementedError
# class SelectBlock(nn.Module):
# def __init__(
# self,
# dim,
# mixer_cls=None,
# mlp_cls=None,
# mlp_router=None,
# mha_router=None,
# norm_cls=nn.LayerNorm,
# dropout_cls=nn.Dropout,
# prenorm=True,
# resid_dropout1=0.0,
# resid_dropout2=0.0,
# drop_path1=0.0,
# drop_path2=0.0,
# fused_dropout_add_ln=False,
# return_residual=False,
# residual_in_fp32=False,
# sequence_parallel=False,
# mark_shared_params=False,
# ):
# """
# For prenorm=True, this Block has a slightly different structure compared to a regular
# prenorm Transformer block.
# The standard block is: LN -> MHA -> Dropout -> Add -> LN -> MLP -> Dropout -> Add.
# Here we do: Dropout -> Add -> LN -> MHA -> Dropout -> Add -> LN -> MLP, etc.
# If you want to do concurrency with CUDA graphs, your shapes must remain fixed
# (batch_size, seq_len, etc.) across captures and replays. Also avoid any operations
# that cause dynamic shape changes or memory allocations.
# """
# super().__init__()
# self.prenorm = prenorm
# self.fused_dropout_add_ln = fused_dropout_add_ln
# self.return_residual = return_residual
# self.residual_in_fp32 = residual_in_fp32
# if self.residual_in_fp32:
# assert self.prenorm, "residual_in_fp32 is only compatible with prenorm=True"
# assert mixer_cls is not None and mlp_cls is not None, (
# "mixer_cls and mlp_cls cannot be None in SelectBlock"
# )
# # MHA & MLP submodules
# self.mixer = mixer_cls(dim)
# self.dropout1 = dropout_cls(resid_dropout1)
# self.drop_path1 = StochasticDepth(drop_path1, mode="row")
# self.norm1 = norm_cls(dim)
# self.mlp = mlp_cls(dim)
# # Routers
# self.mlp_router = mlp_router(dim)
# self.mha_router = mha_router(dim)
# if not isinstance(self.mlp, nn.Identity):
# self.dropout2 = dropout_cls(resid_dropout2)
# self.drop_path2 = StochasticDepth(drop_path2, mode="row")
# self.norm2 = norm_cls(dim)
# if self.fused_dropout_add_ln:
# assert layer_norm_fn is not None, "Triton layer_norm_fn not installed"
# assert isinstance(self.norm1, (nn.LayerNorm, RMSNorm)) and isinstance(self.dropout1, nn.Dropout)
# # Mark the norm parameters for sequence parallel / shared params if needed
# if sequence_parallel:
# for p in self.norm1.parameters():
# p._sequence_parallel = True
# if hasattr(self, "norm2"):
# for p in self.norm2.parameters():
# p._sequence_parallel = True
# if mark_shared_params:
# for p in self.norm1.parameters():
# p._shared_params = True
# if hasattr(self, "norm2"):
# for p in self.norm2.parameters():
# p._shared_params = True
# self.mlp_topk = None
# self.skip_mlp_router = False
# self.skip_attn_router = False
# # We'll use an extra stream for concurrency
# self.sparse_stream = torch.cuda.Stream(device="cuda", priority=0)
# self.main_stream = torch.cuda.Stream(device="cuda", priority=-5)
# # We'll record events to coordinate concurrency
# self.mha_event = torch.cuda.Event(enable_timing=False, blocking=False)
# self.mlp_event = torch.cuda.Event(enable_timing=False, blocking=False)
# self.use_tensor_parallel = mark_shared_params
# if self.use_tensor_parallel:
# # TODO: save the routers in the mixer and mlp classes
# # save the stream and events in the mixer and mlp classes
# pass
# def allocate_inference_cache(self, batch_size, max_seqlen, dtype=None, **kwargs):
# return self.mixer.allocate_inference_cache(batch_size, max_seqlen, dtype=dtype, **kwargs)
# def prefill_forward(self, hidden_states: Tensor, residual: Optional[Tensor] = None, mixer_kwargs=None, mixer_subset=None):
# hidden_states = self.mixer(hidden_states, **mixer_kwargs)
# if mixer_subset is not None:
# residual = residual[:, mixer_subset]
# if not isinstance(self.mlp, nn.Identity):
# if not self.fused_dropout_add_ln:
# dropped = self.drop_path2(self.dropout2(hidden_states))
# if dropped.shape != residual.shape:
# dropped = dropped.view(residual.shape)
# residual = (dropped + residual) if residual is not None else dropped
# hidden_states = self.norm2(residual.to(dtype=self.norm2.weight.dtype))
# if self.residual_in_fp32:
# residual = residual.to(torch.float32)
# else:
# if self.drop_path2.p == 0 or not self.training:
# rowscale2 = None
# else:
# rowscale2 = self.drop_path2(
# torch.ones(
# hidden_states.shape[:-1],
# device=hidden_states.device,
# dtype=hidden_states.dtype,
# )
# )
# if hidden_states.shape != residual.shape:
# hidden_states = hidden_states.view(residual.shape)
# hidden_states, residual = layer_norm_fn(
# hidden_states,
# self.norm2.weight,
# self.norm2.bias,
# residual=residual,
# eps=self.norm2.eps,
# dropout_p=self.dropout2.p if self.training else 0.0,
# rowscale=rowscale2,
# prenorm=True,
# residual_in_fp32=self.residual_in_fp32,
# is_rms_norm=isinstance(self.norm2, RMSNorm),
# )
# hidden_states = self.mlp(hidden_states)
# return hidden_states, residual
# def decode_forward(self, hidden_states: Tensor, residual: Optional[Tensor] = None, mixer_subset=None, mixer_kwargs=None):
# """ Single GPU Decode Forward
# Args:
# hidden_states (Tensor): _description_
# residual (Optional[Tensor], optional): _description_. Defaults to None.
# mixer_subset (_type_, optional): _description_. Defaults to None.
# """
# curr_stream = torch.cuda.current_stream()
# # We want to run MHA & mlp_router in parallel on different streams
# router_inputs = hidden_states.squeeze(1) # shape (batch_size, dim)
# self.main_stream.wait_stream(curr_stream)
# self.sparse_stream.wait_stream(curr_stream)
# # We'll do MHA on the "main_stream" and the router on "sparse_stream"
# main_stream = self.main_stream
# # In a captured region, each 'with torch.cuda.stream(...)' block
# # is replayed in concurrency. The shape must remain consistent.
# # [Sparse stream] mlp_router
# if not self.skip_mlp_router:
# with torch.cuda.stream(self.sparse_stream): # <-- CHANGED
# # index_size, index_vec = self.mlp_router._select_neurons_cuda_safe(router_inputs) # need to fix this; make CUDA Graph safe
# # vec = self.mlp_router(router_inputs)
# index_vec = self.mlp_router._select_neurons_topk(router_inputs, topk = self.mlp_topk)
# self.sparse_stream.record_event(self.mlp_event)
# # [Main stream] MHA
# with torch.cuda.stream(main_stream): # <-- CHANGED
# batch_head_idx = self.mha_router._select_heads(router_inputs)
# hidden_states = self.mixer(
# hidden_states,
# batch_head_idx=batch_head_idx,
# # batch_head_idx=None,
# **mixer_kwargs
# )
# main_stream.record_event(self.mha_event)
# # Now we unify after both are done, then do the next steps
# with torch.cuda.stream(main_stream): # <-- CHANGED
# # Wait on router & MHA
# curr_stream.wait_stream(main_stream)
# main_stream.wait_event(self.mha_event)
# # normal residual / layernorm
# if mixer_subset is not None:
# residual = residual[:, mixer_subset]
# if not isinstance(self.mlp, nn.Identity):
# if not self.fused_dropout_add_ln:
# dropped = self.drop_path2(self.dropout2(hidden_states))
# residual = (dropped + residual) if residual is not None else dropped
# hidden_states = self.norm2(
# residual.to(dtype=self.norm2.weight.dtype)
# )
# if self.residual_in_fp32:
# residual = residual.to(torch.float32)
# else:
# if self.drop_path2.p == 0 or not self.training:
# rowscale2 = None
# else:
# rowscale2 = self.drop_path2(
# torch.ones(
# hidden_states.shape[:-1],
# device=hidden_states.device,
# dtype=hidden_states.dtype,
# )
# )
# if hidden_states.shape != residual.shape:
# hidden_states = hidden_states.view(residual.shape)
# hidden_states, residual = layer_norm_fn(
# hidden_states,
# self.norm2.weight,
# self.norm2.bias,
# residual=residual,
# eps=self.norm2.eps,
# dropout_p=self.dropout2.p if self.training else 0.0,
# rowscale=rowscale2,
# prenorm=True,
# residual_in_fp32=self.residual_in_fp32,
# is_rms_norm=isinstance(self.norm2, RMSNorm),
# )
# # Finally do MLP with the router's index vector
# curr_stream.wait_stream(self.sparse_stream)
# main_stream.wait_event(self.mlp_event)
# # hidden_states = self.mlp(hidden_states, index_vec=test_index_vec, index_size=test_index_size)
# if self.skip_mlp_router:
# hidden_states = self.mlp(hidden_states, index_vec=None)
# else:
# hidden_states = self.mlp(hidden_states, index_vec=index_vec)
# curr_stream.wait_stream(main_stream)
# curr_stream.wait_stream(self.sparse_stream)
# return hidden_states, residual
# def tp_decode_forward(self, hidden_states: Tensor, residual: Optional[Tensor] = None, mixer_subset=None, mixer_kwargs=None):
# """
# Tensor Parallel Decode Forward
# Args:
# hidden_states (Tensor): _description_
# residual (Optional[Tensor], optional): _description_. Defaults to None.
# mixer_subset (_type_, optional): _description_. Defaults to None.
# """
# # TODO: need to add routing
# hidden_states = self.mixer(hidden_states, **mixer_kwargs)
# if mixer_subset is not None:
# residual = residual[:, mixer_subset]
# if not isinstance(self.mlp, nn.Identity):
# if not self.fused_dropout_add_ln:
# dropped = self.drop_path2(self.dropout2(hidden_states))
# if dropped.shape != residual.shape:
# dropped = dropped.view(residual.shape)
# residual = (dropped + residual) if residual is not None else dropped
# hidden_states = self.norm2(residual.to(dtype=self.norm2.weight.dtype))
# if self.residual_in_fp32:
# residual = residual.to(torch.float32)
# else:
# if self.drop_path2.p == 0 or not self.training:
# rowscale2 = None
# else:
# rowscale2 = self.drop_path2(
# torch.ones(
# hidden_states.shape[:-1],
# device=hidden_states.device,
# dtype=hidden_states.dtype,
# )
# )
# if hidden_states.shape != residual.shape:
# hidden_states = hidden_states.view(residual.shape)
# hidden_states, residual = layer_norm_fn(
# hidden_states,
# self.norm2.weight,
# self.norm2.bias,
# residual=residual,
# eps=self.norm2.eps,
# dropout_p=self.dropout2.p if self.training else 0.0,
# rowscale=rowscale2,
# prenorm=True,
# residual_in_fp32=self.residual_in_fp32,
# is_rms_norm=isinstance(self.norm2, RMSNorm),
# )
# hidden_states = self.mlp(hidden_states)
# return hidden_states, residual
# def forward(
# self,
# hidden_states: Tensor,
# residual: Optional[Tensor] = None,
# mixer_subset=None,
# mixer_kwargs=None,
# ):
# """
# This forward pass includes concurrency logic in the decode branch.
# If you're capturing with a CUDA graph, the concurrency (two-stream usage) must be
# inside the captured region so that the replay reproduces the parallel streams.
# """
# if mixer_kwargs is None:
# mixer_kwargs = {"inference_params": None}
# else:
# # Ensure 'inference_params' key exists
# if "inference_params" not in mixer_kwargs:
# mixer_kwargs["inference_params"] = None
# if self.prenorm:
# # --- 1) Prenorm’s dropout/add/layernorm
# if not self.fused_dropout_add_ln:
# dropped = self.drop_path1(self.dropout1(hidden_states))
# residual = (dropped + residual) if residual is not None else dropped
# hidden_states = self.norm1(residual.to(dtype=self.norm1.weight.dtype))
# if self.residual_in_fp32:
# residual = residual.to(torch.float32)
# else:
# # fused dropout + add + layernorm
# if self.drop_path1.p == 0 or not self.training:
# rowscale1 = None
# else:
# rowscale1 = self.drop_path1(
# torch.ones(
# hidden_states.shape[:-1],
# device=hidden_states.device,
# dtype=hidden_states.dtype,
# )
# )
# if residual is not None and hidden_states.shape != residual.shape:
# hidden_states = hidden_states.view(residual.shape)
# hidden_states, residual = layer_norm_fn(
# hidden_states,
# self.norm1.weight,
# self.norm1.bias,
# residual=residual,
# eps=self.norm1.eps,
# dropout_p=self.dropout1.p if self.training else 0.0,
# rowscale=rowscale1,
# prenorm=True,
# residual_in_fp32=self.residual_in_fp32,
# is_rms_norm=isinstance(self.norm1, RMSNorm),
# )
# if mixer_subset is not None:
# mixer_kwargs["mixer_subset"] = mixer_subset
# # Check if we are in the prefill or decode stage
# prefill_stage = (
# mixer_kwargs["inference_params"] is None
# or mixer_kwargs["inference_params"].seqlen_offset == 0
# )
# if prefill_stage:
# # --- 2) Prefill stage (no concurrency): just do normal forward
# hidden_states, residual = self.prefill_forward(hidden_states, residual, mixer_kwargs, mixer_subset)
# else:
# # # --- 3) Decode stage:
# if not self.use_tensor_parallel:
# hidden_states, residual = self.decode_forward(hidden_states, residual, mixer_subset, mixer_kwargs)
# else:
# # routing is slightly different in tensor parallel; we overlap the router with allreduce
# hidden_states, residual = self.tp_decode_forward(hidden_states, residual, mixer_subset)
# return hidden_states, residual
# else:
# # post-norm architecture not implemented here
# raise NotImplementedError
|