vllm.lora.ops.triton_ops.fused_moe_lora_op ¶
_adjust_kernel_inputs ¶
_adjust_kernel_inputs(
num_active_loras: Tensor,
sorted_token_ids: Tensor | None,
expert_ids: Tensor,
)
helper function to adjust kernel inputs when sorted_token_ids is None
Source code in vllm/lora/ops/triton_ops/fused_moe_lora_op.py
_fused_moe_lora_small_batch_kernel ¶
_fused_moe_lora_small_batch_kernel(
x_ptr,
A_ptrs,
B_ptrs,
out_ptr,
topk_weights_ptr,
expert_ids_ptr,
token_lora_mapping_ptr,
adapter_enabled_ptr,
N,
K,
top_k_num,
max_loras,
work_total,
pair_slices,
stride_xm,
stride_xk,
stride_A_lora,
stride_A_expert,
stride_A_r,
stride_A_k,
stride_B_lora,
stride_B_expert,
stride_B_n,
stride_B_r,
stride_om,
stride_on,
slice_n_offset,
n_tiles_per_program,
n_chunks_per_pair_slice,
token_mapping_factor: constexpr,
MUL_ROUTED_WEIGHT: constexpr,
ADD_INPUTS: constexpr,
BLOCK_R: constexpr,
actual_rank: constexpr,
BLOCK_N: constexpr,
BLOCK_K: constexpr,
NUM_SLICES: constexpr,
EVEN_K: constexpr,
)
Persistent fused MoE-LoRA kernel for naive_block_assignment inputs.
Each program owns one (pair × slice × n_chunk) work item. A "chunk" covers n_tiles_per_program consecutive output-N tiles, all of which share a single shrink — so the rank-vector is computed once per program and the A weights for that (lora, expert, slice) are loaded once instead of n_tiles_per_program times.
The wrapper picks n_tiles_per_program to keep the grid close to 2*SM_count: at very small batch (work_total ≤ SM_count) the chunk size collapses to 1 and behaviour matches a per-tile GEMV; as batch grows the chunk grows so we trade some N-axis parallelism for shrink reuse. When work_total exceeds the launched grid, the outer stride loop drains the leftover work units serially.
Source code in vllm/lora/ops/triton_ops/fused_moe_lora_op.py
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 | |
_get_expert_id ¶
_get_expert_id(
expert_ids_ptr,
lora_id,
pid_m,
stride_el,
max_loras,
naive_block_assignment: constexpr,
)
Returns expert_id
Source code in vllm/lora/ops/triton_ops/fused_moe_lora_op.py
_get_lora_id ¶
_get_lora_id(
lora_ids,
token_lora_mapping_ptr,
lora_idx,
pid_m,
top_k_num,
naive_block_assignment: constexpr,
)
Returns lora_id
Source code in vllm/lora/ops/triton_ops/fused_moe_lora_op.py
_get_ptr ¶
_LORA_PTR_DICT collects the required information during profile_run, After this, it remains constant and subsequent usage is through LUT. Refer to: https://github.com/triton-lang/triton/blob/release/3.1.x/python/tutorials/08-grouped-gemm.py
Source code in vllm/lora/ops/triton_ops/fused_moe_lora_op.py
_get_token_offs ¶
_get_token_offs(
sorted_token_ids_ptr,
lora_id,
pid_m,
offs,
stride_tl,
max_loras,
num_valid_tokens,
naive_block_assignment: constexpr,
BLOCK_SIZE_M: constexpr,
)
Returns token offsets
Source code in vllm/lora/ops/triton_ops/fused_moe_lora_op.py
_pick_small_batch_chunk ¶
Pick n_tiles_per_program so the launched grid stays near 2*SM_count.
Sizes for occupancy first (more programs in flight → better latency hiding for the K-loop A/x loads). Once the per-tile grid already exceeds 2*SM_count we increase the chunk size to amortise the shrink cost — at that point the GPU is saturated by per-program work and packing more tiles per program lets the rank_vec be reused.
Source code in vllm/lora/ops/triton_ops/fused_moe_lora_op.py
_run_fused_moe_lora_one_shot ¶
_run_fused_moe_lora_one_shot(
output: Tensor,
qcurr_hidden_states: Tensor,
lora_a_stacked: list[Tensor],
lora_b_stacked: list[Tensor],
topk_weights: Tensor,
sorted_token_ids: Tensor | None,
expert_ids: Tensor,
num_tokens_post_padded: Tensor | None,
token_lora_mapping: Tensor,
max_lora_rank: int,
top_k_num: int,
lora_ids: Tensor,
num_active_loras: Tensor,
adapter_enabled: Tensor,
mul_routed_weight: bool,
block_size_m: int,
add_inputs: bool = True,
) -> None
Fast-path wrapper: launches one fused shrink+expand kernel.
The shape contract matches _fused_moe_lora. output has shape (num_tokens, top_k_num, num_slices * N_per_slice). When add_inputs=True (default) the kernel reads-modifies-writes output in place; when add_inputs=False the kernel overwrites output with the LoRA delta only. The latter is used by the dual-stream path that sums LoRA into the base output on a separate stream.
Source code in vllm/lora/ops/triton_ops/fused_moe_lora_op.py
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 | |
_run_fused_moe_lora_small_batch ¶
_run_fused_moe_lora_small_batch(
output: Tensor,
qcurr_hidden_states: Tensor,
lora_a_stacked: list[Tensor],
lora_b_stacked: list[Tensor],
topk_weights: Tensor,
expert_ids_flat: Tensor,
token_lora_mapping: Tensor,
top_k_num: int,
adapter_enabled: Tensor,
mul_routed_weight: bool,
add_inputs: bool = True,
) -> None
Small-batch GEMV-style wrapper. Naive-block-assignment inputs only.
Shape contract matches _run_fused_moe_lora_one_shot: output is (num_tokens, top_k_num, num_slices * N_per_slice) with contiguous-style strides, expert_ids_flat is the flattened topk_ids of shape (num_tokens * top_k_num,), and the rank-padded LoRA weights live in lora_a_stacked / lora_b_stacked.
The kernel is persistent over (pair × slice × n_chunk) work items — each program does one shrink and reuses the rank vector across n_tiles_per_program expand tiles. The chunk size scales with the pair-slice count so very small batches keep per-tile parallelism while medium batches cut redundant shrinks.
Source code in vllm/lora/ops/triton_ops/fused_moe_lora_op.py
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 | |