| 65 | |
| 66 | |
| 67 | class CommitClassifier(nn.Module): |
| 68 | def __init__( |
| 69 | self, |
| 70 | encoder_base: torchtext.models.XLMR_BASE_ENCODER, |
| 71 | author_map: Dict[str, int], |
| 72 | file_map: [str, int], |
| 73 | config: CategoryConfig, |
| 74 | ): |
| 75 | super().__init__() |
| 76 | self.encoder = encoder_base.get_model().requires_grad_(False) |
| 77 | self.transform = encoder_base.transform() |
| 78 | self.author_map = author_map |
| 79 | self.file_map = file_map |
| 80 | self.categories = config.categories |
| 81 | self.num_authors = len(author_map) |
| 82 | self.num_files = len(file_map) |
| 83 | self.embedding_table = nn.Embedding(self.num_authors, config.embedding_dim) |
| 84 | self.file_embedding_bag = nn.EmbeddingBag( |
| 85 | self.num_files, config.file_embedding_dim, mode="sum" |
| 86 | ) |
| 87 | self.dense_title = nn.Linear(config.input_dim, config.inner_dim) |
| 88 | self.dense_files = nn.Linear(config.file_embedding_dim, config.inner_dim) |
| 89 | self.dense_author = nn.Linear(config.embedding_dim, config.inner_dim) |
| 90 | self.dropout = nn.Dropout(config.dropout) |
| 91 | self.out_proj_title = nn.Linear(config.inner_dim, len(self.categories)) |
| 92 | self.out_proj_files = nn.Linear(config.inner_dim, len(self.categories)) |
| 93 | self.out_proj_author = nn.Linear(config.inner_dim, len(self.categories)) |
| 94 | self.activation_fn = config.activation() |
| 95 | |
| 96 | def forward(self, input_batch: CommitClassifierInputs): |
| 97 | # Encode input title |
| 98 | title: List[str] = input_batch.title |
| 99 | model_input = to_tensor(self.transform(title), padding_value=1).to(device) |
| 100 | title_features = self.encoder(model_input) |
| 101 | title_embed = title_features[:, 0, :] |
| 102 | title_embed = self.dropout(title_embed) |
| 103 | title_embed = self.dense_title(title_embed) |
| 104 | title_embed = self.activation_fn(title_embed) |
| 105 | title_embed = self.dropout(title_embed) |
| 106 | title_embed = self.out_proj_title(title_embed) |
| 107 | |
| 108 | files: list[str] = input_batch.files |
| 109 | batch_file_indexes = [] |
| 110 | for file in files: |
| 111 | paths = [ |
| 112 | truncate_file(Path(file_part), MAX_LEN_FILE) |
| 113 | for file_part in file.split(" ") |
| 114 | ] |
| 115 | batch_file_indexes.append( |
| 116 | [ |
| 117 | self.file_map.get(file, self.file_map[UNKNOWN_TOKEN]) |
| 118 | for file in paths |
| 119 | ] |
| 120 | ) |
| 121 | |
| 122 | flat_indexes = torch.tensor( |
| 123 | list(chain.from_iterable(batch_file_indexes)), |
| 124 | dtype=torch.long, |