| 1149 | |
| 1150 | |
| 1151 | class EarlyStopConfig: |
| 1152 | def __init__( |
| 1153 | self, |
| 1154 | args, |
| 1155 | ): |
| 1156 | """ |
| 1157 | Early Stop Configuration class. |
| 1158 | |
| 1159 | Attributes: |
| 1160 | window_size: size of the window |
| 1161 | threshold: trigger early stop when the ratio of probs exceeds the threshold |
| 1162 | """ |
| 1163 | """enable to use early stop""" |
| 1164 | self.enable_early_stop: bool = False |
| 1165 | """strategy for early stop, the strategy lists are ['repetition']""" |
| 1166 | self.strategy: str = "repetition" |
| 1167 | """ the maximum length of verify window for early stop """ |
| 1168 | self.window_size: int = 3000 |
| 1169 | """ the probs threshold for early stop """ |
| 1170 | self.threshold: float = 0.99 |
| 1171 | |
| 1172 | if args is not None: |
| 1173 | for key, value in args.items(): |
| 1174 | if hasattr(self, key): |
| 1175 | setattr(self, key, value) |
| 1176 | self.check_legality_parameters() |
| 1177 | |
| 1178 | def to_json_string(self): |
| 1179 | """ |
| 1180 | Convert early_stop_config to json string. |
| 1181 | """ |
| 1182 | return json.dumps({key: value for key, value in self.__dict__.items()}) |
| 1183 | |
| 1184 | def __str__(self) -> str: |
| 1185 | return self.to_json_string() |
| 1186 | |
| 1187 | def check_legality_parameters( |
| 1188 | self, |
| 1189 | ) -> None: |
| 1190 | """Check the legality of parameters passed in from the command line""" |
| 1191 | if self.enable_early_stop is not None: |
| 1192 | assert isinstance( |
| 1193 | self.enable_early_stop, bool |
| 1194 | ), "In early stop config, type of enable_early_stop must is bool." |
| 1195 | if self.window_size is not None: |
| 1196 | assert isinstance(self.window_size, int), "In early stop config, type of window_size must be int." |
| 1197 | assert self.window_size > 0, "window_size must large than 0" |
| 1198 | if self.threshold is not None: |
| 1199 | assert isinstance(self.threshold, float), "In early stop config, type of threshold must be float." |
| 1200 | assert self.threshold >= 0 and self.threshold <= 1, "threshold must between 0 and 1" |
| 1201 | |
| 1202 | def update_enable_early_stop(self, argument: bool): |
| 1203 | """ |
| 1204 | Unified user specifies the enable_early_stop parameter through two methods, |
| 1205 | '--enable-early-stop' and '--early-stop-config' |
| 1206 | """ |
| 1207 | if self.enable_early_stop is None: |
| 1208 | # User only set '--enable-early-stop' |
no outgoing calls