            Large Language Models Can Perform Tasks Without Instruction via Selective Attention Head Masking



usually require carefully designed prompts to work well. In            in the mask. Using the similar approach, we can also block

our work, the training of attention masks allows any ordi-             the output of certain FFN layers, though the mask mostly

nary task datasets, and the usage or removal of heads can              just simply use all the layers (see Appendix C.3.3). Here,

directly reflect their inherent functionalities on the task.           we will only apply masks to attention heads.



3. Attention Head Selection Extensively                                3.2. Mask Training

   Triggers Functionality                                              For a certain task t, We use the training-based method to

                                                                       get its attention head mask M. Specifically, we freeze all

In this section, we provide our main experiment results of

                                                                       the original parameters in the model, and only train a head

the attention head selection. We first define the notations,

                                                                       weight M that has the same size with M but in the Rn×h

methods and settings used in the module selection, then

                                                                       space. Weights in M can indicate the importance of heads

we show the conclusion that attention head selection can

                                                                       to the task, and the binary mask M can be obtained by the

extensively trigger functionalities in LLMs.

                                                                       sigmoid function M = I(σ(M) ≥ 0.5). Inspired by Fang

                                                                       et al. (2024) and Csordás et al. (2021), we introduce the

3.1. Preliminaries and Notations                                       Gumbel trick (Gumbel, 1954) into the sigmoid function to

Modern LLMs are commonly decoder-only Transform-                       encourage σ(M) to converge towards 0 or 1 in the training:

ers, and a decoder layer includes a multi-head attention                                                       

(MHA) layer and an FFN layer. For an LLM that has                                                  M+G

                                                                                     M=I σ                   ≥ 0.5 ,            (3)

a hidden size of d, consider the input X ∈ Rs×d with                                                 τ

the length s at the decoder layer l. Four projection matri-

                                                                       where G = log(− log E), ϵij ∈ E, ϵij ∼ U (0, 1) is the left-

ces WQ , WK , WV , WO ∈ Rd×d compute the attention

                                                                       tail Gumbel noise matrix and τ is the temperature. Smaller

scores and write the processed hidden states MHA(X) into

                                                                       τ amplifies the impact of noise while also encouraging the

the residual stream. For the MHA part, suppose the atten-

                                                                       weights M to shift toward the positive or negative extremes.

tion layer has h heads1 , and each head has a dim size of

                                                                       Using the reparametrization trick, we can get a discrete but

dhead = d/h. Thus for head i, its contribution Y(i) ∈ Rs×d

                                                                       still differentiable mask M during the training.

to the MHA output is

                                                                       Just like fine-tuning an ordinary language model, it’s

                           (i)       (i) T

                                           !

                      XW   Q   (XW   K  )       (i)   (i)              straightforward to train an attention head mask: for a sam-

   Y(i) = softmax          √                 XWV WO ,

                               dhead                                   ple (x, y) in the task dataset Dt , we directly take the x||y

                                                          (1)          as input without any task instructions and optimize the

          (i)    (i)   (i)

where WQ , WK , WV ∈ Rd×dhead are submatrices be-                      head weight M on the output sequence y to minimize the

                            (i)                                        loss. Note that the weight M is initialized with a positive

longing to head i, and WO ∈ Rdhead ×d is the “head” sub-               value so that initial σ(M) ≈ 1, and the training process is

matrix that projects the output of head i to the hidden space.         like removing attention heads from the full model. After

In this way, the final output of MHA can be calculated as              training, we remove the Gumbel noise from (3) and use

              Ph

MHA(X) = i Y(i) .                                                      M = I(σ(M) ≥ 0.5) as the final mask for inference.

Using this notation, it’s easy to block the output of certain

heads by simply setting their contributions to zero. Specif-           3.3. Experiment Setting

ically, for layer l we use a binary mask m ∈ {0, 1}h to                Models To verify the generalization of this attention selec-

indicate which heads are selected in the model, and the                tion strategy, we experiment with different types and sizes

layer MHA output can be written as:                                    of LLMs as follows:

                                  h

                                  X

                 MHA(X) =             mi Y(i) .              (2)          • Llama 3.1 series (Dubey et al., 2024): 8B, 8B-instruct

                                  i

                                                                          • Qwen2.5 series (Team, 2024b): 0.5B-instruct, 1.5B-

There will be different masks at different layers of the                    instruct, 3B-instruct, 7B, 7B-instruct, 14B-instruct

model. For an LLM with n layers, we expect a mask                         • Other series of LLMs: Phi-3.5-mini-instruct (Adina-

M ∈ {0, 1}n×h to indicate all the selected attention heads                  Tru, 2024, with 3.8B params), Mistral-7B-Instruct-v0.3

in the model. When applying M to the model, it can be re-                   (Jiang et al., 2023)

garded as combining the selected attention heads as a group

   1

     Some LLMs have fewer KV heads than Q heads, and the               Details of their size and attention head settings are listed in

matrices of KV heads are repeated to be equal in size during the       Appendix A. We primarily conduct experiments using two

runtime. We take WK , WV as already repeated for simplicity.           8B Llama 3.1 models, while other models are mainly used



                                                                   3

