TorchLean API

NN.API.Models.Vit

ViT-Style Model Helpers (API) #

This module provides a compact, reusable ViT-style model constructor used by runnable examples.

This constructor keeps the architecture compact:

The point is to keep examples readable while still exercising: Conv2d + tokenization + attention + FFN on both CPU and CUDA eager backends.

Configuration for a small ViT-style classifier.

Shapes:

  • input: N×C×H×W
  • output: N×outDim
Instances For

    Patch-grid height after strided patch embedding.

    Instances For

      Patch-grid width after strided patch embedding.

      Instances For

        Number of patch tokens produced by the patch embedding.

        Instances For

          Flattened token representation size used before the classifier head.

          Instances For
            @[reducible, inline]

            Batched image input shape for the ViT helper.

            Instances For
              @[reducible, inline]

              Batched classifier-logit output shape for the ViT helper.

              Instances For
                @[reducible, inline]

                Convolutional patch-embedding output before tokenization.

                Instances For
                  @[reducible, inline]

                  Token sequence shape consumed by the Transformer block.

                  Instances For

                    Patch-tokenization adapter: N×C×H×W -> N×(H*W)×C.

                    This is the “low-hanging fruit” to move out of examples: the reshape needs a small size proof.

                    Instances For
                      def NN.API.nn.models.vit1 (cfg : VitConfig) (h_inC : cfg.inC 0 := by decide) (h_patchH : cfg.patchH 0 := by decide) (h_patchW : cfg.patchW 0 := by decide) (h_seqLen : cfg.seqLen 0 := by decide) (h_dModel : cfg.dModel 0 := by decide) :

                      One-block ViT-style classifier.

                      This is the constructor used by torchlean vit. Keeping it here makes the example a one-liner: def mkModel := nn.models.vit1 cfg.

                      Instances For