Skip to content

NameError: name 'residual' is not defined #100

Closed

Description

@Stopwolf

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..

Activity

  1. atuldubey007 commented on Jan 19, 2024

    @atuldubey007

    I'm facing the same issue. The code worked few days back.

  2. Stopwolf commented on Jan 19, 2024

    @Stopwolf
    Author

    @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:

    %%capture
    import torch
    major_version, minor_version = torch.cuda.get_device_capability()
    if major_version >= 8:
        # Use this for new GPUs like Ampere, Hopper GPUs (RTX 30xx, RTX 40xx, A100, H100, L40)
        %pip install -q -U "unsloth[colab_ampere] @ git+https://github.com/unslothai/unsloth.git@c1ac4d2707574868767345e76ebe49c8353f9057"
    else:
        # Use this for older GPUs (V100, Tesla T4, RTX 20xx)
        %pip install -q -U "unsloth[colab] @ git+https://github.com/unslothai/unsloth.git@c1ac4d2707574868767345e76ebe49c8353f9057"
    pass
    
    %pip install -q -U "git+https://github.com/huggingface/transformers.git" # Native 4bit loading works!
    
  3. danielhanchen commented on Jan 19, 2024

    @danielhanchen
    Member

    OOPS sorry - I'm working on a fix ASAP (as in now)

  4. danielhanchen commented on Jan 19, 2024

    @danielhanchen
    Member

    @atuldubey007 @Stopwolf Solved just now! Sorry again!

  5. Stopwolf commented on Jan 19, 2024

    @Stopwolf
    Author

    @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! 🤗

  6. danielhanchen commented on Jan 19, 2024

    @danielhanchen
    Member

    Thanks!!! :)) Well I'll close this issue once it's confirmed it's fixed :)))

  7. neph1 commented on Jan 19, 2024

    @neph1
    Contributor

    I get this exception when running validation (eval) during training. I've tried updating since the fix came in. Is it related?

  8. danielhanchen commented on Jan 19, 2024

    @danielhanchen
    Member

    @neph1 Yep fixed! Just re-update via pip install --upgrade git+https://github.com/unslothai/unsloth.git and all good to go! Again sorry!

  9. neph1 commented on Jan 20, 2024

    @neph1
    Contributor

    I still get the issue on eval:


    NameError Traceback (most recent call last)
    in
    ----> 1 trainer_stats = trainer.train()

    ~/.local/lib/python3.10/site-packages/trl/trainer/sft_trainer.py in train(self, *args, **kwargs)
    315 self.model = self._trl_activate_neftune(self.model)
    316
    --> 317 output = super().train(*args, **kwargs)
    318
    319 # After training we make sure to retrieve back the original forward pass method

    ~/.local/lib/python3.10/site-packages/transformers/trainer.py in train(self, resume_from_checkpoint, trial, ignore_keys_for_eval, **kwargs)
    1535 hf_hub_utils.enable_progress_bars()
    1536 else:
    -> 1537 return inner_training_loop(
    1538 args=args,
    1539 resume_from_checkpoint=resume_from_checkpoint,

    ~/.local/lib/python3.10/site-packages/transformers/trainer.py in _inner_training_loop(self, batch_size, args, resume_from_checkpoint, trial, ignore_keys_for_eval)
    1912 self.control = self.callback_handler.on_step_end(args, self.state, self.control)
    1913
    -> 1914 self._maybe_log_save_evaluate(tr_loss, model, trial, epoch, ignore_keys_for_eval)
    1915 else:
    1916 self.control = self.callback_handler.on_substep_end(args, self.state, self.control)

    ~/.local/lib/python3.10/site-packages/transformers/trainer.py in _maybe_log_save_evaluate(self, tr_loss, model, trial, epoch, ignore_keys_for_eval)
    2266 metrics.update(dataset_metrics)
    2267 else:
    -> 2268 metrics = self.evaluate(ignore_keys=ignore_keys_for_eval)
    2269 self._report_to_hp_search(trial, self.state.global_step, metrics)
    2270

    ~/.local/lib/python3.10/site-packages/transformers/trainer.py in evaluate(self, eval_dataset, ignore_keys, metric_key_prefix)
    3017
    3018 eval_loop = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
    -> 3019 output = eval_loop(
    3020 eval_dataloader,
    3021 description="Evaluation",

    ~/.local/lib/python3.10/site-packages/transformers/trainer.py in evaluation_loop(self, dataloader, description, prediction_loss_only, ignore_keys, metric_key_prefix)
    3206
    3207 # Prediction step
    -> 3208 loss, logits, labels = self.prediction_step(model, inputs, prediction_loss_only, ignore_keys=ignore_keys)
    3209 main_input_name = getattr(self.model, "main_input_name", "input_ids")
    3210 inputs_decode = self._prepare_input(inputs[main_input_name]) if args.include_inputs_for_metrics else None

    ~/.local/lib/python3.10/site-packages/transformers/trainer.py in prediction_step(self, model, inputs, prediction_loss_only, ignore_keys)
    3423 if has_labels or loss_without_labels:
    3424 with self.compute_loss_context_manager():
    -> 3425 loss, outputs = self.compute_loss(model, inputs, return_outputs=True)
    3426 loss = loss.mean().detach()
    3427

    ~/.local/lib/python3.10/site-packages/transformers/trainer.py in compute_loss(self, model, inputs, return_outputs)
    2756 else:
    2757 labels = None
    -> 2758 outputs = model(**inputs)
    2759 # Save past state if it exists
    2760 # TODO: this needs to be fixed and made cleaner later.

    ~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py in _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)
    1519
    1520 def _call_impl(self, *args, **kwargs):

    ~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
    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)
    1528
    1529 try:

    ~/.local/lib/python3.10/site-packages/accelerate/utils/operations.py in forward(*args, **kwargs)
    685
    686 def forward(*args, **kwargs):
    --> 687 return model_forward(*args, **kwargs)
    688
    689 # To act like a decorator so that it can be popped when doing extract_model_from_parallel

    ~/.local/lib/python3.10/site-packages/accelerate/utils/operations.py in call(self, *args, **kwargs)
    673
    674 def call(self, *args, **kwargs):
    --> 675 return convert_to_fp32(self.model_forward(*args, **kwargs))
    676
    677 def getstate(self):

    ~/.local/lib/python3.10/site-packages/torch/amp/autocast_mode.py in decorate_autocast(*args, **kwargs)
    14 def decorate_autocast(*args, **kwargs):
    15 with autocast_instance:
    ---> 16 return func(*args, **kwargs)
    17
    18 decorate_autocast.__script_unsupported = "@autocast() decorator is not supported in script mode" # type: ignore[attr-defined]

    ~/.local/lib/python3.10/site-packages/accelerate/utils/operations.py in forward(*args, **kwargs)
    685
    686 def forward(*args, **kwargs):
    --> 687 return model_forward(*args, **kwargs)
    688
    689 # To act like a decorator so that it can be popped when doing extract_model_from_parallel

    ~/.local/lib/python3.10/site-packages/accelerate/utils/operations.py in call(self, *args, **kwargs)
    673
    674 def call(self, *args, **kwargs):
    --> 675 return convert_to_fp32(self.model_forward(*args, **kwargs))
    676
    677 def getstate(self):

    ~/.local/lib/python3.10/site-packages/torch/amp/autocast_mode.py in decorate_autocast(*args, **kwargs)
    14 def decorate_autocast(*args, **kwargs):
    15 with autocast_instance:
    ---> 16 return func(*args, **kwargs)
    17
    18 decorate_autocast.__script_unsupported = "@autocast() decorator is not supported in script mode" # type: ignore[attr-defined]

    ~/.local/lib/python3.10/site-packages/unsloth/models/llama.py in PeftModelForCausalLM_fast_forward(self, input_ids, causal_mask, attention_mask, inputs_embeds, labels, output_attentions, output_hidden_states, return_dict, task_ids, **kwargs)
    630 **kwargs,
    631 ):
    --> 632 return self.base_model(
    633 input_ids=input_ids,
    634 causal_mask=causal_mask,

    ~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py in _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)
    1519
    1520 def _call_impl(self, *args, **kwargs):

    ~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
    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)
    1528
    1529 try:

    ~/.local/lib/python3.10/site-packages/peft/tuners/tuners_utils.py in forward(self, *args, **kwargs)
    101
    102 def forward(self, *args: Any, **kwargs: Any):
    --> 103 return self.model.forward(*args, **kwargs)
    104
    105 @AbstractMethod

    ~/.local/lib/python3.10/site-packages/accelerate/hooks.py in new_forward(module, *args, **kwargs)
    163 output = module._old_forward(*args, **kwargs)
    164 else:
    --> 165 output = module._old_forward(*args, **kwargs)
    166 return module._hf_hook.post_forward(module, output)
    167

    ~/.local/lib/python3.10/site-packages/unsloth/models/mistral.py 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)
    195
    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,

    ~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py in _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)
    1519
    1520 def _call_impl(self, *args, **kwargs):

    ~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
    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)
    1528
    1529 try:

    ~/.local/lib/python3.10/site-packages/accelerate/hooks.py in new_forward(module, *args, **kwargs)
    163 output = module._old_forward(*args, **kwargs)
    164 else:
    --> 165 output = module._old_forward(*args, **kwargs)
    166 return module._hf_hook.post_forward(module, output)
    167

    ~/.local/lib/python3.10/site-packages/unsloth/models/llama.py 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)
    504 )
    505 else:
    --> 506 layer_outputs = decoder_layer(
    507 hidden_states,
    508 causal_mask=causal_mask,

    ~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py in _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)
    1519
    1520 def _call_impl(self, *args, **kwargs):

    ~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py in _call_impl(self, *args, **kwargs)
    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)
    1528
    1529 try:

    ~/.local/lib/python3.10/site-packages/accelerate/hooks.py in new_forward(module, *args, **kwargs)
    163 output = module._old_forward(*args, **kwargs)
    164 else:
    --> 165 output = module._old_forward(*args, **kwargs)
    166 return module._hf_hook.post_forward(module, output)
    167

    ~/.local/lib/python3.10/site-packages/unsloth/models/llama.py in LlamaDecoderLayer_fast_forward(self, hidden_states, causal_mask, attention_mask, position_ids, past_key_value, output_attentions, use_cache, padding_mask, *args, **kwargs)
    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,

    ~/.local/lib/python3.10/site-packages/unsloth/models/llama.py 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

  10. danielhanchen commented on Jan 20, 2024

    @danielhanchen
    Member

    @neph1 Fixed in the latest release - sorry for the late reply! pip install --upgrade git+https://github.com/unslothai/unsloth.git to update just Unsloth! :) Sorry again!

  11. neph1 commented on Jan 20, 2024

    @neph1
    Contributor

    Confirmed! Although I had to uninstall and reinstall. Thanks for the quick turn around.

  12. danielhanchen commented on Jan 22, 2024

    @danielhanchen
    Member

    @neph1 :) Well closing for now if this is solved!

  13. added a commit that references this issue on Mar 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions