Skip to main content

physics.layers

physics.layers

Layer-level counting laws for neural network architectures (added 2026-10-20).

Weight, multiply-add, and byte counts for one layer are products of its dimensions and live on the calculation scenarios (engine/scenario_types.py: DenseLayerScenario, ConvLayerScenario, RecurrentStepScenario). This module holds the few laws those counts compose with: how a stack of convolutions widens its view, how a depthwise separable factoring shrinks a convolution, and how a product of per-layer gradient gains decays with depth.

Functions

Name Description
calc_gradient_product Multiply equal gradient gains through a plain layer stack.
calc_layers_for_receptive_field Find the layer count needed to reach a receptive-field width.
calc_receptive_field Calculate receptive-field width for stride-one convolutions.
calc_resnet_bottleneck_macs Count convolution and classifier MACs in a bottleneck ResNet.
calc_separable_cost_fraction Compare depthwise-separable and dense convolution work.

calc_gradient_product

physics.layers.calc_gradient_product(gain, depth)

Multiply equal gradient gains through a plain layer stack.

Parameters

Name Type Description Default
gain float Dimensionless per-layer scalar derivative factor. required
depth int Nonnegative number of multiplied factors. required

Returns

Name Type Description
float Dimensionless product of local derivative factors.

Notes

Equation: g_total = gain**depth.

Assumptions: All scalar gains are identical and the computation follows one serial path.

Excludes: Residual paths, Jacobian spectra, and variation among layers.

References

Derivation: Apply the scalar chain rule repeatedly across depth layers.

Examples

>>> calc_gradient_product(0.5, 3) == 0.5**3
True

calc_layers_for_receptive_field

physics.layers.calc_layers_for_receptive_field(field, kernel)

Find the layer count needed to reach a receptive-field width.

Parameters

Name Type Description Default
field int Target width in input pixels, at least one. required
kernel int Kernel width in pixels, greater than one. required

Returns

Name Type Description
int Minimum nonnegative count of stride-one convolution layers.

Notes

Equation: L = ceil((R - 1) / (K - 1)).

Assumptions: Each layer increases receptive-field width by K - 1.

Excludes: Strides, dilation, and a kernel of width one, which cannot enlarge the field.

References

Derivation: Solve R = 1 + L(K - 1) for L and round upward to a whole layer.

Examples

>>> calc_layers_for_receptive_field(7, 3) == 3
True

calc_receptive_field

physics.layers.calc_receptive_field(layers, kernel)

Calculate receptive-field width for stride-one convolutions.

Parameters

Name Type Description Default
layers int Nonnegative convolution layer count. required
kernel int Positive kernel width in pixels. required

Returns

Name Type Description
int Receptive-field width in input pixels.

Notes

Equation: R = 1 + L (K - 1).

Assumptions: Every convolution has the same kernel width, unit stride, and unit dilation.

Excludes: Pooling, strided layers, dilation, and boundary clipping.

References

Derivation: R = 1 + L (K - 1).

Examples

>>> calc_receptive_field(3, 3) == 1 + 3 * (3 - 1)
True

calc_resnet_bottleneck_macs

physics.layers.calc_resnet_bottleneck_macs(
    stages=((64, 3), (128, 4), (256, 6), (512, 3)),
    input_size=224,
    num_classes=1000,
    stride_on_3x3=False,
)

Count convolution and classifier MACs in a bottleneck ResNet.

Parameters

Name Type Description Default
stages sequence of tuple of int Positive (bottleneck width, block count) pairs; default describes ResNet-50. ((64, 3), (128, 4), (256, 6), (512, 3))
input_size int Square image side in pixels; default 224. Spatial downsampling uses integer division. 224
num_classes int Positive classifier output width; default 1000. 1000
stride_on_3x3 bool Put downsampling stride on the 3-by-3 convolution for v1.5; default False puts it on the first 1-by-1. False

Returns

Name Type Description
dict MAC counts under keys 7x7, 3x3, 1x1, fc, and total, per image.

Notes

Equation: MAC_conv = H_out**2 C_in C_out K**2; MAC_fc = C_in C_out; total sums all counted layers.

Assumptions: A 7-by-7 stride-two stem, stride-two pooling, bottleneck expansion by four, and projection at each stage’s first block.

Excludes: Bias, batch normalization, pooling, nonlinearities, and residual-add operations.

The original bottleneck places a downsampling stride in its first 1-by-1 convolution. Moving that stride to the 3-by-3 convolution retains more first-projection work, so the two variants need not have identical MAC counts.

References

Derivation: Enumerate the stem, three convolutions per bottleneck, stage-entry projection, and classifier; multiply each output element by its convolution reduction length. The architecture follows He et al. (2016), “Deep Residual Learning for Image Recognition,” Table 1. https://arxiv.org/abs/1512.03385

Examples

>>> work = calc_resnet_bottleneck_macs(stages=((4, 1),), input_size=32, num_classes=2)
>>> work["total"] == sum(work[k] for k in ("7x7", "3x3", "1x1", "fc"))
True

calc_separable_cost_fraction

physics.layers.calc_separable_cost_fraction(kernel, out_channels)

Compare depthwise-separable and dense convolution work.

Parameters

Name Type Description Default
kernel int Positive square kernel width. required
out_channels int Positive output channel count. required

Returns

Name Type Description
float Separable-to-dense MAC or weight-count ratio.

Notes

Equation: rho = 1 / C_out + 1 / K**2.

Assumptions: A depthwise K-by-K convolution is followed by a dense pointwise convolution; spatial shape is identical.

Excludes: Biases, nonlinearities, memory movement, and achieved kernel speed.

References

Derivation: Divide K**2 C_in + C_in C_out by the dense cost K**2 C_in C_out. This is the counting argument used for depthwise-separable convolutions.

Examples

>>> calc_separable_cost_fraction(3, 8) == 1 / 8 + 1 / 3**2
True
Back to top