MCPcopy Create free account
hub / github.com/pytorch/pytorch / forward

Method forward

scripts/release_notes/classifier.py:96–151  ·  view source on GitHub ↗
(self, input_batch: CommitClassifierInputs)

Source from the content-addressed store, hash-verified

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,
125 device=device,
126 )
127 offsets = [0]
128 offsets.extend(len(files) for files in batch_file_indexes[:-1])
129 offsets = torch.tensor(offsets, dtype=torch.long, device=device)
130 offsets = offsets.cumsum(dim=0)
131
132 files_embed = self.file_embedding_bag(flat_indexes, offsets)
133 files_embed = self.dense_files(files_embed)
134 files_embed = self.activation_fn(files_embed)
135 files_embed = self.dropout(files_embed)
136 files_embed = self.out_proj_files(files_embed)
137
138 # Add author embedding
139 authors: List[str] = input_batch.author
140 author_ids = [
141 self.author_map.get(author, self.author_map[UNKNOWN_TOKEN])
142 for author in authors
143 ]
144 author_ids = torch.tensor(author_ids).to(device)
145 author_embed = self.embedding_table(author_ids)
146 author_embed = self.dense_author(author_embed)
147 author_embed = self.activation_fn(author_embed)
148 author_embed = self.dropout(author_embed)
149 author_embed = self.out_proj_author(author_embed)
150
151 return title_embed + files_embed + author_embed
152
153 def convert_index_to_category_name(self, most_likely_index):

Callers 1

Calls 12

truncate_fileFunction · 0.85
PathClass · 0.85
listFunction · 0.85
cumsumMethod · 0.80
to_tensorFunction · 0.50
toMethod · 0.45
transformMethod · 0.45
splitMethod · 0.45
appendMethod · 0.45
getMethod · 0.45
tensorMethod · 0.45
extendMethod · 0.45

Tested by

no test coverage detected