src/transformers/models/xmod/modeling_xmod.py/XmodOutput/lang_adapter
class XmodOutput:    def lang_adapter(self, lang_ids: torch.Tensor, hidden_states: torch.Tensor):
        # Process subsequent samples with the same lang_id in parallel
        lang_ids, lang_lengths = torch.unique_consecutive(lang_ids, return_counts=True)

        if not self.ln_before_adapter:
            residual = hidden_states

        if self.adapter_layer_norm is not None:
            hidden_states = self.adapter_layer_norm(hidden_states)
        elif self.adapter_reuse_layer_norm:
            hidden_states = self.LayerNorm(hidden_states)

        if self.ln_before_adapter:
            residual = hidden_states

        split_hidden_states = torch.split(hidden_states, lang_lengths.tolist(), 0)
        lang_wise_outputs = []
        for i, (lang_id, split_hidden_state) in enumerate(zip(lang_ids, split_hidden_states)):
            lang = list(self.adapter_modules.keys())[int(lang_id.item())]
            lang_wise_outputs.append(self.adapter_modules[lang](split_hidden_state))
        hidden_states = torch.cat(lang_wise_outputs, 0)

        hidden_states = self.dropout(hidden_states)
        hidden_states += residual
        return hidden_states