TypeError: 'HybridCache' object is not subscriptable

#24
by muxiao - opened

TypeError Traceback (most recent call last)
Cell In[20], line 5
3 inputs = tokenizer("hello", return_tensors="pt")
4 print(inputs["input_ids"])
----> 5 outputs = model.generate(input_ids=inputs["input_ids"].to("cuda"), max_new_tokens=50,use_cache=False)
6 print(tokenizer.batch_decode(outputs.detach().cpu().numpy(), skip_special_tokens=True)[0])

File D:\Anaconda\envs\MX\lib\site-packages\peft\peft_model.py:1493, in PeftModelForCausalLM.generate(self, *args, **kwargs)
1491 outputs = self.base_model.generate(*args, **kwargs)
1492 else:
-> 1493 outputs = self.base_model.generate(**kwargs)
1494 except:
1495 self.base_model.prepare_inputs_for_generation = self.base_model_prepare_inputs_for_generation

File D:\Anaconda\envs\MX\lib\site-packages\torch\utils_contextlib.py:115, in context_decorator..decorate_context(*args, **kwargs)
112 @functools.wraps(func)
113 def decorate_context(*args, **kwargs):
114 with ctx_factory():
--> 115 return func(*args, **kwargs)

File D:\Anaconda\envs\MX\lib\site-packages\transformers\generation\utils.py:2024, in GenerationMixin.generate(self, inputs, generation_config, logits_processor, stopping_criteria, prefix_allowed_tokens_fn, synced_gpus, assistant_model, streamer, negative_prompt_ids, negative_prompt_attention_mask, **kwargs)
2016 input_ids, model_kwargs = self._expand_inputs_for_generation(
2017 input_ids=input_ids,
2018 expand_size=generation_config.num_return_sequences,
2019 is_encoder_decoder=self.config.is_encoder_decoder,
2020 **model_kwargs,
2021 )
2023 # 13. run sample (it degenerates to greedy search when generation_config.do_sample=False)
-> 2024 result = self._sample(
2025 input_ids,
2026 logits_processor=prepared_logits_processor,
2027 logits_warper=prepared_logits_warper,
2028 stopping_criteria=prepared_stopping_criteria,
2029 generation_config=generation_config,
2030 synced_gpus=synced_gpus,
2031 streamer=streamer,
2032 **model_kwargs,
2033 )
2035 elif generation_mode in (GenerationMode.BEAM_SAMPLE, GenerationMode.BEAM_SEARCH):
2036 # 11. prepare logits warper
2037 prepared_logits_warper = (
2038 self._get_logits_warper(generation_config, device=input_ids.device)
2039 if generation_config.do_sample
2040 else None
2041 )

File D:\Anaconda\envs\MX\lib\site-packages\transformers\generation\utils.py:2975, in GenerationMixin._sample(self, input_ids, logits_processor, stopping_criteria, generation_config, synced_gpus, streamer, logits_warper, **model_kwargs)
2969 model_kwargs = self._get_initial_cache_position(input_ids, model_kwargs)
2971 while self._has_unfinished_sequences(
2972 this_peer_finished, synced_gpus, device=input_ids.device, cur_len=cur_len, max_length=max_length
2973 ):
2974 # prepare model inputs
-> 2975 model_inputs = self.prepare_inputs_for_generation(input_ids, **model_kwargs)
2977 # prepare variable output controls (note: some models won't accept all output controls)
2978 model_inputs.update({"output_attentions": output_attentions} if output_attentions else {})

File D:\Anaconda\envs\MX\lib\site-packages\peft\peft_model.py:1522, in PeftModelForCausalLM.prepare_inputs_for_generation(self, task_ids, *args, **kwargs)
1517 if peft_config.is_prompt_learning:
1518 if uses_cache and (model_kwargs["past_key_values"] is not None):
1519 # change in the logic of prepare_inputs_for_generation makes the below code necessary
1520 # In prompt learning methods, past key values are longer when compared to the input_ids.
1521 # As such only consider the last input ids in the autogressive generation phase.
-> 1522 if model_kwargs["past_key_values"][0][0].shape[-2] >= model_kwargs["input_ids"].shape[1]:
1523 model_kwargs["input_ids"] = model_kwargs["input_ids"][:, -1:]
1525 if model_kwargs.get("attention_mask", None) is not None:

Google org

Hi @muxiao ,

I ran the sample code successfully in Google Colab with the runtime type set to "T4 GPU" but couldn't reproduce the issue. Please refer to the following notebook for details: https://colab.research.google.com/drive/1mOcOBPgR_wPxu79nEI1S01MyWQlb63Z1#scrollTo=gcYjXEputjm6

Ensure the library versions are as follows:

  transformers = 4.44.2
  peft = 0.12.0
  torch = 2.4.0+cu121

If the issue still persists, please share the code files so we can assist you more effectively.

Thank you.

Sign up or log in to comment