r""" Compute a single vector summary of a sequence hidden states according to various possibilities: Args of the config class: summary_type: - 'last' => [default] take the last token hidden state (like XLNet) - 'first' => take the first token hidde
| 1067 | |
| 1068 | |
| 1069 | class SequenceSummary(nn.Module): |
| 1070 | r""" Compute a single vector summary of a sequence hidden states according to various possibilities: |
| 1071 | Args of the config class: |
| 1072 | summary_type: |
| 1073 | - 'last' => [default] take the last token hidden state (like XLNet) |
| 1074 | - 'first' => take the first token hidden state (like Bert) |
| 1075 | - 'mean' => take the mean of all tokens hidden states |
| 1076 | - 'cls_index' => supply a Tensor of classification token position (GPT/GPT-2) |
| 1077 | - 'attn' => Not implemented now, use multi-head attention |
| 1078 | summary_use_proj: Add a projection after the vector extraction |
| 1079 | summary_proj_to_labels: If True, the projection outputs to config.num_labels classes (otherwise to hidden_size). Default: False. |
| 1080 | summary_activation: 'tanh' or another string => add an activation to the output, Other => no activation. Default |
| 1081 | summary_first_dropout: Add a dropout before the projection and activation |
| 1082 | summary_last_dropout: Add a dropout after the projection and activation |
| 1083 | """ |
| 1084 | |
| 1085 | def __init__(self, config: PretrainedConfig): |
| 1086 | super().__init__() |
| 1087 | |
| 1088 | self.summary_type = getattr(config, "summary_type", "last") |
| 1089 | if self.summary_type == "attn": |
| 1090 | # We should use a standard multi-head attention module with absolute positional embedding for that. |
| 1091 | # Cf. https://github.com/zihangdai/xlnet/blob/master/modeling.py#L253-L276 |
| 1092 | # We can probably just use the multi-head attention module of PyTorch >=1.1.0 |
| 1093 | raise NotImplementedError |
| 1094 | |
| 1095 | self.summary = Identity() |
| 1096 | if hasattr(config, "summary_use_proj") and config.summary_use_proj: |
| 1097 | if hasattr(config, "summary_proj_to_labels") and config.summary_proj_to_labels and config.num_labels > 0: |
| 1098 | num_classes = config.num_labels |
| 1099 | else: |
| 1100 | num_classes = config.hidden_size |
| 1101 | self.summary = nn.Linear(config.hidden_size, num_classes) |
| 1102 | |
| 1103 | activation_string = getattr(config, "summary_activation", None) |
| 1104 | self.activation: Callable = (get_activation(activation_string) if activation_string else Identity()) |
| 1105 | |
| 1106 | self.first_dropout = Identity() |
| 1107 | if hasattr(config, "summary_first_dropout") and config.summary_first_dropout > 0: |
| 1108 | self.first_dropout = nn.Dropout(config.summary_first_dropout) |
| 1109 | |
| 1110 | self.last_dropout = Identity() |
| 1111 | if hasattr(config, "summary_last_dropout") and config.summary_last_dropout > 0: |
| 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) |
nothing calls this directly
no outgoing calls
no test coverage detected