TorchLean API

NN.Spec.Layers.GlobalPooling

Global pooling (spec layer) #

Global pooling reduces spatial dimensions (H×W) either to 1×1 (retain channel axis) or to a flat vector of size inC. This file provides both average and max variants, together with explicit backward rules.

We tried to mimic PyTorch closely:

Forward generalizes cleanly (and we intentionally structure the code that way):

Max-pooling subtlety:

Why the backward does not unify for free:

Layer tags #

Global pooling has no trainable parameters. We still keep a compact "layer spec" record so call sites can carry a tag (and so the API matches the style of other layer files).

Tag structure for global average pooling (no trainable parameters).

    Instances For

      Tag structure for global max pooling (no trainable parameters).

        Instances For

          Output shape for global pooling that keeps a 1 x 1 spatial grid: (C,H,W) -> (C,1,1).

          Instances For

            Output shape for global pooling that flattens spatial dims away: (C,H,W) -> (C).

            Instances For

              Helper: reduce a single channel over its spatial grid #

              This is the shared "walk the (H,W) grid" loop used by avg/max pooling.

              def Spec.reduceSpatial {α : Type} (inH inW : ) (init : α) (f : ααα) (channel_data : Image inH inW α) :
              α

              Reduce a single channel Image inH inW α down to a scalar using a fold over (H,W).

              Instances For
                def Spec.channelSpatialMax {α : Type} [Max α] {inH inW : } (hH : inH 0) (hW : inW 0) (channel_data : Image inH inW α) :
                α

                Compute the exact spatial maximum of a non-empty (inH × inW) channel.

                Instances For
                  def Spec.globalChannelReduce {α : Type} (inH inW : ) (channel_data : Image inH inW α) (init : α) (f : ααα) :
                  α

                  Alias for reduce_spatial (kept to make call sites read like "reduce this channel").

                  Instances For

                    Helper: "wrap a scalar result back into an image" #

                    PyTorch mental picture: after pooling you conceptually have a scalar per channel; these helpers put that scalar back into a scalar tensor shape.

                    Broadcast a scalar into a 1 x 1 image.

                    Instances For
                      def Spec.globalPool2d1x1 {α : Type} [Zero α] (inC inH inW : ) (reduce : Image inH inW αTensor α Shape.scalar) (input : MultiChannelImage inC inH inW α) :

                      Generic global pooling helper producing (C,1,1).

                      Instances For
                        def Spec.globalPool2dFlat {α : Type} [Zero α] (inC inH inW : ) (reduce : Image inH inW αTensor α Shape.scalar) (input : MultiChannelImage inC inH inW α) :

                        Generic global pooling helper producing (C).

                        Instances For

                          Forward specs #

                          These are the layer-level forward meanings, written in the same style as PyTorch.

                          def Spec.globalAvgPool2dSpec {α : Type} [Zero α] [Add α] [HDiv α α] [OfNat α 0] [OfNat α 1] {inC inH inW : } (_h1 : inH 0) (_h2 : inW 0) (_layer : GlobalAvgPool2DSpec) (input : MultiChannelImage inC inH inW α) :

                          Global average pooling: (C,H,W) -> (C,1,1).

                          Instances For
                            def Spec.globalMaxPool2dSpec {α : Type} [Numbers α] [Max α] [Zero α] {inC inH inW : } (h1 : inH 0) (h2 : inW 0) (_layer : GlobalMaxPool2DSpec) (input : MultiChannelImage inC inH inW α) :

                            Global max pooling: (C,H,W) -> (C,1,1).

                            Instances For
                              def Spec.globalAvgPool2dFlatSpec {α : Type} [Coe α] [Div α] [Zero α] [Add α] [Zero α] [One α] {inC inH inW : } (_h1 : inH 0) (_h2 : inW 0) (_layer : GlobalAvgPool2DSpec) (input : MultiChannelImage inC inH inW α) :

                              Global average pooling (flattened): (C,H,W) -> (C).

                              Instances For
                                def Spec.globalMaxPool2dFlatSpec {α : Type} [Numbers α] [Max α] [Zero α] {inC inH inW : } (h1 : inH 0) (h2 : inW 0) (_layer : GlobalMaxPool2DSpec) (input : MultiChannelImage inC inH inW α) :

                                Global max pooling (flattened): (C,H,W) -> (C).

                                Instances For

                                  Backward/VJP specs #

                                  These are reverse-mode rules that match the intended math:

                                  def Spec.globalAvgPool2dBackwardSpec {α : Type} [Context α] {inC inH inW : } (_h1 : inH 0) (_h2 : inW 0) (_layer : GlobalAvgPool2DSpec) (grad_output : MultiChannelImage inC 1 1 α) :
                                  MultiChannelImage inC inH inW α

                                  Backward/VJP for global average pooling (C,1,1) output.

                                  Instances For
                                    def Spec.globalAvgPool2dFlatBackwardSpec {α : Type} [Context α] {inC inH inW : } (_h1 : inH 0) (_h2 : inW 0) (_layer : GlobalAvgPool2DSpec) (grad_output : Tensor α (Shape.dim inC Shape.scalar)) :
                                    MultiChannelImage inC inH inW α

                                    Backward/VJP for flattened global average pooling (C) output.

                                    Instances For
                                      def Spec.globalMaxPool2dBackwardSpec {α : Type} [Context α] {inC inH inW : } (h1 : inH 0) (h2 : inW 0) (_layer : GlobalMaxPool2DSpec) (input : MultiChannelImage inC inH inW α) (grad_output : MultiChannelImage inC 1 1 α) :
                                      MultiChannelImage inC inH inW α

                                      Backward/VJP for global max pooling (C,1,1) output.

                                      Tie convention: every spatial position equal to the maximum receives the full upstream gradient.

                                      Instances For
                                        def Spec.globalMaxPool2dFlatBackwardSpec {α : Type} [Context α] {inC inH inW : } (h1 : inH 0) (h2 : inW 0) (_layer : GlobalMaxPool2DSpec) (input : MultiChannelImage inC inH inW α) (grad_output : Tensor α (Shape.dim inC Shape.scalar)) :
                                        MultiChannelImage inC inH inW α

                                        Backward/VJP for flattened global max pooling (C) output.

                                        Tie convention: every spatial position equal to the maximum receives the full upstream gradient.

                                        Instances For
                                          def Spec.globalMaxPool2dBackwardDistributedSpec {α : Type} [Context α] {inC inH inW : } (h1 : inH 0) (h2 : inW 0) (_layer : GlobalMaxPool2DSpec) (input : MultiChannelImage inC inH inW α) (grad_output : MultiChannelImage inC 1 1 α) :
                                          MultiChannelImage inC inH inW α

                                          Alternative max-pooling backward that splits the gradient evenly across max positions.

                                          This is often a nicer mathematical choice when the max is not unique.

                                          Instances For