Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.

Commit 9bdcbf8

Browse files
authored
[Dev] Fix a correctness issue when block reduce is applied with pipeline stage (#94)
* Refactor BatchMatMulEmitter and BatchMatMulSelector for improved readability and maintainability * Refactor import statements for improved readability and maintainability * Refactor import statements for improved readability and maintainability * disable failure email for ci * remove email notifications. * move relax pass from testing to mlc_llm * Refactor scripts with se check_eual_ref_scripts_with_emitter function * Lint Fix * Refactor scripts with se check_eual_ref_scripts_with_emitter function * bug fix in test * lint fix. * test cuda i4 kernel * Refactor copyright notice in i4matmul.hpp * Refactor BitBLASLinear test module for improved readability and maintainability * refactor test as version below python 3.9 cannot handle int32 overflow. * format lint for test * Refactor test_int4b_fp16_convert.py for improved readability and maintainability * remove unused design file * move tile device from package to base * dummy impl for codegen * Refactor file structure for ladder_permutate module * Refactor backend class and fix typos in comments * Deep refactor Lib related code. * remove ci pull. * LintFix * refactor builder for whl build * Refactor TIRWrapper.wrap() method to include an assertion for the optimized module * Refactor lib_generator to set library and source paths * lint fix * BitNet vllm integration * chore: update codespell to version 2.3.0 * Lintfix * Bump version to 0.0.1.dev13 * lint fix * disable fast decoding [u]int4xint8 by default. * optimize from dict design in Hint * Implement SplitK * bitnet benchmark generation. * Add benchmark script for BitNet integration * AtomicAdd Support * LintFix * ci fix when 3rdparty tvm is initialized. * bug fix for setup * fix a bug in block reduce * typo fix * BUG Fix for block reduce. * Lint fix * Refactor block reduce schedule template
1 parent 853522d commit 9bdcbf8

2 files changed

Lines changed: 23 additions & 13 deletions

File tree

bitblas/base/roller/policy/tensorcore.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
from bitblas import tvm
55
from typing import Dict, List, Tuple, Optional
66
import numpy as np
7-
7+
import logging
88
from ...arch import TileDevice
99
from ..hint import Hint, Stride, TileDict, IntrinInfo
1010
from ..node import PrimFuncNode
1111
from .common import coalesced_factor, factorize, get_all_factors
1212
from .default import DefaultPolicy
1313
from ..rasterization import NoRasterization, Rasterization2DColumn
1414

15+
logger = logging.getLogger(__name__)
16+
1517

1618
class TensorCorePolicy(DefaultPolicy):
1719

@@ -47,9 +49,9 @@ def _legalize_info(self):
4749
self.use_async_copy = False
4850
# TODO: block reduction depth is not used for now.
4951
# As there still exists some performance issues for block reduction.
50-
# block_reduction_depth = self.prim_func_node.get_tag("block_reduction_depth")
51-
# if block_reduction_depth:
52-
# self.block_reduction_depth = block_reduction_depth
52+
block_reduction_depth = self.prim_func_node.get_tag("block_reduction_depth")
53+
if block_reduction_depth:
54+
self.block_reduction_depth = block_reduction_depth
5355

5456
def _compute_tc_strides(
5557
self,
@@ -120,7 +122,6 @@ def _check_small_tile(td: TileDict):
120122

121123
smem_limit = min(self.arch.max_smem_usage // td.block_per_SM, self.arch.smem_cap)
122124
rstep_map = td.rstep_map.copy()
123-
is_block_reduction = self.block_reduction_depth is not None
124125

125126
def _optimize(node, rstep):
126127
all_steps = self.get_node_reduce_step_candidates(node)
@@ -185,12 +186,12 @@ def _enlarge(rstep_id):
185186
rstep = _optimize(node, rstep_map)
186187
rstep_map = rstep
187188

188-
if is_block_reduction:
189-
# If block reduction, we should constrain the max value is 64
190-
# Otherwise it will introduce an issue of cuda invalid args.
191-
MAX_REDUCE_K = 64
192-
for k in rstep_map:
193-
rstep_map[k] = min(rstep_map[k], MAX_REDUCE_K)
189+
# if is_block_reduction:
190+
# # If block reduction, we should constrain the max value is 64
191+
# # Otherwise it will introduce an issue of cuda invalid args.
192+
# MAX_REDUCE_K = 64
193+
# for k in rstep_map:
194+
# rstep_map[k] = min(rstep_map[k], MAX_REDUCE_K)
194195
td.rstep_map = rstep_map
195196
td.smem_cost, td.cached_tensors_map = self._compute_shared_memory_usage(td)
196197
return
@@ -315,7 +316,12 @@ def _score(node, thread): # small is better
315316
if intrin_info["out_dtype"] in ["float32"]:
316317
codegen_dict.shared_scope = "shared.dyn"
317318
# smem capacity
318-
if td.smem_cost > self.arch.smem_cap:
319+
# TODO: This is a dummy mul which avoid reusing some shared memory.
320+
# Should be removed in the future.
321+
if td.smem_cost > (self.arch.smem_cap * 1.3):
322+
info_message = f"Tile Dict: {td.output_tile} Shared memory exceeds the static capacity," \
323+
" use dynamic shared memory."
324+
logger.info(info_message)
319325
codegen_dict.shared_scope = "shared.dyn"
320326

321327
codegen_dict.complete_config(node)

bitblas/gpu/matmul_mma_dequantize.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1986,7 +1986,7 @@ def get_param_indices(
19861986
k0, kr = sch.split(k0, [None, reduce_k])
19871987

19881988
sch.reorder(i0, j0, i1, j1, i2, j2, kr, k0, k1, i3, j3)
1989-
# sch.reorder(i0, j0, i1, j1, i2, j2, k0, k1, i3, j3)
1989+
19901990
block_idy = sch.fuse(i0, j0)
19911991
block_idx = sch.fuse(i1, j1)
19921992
thread_idy = i2
@@ -1998,6 +1998,10 @@ def get_param_indices(
19981998
thread_idz = j2 = thread_idy = sch.fuse(thread_idy, thread_idz)
19991999
sch.bind(thread_idy, "threadIdx.y")
20002000

2001+
# Put the thread binding after the shared memory prefetch
2002+
# Otherwise there's a axis missing bug behind tvm
2003+
sch.bind(kr, "threadIdx.z")
2004+
20012005
def smooth_layout_recover(block, scope, l=16, r=16, enable=True): # noqa: E741
20022006
if not enable:
20032007
return

0 commit comments

Comments
 (0)