2026-07-30 15:33:44 +00:00
|
|
|
|
FROM git.modelhub.org.cn:9443/enginex-iluvatar/bi100-3.2.3-x86-ubuntu20.04-py3.10-poc-llm-infer:v1.2.3
|
|
|
|
|
|
|
|
|
|
|
|
RUN mkdir /workspace
|
|
|
|
|
|
WORKDIR /workspace/
|
[OPT] PagedAttention V2 implementation — fill the NotImplementedError hole
The single biggest performance bottleneck in the baseline:
paged_attention_v2 = raise NotImplementedError()
paged_attn.py: use_v1 = True (hardcoded to avoid calling V2)
V1 limitation: processes entire KV sequence in one kernel launch.
For seq_len=100K, this is a single massive attention computation.
V2: splits into PARTITION_SIZE=512 chunks, runs them in parallel,
then reduces with log-sum-exp. 195 parallel partitions vs 1.
Implementation (paged_attention_v2_pytorch.py):
Phase 1: Per-partition attention
- For each (seq, head, partition): compute QK^T, softmax, weighted V sum
- Store partial: tmp_output, exp_sums, max_logits (per partition)
Phase 2: Cross-partition reduction (log-sum-exp)
- global_max = max(max_logits across partitions)
- rescale = exp(partition_max - global_max) × partition_exp_sum
- output = Σ (rescale / total_sum) × partition_output
This is the same algorithm as vllm's paged_attention_v2_kernel.cu:
- The reduction pattern is identical to CCCL's block_reduce_warp_reductions
(combine partial statistics from independent segments)
- The online softmax tiling is the same as Flash Attention's partitioning
Integration:
- patch_paged_attention_v2.py patches _custom_ops.py and paged_attn.py
- Removes use_v1=True hardcode → V2 used for seq_len > 8192
- Dockerfile adds the patch step
This is a PyTorch implementation (no CUDA compilation needed).
Next step: if /usr/local/corex/ has ixcc or nvcc-compatible compiler,
replace with compiled CUDA kernel for further speedup.
2026-07-30 15:40:14 +00:00
|
|
|
|
|
2026-08-03 08:30:16 +00:00
|
|
|
|
# Copy all scripts, V2 kernels, CCCL-tuned prefill, and muh dispatch
|
2026-07-30 15:33:44 +00:00
|
|
|
|
COPY ./qwen3_6_scripts /workspace/qwen3_6_scripts
|
[OPT] PagedAttention V2 implementation — fill the NotImplementedError hole
The single biggest performance bottleneck in the baseline:
paged_attention_v2 = raise NotImplementedError()
paged_attn.py: use_v1 = True (hardcoded to avoid calling V2)
V1 limitation: processes entire KV sequence in one kernel launch.
For seq_len=100K, this is a single massive attention computation.
V2: splits into PARTITION_SIZE=512 chunks, runs them in parallel,
then reduces with log-sum-exp. 195 parallel partitions vs 1.
Implementation (paged_attention_v2_pytorch.py):
Phase 1: Per-partition attention
- For each (seq, head, partition): compute QK^T, softmax, weighted V sum
- Store partial: tmp_output, exp_sums, max_logits (per partition)
Phase 2: Cross-partition reduction (log-sum-exp)
- global_max = max(max_logits across partitions)
- rescale = exp(partition_max - global_max) × partition_exp_sum
- output = Σ (rescale / total_sum) × partition_output
This is the same algorithm as vllm's paged_attention_v2_kernel.cu:
- The reduction pattern is identical to CCCL's block_reduce_warp_reductions
(combine partial statistics from independent segments)
- The online softmax tiling is the same as Flash Attention's partitioning
Integration:
- patch_paged_attention_v2.py patches _custom_ops.py and paged_attn.py
- Removes use_v1=True hardcode → V2 used for seq_len > 8192
- Dockerfile adds the patch step
This is a PyTorch implementation (no CUDA compilation needed).
Next step: if /usr/local/corex/ has ixcc or nvcc-compatible compiler,
replace with compiled CUDA kernel for further speedup.
2026-07-30 15:40:14 +00:00
|
|
|
|
COPY ./paged_attention_v2_pytorch.py /workspace/paged_attention_v2_pytorch.py
|
2026-07-30 16:07:09 +00:00
|
|
|
|
COPY ./paged_attention_v2_triton.py /workspace/paged_attention_v2_triton.py
|
2026-08-03 08:30:16 +00:00
|
|
|
|
COPY ./prefix_prefill.py /workspace/prefix_prefill.py
|
|
|
|
|
|
COPY ./muh_dispatch.py /workspace/muh_dispatch.py
|
[OPT] PagedAttention V2 implementation — fill the NotImplementedError hole
The single biggest performance bottleneck in the baseline:
paged_attention_v2 = raise NotImplementedError()
paged_attn.py: use_v1 = True (hardcoded to avoid calling V2)
V1 limitation: processes entire KV sequence in one kernel launch.
For seq_len=100K, this is a single massive attention computation.
V2: splits into PARTITION_SIZE=512 chunks, runs them in parallel,
then reduces with log-sum-exp. 195 parallel partitions vs 1.
Implementation (paged_attention_v2_pytorch.py):
Phase 1: Per-partition attention
- For each (seq, head, partition): compute QK^T, softmax, weighted V sum
- Store partial: tmp_output, exp_sums, max_logits (per partition)
Phase 2: Cross-partition reduction (log-sum-exp)
- global_max = max(max_logits across partitions)
- rescale = exp(partition_max - global_max) × partition_exp_sum
- output = Σ (rescale / total_sum) × partition_output
This is the same algorithm as vllm's paged_attention_v2_kernel.cu:
- The reduction pattern is identical to CCCL's block_reduce_warp_reductions
(combine partial statistics from independent segments)
- The online softmax tiling is the same as Flash Attention's partitioning
Integration:
- patch_paged_attention_v2.py patches _custom_ops.py and paged_attn.py
- Removes use_v1=True hardcode → V2 used for seq_len > 8192
- Dockerfile adds the patch step
This is a PyTorch implementation (no CUDA compilation needed).
Next step: if /usr/local/corex/ has ixcc or nvcc-compatible compiler,
replace with compiled CUDA kernel for further speedup.
2026-07-30 15:40:14 +00:00
|
|
|
|
|
|
|
|
|
|
# Run baseline patches (model registration, xformers fallback, tool parser, etc.)
|
2026-07-30 15:33:44 +00:00
|
|
|
|
RUN cd ./qwen3_6_scripts && ./patch_ops.sh
|
|
|
|
|
|
|
[CRITICAL] Enable ixformer native V1/V2 paged attention kernels
Hardware diagnostics revealed three fatal issues:
1. V1 CRASH: paged_attn.py passes num_kv_heads=4 (int) but ixformer's
vllm_single_query_cached_kv_attention requires head_mapping as Tensor:
torch.repeat_interleave(arange(4), 6) = [0,0,0,0,0,0,1,...,3,3,3,3,3,3]
RuntimeError: Expected Tensor for argument '_4' but found int.
FIX: Convert int→Tensor in _custom_ops.py paged_attention_v1().
2. V2 NATIVE KERNEL EXISTS but was never called:
ixformer has vllm_single_query_cached_kv_attention_v2() — a compiled,
EX-engine-optimized V2 kernel. _custom_ops.py had raise NotImplementedError().
Our Python V2 (paged_attention_v2_pytorch.py) was a workaround for
something that already existed in the runtime.
FIX: Replace NotImplementedError with ixf_F call. V2 signature:
(output, partition, exp_sums, max_logits, temp_output, query,
key_cache, value_cache, head_mapping, scale, block_tables,
context_lens, block_size, max_context_len, alibi_slopes)
Note 'partition' (int) = max_num_partitions, between output and exp_sums.
3. Triton path: installed at /usr/local/lib/python3.10/ but vllm looks in
/usr/local/corex/lib64/python3/. Symlink + sys.path fix.
Impact: This replaces ALL Python attention fallbacks with native kernels.
V1: EX-engine compiled kernel for seq ≤ 8192 (was crashing)
V2: EX-engine compiled kernel for seq > 8192 (was Python fallback)
Combined: expect 10-100x speedup on decode path.
2026-07-31 06:18:32 +00:00
|
|
|
|
# CRITICAL: Enable ixformer native V1/V2 paged attention kernels.
|
|
|
|
|
|
# Fixes: V1 head_mapping int→Tensor, V2 NotImplementedError → native kernel,
|
|
|
|
|
|
# Triton path mismatch.
|
|
|
|
|
|
RUN python3 /workspace/qwen3_6_scripts/patch_ixformer_native.py
|
|
|
|
|
|
|
[OPT] PagedAttention V2 implementation — fill the NotImplementedError hole
The single biggest performance bottleneck in the baseline:
paged_attention_v2 = raise NotImplementedError()
paged_attn.py: use_v1 = True (hardcoded to avoid calling V2)
V1 limitation: processes entire KV sequence in one kernel launch.
For seq_len=100K, this is a single massive attention computation.
V2: splits into PARTITION_SIZE=512 chunks, runs them in parallel,
then reduces with log-sum-exp. 195 parallel partitions vs 1.
Implementation (paged_attention_v2_pytorch.py):
Phase 1: Per-partition attention
- For each (seq, head, partition): compute QK^T, softmax, weighted V sum
- Store partial: tmp_output, exp_sums, max_logits (per partition)
Phase 2: Cross-partition reduction (log-sum-exp)
- global_max = max(max_logits across partitions)
- rescale = exp(partition_max - global_max) × partition_exp_sum
- output = Σ (rescale / total_sum) × partition_output
This is the same algorithm as vllm's paged_attention_v2_kernel.cu:
- The reduction pattern is identical to CCCL's block_reduce_warp_reductions
(combine partial statistics from independent segments)
- The online softmax tiling is the same as Flash Attention's partitioning
Integration:
- patch_paged_attention_v2.py patches _custom_ops.py and paged_attn.py
- Removes use_v1=True hardcode → V2 used for seq_len > 8192
- Dockerfile adds the patch step
This is a PyTorch implementation (no CUDA compilation needed).
Next step: if /usr/local/corex/ has ixcc or nvcc-compatible compiler,
replace with compiled CUDA kernel for further speedup.
2026-07-30 15:40:14 +00:00
|
|
|
|
# 1. PagedAttention V2 — fills the NotImplementedError hole
|
|
|
|
|
|
# Enables partitioned attention for long sequences (>8192 tokens)
|
2026-08-03 06:45:54 +00:00
|
|
|
|
# Deploy BOTH PyTorch and Triton V2 to vllm package — _custom_ops.py
|
|
|
|
|
|
# tries Triton first, falls back to PyTorch if import/runtime fails.
|
|
|
|
|
|
# Triton V2 risk: SMEM=32KB zero margin at head_dim=256 BLOCK_N=32.
|
|
|
|
|
|
# If Triton V2 crashes, PyTorch V2 (batched bmm, no intermediate tensor
|
|
|
|
|
|
# savings but correct) takes over automatically via try/except.
|
2026-08-03 08:30:16 +00:00
|
|
|
|
# Deploy Triton V2 kernel into vllm package
|
2026-08-03 06:45:54 +00:00
|
|
|
|
RUN cp /workspace/paged_attention_v2_triton.py \
|
|
|
|
|
|
/usr/local/corex/lib/python3/dist-packages/vllm/paged_attention_v2_triton.py 2>/dev/null || \
|
|
|
|
|
|
cp /workspace/paged_attention_v2_triton.py \
|
|
|
|
|
|
/usr/local/corex/lib64/python3/dist-packages/vllm/paged_attention_v2_triton.py 2>/dev/null || true
|
[OPT] PagedAttention V2 implementation — fill the NotImplementedError hole
The single biggest performance bottleneck in the baseline:
paged_attention_v2 = raise NotImplementedError()
paged_attn.py: use_v1 = True (hardcoded to avoid calling V2)
V1 limitation: processes entire KV sequence in one kernel launch.
For seq_len=100K, this is a single massive attention computation.
V2: splits into PARTITION_SIZE=512 chunks, runs them in parallel,
then reduces with log-sum-exp. 195 parallel partitions vs 1.
Implementation (paged_attention_v2_pytorch.py):
Phase 1: Per-partition attention
- For each (seq, head, partition): compute QK^T, softmax, weighted V sum
- Store partial: tmp_output, exp_sums, max_logits (per partition)
Phase 2: Cross-partition reduction (log-sum-exp)
- global_max = max(max_logits across partitions)
- rescale = exp(partition_max - global_max) × partition_exp_sum
- output = Σ (rescale / total_sum) × partition_output
This is the same algorithm as vllm's paged_attention_v2_kernel.cu:
- The reduction pattern is identical to CCCL's block_reduce_warp_reductions
(combine partial statistics from independent segments)
- The online softmax tiling is the same as Flash Attention's partitioning
Integration:
- patch_paged_attention_v2.py patches _custom_ops.py and paged_attn.py
- Removes use_v1=True hardcode → V2 used for seq_len > 8192
- Dockerfile adds the patch step
This is a PyTorch implementation (no CUDA compilation needed).
Next step: if /usr/local/corex/ has ixcc or nvcc-compatible compiler,
replace with compiled CUDA kernel for further speedup.
2026-07-30 15:40:14 +00:00
|
|
|
|
RUN python3 /workspace/qwen3_6_scripts/patch_paged_attention_v2.py
|
|
|
|
|
|
|
2026-08-03 08:30:16 +00:00
|
|
|
|
# Deploy CCCL-tuned prefix_prefill.py (SM=16: BLOCK=64, NUM_WARPS=4)
|
|
|
|
|
|
RUN cp /workspace/prefix_prefill.py \
|
|
|
|
|
|
/usr/local/corex/lib/python3/dist-packages/vllm/attention/ops/prefix_prefill.py 2>/dev/null || \
|
|
|
|
|
|
cp /workspace/prefix_prefill.py \
|
|
|
|
|
|
/usr/local/corex/lib64/python3/dist-packages/vllm/attention/ops/prefix_prefill.py 2>/dev/null || true
|
|
|
|
|
|
|
|
|
|
|
|
# Deploy muh_dispatch.py (CCCL-style type dispatch for kernel configs)
|
|
|
|
|
|
RUN cp /workspace/muh_dispatch.py \
|
|
|
|
|
|
/usr/local/corex/lib/python3/dist-packages/vllm/muh_dispatch.py 2>/dev/null || \
|
|
|
|
|
|
cp /workspace/muh_dispatch.py \
|
|
|
|
|
|
/usr/local/corex/lib64/python3/dist-packages/vllm/muh_dispatch.py 2>/dev/null || true
|
|
|
|
|
|
|
2026-07-30 15:41:01 +00:00
|
|
|
|
# 2. Triton kernel tuning: BLOCK=64, NUM_WARPS=4
|
|
|
|
|
|
# SMEM: BLOCK_N=64 × head_dim=128 × 2B × 2(K+V) = 32KB ≤ 48KB
|
|
|
|
|
|
# Occupancy: 4 warps allows 2 blocks/SM vs 1 at 8 warps
|
2026-07-30 15:33:44 +00:00
|
|
|
|
RUN python3 /workspace/qwen3_6_scripts/patch_triton_tuning.py
|
2026-07-30 15:41:01 +00:00
|
|
|
|
|
|
|
|
|
|
# 3. Enable Triton kernels with automatic fallback to PyTorch if they hang
|
|
|
|
|
|
# Triton Flash Attention is 10-50x faster than PyTorch for-loop fallback
|
|
|
|
|
|
RUN python3 /workspace/qwen3_6_scripts/patch_enable_triton.py
|
|
|
|
|
|
|
2026-07-30 16:05:01 +00:00
|
|
|
|
# 5. head_dim=256 support: Qwen3.6 uses head_dim=256
|
|
|
|
|
|
# BLOCK=64 overflows SMEM (64×256×2×2=64KB > 48KB)
|
|
|
|
|
|
# → BLOCK=32 for head_dim=256 (32×256×2×2=32KB ≤ 48KB)
|
|
|
|
|
|
RUN python3 /workspace/qwen3_6_scripts/patch_head256_triton.py
|
|
|
|
|
|
|
2026-07-30 15:41:01 +00:00
|
|
|
|
# 4. Raise decode threshold: compiled paged_attention_v1 up to 65536
|
|
|
|
|
|
# instead of falling back to Python at 32768
|
|
|
|
|
|
RUN python3 /workspace/qwen3_6_scripts/patch_vectorized_decode.py
|