Skip to content

fix: CodeRabbit auto-fixes for PR #1396 - #1401

Merged
ooples merged 1 commit into
fix/issue-1305-diffusion-szfrom
coderabbitai/autofix/1fbae7a
May 20, 2026
Merged

ooples merged 1 commit into
fix/issue-1305-diffusion-szfrom
coderabbitai/autofix/1fbae7a

Conversation

@coderabbitai

@coderabbitai coderabbitai Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor

This stacked PR contains CodeRabbit auto-fixes for #1396.

Files modified:

  • src/Diffusion/NoisePredictors/FluxDoubleStreamPredictor.cs

Fixed 1 file(s) based on 1 unresolved review comment.

Co-authored-by: CodeRabbit <noreply@coderabbit.ai>
@coderabbitai
coderabbitai Bot requested a review from ooples as a code owner May 20, 2026 02:36
@vercel

vercel Bot commented May 20, 2026

Copy link
Copy Markdown

Deployment failed with the following error:

Resource is limited - try again in 24 hours (more than 100, code: "api-deployments-free-per-day").

Learn More: https://vercel.com/franklins-projects-02a0b5a0?upgradeToPro=build-rate-limit

@coderabbitai

coderabbitai Bot commented May 20, 2026

Copy link
Copy Markdown
Contributor Author

Important

Review skipped

This PR was authored by the user configured for CodeRabbit reviews. CodeRabbit does not review PRs authored by this user. It's recommended to use a dedicated user account to post CodeRabbit review feedback.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: e0947ec6-0459-49f7-a8f9-d8accd86d4b8

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

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

@ooples
ooples merged commit 88c3703 into fix/issue-1305-diffusion-sz May 20, 2026
7 of 10 checks passed
@ooples
ooples deleted the coderabbitai/autofix/1fbae7a branch May 20, 2026 02:52
ooples added a commit that referenced this pull request May 20, 2026
…dictor — fixes 2× output-length shape mismatch (#1396)

* fix(#1304 c6): drop Dropout from OccupancyNN defaults; fix memorization invariant

PR #1290 CI Cluster 6 #1304:
OccupancyNeuralNetworkTests.LossStrictlyDecreasesOnMemorizationTask was
reported to be fixed by PR #1329's BatchNorm→LayerNorm swap, but the test
was still red on master with loss step 1=0.6936, step 100=0.7032 (slightly
INCREASING) — model stuck at the BCE-ln(2) baseline through 100 gradient
steps.

## Root cause

PR #1329 fixed the BN-at-batch-1 degeneracy (σ²=0 → y=β collapses the
gradient through normalization) but the *Dropout layer*'s
memorization-blocking effect was not addressed. The default Occupancy
layer stack was:

  Dense(64)+ReLU → LayerNorm → Dropout(0.3) → Dense(32)+ReLU
  → LayerNorm → Dropout(0.2) → Dense(16)+ReLU → Dense(out)+Sigmoid

Under the model-family LossStrictlyDecreasesOnMemorizationTask invariant
— train the SAME (x, target) pair for 100 iterations and assert loss
strictly decreases — every forward pass under Dropout sees a DIFFERENT
random sub-network (~56% of hidden units active = 0.7 × 0.8). On a
3 → 64 → 32 → 16 → 1 MLP (~2k params), the per-step mask randomness
injects more variance than the gradient can subtract over 100 steps,
leaving loss flat or slightly RISING at the BCE-ln(2) baseline.

## Fix

Remove Dropout from both `CreateDefaultOccupancyLayers` and
`CreateDefaultOccupancyTemporalLayers` in `LayerHelper<T>`. At this
network size Dropout adds no useful regularization (the model has fewer
params than typical sensor batches have rows); callers who genuinely
need regularization on a larger Occupancy MLP can pass an explicit
architecture with their preferred Dropout rate.

## Verification

  $ dotnet test --filter "FullyQualifiedName~OccupancyNeuralNetworkTests"
  Passed!  - Failed: 0, Passed: 21, Skipped: 0, Total: 21

All 21 OccupancyNN tests pass (was 1 failing).

The 4 remaining #1304 tests post-fix:
  - SimCSETests.TrainingError_ShouldNotExceedTestError
    PASS (was passing already on current master)
  - SimCSETests.Training_ShouldChangeParameters
    PASS (was passing already on current master)
  - DenseNetNetworkTests.MoreData_ShouldNotDegrade
    Adam-overshoot divergence (200-iter loss > 50-iter loss);
    separate follow-up issue
  - NEATTests.Training_ShouldReduceLoss
    timeout (perf gap, similar to #1390); separate follow-up issue

Closes #1304 partially. DenseNet + NEAT follow-ups tracked separately.

* fix(#1305 cluster-6): port patchify/unpatchify to FluxDoubleStreamPredictor — fixes 2× output-length shape mismatch

PR #1290 CI Cluster 6 #1305: Flux2SchnellModelTests.ScaledInput_ShouldChangeOutput was failing on master with:

  System.InvalidOperationException : PredictNoise output length (32768) does not match the latent/sample length (16384). Check that the noise predictor's output shape matches the input.

Identical class of bug the MMDiTXNoisePredictor fix in #1224 Cluster F (ControlNetSD3) closed for the SD3 MMDiT-X variant. Same root cause; same fix shape.

## Root cause

`FluxDoubleStreamPredictor.PredictNoise` ran the Dense block stack directly on the rank-4 spatial tensor `[B, C, H, W]`:

```csharp
var x = _patchEmbed.Forward(noisySample);
foreach (var block in _doubleBlocks) x = block.Forward(x);
foreach (var block in _singleBlocks) x = block.Forward(x);
return _finalLayer.Forward(x);
```

DenseLayer applied along the last axis projects `W → patchDim` and emits `[B, C, H, patchDim]`. For the FLUX default `[1, 16, 32, 32]` with `patchSize=2` (so `patchDim = inputChannels * 4 = 64`), the output is `[1, 16, 32, 64] = 32768` elements — exactly 2× the latent at 16384, which is what `DiffusionModelBase.Generate`'s shape check catches.

The DiT-style architecture FLUX implements (Esser et al. 2024 §3, Black Forest Labs 2024) requires patchify before the block stack and unpatchify after:

```
[B, C, H, W] → Patchify → [B, (H/P)·(W/P), C·P²] → DenseBlocks → Unpatchify → [B, C, H, W]
```

The MMDiTXNoisePredictor fix in `src/Diffusion/NoisePredictors/MMDiTXNoisePredictor.cs:165-283` already implements this exact pattern. Port it.

## Fix

Apply the same Patchify/Unpatchify + rank normalization scaffolding from MMDiTXNoisePredictor to `FluxDoubleStreamPredictor.PredictNoise`:

1. Normalize rank-3 [C,H,W] → rank-4 [1,C,H,W] for unbatched test inputs.
2. Patchify [B,C,H,W] → [B, (H/P)·(W/P), C·P²] using the standard
   `rearrange("b c (h p1) (w p2) → b (h w) (c p1 p2)")` pattern.
3. Run `_patchEmbed → _doubleBlocks → _singleBlocks → _finalLayer` on the token tensor as the implementation intended.
4. Unpatchify back to [B,C,H,W].
5. Re-strip the batch dim if the input was unbatched.

## Verification

  $ dotnet test --filter "FullyQualifiedName=AiDotNet.Tests.ModelFamilyTests.Diffusion.Flux2SchnellModelTests.ScaledInput_ShouldChangeOutput" --framework net10.0
  Passed!  - Failed: 0, Passed: 1, Skipped: 0, Total: 1, Duration: 55 s

The test passes in 55 s under isolation (the 120 s test envelope). Under parallel xUnit execution Flux can hit the timeout because the foundation-scale UNet at [1, 16, 32, 32] competes for CPU with adjacent diffusion tests — that's a separate perf-gap issue, not a shape bug.

## Adjacent state for #1305

- `Flux2SchnellModelTests.ScaledInput_ShouldChangeOutput` — PASS in isolation after this fix
- `VideoCrafterModelTests.ScaledInput_ShouldChangeOutput` — already PASS on master (intervening work)
- `ConsistencyModelTests.ScaledInput_ShouldChangeOutput` — still times out at 120 s on the [1, 4, 64, 64] UNet at foundation scale; perf-gap issue, sibling to #1394 (ResNet/VGG ImageNet-scale perf)

Closes #1305 partially (the actual shape-contract bug). Foundation-scale diffusion perf gap is a separate follow-up.

* fix(pr1396-review): extract patchsize to class-level const

addresses coderabbit nit on pr #1396: line 110 had
    int patchdim = _inputchannels * 4; // 2x2 patches
while line 185 in predictnoise had
    int patchdim = channels * patchsize * patchsize;
where patchsize was a local const inside predictnoise. drift-prone
since the magic 4 in initializelayers must stay in sync with the
patchsize used in patchify/unpatchify.

fix: promote patchsize to a class-level const near the other fields,
update initializelayers to derive patchdim from it, and remove the
local const in predictnoise. one definition, no drift.

build passes.

* fix: apply CodeRabbit auto-fixes (#1401)

Fixed 1 file(s) based on 1 unresolved review comment.

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: CodeRabbit <noreply@coderabbit.ai>

---------

Co-authored-by: franklinic <franklin@ivorycloud.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: CodeRabbit <noreply@coderabbit.ai>
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.

1 participant