Skip to content

Catch ValueError when importing comfy_kitchen in quant_ops - #15457

Open
chelsealong wants to merge 2 commits into
Comfy-Org:masterfrom
chelsealong:fix-quant-ops-comfy-kitchen-valueerror-15441
Open

Catch ValueError when importing comfy_kitchen in quant_ops#15457
chelsealong wants to merge 2 commits into
Comfy-Org:masterfrom
chelsealong:fix-quant-ops-comfy-kitchen-valueerror-15441

Conversation

@chelsealong

Copy link
Copy Markdown

Fixes #15441

Problem

On PyTorch < 2.7, importing comfy_kitchen >= 0.2.28 can crash ComfyUI at
startup instead of degrading gracefully. comfy_kitchen's custom ops (e.g.
na3d in backends/eager/na.py) annotate arguments with PEP 585 generics
such as list[int], and torch.library.infer_schema only accepts that
syntax starting with torch>=2.7. On older torch versions the import raises:

ValueError: infer_schema(func): Parameter kernel_size has unsupported type list[int].

comfy/quant_ops.py already guards the comfy_kitchen import to let
ComfyUI keep running (without fp8/fp4 support) when comfy_kitchen is
missing or incompatible, but the guard only caught ImportError, not this
ValueError, so it propagated and crashed startup instead.

This is not ROCm-specific — any environment on torch <= 2.6 (CUDA included)
hits it. See the issue discussion, where this root cause was identified by
@0xDELUXA.

Fix

Widen the except clause in comfy/quant_ops.py to also catch ValueError,
matching the existing pattern already used in comfy/float.py for the same
kind of optional-dependency probing (except (AttributeError, ImportError)).

Test plan

Added tests-unit/comfy_quant/test_quant_ops_import_guard.py, which loads
comfy/quant_ops.py as a standalone module with comfy_kitchen's import
mocked to raise the exact ValueError from the issue, and asserts the module
still imports successfully with _CK_AVAILABLE = False.

Confirmed the test fails without the fix and passes with it:

$ git stash push -- comfy/quant_ops.py   # revert to original except ImportError
$ python -m pytest tests-unit/comfy_quant/test_quant_ops_import_guard.py -v
...
E           ValueError: infer_schema(func): Parameter kernel_size has unsupported type list[int]
=========================== 1 failed in 1.18s ===========================

$ git stash pop   # restore the fix
$ python -m pytest tests-unit/comfy_quant/test_quant_ops_import_guard.py -v
tests-unit/comfy_quant/test_quant_ops_import_guard.py::TestQuantOpsImportGuard::test_survives_comfy_kitchen_schema_value_error PASSED
=========================== 1 passed in 1.53s ===========================

Also ran the existing quant test suite and the broader tests-unit suite
(with comfy_kitchen==0.2.28 actually installed) to confirm no regressions:

$ python -m pytest tests-unit/comfy_quant -v
...8 passed in 2.26s

$ python -m pytest tests-unit -q
...56 failed, 834 passed, 10 skipped, 213 errors   # identical to the
    unmodified baseline (833 passed) aside from the one new passing test;
    the pre-existing failures/errors are environment-only (missing
    pytest-asyncio plugin, torchvision/torch ABI mismatch in this sandbox)
    and reproduce identically without this change.

Ran ruff check on the changed files: all checks passed.

AI disclosure

This PR was prepared with AI assistance (Claude Code).

comfy_kitchen's custom ops (e.g. na3d) annotate arguments with PEP 585
generics like list[int]. torch.library.infer_schema only accepts that
syntax from torch>=2.7, so importing comfy_kitchen on an older torch
raises ValueError instead of ImportError, which the quant_ops import
guard didn't catch, crashing ComfyUI at startup instead of falling
back to fp8/fp4 being unavailable.

Fixes Comfy-Org#15441
@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 23a2e853-deef-4c86-aac3-b4f7c8289570

📥 Commits

Reviewing files that changed from the base of the PR and between 834676a and e7a7fcb.

📒 Files selected for processing (2)
  • comfy/quant_ops.py
  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
📜 Recent review details
🧰 Additional context used
📓 Path-based instructions (6)
**/*

📄 CodeRabbit inference engine (AGENTS.md)

**/*: Keep changes small, direct, and limited to the narrowest necessary code path and smallest number of files.
Prefer practical fixes, minimal dependencies, and existing repository patterns; remove obsolete, dead, unreachable, or unused code.
Preserve existing APIs, node names, model-loading behavior, file layout, and workflow compatibility unless replacement is explicitly intended.
Core ComfyUI must not add outbound internet requests, telemetry, tracking, reporting, remote configuration, or background network activity. User-authorized model downloads are limited to the requested artifact and must exclude telemetry and unrelated metadata.

Files:

  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
  • comfy/quant_ops.py
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

**/*.py: Keep state and capability flags on the object that owns the behavior. Prefer explicit parent-owned attributes over probing child objects with getattr; use child checks only when the child owns the delegated behavior.
Preserve shared method signatures, argument order, return shapes, side effects, and error behavior unless every affected caller and interface is intentionally updated.
Do not add unused compatibility parameters, flags, attributes, constructor options, or model-specific options to shared helpers; keep one-off behavior at the integration boundary.
Normalize third-party return conventions at integration boundaries so core code receives the expected type and shape; avoid undocumented caller-side unwrapping.
Do not add torch.no_grad, torch.inference_mode, or inference-mode wrappers. Do not add model freeze/unfreeze toggles; only disable globally enabled inference mode when a training path requires gradients.
Remove inference-only training behavior such as dropout while preserving checkpoint and state-dict compatibility; use nn.Identity when deleting a module would alter keys or ordering.
Keep imports at module scope except established optional-backend probes or imports required to avoid cycles; avoid unnecessary try/except blocks and use specific exceptions with useful fallbacks.
Do not add workarounds for unsupported library versions, especially PyTorch exception-and-float-cast retries, unless a comment names the exact versions still requiring them.
Let unsupported model formats, invalid quantization metadata, and bad states fail with clear errors instead of silently degrading output.
Match local style, keep comments sparse and useful, and remove comments that merely restate obvious code.
Treat dtype, device placement, VRAM use, and offloading as correctness concerns across CPU, CUDA, ROCm, MPS, DirectML, XPU, NPU, and low-VRAM environments.
Prefer existing ComfyUI and Comfy Kitchen operations, quantization helpers, cast/offload helpe...

Files:

  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
  • comfy/quant_ops.py
**/*.{py,json}

📄 CodeRabbit inference engine (AGENTS.md)

Treat legacy combo, io.Combo, and io.DynamicCombo values affecting filesystem access as untrusted; revalidate them at load/save boundaries with folder_paths, containment checks, or fixed allowlists.

Files:

  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
  • comfy/quant_ops.py
**/*.{py,md,txt,json}

📄 CodeRabbit inference engine (AGENTS.md)

Keep warning and info messages short and actionable, remove noisy or misleading logging, and make documentation edits concise, factual, and tied to changed behavior.

Files:

  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
  • comfy/quant_ops.py
**

⚙️ CodeRabbit configuration file

**: IMPORTANT: Only comment on issues directly introduced by this PR's code changes.
Treat AGENTS.md as mandatory repository policy, not optional style guidance.
Flag PR changes that violate AGENTS.md even when the code is otherwise functional.
In particular, enforce architecture boundaries, dtype/device/memory rules,
interface contracts, import style, no unnecessary try/except blocks, no inline
imports, no outbound internet paths in core ComfyUI, and narrow scoped fixes.
Prefer direct findings over suggestions when a rule is violated. Only ignore
AGENTS.md when it clearly conflicts with a newer explicit maintainer instruction
in the PR.
Do NOT flag pre-existing issues in code that was merely moved, re-indented,
de-indented, or reformatted without logic changes. If code appears in the diff
only due to whitespace or structural reformatting (e.g., removing a with: block),
treat it as unchanged. Contributors should not feel obligated to address
pre-existing issues outside the scope of their contribution.

Files:

  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
  • comfy/quant_ops.py
comfy/**

⚙️ CodeRabbit configuration file

comfy/**: Core ML/diffusion engine. Focus on:

  • Backward compatibility (breaking changes affect all custom nodes)
  • Memory management and GPU resource handling
  • Performance implications in hot paths
  • Thread safety for concurrent execution

Files:

  • comfy/quant_ops.py
🧠 Learnings (3)
📚 Learning: 2026-02-21T14:01:41.482Z
Learnt from: pythongosssss
Repo: Comfy-Org/ComfyUI PR: 12555
File: comfy_extras/nodes_glsl.py:719-724
Timestamp: 2026-02-21T14:01:41.482Z
Learning: In PyOpenGL, bare Python scalars can be accepted for 1-element array parameters by NumberHandler. This means you can pass an int/float directly to OpenGL texture deletion (e.g., glDeleteTextures(tex)) without wrapping in a list. Verify function-specific expectations and ensure types match what the OpenGL call expects; use explicit lists only when the API requires an array.

Applied to files:

  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
  • comfy/quant_ops.py
📚 Learning: 2026-05-13T12:31:45.069Z
Learnt from: rattus128
Repo: Comfy-Org/ComfyUI PR: 13802
File: comfy/pinned_memory.py:19-30
Timestamp: 2026-05-13T12:31:45.069Z
Learning: When reviewing code that uses comfy/pinned_memory.py’s `HostBuffer.extend(size=..., reallocate=...)`: by default (`reallocate` is not True / False), `extend(size=...)` is a *relative increment* that grows the buffer by `size` bytes—so slicing like `[offset:offset+size]` after `hostbuf.extend(size=size)` is correct and the argument should not be rewritten to `offset + size`. Only in the single-segment reallocation mode (`reallocate=True`, e.g., as used by `resize_pin_buffer()` in `comfy/model_management.py`) should `size` be treated as an *absolute target* and the call/arguments should be checked accordingly.

Applied to files:

  • comfy/quant_ops.py
📚 Learning: 2026-08-06T22:18:59.719Z
Learnt from: kijai
Repo: Comfy-Org/ComfyUI PR: 15362
File: comfy/ldm/wan/model_animate2.py:186-223
Timestamp: 2026-08-06T22:18:59.719Z
Learning: When reviewing ComfyUI quantization code, treat `comfy.quant_ops.TensorWiseINT8Layout` and `comfy.quant_ops.TensorCoreConvRotW4A4Layout` as re-exports from `comfy_kitchen`. Validate their behavior against the re-exported `comfy_kitchen` implementations rather than assuming they are local fallback classes.

Applied to files:

  • comfy/quant_ops.py
🔇 Additional comments (3)
comfy/quant_ops.py (1)

72-75: LGTM!

tests-unit/comfy_quant/test_quant_ops_import_guard.py (2)

35-37: LGTM!

Also applies to: 38-60


62-63: 🩺 Stability & Availability

No change needed. The test entry point runs unittest.main() only when the file is executed directly.


📝 Walkthrough

Walkthrough

quant_ops.py now catches schema-related ValueError exceptions during comfy_kitchen import. It logs the failure, sets _CK_AVAILABLE to false, and uses the existing fallback implementations. It re-raises unrelated ValueError exceptions. Unit tests cover both behaviors.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the primary change: handling the known comfy_kitchen import ValueError.
Description check ✅ Passed The description accurately explains the startup crash, the targeted fix, and the added tests.
Linked Issues check ✅ Passed The changes satisfy issue #15441 by suppressing the known infer_schema error while preserving unrelated ValueError failures.
Out of Scope Changes check ✅ Passed The code and tests remain focused on the comfy_kitchen import fallback and issue #15441 requirements.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@comfy/quant_ops.py`:
- Around line 68-71: In the comfy_kitchen import handling around the
`_CK_AVAILABLE` initialization, only convert the known
torch.library.infer_schema PEP 585 `list[int]` incompatibility into the
unavailable fallback. Inspect the caught ValueError’s message or relevant
exception details to identify that specific schema-inference failure, and
re-raise all other ValueError instances unchanged; preserve the existing
ImportError fallback behavior.

In `@tests-unit/comfy_quant/test_quant_ops_import_guard.py`:
- Around line 20-35: In the test around fake_import and spec.loader.exec_module,
assert that triggered is populated immediately after module execution and before
asserting module._CK_AVAILABLE is false, confirming the injected ValueError path
was exercised.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 90a9419b-2240-4d41-af00-f0704b358f89

📥 Commits

Reviewing files that changed from the base of the PR and between 2a68ce3 and 834676a.

📒 Files selected for processing (2)
  • comfy/quant_ops.py
  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
📜 Review details
🧰 Additional context used
📓 Path-based instructions (6)
**/*

📄 CodeRabbit inference engine (AGENTS.md)

**/*: Keep changes small, direct, and limited to the narrowest necessary code path and smallest number of files.
Prefer practical fixes, minimal dependencies, and existing repository patterns; remove obsolete, dead, unreachable, or unused code.
Preserve existing APIs, node names, model-loading behavior, file layout, and workflow compatibility unless replacement is explicitly intended.
Core ComfyUI must not add outbound internet requests, telemetry, tracking, reporting, remote configuration, or background network activity. User-authorized model downloads are limited to the requested artifact and must exclude telemetry and unrelated metadata.

Files:

  • comfy/quant_ops.py
  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
**/*.py

📄 CodeRabbit inference engine (AGENTS.md)

**/*.py: Keep state and capability flags on the object that owns the behavior. Prefer explicit parent-owned attributes over probing child objects with getattr; use child checks only when the child owns the delegated behavior.
Preserve shared method signatures, argument order, return shapes, side effects, and error behavior unless every affected caller and interface is intentionally updated.
Do not add unused compatibility parameters, flags, attributes, constructor options, or model-specific options to shared helpers; keep one-off behavior at the integration boundary.
Normalize third-party return conventions at integration boundaries so core code receives the expected type and shape; avoid undocumented caller-side unwrapping.
Do not add torch.no_grad, torch.inference_mode, or inference-mode wrappers. Do not add model freeze/unfreeze toggles; only disable globally enabled inference mode when a training path requires gradients.
Remove inference-only training behavior such as dropout while preserving checkpoint and state-dict compatibility; use nn.Identity when deleting a module would alter keys or ordering.
Keep imports at module scope except established optional-backend probes or imports required to avoid cycles; avoid unnecessary try/except blocks and use specific exceptions with useful fallbacks.
Do not add workarounds for unsupported library versions, especially PyTorch exception-and-float-cast retries, unless a comment names the exact versions still requiring them.
Let unsupported model formats, invalid quantization metadata, and bad states fail with clear errors instead of silently degrading output.
Match local style, keep comments sparse and useful, and remove comments that merely restate obvious code.
Treat dtype, device placement, VRAM use, and offloading as correctness concerns across CPU, CUDA, ROCm, MPS, DirectML, XPU, NPU, and low-VRAM environments.
Prefer existing ComfyUI and Comfy Kitchen operations, quantization helpers, cast/offload helpe...

Files:

  • comfy/quant_ops.py
  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
**/*.{py,json}

📄 CodeRabbit inference engine (AGENTS.md)

Treat legacy combo, io.Combo, and io.DynamicCombo values affecting filesystem access as untrusted; revalidate them at load/save boundaries with folder_paths, containment checks, or fixed allowlists.

Files:

  • comfy/quant_ops.py
  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
**/*.{py,md,txt,json}

📄 CodeRabbit inference engine (AGENTS.md)

Keep warning and info messages short and actionable, remove noisy or misleading logging, and make documentation edits concise, factual, and tied to changed behavior.

Files:

  • comfy/quant_ops.py
  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
**

⚙️ CodeRabbit configuration file

**: IMPORTANT: Only comment on issues directly introduced by this PR's code changes.
Treat AGENTS.md as mandatory repository policy, not optional style guidance.
Flag PR changes that violate AGENTS.md even when the code is otherwise functional.
In particular, enforce architecture boundaries, dtype/device/memory rules,
interface contracts, import style, no unnecessary try/except blocks, no inline
imports, no outbound internet paths in core ComfyUI, and narrow scoped fixes.
Prefer direct findings over suggestions when a rule is violated. Only ignore
AGENTS.md when it clearly conflicts with a newer explicit maintainer instruction
in the PR.
Do NOT flag pre-existing issues in code that was merely moved, re-indented,
de-indented, or reformatted without logic changes. If code appears in the diff
only due to whitespace or structural reformatting (e.g., removing a with: block),
treat it as unchanged. Contributors should not feel obligated to address
pre-existing issues outside the scope of their contribution.

Files:

  • comfy/quant_ops.py
  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
comfy/**

⚙️ CodeRabbit configuration file

comfy/**: Core ML/diffusion engine. Focus on:

  • Backward compatibility (breaking changes affect all custom nodes)
  • Memory management and GPU resource handling
  • Performance implications in hot paths
  • Thread safety for concurrent execution

Files:

  • comfy/quant_ops.py
🧠 Learnings (3)
📚 Learning: 2026-02-21T14:01:41.482Z
Learnt from: pythongosssss
Repo: Comfy-Org/ComfyUI PR: 12555
File: comfy_extras/nodes_glsl.py:719-724
Timestamp: 2026-02-21T14:01:41.482Z
Learning: In PyOpenGL, bare Python scalars can be accepted for 1-element array parameters by NumberHandler. This means you can pass an int/float directly to OpenGL texture deletion (e.g., glDeleteTextures(tex)) without wrapping in a list. Verify function-specific expectations and ensure types match what the OpenGL call expects; use explicit lists only when the API requires an array.

Applied to files:

  • comfy/quant_ops.py
  • tests-unit/comfy_quant/test_quant_ops_import_guard.py
📚 Learning: 2026-05-13T12:31:45.069Z
Learnt from: rattus128
Repo: Comfy-Org/ComfyUI PR: 13802
File: comfy/pinned_memory.py:19-30
Timestamp: 2026-05-13T12:31:45.069Z
Learning: When reviewing code that uses comfy/pinned_memory.py’s `HostBuffer.extend(size=..., reallocate=...)`: by default (`reallocate` is not True / False), `extend(size=...)` is a *relative increment* that grows the buffer by `size` bytes—so slicing like `[offset:offset+size]` after `hostbuf.extend(size=size)` is correct and the argument should not be rewritten to `offset + size`. Only in the single-segment reallocation mode (`reallocate=True`, e.g., as used by `resize_pin_buffer()` in `comfy/model_management.py`) should `size` be treated as an *absolute target* and the call/arguments should be checked accordingly.

Applied to files:

  • comfy/quant_ops.py
📚 Learning: 2026-08-06T22:18:59.719Z
Learnt from: kijai
Repo: Comfy-Org/ComfyUI PR: 15362
File: comfy/ldm/wan/model_animate2.py:186-223
Timestamp: 2026-08-06T22:18:59.719Z
Learning: When reviewing ComfyUI quantization code, treat `comfy.quant_ops.TensorWiseINT8Layout` and `comfy.quant_ops.TensorCoreConvRotW4A4Layout` as re-exports from `comfy_kitchen`. Validate their behavior against the re-exported `comfy_kitchen` implementations rather than assuming they are local fallback classes.

Applied to files:

  • comfy/quant_ops.py
🔇 Additional comments (1)
tests-unit/comfy_quant/test_quant_ops_import_guard.py (1)

1-17: LGTM!

Also applies to: 36-39

Comment thread comfy/quant_ops.py
Comment thread tests-unit/comfy_quant/test_quant_ops_import_guard.py
Re-raise ValueErrors from comfy_kitchen import that aren't the known
torch.library.infer_schema list[int] incompatibility, so unrelated
failures (e.g. bad backend state) surface instead of silently
degrading to the fallback. Also assert the mocked import was actually
intercepted in the existing test.
@chelsealong

Copy link
Copy Markdown
Author

Addressed both CodeRabbit comments: comfy/quant_ops.py now re-raises any ValueError from the comfy_kitchen import that isn't the known infer_schema schema-inference failure (checks for "infer_schema" in the message), so unrelated failures like a bad backend state surface instead of being swallowed. Added a test (test_reraises_unrelated_value_error) confirming an unrelated ValueError propagates, and added the requested assertTrue(triggered, ...) check to the existing test.

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.

ComfyUI startup crash on ROCm torch 2.5.1+rocm6.2 with comfy_kitchen 0.2.28 (torch custom op schema infer_schema: kernel_size list[int])

1 participant