Skip to content

Add text-only mode support for Qwen 3.5 model builder - #2157

Merged
apsonawane merged 11 commits into
mainfrom
asonawane/qwen_text
May 21, 2026
Merged

Add text-only mode support for Qwen 3.5 model builder#2157
apsonawane merged 11 commits into
mainfrom
asonawane/qwen_text

Conversation

@apsonawane

@apsonawane apsonawane commented May 12, 2026

Copy link
Copy Markdown
Contributor

Description

Adds support for running Qwen 3.5 as a standalone text-only LLM (without vision/embedding pipelines).

Changes

  • 2D position_ids support: When exclude_embeds=false, the builder creates a 2D [B, S] position_ids graph input and internally expands it to 3D [3, B, S] for mRoPE compatibility. This allows the standard onnxruntime-genai runtime to provide position_ids without requiring the multimodal pipeline.
  • Tokenizer regex fix: Added save_processing override that patches unsupported \p{M} (Unicode Mark category) from tokenizer regex patterns after export. The C++ std::regex engine in onnxruntime-extensions does not support this Unicode property class.
  • genai_config correctness: The internal 3D expanded tensor name is stored in a separate _pos_ids_3d attribute so self.input_names["position_ids"] remains as "position_ids" — ensuring genai_config.json references the actual graph input.

Usage

python3 -m onnxruntime_genai.models.builder \
  --model_name Qwen/Qwen3.5-2B \
  --precision int4 \
  --execution_provider cuda \
  --output /path/to/output \
  --extra_options exclude_embeds=false prune_lm_head=true int4_algo_config=k_quant_last

Copilot AI review requested due to automatic review settings May 12, 2026 21:47
@apsonawane
apsonawane requested a review from a team as a code owner May 12, 2026 21:47

Copilot AI 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.

Pull request overview

Adds support for building and running Qwen 3.5 as a standalone text-only LLM (without multimodal embedding/vision pipeline), including runtime-side fixes to avoid incorrectly injecting input_ids into decoders that only accept inputs_embeds.

Changes:

  • Add “text-only mode” to the Qwen 3.5 builder, including 2D position_ids support with internal expansion for mRoPE and a tokenizer-regex post-export patch.
  • Fix multimodal runtime logic to check decoder inputs against decoder-only session metadata (avoids false positives from the embedding session).
  • Register qwen3_5_text as an LLM model type in C++.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/python/py/models/builders/qwen.py Adds text-only build path for Qwen 3.5, adjusts position_ids handling, and patches exported tokenizer regex.
src/models/multi_modal.cpp Refines decoder input detection to avoid injecting input_ids when the decoder session doesn’t accept it.
src/models/model_type.h Adds qwen3_5_text to the LLM model-type allowlist.

Comment thread src/python/py/models/builders/qwen.py Outdated
Comment thread src/python/py/models/builders/qwen.py Outdated
Comment thread src/models/multi_modal.cpp Outdated
Comment thread test/python/test_qwen35_text_only.py Fixed

Copilot AI 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.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

Comment thread src/python/py/models/builders/qwen.py Outdated
Comment thread src/python/py/models/builders/qwen.py Outdated
Comment thread src/python/py/models/builders/qwen.py Outdated
Comment thread test/python/test_qwen35_text_only.py Outdated
Comment thread src/python/py/models/builders/qwen.py Outdated
Comment thread src/python/py/models/builders/qwen.py Outdated
Comment thread src/python/py/models/builders/qwen.py
Comment thread src/python/py/models/builders/qwen.py Fixed
apsonawane and others added 2 commits May 15, 2026 18:04
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Comment thread src/python/py/models/builders/qwen.py
@apsonawane
apsonawane enabled auto-merge (squash) May 20, 2026 22:36
@apsonawane
apsonawane merged commit d6c139e into main May 21, 2026
16 of 19 checks passed
@apsonawane
apsonawane deleted the asonawane/qwen_text branch May 21, 2026 08:54
baijumeswani pushed a commit that referenced this pull request May 23, 2026
This PR enables Qwen3.5 text-only INT4 QDQ export and TRT-RTX EP
inference with CUDA graph/shared past-present buffers.

## Structure

The branch is rebased on latest `main` and intentionally split into two
commits:

1. `Add Qwen3.5 text-only export support`
- Mirrors the overlapping Qwen3.5 text-only builder/model-type work from
PR #2157.
- Keeps Qwen3.5 text-only ONNX input `position_ids` as `[B, S]` and
expands inside the graph to `[3, B, S]` for mRoPE.
2. `Enable Qwen3.5 TRT-RTX shared-buffer inference`
- Shares Qwen3.5 recurrent/conv state buffers when
`past_present_share_buffer` is enabled, preserving stable input/output
addresses for TRT-RTX graph replay.
   - Fixes QDQ SkipLayerNorm `output_3` producer wiring.
- Keeps mixed-precision quantization logic in the shared base
`k_quant_linear` path rather than Qwen-local code.
   - Adds the canonical `NvTensorRtRtx` name to the example EP choices.

## PR #2157 compatibility

This branch was compared against #2157 using
`git merge-tree`. The same Qwen files are touched, but Git auto-merges
them cleanly and the simulation produced no conflict markers.

If #2157 merges first, the first commit in this branch is the overlap
and can be dropped/rebased away; the second commit contains the
TRT-RTX-specific delta.

## Validation

- Rebased onto latest upstream `main` (`bf6cf3fe`).
- Built CUDA Release wheel with CUDA 13.2:
`python build.py --use_cuda --cuda_home="C:\Program Files\NVIDIA GPU
Computing Toolkit\CUDA\v13.2" --config Release --update --build
--parallel --skip_tests --skip_examples`
- Installed the rebuilt wheel in the minimal TRT-RTX package
environment.
- Exported and ran Qwen3.5 0.8B and 9B text-only INT4 QDQ models with
TRT-RTX EP + CUDA graph enabled.
- 0.8B: TTFT 1.18s, decode 64.44 tok/s, answer starts: "The history of
artificial intelligence..."
- 9B: TTFT 1.47s, decode 48.00 tok/s, answer is a coherent
reasoning-style response.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants