Skip to content

PR5: ruff/pyrefly rollout + real-bug lint fixes#8

Merged
forkni merged 45 commits into
SDTD_032_devfrom
chore/ruff-pyrefly-rollout
Jul 19, 2026
Merged

PR5: ruff/pyrefly rollout + real-bug lint fixes#8
forkni merged 45 commits into
SDTD_032_devfrom
chore/ruff-pyrefly-rollout

Conversation

@forkni

@forkni forkni commented Jul 14, 2026

Copy link
Copy Markdown
Owner

Summary

5 commits adopting an expanded ruff ruleset repo-wide, freezing a pyrefly baseline, fixing real lint-surfaced bugs, and hardening a checkpoint loader.

  • e81e119 style: adopt expanded ruff ruleset + safe auto-fixes repo-wide
  • 6bd9df9 chore: add pyrefly baseline freezing pre-existing type errors
  • d23e79d style: whitelist idiomatic call-defaults; re-enable B008/RUF009
  • d5140d7 fix: chain exception causes + resolve real-bug lints; re-enable rules
  • d25a291 fix: harden RealESRGAN fallback loader with torch.load(weights_only=True)

Part of the chained remediation-PR review stack (PR1 #4 → PR2 #5 → PR3 #6 → PR4 #7 → this). Base branch is PR4's head (refactor/param-schema-fp8-preflight), so this diff is exactly these 5 commits plus the yaml harness artifact.

Test plan

  • CPU-safe unit tests green (tests/unit, correct PYTHONPATH override to this worktree's src/)
  • Automated review (Claude Code Review + Copilot) completes and posts findings
  • Independent bugs-only pass — fix only confirmed genuine bugs, no architectural/behavioral changes

forkni and others added 30 commits July 10, 2026 12:55
…il-fast)

Re-applies the previously-reverted Phase 1 batch from the perf/best-practices
audit: fix the FP8 Q/DQ gate comment to match the actual threshold, convert a
dict() call to a dict literal (clears the one pre-existing ruff C408 on a
touched file), require an explicit opt_batch_size in EngineBuilder.build() and
its 5 compile_* wrappers (real callers already always pass it), drop a
dead try/except around a single logger.info call, and tighten two type hints
in StreamDiffusion.pipeline (Optional/Union/Tuple).

No behavior change except the opt_batch_size fail-fast, which only affects
callers that previously relied on an unused default of 1.
- Add shared _is_oom_error() helper (typed torch.cuda.OutOfMemoryError +
  string-heuristic fallback) and use it at both TensorRT UNet/VAE engine
  build fallback sites instead of duplicated inline substring checks.
- Log swallowed LoRA candidate-weight-name failures in
  _load_lora_with_offline_fallback instead of silently continuing.
- Log the inner fallback failure in advanced-model-detection instead of
  a bare except/pass.
- Hoist the optional diffusers_ipadapter helper imports in
  IPAdapterModule.build_unet_hook out of the per-frame hook into a single
  build-time resolution, and log all previously-silent per-step fallback
  branches for observability.
- Make _load_model fail fast (raise RuntimeError) when an SDXL pipeline
  retry after a type mismatch also fails, instead of silently continuing
  with the wrong pipeline type; also null out the stale mismatched `pipe`
  reference so a later loading-method failure can't let it slip through
  the final success check.
- Add regression tests for _is_oom_error and the SDXL fail-fast path.
Ruff warns that top-level ignore/select/isort/per-file-ignores are
deprecated in favour of the lint section. Move them under
[tool.ruff.lint] (and .isort / .per-file-ignores); line-length stays at
the top level. Verified against cuda-link's pyproject.toml, which
already uses this layout. No behavior change: ruff check/format report
identical results before and after (same 479 pre-existing repo-wide
findings, 0 on the files touched by the Phase 1/2 commits).

Investigated adding a matching [tool.pyrefly] section for parity with
cuda-link, but did not adopt it: pyrefly falls back to a bundled
'basic' preset when no pyrefly config exists, and that preset is
substantially more lenient than explicit config. Adding even an empty
[tool.pyrefly] section opts out of it and jumps reported errors from
72 to ~900 (mostly dynamically-set attributes on StreamDiffusion /
StreamDiffusionWrapper that were never checked before). Doing this
properly needs the same per-module triage cuda-link performed
(docs/adr/0005-static-typing-hardening.md) before turning on strict
project-includes checking, which is out of scope here.
_make_fake_engine (tests/unit/test_trt_engine_guards.py) built engines via
TensorRTEngine.__new__() to bypass __init__, but never set _dedicated_stream,
_pre_exec_event, _post_exec_event, or _buf_cache -- attributes __init__ has
initialized since infer() gained cross-stream sync and LRU shape-cache
support. This caused 3 AttributeErrors long treated as "pre-existing,
out of scope" failures in every prior phase gate.

Not a production bug: __init__ correctly sets all four to None/empty, and
infer() guards each with `is not None`. Fix mirrors __init__ in the fake.
Full unit suite now fully green (91 passed, 0 failed) with no waived tests.
Four targeted fixes from the perf/best-practices audit, quick-wins #4 and #5:

- StreamDiffusion.prepare(): generator defaulted to a pre-constructed
  torch.Generator(), a mutable default evaluated once at def-time so every
  instance sharing the default shared one Generator. Default is now None,
  constructed fresh in the method body.
- StreamDiffusionWrapper.prepare(): both runtime stream.prepare() calls
  (single-prompt and prompt-blending paths) omitted seed, so every runtime
  prompt change silently reset the RNG to stream.prepare()'s own default.
  Now forwards seed=getattr(self.stream, "current_seed", 2) to preserve the
  active seed across prompt changes.
- postprocess_image()'s "latent" branch returned an internal decode buffer
  by reference; callers retaining it across frames would see it mutate.
  Now clones at this public API boundary. The "np" pinned-buffer alias and
  the internal __call__/txt2img fast-path aliases are documented as
  intentional, not cloned (would defeat the pinned-buffer/reuse
  optimizations).
- cleanup_engines_and_rebuild's hardcoded engines_dir = "engines" now
  honors self._engine_dir when set, matching the existing idiom elsewhere
  in the file.
- cleanup_gpu_memory dropped two explicit Engine.__del__() calls; the
  method already does del + triple gc.collect() + empty_cache() +
  ipc_collect() at its tail, and Engine.__del__ is self-guarding, so the
  explicit dunder calls were redundant.

Adds tests/unit/test_phase3_correctness.py (5 regression tests, model-free
CPU-only). Full unit suite: 91 passed, 0 failed (no pre-existing failures
remain -- see cf3df18).
Guard TRT engine and timing-cache writes against truncation from an
interrupted build via a temp-file + os.replace atomic write helper,
matching the idiom already used for FP8 calibration data. Add
self-healing recovery around cudaGraphLaunch (reset + fallback to
plain execution, re-captures next frame) and a stream-sync guard
around the defensive, disabled-by-default Engine.refit() path.

Verified: 3 new CPU-only unit tests cover the atomic-write helper's
happy path and both failure-preservation cases; a live-GPU smoke run
against a cached VAE engine exercised capture, happy-path replay, a
forced cudaGraphLaunch failure (recovery + safe fallback, no crash),
and self-healed re-capture.
Adds 5 graph-safe profiler.region() spans (prep/sdxl_cond/hooks/engine/post)
inside unet_step to attribute the audit's "~66% unattributed" frame time.
Baseline capture (RTX 4090, SDXL-turbo FP8) shows that slice is GPU kernel
time inside the single TRT UNet engine call (~93% of frame), not host
overhead - every host sub-span measures <0.01ms. Instrumentation only, no
behavior change; validated by the existing 94/0 unit suite.
…ub-phase 5.1)

Engine.infer() unconditionally re-bound every tensor address via
set_tensor_address() before each engine call, even in graphed steady
state where the addresses are already baked into the captured CUDA
graph (self.tensors buffers are persistent and reused via copy_() -
see allocate_buffers/_can_reuse_buffers, which resets
cuda_graph_instance to None whenever a buffer is actually
reallocated). Skip the rebind loop once a graph is captured; only
rebind on first capture or after a reset_cuda_graph() recovery.

Verified live on the RTX 4090 td_config.yaml runtime path (the UNet
engine loads with use_cuda_graph=True unconditionally via
engine_manager.py's loader): 24-frame smoke run with real graph
capture + replay shows no errors and frame timing (p50 32.10ms, mean
47.33ms) matching a pre-fix A/B run (p50 31.74ms, mean 47.00ms) -
confirms correctness with the expected ~0 measured perf delta (the
audit's own 5.0 baseline showed host-side region time is <0.01ms;
this is a hygiene/false-dependency fix, not a throughput win).
Suite 94/0; ruff clean; pyrefly 72 errors (baseline, no new).
Three output-side sync/allocation fixes, ordered by real-world impact on
the TD path:

- 5f (only hot-path site): _ipc_pack_rgba / _ipc_pack_unit_rgba now write
  into a persistent HWC x4 GPU buffer (alpha set once at allocation, B/G/R
  channels written in place) instead of allocating a fresh torch.full_like
  alpha + torch.cat every frame. Safe only because the IPC exporters are
  forced to blocking export (ADR-0001); comments updated at both
  _lazy_init_ipc_exporter and _lazy_init_cn_ipc_exporter to reflect the
  buffer is now persistent, not transient.
- 5e: _tensor_to_pil_safe (preprocessing_orchestrator.py) reordered
  CPU-first, following the in-repo base.py:tensor_to_pil template -- the
  D2H transfer now happens before the tensor.min()/.max() range-check
  syncs instead of after, collapsing 3 GPU host syncs down to 1.
- 5d (hygiene; TD's IPC output path never hits this): _tensor_to_pil_optimized
  now routes through the shared _output_pin_buf/_d2h_event pinned-buffer +
  Event machinery instead of a blocking, unpinned .cpu() into pageable
  memory. Documented the same buffer-reuse caveat as the "np" path (PIL
  images returned wrap a view of the pinned buffer; callers retaining them
  across frames must .copy()).

Verified via tests/unit/test_sync_free_output_5_2.py: byte-identical
parity against frozen pre-5.2 reference formulas for all three sites, plus
persistent-buffer reuse/realloc and independent-buffer-identity checks
(10 new tests, GPU-gated). Suite 104/0 (94 baseline + 10 new); ruff clean
on touched files; pyrefly 72 errors (baseline, no new).
…E FAIL

build_engine() passed dynamic_shapes=build_dynamic_shape into Engine.build(),
which tracks only dynamic resolution. On the default "Flexible" preset the
UNet is resolution-static but batch-dynamic (build_static_batch=False), so
dynamic_shapes was passed False, and _apply_gpu_profile_to_config's tiling
branch ran against a graph that still has a symbolic batch dim -- TRT emitted
"[l2tc] VALIDATE FAIL - Graph contains symbolic shape" as a no-op for every
applicable layer, for the entire ~11.5 min production TD session analyzed
this round.

Fix: dynamic_shapes=build_dynamic_shape or not build_static_batch (one line,
utilities.py:1372) -- True whenever any dim, resolution or batch, is
symbolic, matching l2tc's actual requirement (ALL dims must be concrete).
Fully-static engines (e.g. ControlNet: build_static_batch=True,
build_dynamic_shape=False) are unaffected -- still get tiling as before.
Tightened the _apply_gpu_profile_to_config docstring to match.

Verified via new tests/unit/test_l2tc_dynamic_shapes.py: calls build_engine()
with GPU detection/memory query/the real TRT build all monkeypatched out
(no CUDA/TRT context required) and asserts the dynamic_shapes kwarg it wires
into Engine.build() for the previously-broken case (batch-dynamic,
resolution-static -> True), the unaffected fully-static case (-> False), and
the already-working resolution-dynamic case (-> True). Suite 107/0 (104
baseline + 3 new); ruff clean; pyrefly 72 errors (baseline, no new). Impact
is log-spam-only -- zero perf/numerical change, visible on the next engine
rebuild.
… (Sub-phase 5.6)

Engine.infer() copied every feed_dict tensor into its own persistent staging
buffer each frame solely to give TensorRT a stable contiguous address to bind.
nsys profiling (5.0/5.5) attributed ~9% of frame time (p95 3.1ms, ~161 DtoD
copies/frame) to this region on the UNet call, dominated by the kvo/fio
StreamV2V cache inputs.

Those cache tensors are already persistent, address-stable, TRT-contiguous
buffers (create_kvo_cache/create_fi_cache build them with layers_in_bucket as
the outermost dim specifically for this) — the only event that moves their
address is a batch-size change, which already forces a full buffer
realloc+graph reset today. So copying them every frame is pure waste.

Add an opt-in zero_copy_names parameter to Engine.infer() and a pure
_staging_action() decision helper (copy/bind/bind_and_reset) that lets
TensorRT bind directly to the caller's tensor instead of copying into
self.tensors[name], guarded by contiguity and dtype-match fallbacks. Wire it
in UNet2DConditionModelEngine for the kvo/fio input names only; every other
caller (VAE, ControlNet, safety, preprocessing) passes nothing and keeps the
original copy path byte-for-byte.
…ge_shape (bug #6)

ControlNet's dynamic-shape get_input_profile() hardcoded a 384px floor
independent of the UNet's 256px floor (BaseModel.min_image_shape), so any
resolution in [256, 384) hard-failed at Engine.allocate_buffers the moment
ControlNet was enabled. Derive the ControlNet floor/ceiling from the same
BaseModel.min_image_shape/max_image_shape the UNet already uses (SDXL
inherits via super()), lower the matching engine_manager.py build-option
default, and relabel the ControlNet dynamic-engine cache directory
(--dyn-384-1024 -> --dyn-256-1024) so a re-floored build never collides
with a stale 384-floored cache.

Trade-off: widening the dynamic profile can slightly reduce TRT tactic
specialization at the optimum resolution -- acceptable given the
alternative is a hard failure.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…ning_scale scalar (Sub-phase 5.4)

Two per-frame GPU creation syncs removed:

- _normalize_weights built a CUDA tensor from a Python list every call, but
  every consumer (.item()/.tolist() in slerp/multi_slerp/cosine_weighted
  paths, float*cuda_tensor in the linear paths) immediately reads it back
  or treats it as a scalar operand. Build it on CPU float32 instead --
  same consumers work unchanged (a 0-dim CPU tensor is treated as a
  wrapped scalar when multiplied against a CUDA tensor), the sync and
  readback are both gone, and float32 is more precise than the model's
  fp16 for the normalization divide.

- ControlNetModelEngine rebuilt a conditioning_scale tensor via
  torch.tensor(...) on every __call__. Replace with one lazily-allocated
  persistent scalar updated via fill_() (an async kernel launch, no sync),
  mirroring the existing _fi_strength_tensor.fill_() idiom. Each active
  ControlNet has its own engine instance, so a per-instance buffer is
  correct; the binding rank (_cs_rank1) is fixed at load time so the
  shape never changes underneath it.

Also drops three dead variable assignments and fixes one out-of-order
import in stream_parameter_updater.py (pre-existing lint debt in a file
this change already touches; the repo's commit gate lints the whole
staged file, not just the diff).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Removes the cudaStreamSynchronize that NSFWDetectorEngine.__call__ forced on
the hot path via probs[0,0].item() whenever use_safety_checker=True. The
classifier now writes its GPU probability into a pinned host scalar via a
non_blocking copy; _apply_safety_checker reads the *previous* frame's pinned
verdict before launching this frame's classification, mirroring the existing
SimilarImageFilter 1-frame-delayed readback pattern (image_filter.py).

Edge cases: first frame passes through unconditionally (no prior verdict to
act on); a 1-frame detection-leak window is possible before flagging lands.
Both are acceptable for an opt-in, off-by-default, TensorRT-only feature.
Updated tests/unit/test_safety_checker.py for the new (tensor, prob_pin) -> None
checker contract and the delayed-readback semantics (10/10 passing).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The multi-CN merge previously reallocated `merged_down`/`merged_mid` every
frame (`merged_down[j] = merged_down[j] + ds[j]`), so residual pointers
changed every frame. That would thrash a CUDA graph if these tensors are
later bound zero-copy into the UNet's input_control_* inputs (Phase-2 D2).

Replace it with lazily-allocated, shape-guarded persistent buffers
(_cn_merged_down/_cn_merged_mid) that accumulate in place via copy_/add_,
reallocating only on a genuine shape/dtype/device change. Does not alias
down_samples_list[0]/mid_samples_list[0] (engine A's own persistent output
buffers). Numerically identical to the old sum; single-ControlNet path is
unaffected (already address-stable, no merge buffer allocated).

Also removes pre-existing lint debt in the same file (duplicate
OrchestratorUser import, unused base_kwargs dict), required to pass the
commit gate; verified via git stash that both predated this change.

Adds tests/unit/test_controlnet_residual_merge.py covering numerical
correctness, cross-frame pointer stability, reallocation on shape change,
non-aliasing of engine A's buffer, single-CN bypass, and install() reset.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
setTensorAddress requires >=256-byte alignment; _staging_action's zero-copy
eligibility test didn't enforce it, relying on an arithmetic proof instead of
a runtime invariant. Add cur_ptr % 256 == 0 to the eligibility check so a
mis-aligned pointer always falls back to the safe copy path. Real torch CUDA
allocations are >=512-byte aligned, so this guard never fires in production —
it only defends against future config drift, and is a prerequisite for
Phase-2 D2's new zero-copy input_control_* bindings.

Realigns the test suite's PTR_A/PTR_B fixtures to 256-byte-aligned values
(the old 0xDEAD_BEEF/0xFEED_FACE constants were both mis-aligned and would
have broken under the new guard) and adds two rows asserting the misaligned
fallback.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Widen UNet2DConditionModelEngine._zero_copy_names to include
input_control_00..19 and input_control_middle alongside the existing
kvo/fio cache names, so Engine.infer() binds TensorRT directly to the
ControlNet residual tensors instead of DtoD-copying them into a staging
buffer every frame (the Sub-phase 5.6 mechanism, now audit-M2 alignment
guarded).

In steady state the tensors placed in input_dict[input_control_*] are the
same persistent objects each frame: multi-ControlNet uses D1's persistent
merge buffers (controlnet_module.py), single-ControlNet reuses the engine's
own persistent output buffers, and idle frames reuse the persistent dummy
zero cache. Pointers only change on a resolution/batch/model change or the
idle<->active toggle, which _staging_action already handles via
bind_and_reset (one graph re-capture on the toggle frame, not per-frame).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
M1: TensorRTEngine (trt_base.py) ignored the bool return of set_input_shape
(allocate_buffers, infer) and set_tensor_address (infer). An out-of-profile
shape or invalid address was silently accepted, surfacing later as an
execute_async_v3 failure or garbage output. Mirror the core Engine pattern
(utilities.py) and raise RuntimeError with the tensor name/shape when either
call returns False. Affects depth/pose (+HED/NormalBae via the shared base).

H1: replace the try/except (AttributeError, TypeError) around the SM_120
TacticSource scoping block with an explicit hasattr(trt.TacticSource,
"CUBLAS") gate. CUBLAS/CUBLAS_LT are removed in TRT 11, so the old except
would silently swallow the AttributeError and drop the scoping intent
without a trace; the new branch logs the TRT>=11 case explicitly. No
behavior change on the installed TRT 10.16 (all members still present).

H2: delete the dead reuse_device_memory branch in Engine.activate() — no
caller across any of the ~10 activate() call sites passes it, and the
one-arg context.device_memory setter it used has been superseded since
TRT 10.1. Migrating to set_device_memory(ptr, engine.device_memory_size_v2)
is deferred until device-memory reuse is actually wanted.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
… deprecated export flag, robust external-data detection (audit M3/L1/L3)

M3: replace deprecated create_network(1<<EXPLICIT_BATCH) with create_network()
in compile_depth_anything_tensorrt.py, mirroring compile_raft_tensorrt.py.
Also swaps the unused-import onnx availability check for
importlib.util.find_spec (ruff F401 in the same file's try/except block).

L1: drop the deprecated do_constant_folding=True kwarg from the torch.onnx.export
call in utilities.py; dynamo=False stays explicit (audit L2: no action needed).

L3: replace the ".pb"-suffix directory scan in optimize_onnx() with a check of
TensorProto.data_location == EXTERNAL on the loaded (but not yet materialized)
model, removing the filename-heuristic's dependence on export_onnx's
consolidate-to-weights.pb ordering.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
…>copy gap

Post-mortem: f8ff50f ("zero-copy bind ControlNet residual UNet inputs")
caused a confirmed regression - ControlNet produced no visual change for
both scribble (ramp 0->0.36) and Canny (static 1.0), at any scale. An
interim revert (dropping input_control_* from the UNet engine's
_zero_copy_names) was verified working on the rig for both models. This
commit makes that revert permanent rather than re-enabling the optimization.

Root cause, confirmed by code inspection (not just bisection): a zero-copy
bind drops the copy_() call that runs on the engine stream and orders the
ControlNet engine's residual write before the UNet CUDA graph's read of it
("copy_() executes on engine stream to guarantee ordering" - utilities.py).
An earlier hypothesis assumed the failure was a bind->copy staging fallback
triggered by cfg_type="self" slicing; that was disproven by inspection:
single-ControlNet residuals (controlnet_module.py build_unet_hook) are the
ControlNet engine's own persistent, contiguous, 256-aligned output buffers
used unmodified, so _staging_action never takes the copy-fallback path for
them - re-enabling zero-copy on that assumption would very likely have
re-broken ControlNet.

The staging bind->copy gap is real, however, for any zero-copy input that
CAN take that fallback path (kvo/fio today): _staging_action returned
"copy" on fallback with no signal to reset a live CUDA graph, so the graph
kept replaying a stale bound address. Added a "copy_and_reset" outcome
(utilities.py _staging_action + Engine.infer) as decoupled latent
hardening - copies fresh data AND forces one graph re-capture, then
converges to plain "copy" the following frame. This does not re-add
ControlNet names to _zero_copy_names.

Regression test: test_zero_copy_staging_5_6.py locks the exact decision
that regressed (a previously-bound name falling back to copy while a graph
is live must now return "copy_and_reset", not silently "copy"), plus
coverage for the non-contiguous fallback variant and the never-bound/no-graph
no-op cases.

139/139 unit tests pass (up from 136: +3 new/updated staging cases).
…ource of truth)

- add src/streamdiffusion/param_schema.py: 25 wrapper / 23 updater param names,
  construction-time DEFAULTS (t_index_list as immutable tuple, G3), and shared
  floor_num_inference_steps / rescale_t_index_list helpers
- wrapper.py & stream_parameter_updater.py: source interpolation-method Literals and
  floor/rescale arithmetic from the schema; preserve branch-specific warnings (G2);
  fix D1 comment off-by-one
- config.py: delegate 12 defaults across both _extract_wrapper_params and
  _extract_prepare_params to param_schema.DEFAULTS (G1); drop dead
  _validate_pipeline_hook_configs (zero callers)
- add drift-lock + golden/parity tests (test_param_schema.py,
  test_config_extraction_golden.py, test_td_pending_params.py); tests/unit/: 165 passed
- pyproject.toml: expand ruff select to E,W,F,I,C4,B,UP,SIM,RUF,PERF,NPY; target py310;
  add extend-exclude + per-file-ignores; docstring-code-format; add [tool.pyrefly]
  (baseline=pyrefly-baseline.json, pytorch-efficiency-lints)
- apply safe ruff check --fix + ruff format across the repo (mechanical)
- ignore non-auto-fixable residual families pending incremental adoption
  (SIM/PERF/RUF-unicode/RUF012-013/B905/B00x/RUF006-009/RUF022/RUF046/UP008/NPY002) —
  documented inline
- mark 25 pre-existing debt sites (F401 x2, F841 x21, E722 x2) under already-active
  rules with per-site noqa + TODO comments; zero behavior change
- flake8-bugbear.extend-immutable-calls: fastapi.Depends/File + torch.device
- drop B008/RUF009 from ignore (FastAPI DI + torch.device dataclass defaults are intentional)
- B904: add `raise … from e`/`from None` across src, scripts, and demo routes (47 sites)
- B004 callable(); B006 None-sentinel default; B018 discard via `_ =`; B026 reorder *args
- RUF006: retain InputManager background-task references (prevent GC cancellation)
- NPY002: wrapper random-seed path uses np.random.default_rng().integers
- drop the fixed codes from [tool.ruff.lint].ignore (rules now enforce)
…rue)

- only unguarded torch.load repo-wide; prevents arbitrary-code exec on untrusted checkpoints
forkni added 6 commits July 13, 2026 19:41
…rite) to unblock permission-denied max-turns failures
…n CPU-only builds

_apply_safety_checker previously read the prior frame's async verdict but
applied it to the current frame's pixels, letting an isolated NSFW frame
leak through and wrongly blanking the next clean frame. Buffer the raw
frame in _pending_frame and emit it once its own verdict lands (+1 frame
latency, first call now emits a black startup frame instead of raw
pixels). Also guard .pin_memory() behind torch.cuda.is_available() so the
CPU-only test contract in test_safety_checker.py holds.
@claude

claude Bot commented Jul 14, 2026

Copy link
Copy Markdown

Claude finished @forkni's task in 4m 48s —— View job


Claude finished @alex Korneev's task

Running a code review of this PR via the repo's /code-review skill.

  • Check PR status / prior Claude reviews (prior comment was a stalled placeholder, no real findings — proceeding)
  • Locate relevant CLAUDE.md files (none exist in this repo)
  • Summarize PR diff scope
  • Launch parallel CLAUDE.md-compliance passes (no CLAUDE.md files → no findings)
  • Launch parallel bug-scan passes (in progress)
  • Validate any flagged issues
  • Post final review summary

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

Pull request overview

Expands repository-wide Ruff coverage, establishes a Pyrefly baseline, applies lint-driven fixes, and hardens checkpoint loading.

Changes:

  • Adds expanded Ruff and Pyrefly configuration.
  • Applies formatting, import cleanup, exception chaining, and small correctness fixes.
  • Uses restricted PyTorch checkpoint loading for RealESRGAN.

Reviewed changes

Copilot reviewed 124 out of 125 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
.github/workflows/claude-code-review.yml Adds automated PR review workflow.
pyproject.toml Configures Ruff and Pyrefly.
pyrefly-baseline.json Freezes existing type errors.
setup.py Improves exception handling and file access.
utils/viewer.py Formatting cleanup.
tools/summarize_audit.py Simplifies dictionary checks and file access.
scripts/test_lora_sanity.py Cleans import suppressions.
scripts/profiling/profile_nsys.py Applies formatting cleanup.
scripts/profiling/profile_ncu.py Applies formatting cleanup.
tests/unit/test_zero_copy_staging_5_6.py Formatting cleanup.
tests/unit/test_wrapper_exception_hygiene.py Formatting cleanup.
tests/unit/test_trt_engine_guards.py Formatting cleanup.
tests/unit/test_trt_atomic_engine_write.py Formatting cleanup.
tests/unit/test_td_pending_params.py Formatting cleanup.
tests/unit/test_sync_free_output_5_2.py Formatting cleanup.
tests/unit/test_safety_checker.py Formatting cleanup.
tests/unit/test_prompt_interpolation.py Removes obsolete lint suppression.
tests/unit/test_phase3_correctness.py Formatting cleanup.
tests/unit/test_param_updater_binding.py Removes obsolete lint suppressions.
tests/unit/test_param_schema.py Rewrites equivalent set assertion.
tests/unit/test_normal_bae_fallback.py Formatting cleanup.
tests/unit/test_l2tc_dynamic_shapes.py Formatting cleanup.
tests/unit/test_ipc_producer_stream.py Formatting cleanup.
tests/unit/test_derived_tensor_sync.py Reformats tests and removes unused imports.
tests/unit/test_config_extraction_golden.py Formatting cleanup.
tests/unit/test_cn_preprocessor_residency.py Formatting cleanup.
tests/quality/run_compare.py Formatting cleanup.
tests/quality/regenerate_golden.py Formatting cleanup.
tests/manual/smoke_self_build_preprocessors.py Cleans TensorRT import.
src/streamdiffusion/wrapper.py Modernizes RNG and attribute access.
src/streamdiffusion/utils/__init__.py Formatting cleanup.
src/streamdiffusion/tools/gpu_profiler.py Cleans annotations and no-op profiler methods.
src/streamdiffusion/tools/cuda_l2_cache.py Formatting cleanup.
src/streamdiffusion/tools/compile_raft_tensorrt.py Applies repository-wide formatting.
src/streamdiffusion/tools/compile_depth_anything_tensorrt.py Cleans optional TensorRT import.
src/streamdiffusion/stream_parameter_updater.py Simplifies attribute access.
src/streamdiffusion/preprocessing/processors/trt_base.py Formatting cleanup.
src/streamdiffusion/preprocessing/processors/temporal_net_tensorrt.py Chains engine-loading exceptions.
src/streamdiffusion/preprocessing/processors/standard_lineart.py Applies formatting and import cleanup.
src/streamdiffusion/preprocessing/processors/soft_edge.py Applies formatting and import cleanup.
src/streamdiffusion/preprocessing/processors/scribble_tensorrt.py Formatting cleanup.
src/streamdiffusion/preprocessing/processors/realesrgan_trt.py Hardens checkpoint loading and reformats code.
src/streamdiffusion/preprocessing/processors/pose_tensorrt.py Applies formatting and import cleanup.
src/streamdiffusion/preprocessing/processors/passthrough.py Removes unused imports and reformats code.
src/streamdiffusion/preprocessing/processors/openpose.py Uses callable for detector validation.
src/streamdiffusion/preprocessing/processors/normal_bae_tensorrt.py Formatting cleanup.
src/streamdiffusion/preprocessing/processors/mediapipe_segmentation.py Formatting cleanup.
src/streamdiffusion/preprocessing/processors/mediapipe_pose.py Applies lint-driven expression cleanup.
src/streamdiffusion/preprocessing/processors/lineart.py Suppresses irrelevant import cause.
src/streamdiffusion/preprocessing/processors/ipadapter_embedding.py Applies formatting and import ordering.
src/streamdiffusion/preprocessing/processors/hed.py Applies formatting and import cleanup.
src/streamdiffusion/preprocessing/processors/hed_tensorrt.py Formatting cleanup.
src/streamdiffusion/preprocessing/processors/feedback.py Applies formatting and typing cleanup.
src/streamdiffusion/preprocessing/processors/faceid_embedding.py Chains embedding extraction exceptions.
src/streamdiffusion/preprocessing/processors/depth.py Applies formatting and import cleanup.
src/streamdiffusion/preprocessing/processors/depth_tensorrt.py Applies formatting and import cleanup.
src/streamdiffusion/preprocessing/processors/category_params.py Formatting cleanup.
src/streamdiffusion/preprocessing/processors/canny.py Removes unused import.
src/streamdiffusion/preprocessing/processors/base.py Applies formatting and typing cleanup.
src/streamdiffusion/preprocessing/processors/__init__.py Reorders exports and discovery code.
src/streamdiffusion/preprocessing/preprocessing_orchestrator.py Formatting cleanup.
src/streamdiffusion/preprocessing/postprocessing_orchestrator.py Formatting cleanup.
src/streamdiffusion/preprocessing/pipeline_preprocessing_orchestrator.py Formatting cleanup.
src/streamdiffusion/preprocessing/orchestrator_user.py Simplifies orchestrator assignment.
src/streamdiffusion/preprocessing/base_orchestrator.py Formatting cleanup.
src/streamdiffusion/preprocessing/__init__.py Sorts public exports.
src/streamdiffusion/pipeline.py Formatting cleanup.
src/streamdiffusion/pip_utils.py Suppresses irrelevant import cause.
src/streamdiffusion/param_schema.py Formatting cleanup.
src/streamdiffusion/modules/latent_processing_module.py Simplifies processor attribute access.
src/streamdiffusion/modules/ipadapter_module.py Simplifies IPAdapter attribute assignment.
src/streamdiffusion/modules/image_processing_module.py Simplifies processor attribute access.
src/streamdiffusion/modules/controlnet_module.py Simplifies attributes and annotations.
src/streamdiffusion/modules/__init__.py Formatting cleanup.
src/streamdiffusion/model_detection.py Formatting cleanup.
src/streamdiffusion/config.py Simplifies configuration checks and file access.
src/streamdiffusion/acceleration/tensorrt/utilities.py Removes obsolete lint suppression.
src/streamdiffusion/acceleration/tensorrt/runtime_engines/unet_engine.py Formatting cleanup.
src/streamdiffusion/acceleration/tensorrt/runtime_engines/controlnet_engine.py Formatting cleanup.
src/streamdiffusion/acceleration/tensorrt/runtime_engines/__init__.py Sorts public exports.
src/streamdiffusion/acceleration/tensorrt/fp8_quantize.py Cleans file access and ignored result handling.
src/streamdiffusion/acceleration/tensorrt/export_wrappers/unet_unified_export.py Formatting cleanup.
src/streamdiffusion/acceleration/tensorrt/export_wrappers/unet_sdxl_export.py Simplifies equivalent branches.
src/streamdiffusion/acceleration/tensorrt/export_wrappers/unet_ipadapter_export.py Marks deferred unused values.
src/streamdiffusion/acceleration/tensorrt/export_wrappers/__init__.py Sorts public exports.
src/streamdiffusion/acceleration/tensorrt/engine_manager.py Simplifies engine metadata assignment.
src/streamdiffusion/acceleration/tensorrt/builder.py Formatting cleanup.
src/streamdiffusion/acceleration/tensorrt/__init__.py Formatting cleanup.
src/streamdiffusion/_patches/hf_tracing_patches.py Formatting cleanup.
src/streamdiffusion/_patches/diffusers_kvo_patch.py Corrects variadic argument placement.
src/streamdiffusion/_patches/__init__.py Formatting cleanup.
src/streamdiffusion/__init__.py Sorts public exports and removes suppression.
examples/vid2vid/main.py Formatting cleanup.
examples/txt2img/spacing_compare.py Simplifies string formatting.
examples/txt2img/single.py Formatting cleanup.
examples/txt2img/multi.py Formatting cleanup.
examples/screen/main.py Removes mutable default argument.
examples/optimal-performance/single.py Formatting cleanup.
examples/optimal-performance/multi.py Formatting cleanup.
examples/img2img/single.py Formatting cleanup.
examples/img2img/multi.py Formatting cleanup.
examples/config/config_video_test.py Marks deferred unused values.
examples/config/config_ipadapter_stream_test.py Marks deferred unused result.
examples/benchmark/single.py Formatting cleanup.
examples/benchmark/multi.py Marks deferred unused result.
examples/benchmark/ab_bench.py Cleans typing import suppression.
demo/vid2vid/app.py Formatting cleanup.
demo/realtime-txt2img/main.py Formatting cleanup.
demo/realtime-txt2img/config.py Formatting cleanup.
demo/realtime-img2img/utils/video_utils.py Simplifies exception formatting.
demo/realtime-img2img/util.py Cleans imports and exception causes.
demo/realtime-img2img/routes/websocket.py Formatting cleanup.
demo/realtime-img2img/routes/pipeline_hooks.py Chains API exception causes.
demo/realtime-img2img/routes/parameters.py Improves API exception chaining.
demo/realtime-img2img/routes/ipadapter.py Chains API exception causes.
demo/realtime-img2img/routes/input_sources.py Improves validation exception chaining.
demo/realtime-img2img/routes/inference.py Improves streaming exception chaining.
demo/realtime-img2img/routes/debug.py Improves debug API exception chaining.
demo/realtime-img2img/routes/controlnet.py Improves validation and API exception chaining.
demo/realtime-img2img/routes/common/api_utils.py Standardizes exception formatting and chaining.
demo/realtime-img2img/main.py Applies lint suppressions and condition cleanup.
demo/realtime-img2img/input_control.py Retains background task references.
demo/realtime-img2img/img2img.py Formatting cleanup.
demo/realtime-img2img/connection_manager.py Formatting cleanup.
demo/realtime-img2img/app_config.py Simplifies YAML file access.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pyproject.toml
project-excludes = ["tests/manual/**", "tests/quality/**", "demo/**", "examples/**"]
search-path = ["src"]
python-version = "3.10"
python-interpreter-path = "venv/Scripts/python.exe"
@forkni

forkni commented Jul 14, 2026

Copy link
Copy Markdown
Owner Author

Bugs-only review verdict: no genuine bugs found

Reviewed all 5 commits (e81e119, 6bd9df9, d23e79d, d5140d7, d25a291) against the standing bugs-only mandate — no architectural or behavioral changes beyond what each commit's own stated purpose covers, fix only genuine defects.

Findings: none. Confirmed by Copilot's completed automated review and my own manual pass (see harness note below for Claude's automated review):

  • e81e119 — expanded ruff ruleset + safe auto-fixes across 122 files. Spot-checked the mechanical transforms (setattr(x,"attr",v)x.attr=v, getattr(x,"__class__")x.__class__, int(len(...))len(...), __slots__ reordering, unquoted forward-ref type hints). The unquoted forward-ref change is only safe where from __future__ import annotations is present — verified present in gpu_profiler.py and the other touched files using that pattern. No behavioral change.
  • 6bd9df9pyrefly-baseline.json only, a frozen snapshot of pre-existing type errors. Pure data file, no code change.
  • d23e79dpyproject.toml config-only change (re-enabling B008/RUF009 with idiomatic-default whitelisting). No runtime effect.
  • d5140d7 — exception-chaining (raise ... from e / from None) across 22 files plus targeted lint-driven fixes. Verified the riskier-looking ones individually: the *args-before-kwargs reorder in diffusers_kvo_patch.py is behaviorally identical (Python assembles positional args from *args regardless of textual position relative to keywords); openpose.py's hasattr(x,"__call__")callable(x) is a pure readability change with identical semantics; wrapper.py's np.random.randint(...)int(np.random.default_rng().integers(...)) switch to a local RNG instance has no reproducibility impact — grepped the tree for any dependency on the global np.random.seed() state and found none. The RUF006 asyncio background-task fix in demo/realtime-img2img/input_control.py (self._background_tasks: set + task.add_done_callback(self._background_tasks.discard)) is the standard idiom preventing GC from cancelling in-flight tasks — genuine hardening, not a behavior change to the happy path.
  • d25a291realesrgan_trt.py's stub fallback loader now passes weights_only=True to torch.load(). This is a dead-code path (the loaded state_dict is immediately discarded, # noqa: F841) so there's zero functional risk; the change is pure security hardening against arbitrary code execution via untrusted pickled checkpoints.

Tests: CPU-safe unit suite (165 tests, tests/unit) passes cleanly at this branch tip when resolving source correctly via PYTHONPATH override to this worktree.

Harness note: Copilot's review completed normally with real findings-free content. Claude's automated review run completed at the job level (conclusion: success) but its sticky comment stalled mid-checklist and never posted a final findings summary — confirmed by reading the run log directly (tool-call trace ends after diff-gathering, before the "post final review" step). This is a harness anomaly, not a code-review finding, and doesn't affect the verdict above, which rests on independent manual review plus Copilot's completed pass.

No fix needed — closing this PR's bugs-only review as clean. This is the last PR in the chained review stack.

forkni added 8 commits July 18, 2026 20:11
…subphase-5x

# Conflicts:
#	src/streamdiffusion/acceleration/tensorrt/utilities.py
…opy-audit-hardening

# Conflicts:
#	src/streamdiffusion/acceleration/tensorrt/runtime_engines/unet_engine.py
#	src/streamdiffusion/acceleration/tensorrt/utilities.py
#	tests/unit/test_zero_copy_staging_5_6.py
@forkni
forkni changed the base branch from refactor/param-schema-fp8-preflight to SDTD_032_dev July 19, 2026 00:34
…fly-rollout

# Conflicts:
#	src/streamdiffusion/config.py
#	src/streamdiffusion/param_schema.py
#	src/streamdiffusion/tools/compile_depth_anything_tensorrt.py
#	tests/unit/test_config_extraction_golden.py
#	tests/unit/test_param_schema.py
#	tests/unit/test_param_updater_binding.py
#	tests/unit/test_td_pending_params.py
@forkni
forkni merged commit d692de7 into SDTD_032_dev Jul 19, 2026
1 check passed
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.

2 participants