| 204 | |
| 205 | |
| 206 | class ThroughputTimer: |
| 207 | |
| 208 | def __init__(self, config, batch_size, start_step=2, steps_per_output=None, monitor_memory=False, logging_fn=None): |
| 209 | from deepspeed.utils import logger |
| 210 | self.config = config |
| 211 | self.start_time = 0 |
| 212 | self.end_time = 0 |
| 213 | self.started = False |
| 214 | self.batch_size = 1 if batch_size is None else batch_size |
| 215 | self.start_step = start_step |
| 216 | self.epoch_count = 0 |
| 217 | self.micro_step_count = 0 |
| 218 | self.global_step_count = 0 |
| 219 | self.total_elapsed_time = 0 |
| 220 | self.step_elapsed_time = 0 |
| 221 | self.steps_per_output = steps_per_output |
| 222 | self.monitor_memory = monitor_memory |
| 223 | self.logging = logging_fn |
| 224 | if self.logging is None: |
| 225 | self.logging = logger.info |
| 226 | self.initialized = False |
| 227 | |
| 228 | if self.monitor_memory and importlib.util.find_spec("psutil") is None: |
| 229 | raise ImportError("Unable to import 'psutils', please install package") |
| 230 | |
| 231 | def update_epoch_count(self): |
| 232 | self.epoch_count += 1 |
| 233 | self.micro_step_count = 0 |
| 234 | |
| 235 | def _init_timer(self): |
| 236 | self.initialized = True |
| 237 | |
| 238 | def start(self): |
| 239 | if not self.config.enabled: |
| 240 | return |
| 241 | self._init_timer() |
| 242 | self.started = True |
| 243 | if self.global_step_count >= self.start_step: |
| 244 | if self.config.synchronized: |
| 245 | get_accelerator().synchronize() |
| 246 | self.start_time = time.time() |
| 247 | |
| 248 | def _is_report_boundary(self): |
| 249 | if self.steps_per_output is None: |
| 250 | return False |
| 251 | return self.global_step_count % self.steps_per_output == 0 |
| 252 | |
| 253 | def stop(self, global_step=False, report_speed=True): |
| 254 | if not self.config.enabled or not self.started: |
| 255 | return |
| 256 | self.started = False |
| 257 | self.micro_step_count += 1 |
| 258 | if global_step: |
| 259 | self.global_step_count += 1 |
| 260 | |
| 261 | if self.start_time > 0: |
| 262 | if self.config.synchronized: |
| 263 | get_accelerator().synchronize() |