Skip to content

fix(patches): support conv-shaped LoHa factors - #10048

Merged
Pfannkuchensack merged 3 commits into
invoke-ai:mainfrom
guishiw:fix/10017-loha-conv-factors
Oct 28, 2026
Merged

Pfannkuchensack merged 3 commits into
invoke-ai:mainfrom
guishiw:fix/10017-loha-conv-factors

Conversation

@guishiw

@guishiw guishiw commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Non-Tucker LoHa factors were multiplied without flattening, which fails for Conv2d factors such as [out, rank, 1, 1] and [rank, in, kernel_h, kernel_w].

This change flattens each factor pair to 2D before matrix multiplication. The resulting patch is then reshaped to the original Conv2d weight shape by the existing parameter reconstruction path.

Linear LoHa behavior and the Tucker decomposition path are unchanged.

Related Issues / Discussions

Closes #10017

QA Instructions

Added a CPU tensor regression test using Conv2d-shaped LoHa factors:

  • Input factor shapes:
    • w*_a: [8, 2, 1, 1]
    • w*_b: [2, 4, 3, 3]
  • Before: factor multiplication fails because the convolutional dimensions are not flattened.
  • After: the reconstructed patch has shape [8, 4, 3, 3] and matches the independently calculated expected values.

Checks performed:

  • Focused pytest: 1 passed
  • Ruff check: passed
  • Ruff format check: passed
  • git diff --check: passed

The focused regression was run with real CPU Torch tensors. The complete repository test suite was not run because the locked test-environment dependency synchronization did not complete locally.

Review

No remaining material findings were identified.

The implementation was also checked against linear and Conv2d-style factor shapes. The reshape operations preserve dtype and device placement and do not change the Tucker path.

Remaining limitation: validation was performed on CPU with focused tensor tests; full-suite and GPU integration testing were not available locally.

Checklist

  • The PR has a short but descriptive title, suitable for a changelog
  • Meaningful regression coverage added / updated where needed; obsolete tests/code removed
  • Persisted-state and API changes include required migrations / compatibility validation — N/A, no persisted-state or API changes
  • Relevant performance/efficiency opportunities considered; material claims have evidence
  • Material review findings resolved and relevant checks rerun
  • Documentation added / updated (if applicable) — N/A
  • Updated What's New copy (if doing a release after this PR) — N/A

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

Flatten non-Tucker LoHa factor pairs before multiplication so Conv2d patches can be reconstructed and reshaped to the target weight.
@github-actions github-actions Bot added python PRs that change python files backend PRs that change backend files python-tests PRs that change python tests labels Oct 27, 2026

@Pfannkuchensack Pfannkuchensack left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, this is the fix #10017 asked for. Checked:

  • Repro from #10017. The conv-shaped factor pair (hada_w*_a [8, 2, 1, 1], hada_w*_b [2, 4, 3, 3]) built through any_lora_layer_from_state_dict now patches a [8, 4, 3, 3] weight instead of failing in get_weight.
  • Other layouts. Linear factors and LyCORIS' already-flattened conv factors ([out, rank] / [rank, in*kh*kw]) pass through the reshapes unchanged, _rank() still reads w1_b.shape[0], and the existing get_parameters reshape restores the conv shape. The Tucker path is untouched.
  • Tests. The new test passes; with main's loha_layer.py swapped in it fails. tests/backend/patches: 279 passed, 2 skipped.

@Pfannkuchensack
Pfannkuchensack enabled auto-merge (squash) October 28, 2026 00:11
@Pfannkuchensack
Pfannkuchensack merged commit be25a5b into invoke-ai:main Oct 28, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend PRs that change backend files python PRs that change python files python-tests PRs that change python tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: LoHa on Conv2d layers with conv-shaped factors fails at generation time

2 participants