Example code in README.md doesn't work

#168
by johnucm - opened

When i run

from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
from mistral_common.protocol.instruct.messages import UserMessage
from mistral_common.protocol.instruct.request import ChatCompletionRequest
 
mistral_models_path = "MISTRAL_MODELS_PATH"
 
tokenizer = MistralTokenizer.v1()
 
completion_request = ChatCompletionRequest(messages=[UserMessage(content="Explain Machine Learning to me in a nutshell.")])
 
tokens = tokenizer.encode_chat_completion(completion_request).tokens

from transformers import AutoModelForCausalLM
 
model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
model.to("cuda")
 
generated_ids = model.generate(tokens, max_new_tokens=1000, do_sample=True)

# decode with mistral tokenizer
result = tokenizer.decode(generated_ids[0].tolist())
print(result)

I got error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[9], line 18
     15 model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
     16 model.to("cuda")
---> 18 generated_ids = model.generate(tokens, max_new_tokens=1000, do_sample=True)
     20 # decode with mistral tokenizer
     21 result = tokenizer.decode(generated_ids[0].tolist())

File ~/.conda/envs/olive/lib/python3.10/site-packages/torch/utils/_contextlib.py:116, in context_decorator.<locals>.decorate_context(*args, **kwargs)
    113 @functools.wraps(func)
    114 def decorate_context(*args, **kwargs):
    115     with ctx_factory():
--> 116         return func(*args, **kwargs)

File ~/.conda/envs/olive/lib/python3.10/site-packages/transformers/generation/utils.py:2065, 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)
   2061 # 3. Define model inputs
   2062 inputs_tensor, model_input_name, model_kwargs = self._prepare_model_inputs(
   2063     inputs, generation_config.bos_token_id, model_kwargs
   2064 )
-> 2065 batch_size = inputs_tensor.shape[0]
   2067 device = inputs_tensor.device
   2068 self._prepare_special_tokens(generation_config, kwargs_has_attention_mask, device=device)

AttributeError: 'list' object has no attribute 'shape'
Your need to confirm your account before you can post a new comment.

Sign up or log in to comment