TensorRT-LLM icon indicating copy to clipboard operation
TensorRT-LLM copied to clipboard

Why qkv_bias is always None when calling gpt_attention()?

Open foricee opened this issue 3 years ago • 5 comments

Attention class: https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/tensorrt_llm/layers/attention.py#L322

context, past_key_value = gpt_attention(
                tensor=qkv,
                past_key_value=past_key_value,
...
                max_distance=self.max_distance,
                host_context_lengths=attention_params.host_context_lengths,
            )

In the Attention class above, when the forward function calls gpt_attention(), the last parameter qkv_bias is omitted, meaning that qkv_bias is always None.

qkv = self.qkv(hidden_states)

The code above in attention forward function already uses bias, so why does gpt_attention() provide an additional qkv_bias parameter? I would like to know the relationship between qkv_bias and the qkv projection bias in self.qkv .

gpt_attention(): https://github.com/NVIDIA/TensorRT-LLM/blob/release/0.5.0/tensorrt_llm/functional.py#L3056

foricee avatar Nov 01 '23 09:11 foricee

cc @Shixiaowei02 Could you please help answer this question? Thanks!

QiJune avatar Nov 02 '23 01:11 QiJune

@foricee Hello, qkv bias parameter is used to perform operations inside attention to improve efficiency. It will be used in the graph rewriting process, please refer to here.

Shixiaowei02 avatar Nov 02 '23 08:11 Shixiaowei02

@Shixiaowei02 Thanks for pointing out the code location. After reading the code, I still have some questions and I hope you can answer them.

In function match_and_rewrite, since the value of raw_inputs['tensor'] is qkv, (refer to code here), and qkv = self.qkv(hidden_states),(refer to code here), the raw_inputs['tensor'] should already have the bias added. In that case, why do we need to pass in raw_inputs['qkv_bias'] to gpt_attention when rewriting? And it seems like raw_inputs['qkv_bias'] is just an arbitrary value of raw_inputs['tensor']'s parents?

I'm sorry for asking such a detailed question. I am currently developing the code for the qwen model, which has an additional qkv bias compared to llama. That's why I noticed this code and would appreciate if you could provide an explanation.

foricee avatar Nov 02 '23 09:11 foricee

Thank you for your attention and support. This function splits the qkv before attention and puts it into the trt plugin for fusion.

Shixiaowei02 avatar Nov 02 '23 09:11 Shixiaowei02

@Shixiaowei02 After some logging work, I understand match_and_rewrite better, Thank you~

foricee avatar Nov 03 '23 03:11 foricee