@@ -130,11 +130,24 @@ def forward(self, encoding: BatchEncoding) -> CrossEncoderOutput:
130130 CrossEncoderOutput: Output of the model.
131131 """
132132 num_docs = encoding .pop ("num_docs" , None )
133-
134- self .get_extended_attention_mask = partial (type (self ).get_extended_attention_mask , self , num_docs = num_docs )
133+ if num_docs is not None :
134+ attention_mask = encoding .get ("attention_mask" )
135+ if attention_mask is not None :
136+ device = attention_mask .device
137+ eye = (1 - torch .eye (self .config .depth , device = device )).long ()
138+ if not self .config .sample_missing_docs :
139+ eye = eye [:, : max (num_docs )]
140+ other_doc_attention_mask = torch .cat ([eye [:n ] for n in num_docs ])
141+ attention_mask = torch .cat (
142+ [attention_mask , other_doc_attention_mask .to (attention_mask )],
143+ dim = - 1 ,
144+ )
145+ encoding ["attention_mask" ] = attention_mask
146+
147+ self .get_extended_attention_mask = partial (self .get_extended_attention_mask , num_docs = num_docs )
135148 for name , module in self .named_modules ():
136149 if name .endswith (self .self_attention_pattern ):
137- module .forward = partial (type ( self ) .attention_forward , self , module , num_docs = num_docs )
150+ module .forward = partial (self .attention_forward , self , module , num_docs = num_docs )
138151 return super ().forward (encoding )
139152
140153 @staticmethod
0 commit comments