Exact MaxPool2d encoding in NCET¶
This note explains the current full one-hot formulation used to encode a
PyTorch MaxPool2d operation exactly.
1. Tensor shapes¶
NCET represents one sample without a batch dimension:
For kernel \((K_h,K_w)\), stride \((S_h,S_w)\), padding \((P_h,P_w)\),
unit dilation, and ceil_mode=False,
MaxPool2d operates independently on each channel.
2. Valid window candidates¶
For output position \((c,i,j)\), kernel position \((r,t)\) refers to the original input coordinate
The valid kernel-position set is
PyTorch conceptually pads MaxPool inputs with negative infinity. Padding positions can never win the maximum, so NCET omits them instead of creating fixed padding values or selectors.
3. Scalar operation¶
One output element is
For a fixed output window, write its valid candidates as \(x_q\) for \(q\in\mathcal V\) and its output as \(y\):
Suppose interval propagation provides
The output interval is
4. Full one-hot formulation¶
The current formulation creates one binary selector for every valid candidate:
Exactly one candidate is selected:
The complete formulation is
No stable-window or dominated-candidate elimination is currently applied.
5. Exactness¶
The one-hot equality selects some \(q^\star\) with \(z_{q^\star}=1\). Its upper constraint becomes
The lower constraint for the same candidate gives
so \(y=x_{q^\star}\). Since the formulation also requires \(y\ge x_q\) for every candidate,
If several candidates tie, any one of them may be selected without changing the output value.
6. Candidate-specific big-M¶
Each upper constraint uses
When \(z_q=0\),
because \(x_q\ge L_q\). The inactive constraint therefore does not restrict an output already bounded above by \(U_y\). When \(z_q=1\), the big-M term disappears and forces \(y\le x_q\).
7. Interval bound propagation¶
MaxPool2d is monotone in every input, so NCET applies the same pooling operation to both interval endpoints:
The implementation temporarily adds a batch-size-one dimension only while calling PyTorch's pooling function.
8. Vectorized backend representation¶
The backend stores all selectors of one MaxPool node in a flat binary vector \(z\). For every selector it records:
- its output tensor coordinate \((c,i,j)\);
- its valid input coordinate \((c,h,w)\).
A sparse incidence matrix \(S\) contains one row per output element and one column per candidate. Its entries are
All one-hot equalities are then expressed together as
The candidate inequalities are also vectorized. This changes only the CVXPY construction, not the mathematical formulation.
9. Binary count¶
The full formulation introduces
binary variables. Boundary windows can have fewer selectors because padding positions are not candidates.
10. Current support boundary¶
The exact formulation supports:
- per-sample tensors with shape
(C, H, W); - integer or two-dimensional kernel size, stride, and numeric padding;
dilation == (1, 1);ceil_mode == False;return_indices == False;- full one-hot selection for all valid candidates.
The frontend explicitly rejects non-unit dilation, ceiling-mode output shapes, and returned pooling indices.