fix(bit_exact): support grouped/depthwise Conv1D/2D in produce_kif#1485
Open
LarocheC wants to merge 1 commit into
Open
fix(bit_exact): support grouped/depthwise Conv1D/2D in produce_kif#1485LarocheC wants to merge 1 commit into
LarocheC wants to merge 1 commit into
Conversation
The Conv1D/Conv2D produce_kif handler assumed a non-grouped kernel (kernel.shape[-2] == n_chan). For a grouped or depthwise convolution the kernel stores only in_per_group input channels per filter, so the im2col buffer no longer matched the full-channel input and the bit_exact pass raised "could not broadcast input array from shape ..." for groups > 1. Detect the grouped case (kernel.shape[-2] != n_chan) and process each group as an independent standard convolution over its own channel slice, then concatenate along the channel axis. Depthwise is the degenerate groups == n_chan case. The non-grouped path is left byte-for-byte unchanged. Add a test that runs grouped, depthwise and dense QConv1D/QConv2D models through the bit_exact flow on the Vivado, Vitis and oneAPI backends and asserts the assigned output precision represents the quantized Keras output exactly.
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The
produce_kifhandler forConv1D/Conv2Din thebit_exactprecision propagation pass assumes a non grouped kernel, where the kernel's second to last axis spans every input channel (kernel.shape[-2] == n_chan). A grouped or depthwise convolution stores onlyin_per_groupinput channels per filter, so theim2colbuffer no longer matches the full channel input and conversion of a model containing a grouped or depthwise quantized conv fails inside the pass:This is reproduced with an HGQ2
QConv1D(..., groups=g)for anyg > 1(the pass is triggered by theFixedPointQuantizerthe layer inserts).The fix detects the grouped case (
kernel.shape[-2] != n_chan) and processes each group as an independent standard convolution over its own channel slice, then concatenates the per group results along the channel axis. Depthwise is the degenerategroups == n_chan(in_per_group == 1) case. The non grouped branch is the original code, left byte for byte unchanged, so existing models are unaffected.Scope: this fixes precision propagation only. After this change a grouped or depthwise model converts instead of crashing, but the converter and backend codegen still emit a dense matmul for the grouped case, so the compiled numerics are not yet correct for
groups > 1. Routing grouped/depthwise convolutions to a real grouped kernel is a separate follow up; this PR is the prerequisite for it, since any grouped conv support needs thebit_exactpass to handle grouped kernels rather than crash on them. The non grouped path is unaffected.Type of change
Tests
Added
test/pytest/test_bit_exact_grouped_conv.py. For grouped, depthwise and dense (control)QConv1DandQConv2Dmodels it runs the fullbit_exactconversion flow (which raisedValueErrorbefore this fix forgroups > 1) and asserts that the output precision the pass assigns represents the quantized Keras output exactly, with no rounding and no saturation. The check is done both on the single assignedresult_tand per channel throughproduce_kif, across the Vivado, Vitis and oneAPI backends.To reproduce:
Test Configuration: Keras 3.14 (torch backend), HGQ2 0.1.8, Python 3.13. Result: 18 passed.
Checklist
pre-commiton the files I edited or added.