Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

deepcopy(instance) fails when using mismatched indexer. #4270

Closed
dheerajrajagopal opened this issue May 21, 2020 · 2 comments · Fixed by #4294
Closed

deepcopy(instance) fails when using mismatched indexer. #4270

dheerajrajagopal opened this issue May 21, 2020 · 2 comments · Fixed by #4294
Milestone

Comments

@dheerajrajagopal
Copy link

dheerajrajagopal commented May 21, 2020

Describe the bug
deepcopy(instance) fails when using mismatched indexer.

Stack Trace


20 to the input and returns those gradients normalized and sanitized.
21 """
---> 22 labeled_instances = self.predictor.json_to_labeled_instances(inputs)
23
24 # List of embedding inputs, used for multiplying gradient by the input for normalization
/usr1/home/dheeraj/anaconda3/envs/interpret-baseline/lib/python3.7/site-packages/allennlp/predictors/predictor.py in json_to_labeled_instances(self, inputs)
62 instance = self._json_to_instance(inputs)
63 outputs = self._model.forward_on_instance(instance)
---> 64 new_instances = self.predictions_to_labeled_instances(instance, outputs)
65 return new_instances
66
/usr1/home/dheeraj/anaconda3/envs/interpret-baseline/lib/python3.7/site-packages/allennlp/predictors/text_classifier.py in predictions_to_labeled_instances(self, instance, outputs)
43 self, instance: Instance, outputs: Dict[str, numpy.ndarray]
44 ) -> List[Instance]:
---> 45 new_instance = deepcopy(instance)
46 label = numpy.argmax(outputs["probs"])
47 new_instance.add_field("label", LabelField(int(label), skip_indexing=True))
/usr1/home/dheeraj/anaconda3/envs/interpret-baseline/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
178 y = x
179 else:
--> 180 y = _reconstruct(x, memo, *rv)

To Reproduce

from allennlp.predictors.predictor import Predictor
from allennlp.data.tokenizers import SpacyTokenizer
import allennlp_models.sentiment
from allennlp.interpret.saliency_interpreters import SimpleGradient

predictor = Predictor.from_path("https://storage.googleapis.com/allennlp-public-models/sst-roberta-large-2020.05.05.tar.gz")
predictor.predict(
   sentence="a very well-made, funny and entertaining picture."
 )
tokenizer = SpacyTokenizer()
tokenized_sentence = tokenizer.tokenize("a very well-made, funny and entertaining picture.")
simple_grad_viz =  SimpleGradient(predictor).saliency_interpret_from_json({'sentence':"a very well-made, funny and entertaining picture."}) 

Expected behavior
Should not give an error

System (please complete the following information):

  • OS: Linux (Ubuntu 18.04)
  • Python version: 3.7.4
  • AllenNLP version: v1.0.0.rc4
  • PyTorch version: 1.5.0
@dirkgr
Copy link
Member

dirkgr commented May 22, 2020

Better stack trace:

TypeError                                 Traceback (most recent call last)
<ipython-input-2-694d0a9d199b> in <module>
     10 tokenizer = SpacyTokenizer()
     11 tokenized_sentence = tokenizer.tokenize("a very well-made, funny and entertaining picture.")
---> 12 simple_grad_viz =  SimpleGradient(predictor).saliency_interpret_from_json({'sentence':"a very well-made, funny and entertaining picture."})

~/Documents/allennlp/allennlp/interpret/saliency_interpreters/simple_gradient.py in saliency_interpret_from_json(self, inputs)
     20         to the input and returns those gradients normalized and sanitized.
     21         """
---> 22         labeled_instances = self.predictor.json_to_labeled_instances(inputs)
     23
     24         # List of embedding inputs, used for multiplying gradient by the input for normalization

~/Documents/allennlp/allennlp/predictors/predictor.py in json_to_labeled_instances(self, inputs)
     62         instance = self._json_to_instance(inputs)
     63         outputs = self._model.forward_on_instance(instance)
---> 64         new_instances = self.predictions_to_labeled_instances(instance, outputs)
     65         return new_instances
     66

~/Documents/allennlp/allennlp/predictors/text_classifier.py in predictions_to_labeled_instances(self, instance, outputs)
     43         self, instance: Instance, outputs: Dict[str, numpy.ndarray]
     44     ) -> List[Instance]:
---> 45         new_instance = deepcopy(instance)
     46         label = numpy.argmax(outputs["probs"])
     47         new_instance.add_field("label", LabelField(int(label), skip_indexing=True))

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    178                     y = x
    179                 else:
--> 180                     y = _reconstruct(x, memo, *rv)
    181
    182     # If is its own copy, don't memoize.

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy)
    279     if state is not None:
    280         if deep:
--> 281             state = deepcopy(state, memo)
    282         if hasattr(y, '__setstate__'):
    283             y.__setstate__(state)

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    148     copier = _deepcopy_dispatch.get(cls)
    149     if copier:
--> 150         y = copier(x, memo)
    151     else:
    152         try:

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _deepcopy_dict(x, memo, deepcopy)
    239     memo[id(x)] = y
    240     for key, value in x.items():
--> 241         y[deepcopy(key, memo)] = deepcopy(value, memo)
    242     return y
    243 d[dict] = _deepcopy_dict

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    148     copier = _deepcopy_dispatch.get(cls)
    149     if copier:
--> 150         y = copier(x, memo)
    151     else:
    152         try:

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _deepcopy_dict(x, memo, deepcopy)
    239     memo[id(x)] = y
    240     for key, value in x.items():
--> 241         y[deepcopy(key, memo)] = deepcopy(value, memo)
    242     return y
    243 d[dict] = _deepcopy_dict

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    178                     y = x
    179                 else:
--> 180                     y = _reconstruct(x, memo, *rv)
    181
    182     # If is its own copy, don't memoize.

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy)
    279     if state is not None:
    280         if deep:
--> 281             state = deepcopy(state, memo)
    282         if hasattr(y, '__setstate__'):
    283             y.__setstate__(state)

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    148     copier = _deepcopy_dispatch.get(cls)
    149     if copier:
--> 150         y = copier(x, memo)
    151     else:
    152         try:

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _deepcopy_dict(x, memo, deepcopy)
    239     memo[id(x)] = y
    240     for key, value in x.items():
--> 241         y[deepcopy(key, memo)] = deepcopy(value, memo)
    242     return y
    243 d[dict] = _deepcopy_dict

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    148     copier = _deepcopy_dispatch.get(cls)
    149     if copier:
--> 150         y = copier(x, memo)
    151     else:
    152         try:

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _deepcopy_dict(x, memo, deepcopy)
    239     memo[id(x)] = y
    240     for key, value in x.items():
--> 241         y[deepcopy(key, memo)] = deepcopy(value, memo)
    242     return y
    243 d[dict] = _deepcopy_dict

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    178                     y = x
    179                 else:
--> 180                     y = _reconstruct(x, memo, *rv)
    181
    182     # If is its own copy, don't memoize.

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy)
    279     if state is not None:
    280         if deep:
--> 281             state = deepcopy(state, memo)
    282         if hasattr(y, '__setstate__'):
    283             y.__setstate__(state)

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    148     copier = _deepcopy_dispatch.get(cls)
    149     if copier:
--> 150         y = copier(x, memo)
    151     else:
    152         try:

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _deepcopy_dict(x, memo, deepcopy)
    239     memo[id(x)] = y
    240     for key, value in x.items():
--> 241         y[deepcopy(key, memo)] = deepcopy(value, memo)
    242     return y
    243 d[dict] = _deepcopy_dict

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    178                     y = x
    179                 else:
--> 180                     y = _reconstruct(x, memo, *rv)
    181
    182     # If is its own copy, don't memoize.

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy)
    279     if state is not None:
    280         if deep:
--> 281             state = deepcopy(state, memo)
    282         if hasattr(y, '__setstate__'):
    283             y.__setstate__(state)

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    148     copier = _deepcopy_dispatch.get(cls)
    149     if copier:
--> 150         y = copier(x, memo)
    151     else:
    152         try:

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _deepcopy_dict(x, memo, deepcopy)
    239     memo[id(x)] = y
    240     for key, value in x.items():
--> 241         y[deepcopy(key, memo)] = deepcopy(value, memo)
    242     return y
    243 d[dict] = _deepcopy_dict

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    178                     y = x
    179                 else:
--> 180                     y = _reconstruct(x, memo, *rv)
    181
    182     # If is its own copy, don't memoize.

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy)
    279     if state is not None:
    280         if deep:
--> 281             state = deepcopy(state, memo)
    282         if hasattr(y, '__setstate__'):
    283             y.__setstate__(state)

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    148     copier = _deepcopy_dispatch.get(cls)
    149     if copier:
--> 150         y = copier(x, memo)
    151     else:
    152         try:

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _deepcopy_dict(x, memo, deepcopy)
    239     memo[id(x)] = y
    240     for key, value in x.items():
--> 241         y[deepcopy(key, memo)] = deepcopy(value, memo)
    242     return y
    243 d[dict] = _deepcopy_dict

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    178                     y = x
    179                 else:
--> 180                     y = _reconstruct(x, memo, *rv)
    181
    182     # If is its own copy, don't memoize.

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy)
    279     if state is not None:
    280         if deep:
--> 281             state = deepcopy(state, memo)
    282         if hasattr(y, '__setstate__'):
    283             y.__setstate__(state)

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    148     copier = _deepcopy_dispatch.get(cls)
    149     if copier:
--> 150         y = copier(x, memo)
    151     else:
    152         try:

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _deepcopy_dict(x, memo, deepcopy)
    239     memo[id(x)] = y
    240     for key, value in x.items():
--> 241         y[deepcopy(key, memo)] = deepcopy(value, memo)
    242     return y
    243 d[dict] = _deepcopy_dict

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    178                     y = x
    179                 else:
--> 180                     y = _reconstruct(x, memo, *rv)
    181
    182     # If is its own copy, don't memoize.

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _reconstruct(x, memo, func, args, state, listiter, dictiter, deepcopy)
    279     if state is not None:
    280         if deep:
--> 281             state = deepcopy(state, memo)
    282         if hasattr(y, '__setstate__'):
    283             y.__setstate__(state)

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    148     copier = _deepcopy_dispatch.get(cls)
    149     if copier:
--> 150         y = copier(x, memo)
    151     else:
    152         try:

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in _deepcopy_dict(x, memo, deepcopy)
    239     memo[id(x)] = y
    240     for key, value in x.items():
--> 241         y[deepcopy(key, memo)] = deepcopy(value, memo)
    242     return y
    243 d[dict] = _deepcopy_dict

~/anaconda3/envs/allennlp-models/lib/python3.7/copy.py in deepcopy(x, memo, _nil)
    167                     reductor = getattr(x, "__reduce_ex__", None)
    168                     if reductor:
--> 169                         rv = reductor(4)
    170                     else:
    171                         reductor = getattr(x, "__reduce__", None)

TypeError: can't pickle Tokenizer objects

@dirkgr
Copy link
Member

dirkgr commented May 22, 2020

We're thinking we have to either prevent deep copy during interpret, or figure out a way to prevent it from trying to make a copy of the tokenizer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants