arXiv:2610.17132cs.LGOct 2026

Manuscript Offprint

arXiv:2610.17132

Masked Attention Pooling Is Invariant to Padding and Permutation: A Formal Proof in Lean

Yusuf Khaled Sadek, Enes Yilmaz

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Attention-based multiple instance learning (MIL) pools a variable-size bag of instance embeddings into one vector. In batched training, bags are padded to a common length, and the pooled output is expected not to depend on how much padding was added or on the order in which instances are stored. This expectation holds only if padded rows are removed from both the numerator and the softmax normalizer. Common implementation idioms meet it only under side conditions. Zeroing padded features alone does not work. Masking logits with −∞-\infty fails on empty bags and on nonfinite padding. Masking with a large negative constant is exact only after floating-point underflow. We define masked attention pooling with a guarded normalizer and prove in Lean 4 with mathlib that it is invariant under any bijective reindexing of rows and under appending any finite number of masked rows with arbitrary real features and logits. We also prove that values at masked positions are irrelevant and that an all-masked bag maps to zero. The theorems hold for arbitrary finite index types and feature dimensions. Using the jax-lean certificate pipeline, we connect these theorems to the Jaxpr traced from the JAX kernel itself. Lean checks translation certificates for two concrete shapes, identifies the generated functions with the generic specification, and transfers both invariances to the imported Jaxpr's real-arithmetic semantics. All audited theorems depend only on Lean's three standard axioms. The results concern ideal real arithmetic. Floating-point execution, XLA, and the JAX tracer and importer remain outside the proof. The development is short and publicly available.

1 Introduction

In multiple instance learning [2], a label is attached to a bag of instances rather than to each instance. Attention-based MIL pooling [4] computes a scalar score for every instance, normalizes the scores with a softmax over the bag, and returns the weighted average of the instance embeddings. This construction underlies much of weakly supervised computational pathology, where a whole-slide image is a bag of thousands of tile embeddings and only the slide carries a label [1, 9]. Ilse et al. introduce the operator as a permutation-invariant aggregation over the bag [4], in the same spirit as other set architectures [8, 12].

Bags differ in size, so batched implementations store them in a dense array of n rows together with a validity mask or a length. The model is then expected to satisfy two properties:

  1. Padding invariance. Appending masked rows, with whatever contents, does not change the pooled output.

  2. Permutation invariance. Reordering the rows, with each row's features, score, and mask moving together, does not change the output.

These properties are easy to state and easy to break. Zeroing the features of padded rows is not enough: a padded row with logit 0 still contributes e0=1e^0 = 1 to the softmax normalizer. Two real instances with values 2 and 6 and equal logits should pool to 4. With pp zero-feature padded rows left inside the softmax, they pool to 8/(2+p)8/(2 + p), so the output depends on the batch's padding length. Masking logits with −∞-\infty fixes the normalizer when every bag has a valid row and all padding is finite. It yields NaN when a bag is empty or a padded feature is nonfinite. Masking with a large negative constant gives exact zeros only through floating-point underflow, and an empty bag then returns the mean of its padding. Table 1 summarizes these behaviours.

This note gives a precise definition of masked attention pooling with a guarded normalizer and machine-checked proofs of its invariances. The contributions are:

  • Size-generic Lean 4 theorems: invariance under any bijection of the row index type, invariance under appending any finite set of masked rows with arbitrary real features and logits, irrelevance of masked values, and a zero output for all-masked bags (Section 4).

  • A corollary covering any shared, instance-local scoring function, such as a linear layer, an MLP, or a gated attention scorer.

  • A connection from the theorems to the code that runs. The JAX kernel is traced to a Jaxpr, translated and independently imported by jax-lean [6], and linked to the generic specification by checked certificates (Section 5).

• A precise statement of the trusted base and of what the proof does not cover (Section 7).

The mathematics is elementary: a bijection reindexes a finite sum, and terms with mass zero vanish. The point is to make the specification exact, including the empty-bag convention and the requirement that masked rows lose their mass, and to check that a specific JAX kernel meets it.