TorchLean API

NN.MLTheory.SelfSupervised.Masking

Masking primitives for self-supervised objectives #

This file gives a small finite-index vocabulary for masked prediction objectives. It stays independent of any particular image or transformer implementation: a patch/token collection is just Fin n → α, and a mask is a Boolean predicate on Fin n.

The definitions make MAE/JEPA-style objectives precise enough for local invariants before they are connected to larger executable models.

@[reducible, inline]

A finite mask over n patches/tokens. true means the index is selected.

Instances For

    Proposition stating that index i is selected by the Boolean mask m.

    Instances For

      The all-visible/all-target mask.

      Instances For

        Mask selecting no positions.

        Instances For

          Pointwise Boolean complement of a mask.

          Instances For
            @[simp]

            Every index is selected by the full mask.

            @[simp]

            No index is selected by the empty mask.

            @[simp]

            Complementing a mask negates selection pointwise. This supports a context/target partition when the caller chooses complementary masks; the objective definitions do not enforce that choice.

            def NN.MLTheory.SelfSupervised.maskedLoss {n : Nat} (idxs : Array (Fin n)) (perPatchLoss : Fin nNat) :

            Generic masked loss over an explicit array of selected indices.

            The scalar loss is Nat, so this models natural-valued scores such as quantized patch losses. Each array occurrence contributes once; duplicate indices are not removed.

            Instances For
              @[simp]
              theorem NN.MLTheory.SelfSupervised.maskedLoss_nil {n : Nat} (perPatchLoss : Fin nNat) :
              maskedLoss #[] perPatchLoss = 0

              An empty index array contributes no loss.

              @[simp]
              theorem NN.MLTheory.SelfSupervised.maskedLoss_push {n : Nat} (idxs : Array (Fin n)) (i : Fin n) (perPatchLoss : Fin nNat) :
              maskedLoss (idxs.push i) perPatchLoss = maskedLoss idxs perPatchLoss + perPatchLoss i

              Pushing one index adds that patch's loss.

              theorem NN.MLTheory.SelfSupervised.maskedLoss_append {n : Nat} (xs ys : Array (Fin n)) (perPatchLoss : Fin nNat) :
              maskedLoss (xs ++ ys) perPatchLoss = maskedLoss xs perPatchLoss + maskedLoss ys perPatchLoss

              Masked loss is additive in the index array.

              theorem NN.MLTheory.SelfSupervised.maskedLoss_reverse {n : Nat} (idxs : Array (Fin n)) (perPatchLoss : Fin nNat) :
              maskedLoss idxs.reverse perPatchLoss = maskedLoss idxs perPatchLoss

              Masked loss does not depend on the order of the indices, so a shuffled mask scores the same.

              theorem NN.MLTheory.SelfSupervised.maskedLoss_eq_zero_of_all_zero {n : Nat} (idxs : Array (Fin n)) (perPatchLoss : Fin nNat) (h : ∀ (i : Fin n), i idxsperPatchLoss i = 0) :
              maskedLoss idxs perPatchLoss = 0

              Zero per-patch loss at every selected index gives zero total loss. This theorem states only that direction; it does not assume the per-patch score characterizes perfect reconstruction.