Skip to content

Commit 9528f07

Browse files
committed
refine code: todo_1 scale, todo_2 -32, print dequant value
1 parent f38ae46 commit 9528f07

2 files changed

Lines changed: 9 additions & 57 deletions

File tree

csrc/xpu_cutlass_fusion.cpp

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ using ProblemShape = Shape<int, int, int, int>;
6464
// XE_8x16x16_F32BF16BF16F32_TT -> hardware 指令
6565
// Stride<_4, _1, _0> could be optional?
6666
using TileShape = Shape<_256, _256, _32>;
67+
//using TileShape = Shape<_32, _32, _32>;
6768
using TiledMma =
6869
typename TiledMMAHelper<MMA_Atom<XE_8x16x16_F32BF16BF16F32_TT>, Layout<TileShape>,
6970
Layout<Shape<_8, _4, _1>, Stride<_4, _1, _0>>>::TiledMMA;
@@ -246,74 +247,25 @@ class kgemm_4bit_inference_cutlass_dequant {
246247
Tensor<EngineRef, LayoutRef>& tCrA, //mma_A for debug
247248
float* quant_map
248249
) {
249-
int thread_idx = int(ThreadIdxX());
250-
251250
static_assert(is_rmem<EngineIn>::value, "Input tensor for A conversion must come from registers");
252251
static_assert(size_v<LayoutIn> == cosize_v<LayoutIn>);
253252
static_assert(size_v<LayoutOut> == cosize_v<LayoutOut>);
254253

255-
using SrcType = typename EngineIn::value_type;
256-
using DstType = typename EngineOut::value_type;
257-
258-
#if 0
259-
auto const& src = tCrB_src(_, _, _);
260-
auto const& dst = tCrB_dst(_, _, _);
261-
auto const& A_ref = tCrA(_, _, _);
262-
auto pSrc = const_cast<SrcType*>(raw_pointer_cast(src.data()));
263-
auto pDst = const_cast<DstType*>(raw_pointer_cast(dst.data()));
264-
auto pA = const_cast<DstType*>(raw_pointer_cast(A_ref.data()));
265-
constexpr int num_elements = decltype(size(src))::value;
266-
constexpr int num_elements_dst = decltype(size(dst))::value;
267-
constexpr int num_elements_A = decltype(size(A_ref))::value;
268-
269-
if(cute::thread0()) printf("dequant src num_elements = %d, num_elements_dst = %d, num_elements_A = %d\n", num_elements, num_elements_dst, num_elements_A);
270-
#if 0
271-
for(int i=0; i<num_elements_A; i++){
272-
printf("thread_idx = %d, num_elements_A = %d, i = %d, A_value = %f\n", thread_idx, num_elements_A, i, *(pA+i));
273-
}
274-
#endif
275-
276-
for(int i=0; i<num_elements; i++){
277-
auto src_value = *(pSrc + i);
278-
*(pDst + (2 * i)) = static_cast<DstType>(quant_map[src_value >> 4 & 0x0f]);
279-
*(pDst + (2 * i + 1)) = static_cast<DstType>(quant_map[src_value & 0x0f]);
280-
#if 0
281-
printf("thread_idx = %d, num_elements = %d, i = %d, src_value = %d, src_value >> 4 = %d, src_value & 0x0f = %d, quant_map[src_value >> 4] = %f, quant_map[src_value & 0x0f] = %f, dst_value_%d = %f, dst_value_%d = %f\n", thread_idx, num_elements, i, static_cast<int>(src_value), static_cast<int>(src_value >> 4), static_cast<int>(src_value & 0x0f), quant_map[src_value >> 4], quant_map[src_value & 0x0f], 2*i, *(pDst + (2 * i)), 2 * i + 1, *(pDst + (2 * i + 1)));
282-
#endif
283-
}
284-
285-
// 4 x 2 x 16 values for B
286-
// 2 x 16 of these are same K
287-
// 4 different scale/zero values per thread, no exchange needed
288-
//CUTLASS_PRAGMA_UNROLL
289-
// for (int i = 0; i < 4; ++i) {
290-
// //CUTLASS_PRAGMA_UNROLL
291-
// for (int j = 0; j < 32; ++j) {
292-
// tCrB_dst(_, i, _)[j] *= tCrS(i);
293-
// //printf("thread_idx = %d, i = %d, j = %d, scale_value = %f\n", thread_idx, i, j, tCrS(i));
294-
// }
295-
// }
296-
297-
#if 0
298-
for(int i=0; i<num_elements_dst; i++){
299-
printf("thread_idx = %d, num_elements_dst = %d, i = %d, after scaling, dst_value_%d = %f, dst_value_%d = %f\n", thread_idx, num_elements_dst, i, 2*i, *(pDst + (2 * i)), 2 * i + 1, *(pDst + (2 * i + 1)));
300-
}
301-
#endif
302-
303-
#else
304-
for (int i = 0; i < size(tCrB_src); ++i) {
254+
for (int i = 0; i < size(tCrB_src); ++i) {
255+
// uint8_t src_value = tCrB_src(i);
256+
// tCrB_dst(2*i) = static_cast<ElementMMA>(quant_map[(src_value >> 4) & 0x0F]);// * tCrS(i/4) ;
257+
// tCrB_dst(2*i+1) = static_cast<ElementMMA>(quant_map[src_value & 0x0F]);// * tCrS(i/4);
305258
uint8_t packed = tCrB_src(i);
306259
uint8_t high = (packed >> 4) & 0x0F;
307260
uint8_t low = packed & 0x0F;
308261

309262
// 应用缩放因子
310-
float val_high = quant_map[high];// * tCrS(i/32); // 假设每32个元素共享一个scale
311-
float val_low = quant_map[low];// * tCrS(i/32);
263+
float val_high = quant_map[high];// * tCrS(i/4); // 假设每32个元素共享一个scale
264+
float val_low = quant_map[low];// * tCrS(i/4);
312265

313266
tCrB_dst(2*i) = static_cast<ElementMMA>(val_high);
314267
tCrB_dst(2*i+1) = static_cast<ElementMMA>(val_low);
315-
}
316-
#endif
268+
}
317269
}
318270

319271
CUTLASS_DEVICE

tests/test_xpu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class TestXPU:
4747
[torch.uint8],
4848
ids=describe_dtype,
4949
)
50-
@pytest.mark.parametrize("dim", [256], ids=id_formatter("dim"))
50+
@pytest.mark.parametrize("dim", [32], ids=id_formatter("dim"))
5151
def test_gemm_4bit(self, device, dim, dtype, storage_type, quant_storage, double_quant, kind):
5252
errs1 = []
5353
errs2 = []

0 commit comments

Comments
 (0)