# SPDX-License-Identifier: Apache-2.0# SPDX-FileCopyrightText: Copyright contributors to the vLLM projectimporttempfilefromsafetensorsimportsafe_openfromvllmimportLLM,SamplingParams# Example: Using the custom "extract_hidden_states" speculator method and# ExampleHiddenStatesConnector to extract and save hidden states from vllmwithtempfile.TemporaryDirectory()astmpdirname:llm=LLM(model="Qwen/Qwen3-8B",# Your target modelspeculative_config={"method":"extract_hidden_states","num_speculative_tokens":1,"draft_model_config":{"hf_config":{"eagle_aux_hidden_state_layer_ids":[# Target model layer indices1,2,3,4,],}},},kv_transfer_config={"kv_connector":"ExampleHiddenStatesConnector","kv_role":"kv_producer","kv_connector_extra_config":{"shared_storage_path":tmpdirname,},},)prompts=["Generate a sentence with hidden states","Write a python function"]sampling_params=SamplingParams(max_tokens=1)outputs=llm.generate(prompts,sampling_params)foroutputinoutputs:print("\nPrompt:",output.prompt)print("Prompt token ids:",output.prompt_token_ids)hidden_states_path=output.kv_transfer_params.get("hidden_states_path")asserthidden_states_pathisnotNoneprint("Prompt hidden states path:",hidden_states_path)withsafe_open(hidden_states_path,"pt")asf:token_ids=f.get_tensor("token_ids")hidden_states=f.get_tensor("hidden_states")print("Extracted token ids:",token_ids)# Matches prompt token idsprint("Extracted hidden states shape:",hidden_states.shape)# [num_hidden_layers, prompt len, hidden size]print("Extracted hidden states:",hidden_states)