Repository navigation
Closed
Description
I referenced this notebook for training the model, and after trying the inference code I got the NameError: name 'residual' is not defined. And indeed after looking at the code here I'd say that the method doesn't have access to residual variable.
My code for inferencing (only difference is using Zephyr):
import torch
from peft import PeftModel
from unsloth import FastLanguageModel
new_model, new_tokenizer = FastLanguageModel.from_pretrained(
model_name = "unsloth/zephyr-sft-bnb-4bit",
max_seq_length = config.train.max_length,
dtype = None,
load_in_4bit = True,
)
new_model = PeftModel.from_pretrained(new_model, config.adapter_path)
prompt = 'Who are you?'
system = ""
chat = new_tokenizer.apply_chat_template(
conversation=[
{"role": "system", "content": system},
{"role": "user", "content": prompt},
],
add_generation_prompt=True,
tokenize=False,
)
inputs = new_tokenizer(
chat,
return_tensors="pt",
truncation=True,
max_length=2048,
).to("cuda")
with torch.no_grad():
outputs = new_model.generate(
**inputs,
do_sample=True,
max_new_tokens=128,
top_p=0.9,
repetition_penalty=1.20,
num_beams=4,
temperature=0.7,
)
print(new_tokenizer.batch_decode(outputs, skip_special_tokens=True))
And here is the whole traceback:
NameError Traceback (most recent call last)
File <command-526523487133860>, line 23
15 inputs = new_tokenizer(
16 chat,
17 return_tensors="pt",
18 truncation=True,
19 max_length=2048,
20 ).to("cuda")
22 # with torch.no_grad():
---> 23 outputs = new_model.generate(
24 **inputs,
25 do_sample=True,
26 max_new_tokens=128,
27 # top_k=50,
28 top_p=0.9,
29 repetition_penalty=1.20,
30 num_beams=4,
31 temperature=0.7,
32 )
34 new_tokenizer.batch_decode(outputs, skip_special_tokens=True)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/peft/peft_model.py:1081, in PeftModelForCausalLM.generate(self, **kwargs)
1079 self.base_model.generation_config = self.generation_config
1080 try:
-> 1081 outputs = self.base_model.generate(**kwargs)
1082 except:
1083 self.base_model.prepare_inputs_for_generation = self.base_model_prepare_inputs_for_generation
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/torch/utils/_contextlib.py:115, in context_decorator.<locals>.decorate_context(*args, **kwargs)
112 @functools.wraps(func)
113 def decorate_context(*args, **kwargs):
114 with ctx_factory():
--> 115 return func(*args, **kwargs)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/transformers/generation/utils.py:1588, 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)
1580 input_ids, model_kwargs = self._expand_inputs_for_generation(
1581 input_ids=input_ids,
1582 expand_size=generation_config.num_beams,
1583 is_encoder_decoder=self.config.is_encoder_decoder,
1584 **model_kwargs,
1585 )
1587 # 14. run beam sample
-> 1588 return self.beam_sample(
1589 input_ids,
1590 beam_scorer,
1591 logits_processor=prepared_logits_processor,
1592 logits_warper=logits_warper,
1593 stopping_criteria=prepared_stopping_criteria,
1594 pad_token_id=generation_config.pad_token_id,
1595 eos_token_id=generation_config.eos_token_id,
1596 output_scores=generation_config.output_scores,
1597 return_dict_in_generate=generation_config.return_dict_in_generate,
1598 synced_gpus=synced_gpus,
1599 **model_kwargs,
1600 )
1602 elif generation_mode == GenerationMode.GROUP_BEAM_SEARCH:
1603 # 11. prepare beam search scorer
1604 beam_scorer = BeamSearchScorer(
1605 batch_size=batch_size,
1606 num_beams=generation_config.num_beams,
(...)
1612 max_length=generation_config.max_length,
1613 )
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/transformers/generation/utils.py:3269, in GenerationMixin.beam_sample(self, input_ids, beam_scorer, logits_processor, stopping_criteria, logits_warper, max_length, pad_token_id, eos_token_id, output_attentions, output_hidden_states, output_scores, return_dict_in_generate, synced_gpus, **model_kwargs)
3265 break
3267 model_inputs = self.prepare_inputs_for_generation(input_ids, **model_kwargs)
-> 3269 outputs = self(
3270 **model_inputs,
3271 return_dict=True,
3272 output_attentions=output_attentions,
3273 output_hidden_states=output_hidden_states,
3274 )
3276 if synced_gpus and this_peer_finished:
3277 cur_len = cur_len + 1
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/torch/nn/modules/module.py:1518, in Module._wrapped_call_impl(self, *args, **kwargs)
1516 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1517 else:
-> 1518 return self._call_impl(*args, **kwargs)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/torch/nn/modules/module.py:1527, in Module._call_impl(self, *args, **kwargs)
1522 # If we don't have any hooks, we want to skip the rest of the logic in
1523 # this function, and just call forward.
1524 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1525 or _global_backward_pre_hooks or _global_backward_hooks
1526 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1527 return forward_call(*args, **kwargs)
1529 try:
1530 result = None
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/accelerate/hooks.py:165, in add_hook_to_module.<locals>.new_forward(*args, **kwargs)
163 output = old_forward(*args, **kwargs)
164 else:
--> 165 output = old_forward(*args, **kwargs)
166 return module._hf_hook.post_forward(module, output)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/unsloth/models/mistral.py:197, in MistralForCausalLM_fast_forward(self, input_ids, causal_mask, attention_mask, position_ids, past_key_values, inputs_embeds, labels, use_cache, output_attentions, output_hidden_states, return_dict, *args, **kwargs)
194 return_dict = return_dict if return_dict is not None else self.config.use_return_dict
196 # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
--> 197 outputs = self.model(
198 input_ids=input_ids,
199 causal_mask=causal_mask,
200 attention_mask=attention_mask,
201 position_ids=position_ids,
202 past_key_values=past_key_values,
203 inputs_embeds=inputs_embeds,
204 use_cache=use_cache,
205 output_attentions=output_attentions,
206 output_hidden_states=output_hidden_states,
207 return_dict=return_dict,
208 )
210 hidden_states = outputs[0]
211 logits = self.lm_head(hidden_states)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/torch/nn/modules/module.py:1518, in Module._wrapped_call_impl(self, *args, **kwargs)
1516 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1517 else:
-> 1518 return self._call_impl(*args, **kwargs)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/torch/nn/modules/module.py:1527, in Module._call_impl(self, *args, **kwargs)
1522 # If we don't have any hooks, we want to skip the rest of the logic in
1523 # this function, and just call forward.
1524 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1525 or _global_backward_pre_hooks or _global_backward_hooks
1526 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1527 return forward_call(*args, **kwargs)
1529 try:
1530 result = None
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/accelerate/hooks.py:165, in add_hook_to_module.<locals>.new_forward(*args, **kwargs)
163 output = old_forward(*args, **kwargs)
164 else:
--> 165 output = old_forward(*args, **kwargs)
166 return module._hf_hook.post_forward(module, output)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/unsloth/models/llama.py:506, in LlamaModel_fast_forward(self, input_ids, causal_mask, attention_mask, position_ids, past_key_values, inputs_embeds, use_cache, output_attentions, output_hidden_states, return_dict, *args, **kwargs)
496 layer_outputs = torch.utils.checkpoint.checkpoint(
497 create_custom_forward(decoder_layer),
498 hidden_states,
(...)
503 preserve_rng_state=False,
504 )
505 else:
--> 506 layer_outputs = decoder_layer(
507 hidden_states,
508 causal_mask=causal_mask,
509 attention_mask=attention_mask,
510 position_ids=position_ids,
511 past_key_value=past_key_value,
512 output_attentions=output_attentions,
513 use_cache=use_cache,
514 padding_mask=padding_mask,
515 )
517 hidden_states = layer_outputs[0]
519 if use_cache:
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/torch/nn/modules/module.py:1518, in Module._wrapped_call_impl(self, *args, **kwargs)
1516 return self._compiled_call_impl(*args, **kwargs) # type: ignore[misc]
1517 else:
-> 1518 return self._call_impl(*args, **kwargs)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/torch/nn/modules/module.py:1527, in Module._call_impl(self, *args, **kwargs)
1522 # If we don't have any hooks, we want to skip the rest of the logic in
1523 # this function, and just call forward.
1524 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1525 or _global_backward_pre_hooks or _global_backward_hooks
1526 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1527 return forward_call(*args, **kwargs)
1529 try:
1530 result = None
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/accelerate/hooks.py:165, in add_hook_to_module.<locals>.new_forward(*args, **kwargs)
163 output = old_forward(*args, **kwargs)
164 else:
--> 165 output = old_forward(*args, **kwargs)
166 return module._hf_hook.post_forward(module, output)
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/unsloth/models/llama.py:331, in LlamaDecoderLayer_fast_forward(self, hidden_states, causal_mask, attention_mask, position_ids, past_key_value, output_attentions, use_cache, padding_mask, *args, **kwargs)
328 else:
329 # Self Attention
330 residual = hidden_states
--> 331 hidden_states = fast_rms_layernorm_inference(self.input_layernorm, hidden_states)
332 hidden_states, self_attn_weights, present_key_value = self.self_attn(
333 hidden_states=hidden_states,
334 causal_mask=causal_mask,
(...)
340 padding_mask=padding_mask,
341 )
342 hidden_states += residual
File /local_disk0/.ephemeral_nfs/envs/pythonEnv-3d11eeb3-f4e4-473c-9d86-410db9622bd8/lib/python3.10/site-packages/unsloth/models/llama.py:168, in fast_rms_layernorm_inference(self, X)
166 variance += self.variance_epsilon
167 X *= variance.rsqrt_()
--> 168 X = X.to(residual.dtype)
169 X *= self.weight
170 return X
NameError: name 'residual' is not defined
What is most weird is that my code worked couple of days ago..
Reacted by Atul Dubey
Metadata
Metadata
Assignees
Labels
No labels
Activity
atuldubey007 commentedon Jan 19, 2024
I'm facing the same issue. The code worked few days back.
Stopwolf commentedon Jan 19, 2024
@atuldubey007 seems that the new commit/PR broke it. if you use the previous commit, the code works again..
so just do the following when installing:
danielhanchen commentedon Jan 19, 2024
OOPS sorry - I'm working on a fix ASAP (as in now)
danielhanchen commentedon Jan 19, 2024
@atuldubey007 @Stopwolf Solved just now! Sorry again!
Stopwolf commentedon Jan 19, 2024
@danielhanchen hey man, there's nothing you should be sorry! you've made an awesome library and you're bound to have some hiccups along the way 😌
Continue being awesome! 🤗
danielhanchen commentedon Jan 19, 2024
Thanks!!! :)) Well I'll close this issue once it's confirmed it's fixed :)))
neph1 commentedon Jan 19, 2024
I get this exception when running validation (eval) during training. I've tried updating since the fix came in. Is it related?
danielhanchen commentedon Jan 19, 2024
@neph1 Yep fixed! Just re-update via
pip install --upgrade git+https://github.com/unslothai/unsloth.gitand all good to go! Again sorry!neph1 commentedon Jan 20, 2024
I still get the issue on eval:
danielhanchen commentedon Jan 20, 2024
@neph1 Fixed in the latest release - sorry for the late reply!
pip install --upgrade git+https://github.com/unslothai/unsloth.gitto update just Unsloth! :) Sorry again!neph1 commentedon Jan 20, 2024
Confirmed! Although I had to uninstall and reinstall. Thanks for the quick turn around.
danielhanchen commentedon Jan 22, 2024
@neph1 :) Well closing for now if this is solved!
Merge pull request unslothai#100 from unslothai/feature/chat-compare
Studio Images: preserve gallery selections during generation (unsloth…