Manuscript Offprint

arXiv:2610.07754

Adversarially Trained Linear Transformers Are Optimal Robust In-Context Learners for Gaussian Mixtures

Soichiro Kumano

ABSTRACT

Adversarial training is one of the most reliable defenses against adversarial attacks, but its high computational cost must generally be paid anew for each task. Robust foundation models offer a promising alternative: adversarially pretrain a model once and then transfer its robustness to downstream tasks through lightweight adaptation. However, a fundamental question remains open: can robustness acquired during pretraining transfer to unseen tasks without further adversarial training? In this study, we answer this question affirmatively. A single model adversarially pretrained at scale can achieve optimal robustness on new tasks without additional task-specific training. Specifically, we show that, for a family of Gaussian-mixture classification tasks, a sufficiently deep linear transformer adversarially trained across tasks can asymptotically attain the robust Bayes error on previously unseen tasks through in-context learning from clean demonstrations. By contrast, a standardly trained model cannot. We further analyze convergence under gradient flow, an accuracy–robustness trade-off, and demonstration complexity.

1 INTRODUCTION

Adversarial examples, inputs that are slightly perturbed to cause machine learning models to make incorrect predictions, expose a fundamental vulnerability of deep learning systems (Szegedy et al., 2014). Adversarial training is one of the most reliable defenses against adversarial attacks (Goodfellow et al., 2015; Madry et al., 2018). It improves robustness by minimizing the loss under worst-case perturbations. However, the resulting min-max optimization makes adversarial training substantially more expensive than standard training. While many cheaper defenses have been proposed, some offer only apparent robustness that disappears under stronger attacks (Carlini & Wagner, 2017a;b; Athalye et al., 2018; Croce & Hein, 2020; Tramer et al., 2020). Consequently, obtaining reliable robustness generally requires paying the cost of adversarial training anew for each task.

Meanwhile, foundation models have transformed how models are developed and deployed. Rather than training a separate model for each task, practitioners pretrain a single model at scale and adapt it to downstream tasks using lightweight methods. This paradigm suggests a compelling possibility: robust foundation models. Adversarial training would be performed only once during pretraining, and the resulting robustness could then be transferred to downstream tasks at little additional cost. Although adversarial pretraining incurs an initial cost, model developers (typically large organizations) could recoup it by licensing the model. The fundamental question here is: Can an adversarially pretrained model adapt to unseen downstream tasks while retaining robustness, without further adversarial training?

Kumano et al. (2026) provided the first theoretical evidence that such transfer might be possible. They showed that an adversarially pretrained single-layer transformer with linear attention can adapt robustly to unseen classification tasks through in-context learning. In-context learning is a particularly lightweight adaptation method that enables transformers to learn from demonstrations in the prompt without updating their parameters (Brown et al., 2020). This result suggests that robustness acquired during pretraining can transfer to new tasks without additional training. However, their analysis is limited to a restricted data model and a single-layer architecture. Moreover, they provided only bounds on the expected robust margin. Thus, it does not provide direct guarantees on robust accuracy or evaluate robust optimality. Consequently, our central question remains open in broader settings.

In this study, we provide a stronger affirmative answer to this question under substantially more general conditions. We consider multilayer linear transformers adversarially pretrained on a family of Gaussian-mixture classification tasks. We focus on the robust zero-one error of these transformers on tasks not encountered during pretraining. In particular, we investigate whether this error approaches the robust Bayes error, which is the minimum robust error attainable by any classifier, even one with full knowledge of the task distribution.

Specifically, we show that, through in-context learning from clean demonstrations, a sufficiently deep linear transformer can asymptotically attain the robust Bayes error on every task in this Gaussian family, including tasks unseen during pretraining. In other words, using only standard samples and without task-specific adversarial training or access to adversarial examples, a single linear transformer can asymptotically serve as an optimal robust predictor for each task. We should note that the transformer's parameters remain fixed and shared across all downstream tasks. Nevertheless, the transformer can adapt to each task and implement its optimal robust prediction rule.

Moreover, we show that a standardly pretrained transformer cannot achieve optimal robustness, even with infinite pretraining tasks and in-context demonstrations. This result indicates that optimal robust in-context learning cannot be attributed solely to model capacity or task diversity; rather, it stems from an inherent advantage of adversarial pretraining.

We further investigate several properties of robust in-context learning. Whereas the discussion above focuses on empirical-risk minimizers, we also prove that gradient flow in a two-layer architecture converges to a predictor that attains robust Bayes optimality. We also identify two fundamental challenges in developing robust transformers. The first is an inherent accuracy–robustness trade-off: adversarial pretraining can degrade clean accuracy even in the infinite-data limit. The second is greater demonstration complexity: even at the same Bayes excess risk, robust prediction requires more demonstrations than standard prediction.

In summary, we prove the following:

• A sufficiently deep linear transformer asymptotically achieves optimal robustness across a family of Gaussian-mixture classification tasks through in-context learning from clean demonstrations.

• Adversarial pretraining makes the transformer an optimal robust in-context learner, whereas standard pretraining does not, even with unlimited data.

• Under mild conditions, (i) gradient flow leads the transformer to become such an in-context learner, (ii) an accuracy–robustness trade-off arises, and (iii) robust in-context prediction requires more demonstrations than standard prediction.

References (100)

  • Transformers learn to implement preconditioned gradient descent for in-context learning
  • Understanding and improving fast adversarial training
  • Understanding in-context learning of linear models in transformers through an adversarial lens
  • Obfuscated gradients give a false sense of security: Circumventing defenses to adversarial examples
  • Transformers as statisticians: Provable in-context learning with in-context algorithm selection
  • Lower bounds on adversarial robustness from optimal transport
  • Towards evaluating the robustness of neural networks
  • Adversarial examples are not easily detected: Bypassing ten detection methods
  • Transformers implement functional gradient descent to learn non-linear functions in context
  • Reliable evaluation of adversarial robustness with an ensemble of diverse parameter-free attacks
  • Sharp statistical guarantees for adversarially robust gaussian classification
  • Transformers learn to achieve second-order convergence rates for in-context linear regression
  • Understanding and improving continuous LLM adversarial training via in-context learning theory
  • Short-length adversarial training helps LLMs defend long-length jailbreak attacks: Theoretical and empirical evidence
  • Can looped transformers learn to implement multi-step gradient descent for in-context learning? In ICML, 2024
  • How well can transformers emulate in-context newton's method? arXiv:2403.03183, 2024 (opens arXiv in a new tab)
  • Explaining and harnessing adversarial examples
  • Conserve-update-revise to cure generalization and robustness trade-off in adversarial training
  • Caltech-256 object category dataset
  • The elements of statistical learning: data mining, inference, and prediction
  • Adversarial fine-tune with dynamically regulated adversary
  • Adversarial examples are not bugs, they are features
  • A simple fine-tuning is all you need: Towards robust deep learning via adversarial fine-tuning
  • Prior-guided adversarial initialization for fast adversarial training
  • Understanding catastrophic overfitting in single-step adversarial training
  • Adversarially pretrained transformers may be universally robust in-context learners
  • Tiny ImageNet visual recognition challenge
  • Supervised pretraining can learn in-context reinforcement learning
  • On the robustness of transformers against context hijacking for linear classification
  • Transformers as decision makers: Provable in-context reinforcement learning via supervised pretraining
  • Towards deep learning models resistant to adversarial attacks
  • One step of gradient descent is provably the optimal in-context learner with one layer of linear self-attention
  • Reliably fast adversarial training via latent adversarial perturbation
  • Adversarially robust generalization requires more data
  • Adversarial training for free! In NeurIPS, volume 32, 2019
  • Is robustness the cost of accuracy?—a comprehensive study on the robustness of 18 deep image classification models
  • Adversarial finetuning with latent representation constraint to mitigate accuracy-robustness tradeoff
  • Intriguing properties of neural networks
  • On adaptive attacks to adversarial example defenses
  • Robustness may be at odds with accuracy
  • Fast is better than free: Revisiting adversarial training
  • Efficient adversarial training in llms with continuous attacks
  • Fashion-MNIST: a Novel Image Dataset for Benchmarking Machine Learning Algorithms
  • An explanation of in-context learning as implicit bayesian inference
  • You only propagate once: Accelerating adversarial training via maximal principle
  • Trained transformers learn linear models in-context
  • What and how does in-context learning learn? bayesian model averaging, parameterization, and generalization
  • Efficient adversarial training with transferable adversarial examples
  • The active signal-to-variance ratios $\mu_i / \Sigma_{i,i} = 10^{1-2(i-1)/9}$ therefore range logarithmically from 10 to $10^{-1}$
  • We use the one-head masked linear-attention architecture in Eq
  • The distribution and the three experimental conditions are the same as in Fig
  • We minimize the logistic loss $\mathcal{L}(z) = \log(1 + \exp(-z))$, which satisfies Assumption 3.1(d), by full-batch Adam for at most 2,500 updates on the fixed pretraining dataset
  • Masked linear attention is linear in the query, so both adversarial training and robust evaluation can use the exact worst-case perturbation rather than an approximate attack such as the fast gradient sign method (Goodfellow et al., 2015) or projected gradient descent (PGD) (Madry et al., 2018)
  • For comparison with the theoretical optimum, we obtain the robust Bayes direction $(\mathbf{\Sigma} + \tau \mathbf{I}_d)^{-1} \boldsymbol{\mu}$ by numerically solving
  • To assess the accuracy–robustness trade-off and the qualitative dependence on context length, Tab
  • Each row uses the displayed number of demonstrations in both pretraining and evaluation
  • >N</td><td colspan=
  • We evaluate whether the benefit of adversarial pretraining extends beyond the theoretical Gaussian model by considering CIFAR-100 (Krizhevsky, 2009), Tiny ImageNet (Le & Yang, 2015), and Caltech-256 (Griffin et al., 2007)
  • The real-world threat model follows the theoretical setting by perturbing only the query while keeping all demonstrations clean and fixed
  • For $U = I_d$ and thus $x = ya$, we derive the robust zero-one error of every linear function in Lemma B.1, the standard Bayes error in Lemma B.2, and the robust Bayes error and all linear functions attaining it in Lemma B.3
  • These lemmas are auxiliary to our main results, and we do not claim them as novel contributions
  • Therefore, standardizing the Gaussian signed margin gives
  • Because $\boldsymbol{\Sigma}^{-1} \boldsymbol{\mu} \neq \mathbf{0}_d$, the hyperplane on which $\varphi^\star(\boldsymbol{x}) = 0$ has Lebesgue measure zero
  • Standardizing therefore yields
  • Each map $\tau \mapsto \tau / (\lambda_i + \tau)$ is continuous and strictly increasing from zero to one on $[0, \infty)$
  • The corresponding ambient context matrix is
  • The mask in Eq
  • For any symmetric matrix, the dimension of the Krylov space generated by a vector equals the number of distinct eigenvalues whose spectral projectors do not annihilate that vector
  • Set $a_0(s) := 1$ and $b_0(s) := 0$, and, for $1 \leq \ell \leq m$, recursively define
  • (A53) into Eq
  • This holds initially because $z_i^{(0)} = s_i$
  • For every remaining index $i \neq k_\ell$, the next transformed value is
  • Thus, positivity and pairwise distinctness hold at the next stage
  • Let $T_{r-1} := A_{\Theta_{r-1}}^{(r-1)}(G_\star)$ and define
  • The spectral theorem and activity of $s_1, \ldots, s_r$ give
  • (A58) at depth $r$
  • For every fixed integer $k \geq 1$, Gaussian sample-moment bounds give
  • Polynomial interpolation gives a polynomial $p$ of degree at most $r - 1$ satisfying $p(\lambda_i) = (\lambda_i + \tau)^{-1}$ for every $i \in [r]$
  • (A49) give
  • (A50) gives
  • Relative to the monomial basis $1, s, \ldots, s^{L-1}$, the coefficient matrix of $q_{L,t_1}, \ldots, q_{L,t_L}$ has entries $\binom{L}{j} t_i^j$ and reduces to an invertible Vandermonde matrix after nonzero row and column scalings
  • (8) with $C := C_{\psi}C_{\mathrm{cmp}}$
  • Let $\mathcal{S}_{T,N} := \{(\mathcal{D}_{t,N}, \boldsymbol{x}_t, y_t)\}_{t=1}^T$ consist of $T$ independent tasks drawn from $\mathbb{P}_{\text{task}}$
  • (A105) yield a constant $C_{\text{conc}} > 0$ such that, for every $T, N \in \mathbb{N}$
  • Define $\psi(h) := \Phi(-\Gamma_\epsilon(h)), \psi^\star := \psi(h_\epsilon^\star)$, and
  • Positive scale invariance of $\Gamma_{\epsilon}$ and Eqs
  • Set $\delta_N := \theta_{\Theta_{L,\star}}(G_{a,N}) - h_\epsilon^\star$
  • (A92) gives $\mathcal{R}_{\epsilon,\infty,\infty}(\Theta_{L,\star}) = \mathcal{J}_\epsilon(h_\epsilon^\star)$
  • (A46), the ambient context is $G_N(U) = \text{diag}(U, 1) G_{a,N} \text{diag}(U^\top, 1)$
  • Applying Eqs
  • (A119) to Eq
  • (A130) is zero
  • Equation Eq
  • Thus, $h_0$ is not a positive multiple of $h_\tau$, so the uniqueness conclusion of Lemma B.4 shows that its robust error is strictly larger than the robust Bayes error
  • (A130) equals $C$ for every $U \in \mathcal{O}(d)$
  • Then, for every $U \in \mathcal{O}(d)$ and $s > 0$
  • Since $\mathcal{L}$ is decreasing, query linearity gives
  • (A137) is concave in $Q$
  • Let $\mathcal{K} := \mathcal{K}_r(\Sigma, \mu)$ and recall that $r \leq 2$ by assumption
  • If $r = 2$, the spectral theorem and strict monotonicity of $p$ on the positive active eigenvalues make $\mu$ and $v$ linearly independent

Couldn't load all references. Reload the page to try again.