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

For auto-regressive prefill-phase, how can I get all token's hidden_states?

Open Bobo-y opened this issue 1 year ago • 5 comments

for llama model, I want to return the last hidden_states of all tokens. And I already got the hidden_states in py_session with debug_mode. But it's only return the last token's hidden_states shape like [1, 4096].

I debug the trt-build phase in llama/model.py, the IR's shape of hidden_states in forward func is [-1, 4096], Can I assume that we can only generate one by one?

Bobo-y avatar Mar 31 '25 13:03 Bobo-y

You need to add an output tensor with gather_context = True, then they will outputted as "context_". You will have to build a model that marks this tensor as an output

maxilevi avatar Mar 31 '25 13:03 maxilevi

You need to add an output tensor with gather_context = True, then they will outputted as "context_". You will have to build a model that marks this tensor as an output

hello, can you tell me where to add attr gather_context = True ? thanks!! I add hidden_states as output by hidden_states.mark_output("hidden_states", self.confgi.dtype) or self.register_network_output("hidden_states", hidden_states)[both work]. But both api can't pass "gather_context". Meanwhile, the prefill-context [in trt-llm context-phase], the hidden_states in forward may be with size of [batch_size, prompt_len, hidden_size], but the IR's shape is [-1, hidden_size], so by "self.register_network_output("hidden_states", hidden_states) " or " hidden_states.mark_output("hidden_states", self.confgi.dtype)" only return shape [1, 4096], It make me confused.

Bobo-y avatar Apr 01 '25 02:04 Bobo-y

  • @yweng0828 who may know more details of this question.

June

juney-nvidia avatar Apr 01 '25 06:04 juney-nvidia

Solved

Bobo-y avatar Apr 01 '25 07:04 Bobo-y

Hey @Bobo-y would you mind sharing how you made it work?

aamir-s18 avatar Aug 24 '25 08:08 aamir-s18