Skip to content

[Espressif] Lower Fused Quant Conv2d with planned scratch - #25154

Merged
rascani merged 4 commits into
codex/espressif-scratch-bindingsfrom
codex/espressif-fused-quant-conv2d
Nov 23, 2026
Merged

rascani merged 4 commits into
codex/espressif-scratch-bindingsfrom
codex/espressif-fused-quant-conv2d

Conversation

@rascani

@rascani rascani commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Lower supported Fused Quant Conv2d calls to ESP-NN using the scratch-size API from #25157. Export inserts an explicit byte tensor whose storage and 16-byte-aligned offset are assigned by ordinary memory planning; the device checks buffer capacity before calling the selected kernel. The application supplies an appropriately aligned arena and serializes ESP-NN execution because the vendor scratch pointer is global.

Support covers static 4-D int8 inputs, constant per-output-channel symmetric int8 weights, optional derived int32 bias, groups=1, and unit dilation, including batches, rectangular kernels, stride, and padding. Unsupported configurations or unavailable sizing bindings retain portable convolution. The recipe and pass manager accept EspTarget and EspKernelMode, including zero-byte scratch for generic or ANSI kernels. The tester defaults to ESPRESSIF_TARGET; explicit target overrides apply to both scratch planning and firmware execution.

Numerical preparation rejects reductions too long for its float32 error bound. Tests assert lowering before execution and check serialized scratch capacity, alignment, and non-overlap. The runner removes stale emulator output before export/build, and CI runs Python operator tests after building the sizing bindings.

Review lowering and allocation first, then the wrapper and shared dialect/implementation cases. Validation: Conv2d host tests pass, including selected-target scratch planning and S3 binding-fallback checks; the staged batch, staged no-bias, stride-remainder, and undersized-scratch cases passed on the optimized ESP32-S3 emulator. Scoped lint passed. Other chip profiles have host sizing/export coverage only.

Authored with OpenAI Codex.

@rascani
rascani added this pull request to stack #25135 November 17, 2026 02:24
@pytorch-bot

pytorch-bot Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25154

Note: Links to docs will display an error until the docs builds have been completed.

⏳ No Failures, 27 Pending

As of commit adc6d43 with merge base d811540 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 17, 2026
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@rascani
rascani removed this pull request from stack #25135 November 17, 2026 04:00
@rascani
rascani force-pushed the codex/espressif-fused-quant-conv2d branch from c36b1f3 to 967de28 Compare November 17, 2026 04:00
@rascani
rascani changed the base branch from codex/espressif-fused-quant-linear to codex/espressif-scratch-bindings November 17, 2026 04:00
@rascani
rascani added this pull request to stack #25158 November 17, 2026 04:00
@rascani
rascani force-pushed the codex/espressif-fused-quant-conv2d branch from 5ae7977 to c8a3bf3 Compare November 17, 2026 22:05
@rascani
rascani force-pushed the codex/espressif-fused-quant-conv2d branch from c8a3bf3 to 472cb3b Compare November 17, 2026 23:40
@rascani
rascani force-pushed the codex/espressif-fused-quant-conv2d branch from 472cb3b to 6adbe1f Compare November 18, 2026 00:39
@rascani

rascani commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

@claude /pr-review

Please re-review the current revision (4d6c92bba1afc81feb79b92a656045f94ab6ddf1) against this PR's base branch, codex/espressif-scratch-bindings. This PR is part of stack #25158. Re-check previous findings against the updated code and focus on actionable correctness, quantization/numerical, memory-safety, and regression-test issues in this PR.

Scratch size-query ValueError now retains the portable FQ convolution before weighted constants are inserted. A regression using an actual rejected vendor dimension bound checks numerical fallback, serialization, and strict-export rejection. Please review the ordering, exception scope, and coverage. Focused convolution tests passed, and 969 combined host tests passed at the stack tip with all four sizing bindings rebuilt. A separate shared-FQ fallback issue for a single output channel producing nonconstant per-tensor qparams remains known and unchanged by this fix.

@claude

claude Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Claude finished @rascani's task in 25m 3s —— View job


Re-review of 4d6c92b: the reordered size query and its except ValueError scope are correct and nothing blocks merge, but the new regression never checks that lowering reached the query, and nothing tests the RuntimeError that now sits inside the same try.

Detailed review

PR Review: #25154

Summary

This PR lowers fused_quant.convolution to esp_nn::quantized_conv2d_int8 with an export-planned scratch tensor. 4d6c92b moves the scratch-size query ahead of _weighted_constants and inside the try, so a binding ValueError now keeps portable convolution. I found no issues with the new ordering or with what the except ValueError catches. The remaining problems are minor.

Previous findings (review of 5ae7977), re-checked:

  • Fixed: a binding ValueError no longer aborts export (lower_to_esp.py:506-520). A Conv2d(32768, 2, 1) regression covers it.
  • Partly fixed: the README no longer says memory planning aligns the scratch, but it still doesn't state the 16-byte requirement (API Design).
  • Still open:
    • the shared ESP-NN lock (Thread Safety);
    • the kernel's geometry checks and the three diverging bound formulas, and the silent fallback with its AttributeError probe (Code Quality);
    • the common-shape and non-S3 emulator gaps (Testing).
  • Not re-raised: the int32 bias the quantizer gives to convs that never lower.

I couldn't run the tests because Python execution needs approval in this job. The ESP-NN 1.5.0 sources aren't in the checkout, so claims about vendor internals are marked low confidence.

Code Quality

  • Host planning and the device check call the vendor scratch getter with different arguments (ops/op_quantized_conv2d.cpp:114-131 vs scratch/bindings.cpp:99-105).

    • The binding passes extra = 0 and a conv_params_t with zero offsets and activation {0, 0}.
    • The kernel passes extra = 1, the real offsets and {-128, 127}, as the delegate does (espnn_runtime/src/int8_interpreter.cpp:504-506,571-579).
    • If any profile's getter reads these fields, the plan can be smaller than the device requires, and every call then fails the capacity check at 132-136. It fails closed, but P4/S31 (riscv_pie) only have host coverage. Low confidence: I couldn't read the vendor getters.
    • Fix: build the arguments the same way on both sides, for example extra = 1 and {-128, 127} in conv2d_scratch_size.

    Fix this →

  • Still open: the kernel trusts the PTE's convolution geometry (ops/op_quantized_conv2d.cpp:45-112).

    • It never checks weight.size(0) == outputs, weight.size(3) == channels, out.size(0) == batches, or output_h/output_w == (in + 2·pad − k) / stride + 1. Portable conv checks these with check_convolution_args and get_convolution_out_target_size (kernels/portable/cpu/util/kernel_ops_util.h:515,527).
    • Its int32 guard (93-112) is a third formula, alongside lowering's scratch_bound and the binding's check_conv_arithmetic. The new regression's shape (C=32768, 1x1, 2 outputs) passes the device guard at about 1.43 MB, although the host binding rejects it as overflow-prone.
    • Lowering enforces the geometry (lower_to_esp.py:468-482), so only a malformed PTE reaches the vendor code this way.

    Fix this →

  • Still open: conv fallback reasons are dropped (passes/lower_to_esp.py:519-520,563-569).

    • The new except ValueError: return None also discards the binding's message.
    • A missing binding or query falls back without a warning, and strict export only names aten::convolution.
    • The except AttributeError probe at 566-569 is only reachable through the monkeypatch.delattr test, because the binding always defines conv2d_scratch_size.

    Fix this →

Testing

  • The new regression doesn't pin what 4d6c92b changed (test/ops/test_conv2d.py:334-353).

    • It never shows that lowering reached the query. The test checks that the binding rejects these dims, and separately that the model falls back. Today the shape passes every earlier check in _conv2d_kernel_args. If a later change rejected it earlier (for example a tighter scratch_bound), the test would stay green while the except ValueError path lost its coverage.
      • Fix: after the direct call, wrap the real query with monkeypatch.setattr(s3_queries, "conv2d_scratch_size", spy), and assert the spy was called and raised. test_convolution_falls_back_without_sizing[query] already patches this module.
    • Nothing tests that the RuntimeError at lower_to_esp.py:514-515 escapes the try. It now sits inside the same try, so widening the except would silently turn it into a fallback. It aborts export even with allow_cpu_fallback=True, and since the query now runs first, it would also fire for convs that _weighted_constants would otherwise send to fallback (for example one with a runtime bias).
      • Fix: add a case whose patched query returns -1, and assert the RuntimeError.

    Fix this →

  • Still open: emulator coverage gaps (test/ops/test_conv2d.py:110-180, test/tester.py:215).

    • No case covers a 1x1 conv with stride 2 or bias=False, a non-1x1 kernel with more than 16 input channels, or padding ≥ 2.
    • test_implementation still links the S3-only runtime build for every target.

    Fix this →

API Design

  • The 16-byte scratch alignment requirement is neither documented nor checked (README.md:52, ops/op_quantized_conv2d.cpp:137).

    • The PR description says the application supplies an aligned arena. The README says only "Scratch storage uses the planned arena", and memory planning aligns offsets, not the buffer base.
    • The kernel hands the pointer to esp_nn_set_conv_scratch_buf unchecked.
    • Low confidence that ESP-NN needs the alignment. The delegate allocates with ARENA_ALIGNMENT = 16 (int8_interpreter.cpp:31), but the binding's 256 bytes of slack may already cover internal realignment.

    Fix this →

Thread Safety

  • Still open: the kernel sets and clears ESP-NN's global conv scratch pointer without the delegate's lock (ops/op_quantized_conv2d.cpp:137,150).

    • The delegate holds InvokeLock for a whole invoke, including the vTaskDelay(1) yield (int8_interpreter.cpp:1677-1702). The lock lives in an anonymous namespace (:29-53), so the operator library can't take it.
    • README:52-53 leaves this to the application. If an application doesn't serialize, an operator-library conv on another task can repoint or null the pointer in the middle of a delegate invoke.

    Fix this →

Recommendation

Approve

Nothing blocks merge. Worth folding in here:

  • the spy and RuntimeError cases for the new regression;
  • matching getter arguments on host and device.

The remaining open items can follow in stack #25158.

Review steps
  • Load the /pr-review skill and checklist
  • Diff 4d6c92b against origin/codex/espressif-scratch-bindings (2 commits, 14 files, +991/−80)
  • Run three independent reviewers
  • Consolidate findings
  • Re-check previous findings against the updated code
  • Fact-check and post

@JakeStevens

Copy link
Copy Markdown
Contributor

actual ci errors here

Lower static groups=1, unit-dilation int8 convolution using the ESP-NN
scratch-size bindings from the preceding commit. EspTarget and
EspKernelMode select the query used to allocate a planned byte tensor;
the runtime checks its size before setting ESP-NN's scratch pointer.

Support constant per-channel weights, derived int32 bias, batches,
rectangular kernels, stride, and padding. Unsupported calls or missing
sizing bindings keep portable convolution. Keep the delegate and shared
Fused Quant transforms unchanged.

Validated 208 backend Python tests, all 24 Conv2d ESP32-S3 emulator tests,
and lintrunner. Emulator cases cover repeated inference, undersized
scratch rejection, ANSI zero scratch, and missing-binding fallback.

Authored with OpenAI Codex.
Treat a sizing binding's bounds rejection as an unsupported convolution configuration. Query before inserting requantization constants so rejected calls remain available to portable decomposition without partial lowering state.

Added a real sizing-bound regression that checks numerical fallback, serialization, and strict-export rejection. Validated 34 host convolution tests with freshly built sizing bindings.

Authored with OpenAI Codex.
Keep checks that remove the S3 sizing binding or exercise its bounds explicitly on S3. Make the device fallback case remove sizing for whichever chip the CI job selects, preserving the target support introduced on main.

Authored with OpenAI Codex.
Exercise selected-target scratch planning and serialization with compute coverage required. Supported convolutions retain their reported layout copies; missing sizing or unsupported bounds still fail strict export.

Authored with OpenAI Codex.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants