You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the LLMGradientAttribution.attribute located in the llm_attr.py file, there is an issue where the target_tokens variable might not get initialized if the target argument passed to the function is neither a string (str) nor a PyTorch Tensor (torch.Tensor) of tokens. This could lead to an error further down in the method where target_tokens is used, as it relies on being initialized within the conditional branches for str and torch.Tensor types.
To Reproduce
Steps to reproduce the behavior:
Invoke the attribute method of the LLMGradientAttribution class, passing a target argument of a type different from str or torch.Tensor.
Since no initialization path exists for target_tokens outside the conditional checks for str and torch.Tensor, observe the potential for an uninitialized variable error.
529
530 attr_list = []
--> 531 for cur_target_idx, _ in enumerate(target_tokens):
532 # attr in shape(batch_size, input+output_len, emb_dim)
533 attr = self.attr_method.attribute(
UnboundLocalError: local variable 'target_tokens' referenced before assignment
Expected behavior
The method should either handle the scenario where target is of an unexpected type by initializing target_tokens to a sensible default or by raising a clear and descriptive error message that informs the user about the acceptable types for target.
Environment
- Captum / PyTorch Version: 0.7.0 / 2.1.2
- OS : Linux
- How you installed Captum / PyTorch (`conda`, `pip`, source): pip
- Python version: 3.10.13
Possible fix using assert:
Current code [llm_attr.py lines: 537: 545]:
else:
assertgen_argsisNone, "gen_args must be None when target is given"iftype(target) isstr:
# exclude sostarget_tokens=self.tokenizer.encode(target)[1:]
target_tokens=torch.tensor(target_tokens)
eliftype(target) istorch.Tensor:
target_tokens=target
After fix:
else:
assertgen_argsisNone, "gen_args must be None when target is given"assertisinstance(target, (str, torch.Tensor)), (
"The target argument must be either a str or torch.Tensor."
)
iftype(target) isstr:
# exclude sostarget_tokens=self.tokenizer.encode(target)[1:]
target_tokens=torch.tensor(target_tokens)
eliftype(target) istorch.Tensor:
target_tokens=target
The text was updated successfully, but these errors were encountered:
🐛 Bug
In the LLMGradientAttribution.attribute located in the llm_attr.py file, there is an issue where the target_tokens variable might not get initialized if the target argument passed to the function is neither a string (str) nor a PyTorch Tensor (torch.Tensor) of tokens. This could lead to an error further down in the method where target_tokens is used, as it relies on being initialized within the conditional branches for str and torch.Tensor types.
To Reproduce
Steps to reproduce the behavior:
Example code:
Output:
Expected behavior
The method should either handle the scenario where target is of an unexpected type by initializing target_tokens to a sensible default or by raising a clear and descriptive error message that informs the user about the acceptable types for target.
Environment
Possible fix using assert:
Current code [llm_attr.py lines: 537: 545]:
After fix:
The text was updated successfully, but these errors were encountered: