Provably Learning Attention with Queries

Satwik Bhattamishra, Kulin Shah, Michael Hahn, Varun Kanade

As Transformer-based models become the ubiquitous backbone of modern AI, they are increasingly deployed behind proprietary APIs. Users are granted "black-box" access—providing inputs and receiving outputs without visibility into the model's internal weights. This paradigm raises a critical security question: Can an adversary reverse-engineer the proprietary weights of a model simply by querying it?

While previous research has explored "model stealing" for standard feedforward (ReLU) networks, the attention mechanism—the core engine of the Transformer—has remained a theoretical blind spot. This paper provides the first theoretical guarantees for extracting the weights of a Transformer's attention mechanism using only black-box API queries, revealing both surprising vulnerabilities and fundamental mathematical walls.

The Vulnerability of Single-Head Attention

The most surprising finding of the paper is that single-head attention is remarkably easy to recover. While training these models from scratch is a non-convex, highly complex optimization problem, recovering them via queries reduces to a straightforward linear algebra problem.

By treating the attention mechanism as a mathematical function, an adversary can design specific input sequences to isolate and solve for its parameters. The researchers introduce the Sigmoid Trick to bypass the complexity of the softmax function. By querying the model with a sequence of exactly two vectors, the softmax simplifies into an invertible sigmoid function. This allows the model's outputs to be translated into a system of linear equations. Consequently, a single-head attention model with width $d$ can be perfectly recovered using just $O(d^2)$ queries.

Efficiency Gains and Real-World Robustness

The researchers also demonstrate how this extraction can be optimized and applied to more realistic scenarios:

  • Compressed Sensing for Low-Rank Heads: In modern Transformers, the head dimension ($r$) is typically much smaller than the total model width ($d$). The authors leverage Matrix Sensing to recover these low-rank weight matrices with significantly fewer queries, dropping the complexity from $O(d^2)$ to $O(rd)$.
  • Antisymmetric Probing: To handle a full one-layer Transformer (which includes both an attention block and a ReLU feedforward block), the authors employ an "antisymmetry" technique. By subtracting the output of a negative input from a positive one ($f(X) - f(-X)$), they effectively cancel out the ReLU non-linearity, isolating the attention parameters for recovery.
  • Robustness to Noise: Real-world API outputs often contain slight noise or rounding errors. The proposed algorithm is mathematically robust; by carefully choosing probes that keep the internal math in a stable range, parameters can be estimated to $\epsilon$-accuracy in polynomial time even with additive noise, provided the model meets certain margin conditions.

The Multi-Head Identifiability Wall

While single-head attention is vulnerable, the paper proves a significant "negative result" for multi-head attention: it is fundamentally not identifiable.

This means multiple distinct sets of weights can produce the exact same input-output behavior. An adversary cannot "steal" the exact weights because a unique "correct" set does not exist to be found. To make multi-head attention learnable via queries, additional structural assumptions are required, such as assuming the different attention heads operate in strictly orthogonal subspaces.

Limitations and Future Directions

As a theoretical work, this paper relies on mathematical proofs rather than empirical training on standard datasets, establishing foundational limits on Transformer security. However, several open challenges remain:

  1. The Multi-Head Gap: Because multi-head attention is unidentifiable under standard value queries, the authors suggest future work could explore Hessian queries (analyzing the second-order derivatives of the output) to break the symmetry and identify individual heads.
  2. Model Depth: This study focuses on one-layer models. Real-world Transformers feature dozens of layers, and recovering weights through deep, non-linear stacks remains a massive open challenge.
  3. Value Projection Assumptions: The current algorithms require the value projection vector ($v$) to be non-zero, though this is generally a safe assumption in functional, trained models.

Ultimately, this research proves that while the "secret sauce" of a single attention head can be easily unmasked by a clever adversary, the multi-head architecture inherently provides a mathematical layer of obfuscation against black-box model stealing.

Summary updated

BibTeX
@inproceedings{bhattamishra2026provably,
  author = {Satwik Bhattamishra and Kulin Shah and Michael Hahn and Varun Kanade},
  title = {{Provably Learning Attention with Queries}},
  booktitle = {International Conference on Machine Learning},
  year = {2026},
  url = {https://openreview.net/forum?id=gC04O9t1WJ},
}
Figure