Skip to content

Add binary group quantization and WebGPU sampler fixes for Bonsai/WebLLM - #3472

Open
HMDRAMS-DEV wants to merge 1 commit into
mlc-ai:mainfrom
HMDRAMS-DEV:bonsai-webllm-support
Open

Add binary group quantization and WebGPU sampler fixes for Bonsai/WebLLM#3472
HMDRAMS-DEV wants to merge 1 commit into
mlc-ai:mainfrom
HMDRAMS-DEV:bonsai-webllm-support

Conversation

@HMDRAMS-DEV

Copy link
Copy Markdown

Summary

  • add q1f16_0 binary group quantization support and wire it into Qwen3 quantization registration
  • fix target kind checks in WebGPU-related compiler passes so sampler/logit logic recognizes webgpu correctly
  • add a regression test covering sampler attachment on WebGPU

Test plan

  • run the new tests/python/compiler_pass/test_attach_sampler.py regression test
  • manually verify the q1f16_0 path can be selected for Qwen3 models

Notes

  • this PR contains the upstreamable mlc-llm changes only
  • local Bonsai rebuild scripts and project-specific website integration were intentionally kept out of the diff

Made with Cursor

Register q1f16_0 for Qwen3 models, fix target-kind checks used by WebGPU compiler passes, and add a regression test for sampler attachment on WebGPU.

Made-with: Cursor

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces binary group quantization (1-bit weights with per-group FP16 scales) to the MLC LLM framework. It includes the implementation of BinaryGroupQuantize, BinaryGroupQuantizeLinear, and BinaryGroupQuantizeEmbedding modules, along with integration into the Qwen3 model configuration. Additionally, the PR refactors target kind checks across several compiler passes to use .kind.name for consistency and adds unit tests for the GPU sampler attachment logic. Feedback is provided regarding the tensor parallel alignment check in the new quantization module, where using tirx.ceildiv is recommended to ensure consistent group count calculations when dimensions are not multiples of the group size.

Comment on lines +287 to +295
if num_shards > 1 and (in_features * num_shards // config.group_size) % num_shards != 0:
raise ValueError(
f"The linear dimension {in_features * num_shards} has "
f"{in_features * num_shards // config.group_size} groups under group size "
f"{config.group_size}. The groups cannot be evenly distributed on "
f"{num_shards} GPUs.\n"
"Possible solutions: reduce number of GPUs, or use quantization with smaller "
"group size."
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The tensor parallel alignment check uses in_features * num_shards // config.group_size to calculate the total number of groups. However, if the global input dimension is not a multiple of the group size, this floor division will underestimate the total group count compared to the ceildiv used for num_group on line 285. This could lead to a ValueError even if the shards are correctly sized, or conversely, allow an invalid configuration if the total groups cannot be evenly distributed. It is safer to use tirx.ceildiv for the global group count calculation to ensure consistency with the per-shard allocation.

Suggested change
if num_shards > 1 and (in_features * num_shards // config.group_size) % num_shards != 0:
raise ValueError(
f"The linear dimension {in_features * num_shards} has "
f"{in_features * num_shards // config.group_size} groups under group size "
f"{config.group_size}. The groups cannot be evenly distributed on "
f"{num_shards} GPUs.\n"
"Possible solutions: reduce number of GPUs, or use quantization with smaller "
"group size."
)
if num_shards > 1 and (tirx.ceildiv(in_features * num_shards, config.group_size)) % num_shards != 0:
raise ValueError(
f"The linear dimension {in_features * num_shards} has "
f"{tirx.ceildiv(in_features * num_shards, config.group_size)} groups under group size "
f"{config.group_size}. The groups cannot be evenly distributed on "
f"{num_shards} GPUs.\n"
"Possible solutions: reduce number of GPUs, or use quantization with smaller "
"group size."
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant