hidden_states: float Tensor in shape [bsz, ..., seq_len, hidden_size], the hidden-states of the last layer. cls_index: [optional] position of the classification token if summary_type == 'cls_index', shape (bsz,) or more generally (bsz, ...) where ... are optional leading
(self, hidden_states, cls_index=None)
| 1112 | self.last_dropout = nn.Dropout(config.summary_last_dropout) |
| 1113 | |
| 1114 | def forward(self, hidden_states, cls_index=None): |
| 1115 | """ hidden_states: float Tensor in shape [bsz, ..., seq_len, hidden_size], the hidden-states of the last layer. |
| 1116 | cls_index: [optional] position of the classification token if summary_type == 'cls_index', |
| 1117 | shape (bsz,) or more generally (bsz, ...) where ... are optional leading dimensions of hidden_states. |
| 1118 | if summary_type == 'cls_index' and cls_index is None: |
| 1119 | we take the last token of the sequence as classification token |
| 1120 | """ |
| 1121 | if self.summary_type == "last": |
| 1122 | output = hidden_states[:, -1] |
| 1123 | elif self.summary_type == "first": |
| 1124 | output = hidden_states[:, 0] |
| 1125 | elif self.summary_type == "mean": |
| 1126 | output = hidden_states.mean(dim=1) |
| 1127 | elif self.summary_type == "cls_index": |
| 1128 | if cls_index is None: |
| 1129 | cls_index = torch.full_like(hidden_states[..., :1, :], hidden_states.shape[-2] - 1, dtype=torch.long,) |
| 1130 | else: |
| 1131 | cls_index = cls_index.unsqueeze(-1).unsqueeze(-1) |
| 1132 | cls_index = cls_index.expand((-1,) * (cls_index.dim() - 1) + (hidden_states.size(-1),)) |
| 1133 | # shape of cls_index: (bsz, XX, 1, hidden_size) where XX are optional leading dim of hidden_states |
| 1134 | output = hidden_states.gather(-2, cls_index).squeeze(-2) # shape (bsz, XX, hidden_size) |
| 1135 | elif self.summary_type == "attn": |
| 1136 | raise NotImplementedError |
| 1137 | |
| 1138 | output = self.first_dropout(output) |
| 1139 | output = self.summary(output) |
| 1140 | output = self.activation(output) |
| 1141 | output = self.last_dropout(output) |
| 1142 | |
| 1143 | return output |
| 1144 | |
| 1145 | |
| 1146 | def prune_linear_layer(layer, index, dim=0): |
nothing calls this directly
no outgoing calls
no test coverage detected