AttributeError: 'DynamicCache' object has no attribute 'get_usable_length'. Did you mean: 'get_seq_length'?
#59
by
liyongkang
- opened
你好你好,在使用 SentenceTransformer 加载模型并编码的时候直接报错无法运行,可以帮忙看一下嘛大佬?
代码示例就是你们提供的那个很简单
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("Alibaba-NLP/gte-Qwen2-7B-instruct", trust_remote_code=True)
# In case you want to reduce the maximum length:
model.max_seq_length = 8192
queries = [
"how much protein should a female eat",
"summit define",
]
documents = [
"As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.",
"Definition of summit for English Language Learners. : 1 the highest point of a mountain : the top of a mountain. : 2 the highest level. : 3 a meeting or series of meetings between the leaders of two or more governments.",
]
query_embeddings = model.encode(queries, prompt_name="query")
document_embeddings = model.encode(documents)
scores = (query_embeddings @ document_embeddings.T) * 100
print(scores.tolist())
报错信息如下:
past_key_values should not be None in from_legacy_cache()
Traceback (most recent call last):
File "/gpfs/work4/0/prjs0928/LLM_Robust/model_test/gte.py", line 16, in <module>
query_embeddings = model.encode(queries, prompt_name="query")
File "/home/yli4/anaconda3/envs/ir/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 120, in decorate_context
return func(*args, **kwargs)
File "/home/yli4/anaconda3/envs/ir/lib/python3.10/site-packages/sentence_transformers/SentenceTransformer.py", line 1051, in encode
out_features = self.forward(features, **kwargs)
File "/home/yli4/anaconda3/envs/ir/lib/python3.10/site-packages/sentence_transformers/SentenceTransformer.py", line 1132, in forward
input = module(input, **module_kwargs)
File "/home/yli4/anaconda3/envs/ir/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1773, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/home/yli4/anaconda3/envs/ir/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1784, in _call_impl
return forward_call(*args, **kwargs)
File "/home/yli4/anaconda3/envs/ir/lib/python3.10/site-packages/sentence_transformers/models/Transformer.py", line 234, in forward
outputs = self.auto_model(**trans_features, **kwargs, return_dict=True)
File "/home/yli4/anaconda3/envs/ir/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1773, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/home/yli4/anaconda3/envs/ir/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1784, in _call_impl
return forward_call(*args, **kwargs)
File "/gpfs/work4/0/prjs0928/cache/modules/transformers_modules/Alibaba-NLP/gte-Qwen2-7B-instruct/a8d08b36ada9cacfe34c4d6f80957772a025daf2/modeling_qwen.py", line 1001, in forward
past_key_values_length = past_key_values.get_usable_length(seq_length)
AttributeError: 'DynamicCache' object has no attribute 'get_usable_length'. Did you mean: 'get_seq_length'?
我所使用的是
transformers = 4.56.0
sentence-transformers = 5.1.0
不确定是版本的问题吗?感谢大佬
我把 transformers 降级到4.52.0 后会变好,但是有的库比如vllm 需要更高版本的 transformers 才能支持。大佬有办法修复一下以支持更高版本的 transformers 吗? 感谢感谢