interpret-text icon indicating copy to clipboard operation
interpret-text copied to clipboard

Using bert-base-chinese causes CUDA error: device-side assert triggered

Open hinneslung opened this issue 5 years ago • 1 comments

I am following the Text Classification of MultiNLI Sentences using BERT notebook to interpret BERT classification work. When I changed the line LANGUAGE = Language.ENGLISH to LANGUAGE = Language.CHINESE, indicating to use the bert-base-chinese pre-trained model, the following error occured:

interpreter_unified.explain_local('', 'fiction')
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
~/Projects/interpret-text/notebooks/text_classification/unified_info_explainer_sample.py in 
----> 1 interpreter_unified.explain_local('', 'fiction')

~/miniconda3/envs/interpret_gpu/lib/python3.7/site-packages/interpret_text/experimental/unified_information.py in explain_local(self, X, y, name, num_iteration)
     95         X = _validate_X(X)
     96 
---> 97         embedded_input, parsed_sentence = _get_single_embedding(self.model, X, self.device)
     98         self.input_embeddings = embedded_input
     99         self.parsed_sentence = parsed_sentence

~/miniconda3/envs/interpret_gpu/lib/python3.7/site-packages/interpret_text/experimental/common/utils_unified.py in _get_single_embedding(model, text, device)
     33     words = [BertTokens.CLS] + tokenizer.tokenize(text) + [BertTokens.SEP]
     34     tokenized_ids = tokenizer.convert_tokens_to_ids(words)
---> 35     token_tensor = torch.tensor([tokenized_ids], device=device)
     36     embedding = model.bert.embeddings(token_tensor)[0]
     37     return embedding, words

I tried with the original dataset as used in the example notebook, and also tried with my own dataset with Chinese texts. Both resulted in the above error. There was no issue when using multilingual model.

hinneslung avatar Jun 27 '20 10:06 hinneslung

it may be vocal size which not matches embedding shape. you can try to modify raw unified_information.py as below.

def explain_local(...)
       # ...
       if self.regular is None:
            assert self.train_dataset is not None, "Training dataset is required"

            # sample the training dataset
            if len(self.train_dataset) <= self.max_points:
                sampled_train_dataset = self.train_dataset
            else:
                sampled_train_dataset = random.sample(self.train_dataset, k=self.max_points)

            training_embeddings = make_bert_embeddings(
                # ======================================
                # MODIFIED HERE, LANGUAGE param are now added
                # ======================================
                sampled_train_dataset, self.model, self.device, LANGUAGE=Language.CHINESE  
            )
       # ...

newman1234 avatar Nov 16 '20 08:11 newman1234