Scaled_dot_product_attention not useful for inference

I am implementing long context inference and noticed an odd deficiency of scaled_dot_product_attention.

In long context inference, a prompt is processed in chunks, so that query is shorter than key, value (in the doc: L < S). I can pass attn_mask and set is_causal=False, but noted that in this case, often only the naive C++ implementation is supported. I’d therefore like to use the is_causal=True case with L < S.

But the way this is implemented, it is not really useful. If i indexes query, j indexes key, the masking is such that what is computed is inner(q_i, k_j) + (-infty) * ind(i < j). This means that most entries are -infty. Say, L = 3, S = 5. The mask matrix is:

tensor([[0., -inf, -inf, -inf, -inf],
        [0., 0., -inf, -inf, -inf],
        [0., 0., 0., -inf, -inf]])

That makes no sense, why would I ever want to mask out most score entries? What is needed for inference, is that the mask is

tensor([[0., 0., 0., -inf, -inf],
        [0., 0., 0., 0., -inf],
        [0., 0., 0., 0., 0.]])

This would correspond to a new chunk of size 3, whereas the KV cache has length 5 (the final 3 slots are for the new tokens).

I do wonder what is the point of this definition of the mask if is_causal=True, given that only in this special case (so attn_mask=None), the most efficient SDPA implementations are given.

Sure, I can just use my own implementation in this case, or send in attn_mask (in which case the efficient SDPA implementations cannot be used), but why?

Note that for large context inference, this IS important. I might have a KV cache of size (say) S = 2 ** 16 and a blocksize for processing the prompt of size L = 2 ** 10, in which case I’d appreciate a fast SDPA implementation.

As it stands, the case L < S and is_causal=True seems not useful to me for anything I’d want to do with SDPA. But if the default mask in this case was defined just a bit differently (see above, more 0s, less -infty), then that would be really useful for long context inference.

Having worked quite a bit more on this, I found it quite striking while the “training mode” (or is_causal=True) is really well covered by a number of different efficient SDPA kernels, the situation for is_causal=False is pretty uncovered. Or at least the offering does not make so much sense.

Do you guys notice that by far the most time in long context inference is spent on the chunks after the first prefill one? Where the rest of the prompt is processed in chunks and a KV cache is served?

Say I have a prompt of 150000 tokens. On GPUs I can afford, no more than 32k of that can be done by the first “prefill” step. If the remaining sequence is split into chunks of size 1k, I have about 120 of them to process sequentially. And each step needs SDPA.

And when I call your SDPA kernels for any of these, I just see OOM immediately. Mainly because of the very simple reason that the mask argument you require is already too large.

I had to implement all of this from scratch, including splitting the tensors into shards, and generating the masks on the fly. I have no idea about Cuda, so this could likely be faster. But at least, it works. Fair enough, but guess how many folks need to run efficient inference with long contexts, vs how many need to do training …