Facing issue at the time of Inferencing
sachin16495 opened this issue · 1 comments
I have trained my multimodel with distibert as a base model but the time of Inferencing
I tried the below script
with torch.no_grad():
_, logits, classifier_outputs = model(
torch.tensor(input_val['input_ids']),
attention_mask=torch.tensor(input_val['attention_mask']),
cat_feats=pre['cat_feats'],
numerical_feats=pre['numerical_feats']
)
I got the following error after that.
IndexError Traceback (most recent call last)
Input In [145], in <cell line: 1>()
1 with torch.no_grad():
----> 2 _, logits, classifier_outputs = model(
3 torch.tensor(input_val['input_ids']),
4 attention_mask=torch.tensor(input_val['attention_mask']),
5 cat_feats=pre['cat_feats'],
6 numerical_feats=pre['numerical_feats']
7 )
File ~/cat_multimodel_env/lib/python3.8/site-packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don't have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
File ~/cat_multimodel_env/lib/python3.8/site-packages/multimodal_transformers/model/tabular_transformers.py:314, in DistilBertWithTabular.forward(self, input_ids, attention_mask, head_mask, inputs_embeds, labels, output_attentions, output_hidden_states, class_weights, cat_feats, numerical_feats)
277 @add_start_docstrings_to_callable(DISTILBERT_INPUTS_DOCSTRING.format("(batch_size, sequence_length)"))
278 def forward(
279 self,
(...)
289 numerical_feats=None
290 ):
291 r"""
292 class_weights (:obj:torch.FloatTensor
of shape :obj:(tabular_config.num_labels,)
,optional
, defaults to :obj:None
):
293 Class weights to be used for cross entropy loss function for classification task
(...)
311 combining module's output
312 """
--> 314 distilbert_output = self.distilbert(
315 input_ids,
316 attention_mask=attention_mask,
317 head_mask=head_mask,
318 inputs_embeds=inputs_embeds,
319 output_attentions=output_attentions,
320 output_hidden_states=output_hidden_states,
321 )
322 hidden_state = distilbert_output[0] # (bs, seq_len, dim)
323 pooled_output = hidden_state[:, 0] # (bs, dim)
File ~/cat_multimodel_env/lib/python3.8/site-packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don't have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
File ~/cat_multimodel_env/lib/python3.8/site-packages/transformers/modeling_distilbert.py:488, in DistilBertModel.forward(self, input_ids, attention_mask, head_mask, inputs_embeds, output_attentions, output_hidden_states, return_dict)
485 head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers)
487 if inputs_embeds is None:
--> 488 inputs_embeds = self.embeddings(input_ids) # (bs, seq_length, dim)
489 return self.transformer(
490 x=inputs_embeds,
491 attn_mask=attention_mask,
(...)
495 return_dict=return_dict,
496 )
File ~/cat_multimodel_env/lib/python3.8/site-packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don't have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
File ~/cat_multimodel_env/lib/python3.8/site-packages/transformers/modeling_distilbert.py:108, in Embeddings.forward(self, input_ids)
96 def forward(self, input_ids):
97 """
98 Parameters
99 ----------
(...)
106 The embedded tokens (plus position embeddings, no token_type embeddings)
107 """
--> 108 seq_length = input_ids.size(1)
109 position_ids = torch.arange(seq_length, dtype=torch.long, device=input_ids.device) # (max_seq_length)
110 position_ids = position_ids.unsqueeze(0).expand_as(input_ids) # (bs, max_seq_length)
IndexError: Dimension out of range (expected to be in range of [-1, 0], but got 1)
To resolve this i tried to reshape the input with the following code.
pre['input_ids']=pre['input_ids'].reshape(-1,len(pre['input_ids'])).to("cuda:0")
pre['attention_mask']=pre['attention_mask'].reshape(-1,len(pre['attention_mask'])).to("cuda:0")
pre['numerical_feats']=pre['numerical_feats'].reshape(-1,len(pre['numerical_feats'])).to("cuda:0")
pre['cat_feats']=pre['cat_feats'].reshape(-1,len(pre['cat_feats'])).to("cuda:0")
Then I got this error
RuntimeError Traceback (most recent call last)
Input In [94], in <cell line: 1>()
----> 1 model(input_ids=pre['input_ids'],attention_mask=pre['attention_mask'],cat_feats=pre['cat_feats'],numerical_feats=pre['numerical_feats'])
File ~/cat_multimodel_env/lib/python3.8/site-packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don't have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
File ~/cat_multimodel_env/lib/python3.8/site-packages/multimodal_transformers/model/tabular_transformers.py:327, in DistilBertWithTabular.forward(self, input_ids, attention_mask, head_mask, inputs_embeds, labels, output_attentions, output_hidden_states, class_weights, cat_feats, numerical_feats)
325 pooled_output = nn.ReLU()(pooled_output) # (bs, dim)
326 text_feats = self.dropout(pooled_output)
--> 327 combined_feats = self.tabular_combiner(text_feats,
328 cat_feats,
329 numerical_feats)
330 loss, logits, classifier_layer_outputs = hf_loss_func(combined_feats,
331 self.tabular_classifier,
332 labels,
333 self.num_labels,
334 class_weights)
335 return loss, logits, classifier_layer_outputs
File ~/cat_multimodel_env/lib/python3.8/site-packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don't have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
File ~/cat_multimodel_env/lib/python3.8/site-packages/multimodal_transformers/model/tabular_combiner.py:370, in TabularFeatCombiner.forward(self, text_feats, cat_feats, numerical_feats)
368 elif self.combine_feat_method == 'mlp_on_concatenated_cat_and_numerical_feats_then_concat':
369 tabular_feats = torch.cat((cat_feats, numerical_feats), dim=1)
--> 370 tabular_feats = self.cat_and_numerical_mlp(tabular_feats)
371 combined_feats = torch.cat((text_feats, tabular_feats), dim=1)
372 elif self.combine_feat_method == 'individual_mlps_on_cat_and_numerical_feats_then_concat':
File ~/cat_multimodel_env/lib/python3.8/site-packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don't have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
File ~/cat_multimodel_env/lib/python3.8/site-packages/multimodal_transformers/model/layer_utils.py:52, in MLP.forward(self, x)
50 input = layer_inputs[-1]
51 if layer == self.layers[-1]:
---> 52 layer_inputs.append(layer(input))
53 else:
54 if self.bn:
File ~/cat_multimodel_env/lib/python3.8/site-packages/torch/nn/modules/module.py:1110, in Module._call_impl(self, *input, **kwargs)
1106 # If we don't have any hooks, we want to skip the rest of the logic in
1107 # this function, and just call forward.
1108 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1109 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1110 return forward_call(*input, **kwargs)
1111 # Do not call functions when jit is used
1112 full_backward_hooks, non_full_backward_hooks = [], []
File ~/cat_multimodel_env/lib/python3.8/site-packages/torch/nn/modules/linear.py:103, in Linear.forward(self, input)
102 def forward(self, input: Tensor) -> Tensor:
--> 103 return F.linear(input, self.weight, self.bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x19 and 4x1).
Kindly help me with this issue.
Hi @sachin16495 could you share what your input shape is? Happy to help you debug this. Closing this issue since it's been a while but feel free to re-open it!