interpretml / interpret-text

A library that incorporates state-of-the-art explainers for text-based machine learning models and visualizes the result with a built-in dashboard.
MIT License
414 stars 68 forks source link

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

Open hinneslung opened 4 years ago

hinneslung commented 4 years ago

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.

newman1234 commented 3 years ago

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