Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update vilbert.py #1065

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions mmf/models/vilbert.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import torch.nn.functional as F
from mmf.common.registry import registry
from mmf.models import BaseModel
from mmf.models.transformers.heads.itm import ITM
from mmf.modules.hf_layers import replace_with_jit
from mmf.utils.configuration import get_mmf_cache_dir
from mmf.utils.modeling import get_optimizer_parameters_for_bert
Expand Down Expand Up @@ -1061,6 +1062,7 @@ def __init__(self, config):
self.visual_target = config.visual_target
self.num_negative = config.num_negative
self.loss_fct = CrossEntropyLoss(ignore_index=-1)


if self.visual_target == 0:
self.vis_criterion = nn.KLDivLoss(reduction="none")
Expand Down Expand Up @@ -1099,6 +1101,8 @@ def forward(
image_label: Optional[Tensor] = None,
image_target: Optional[Tensor] = None,
output_all_attention_masks: bool = False,
itm_loss: bool = False,
next_sentence_label: Optional[Dict[str, Dict[str, torch.Tensor]]] = None,
) -> Dict[str, Tensor]:
masked_img_loss: Optional[Tensor] = None
(
Expand Down Expand Up @@ -1226,6 +1230,14 @@ def forward(
prediction_scores_t.view(-1, self.vocab_size), masked_lm_labels.view(-1)
)
output["masked_lm_loss"] = masked_lm_loss.unsqueeze(0)

if itm_loss is not False:
itm_head = ITM({"type": "itm", "hidden_size": self.vocab_size})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be initialized in the init not in the forward pass.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! corrected the initialization thing. I checked the snippet I added separately in similar manner to "ITM head test" but was not able to test the ViLBERTForPretraining (As after loading yaml file its throwing 'dict' object has no attribute 'bert_model_name') even without making changes in original code.

seq_output = torch.cat(sequence_output_t, sequence_output_v)
multimodal_alignment_loss = itm_head(seq_output, processed_sample_list = next_sentence_label)
if multimodal_alignment_loss is not None:
output["itm_loss"] = multimodal_alignment_loss["losses"]["itm_loss"]

# next_sentence_loss = self.loss_fct(
# seq_relationship_score.view(-1, 2), next_sentence_label.view(-1)
# )
Expand Down