| 204 | |
| 205 | |
| 206 | class ThroughputTimer: |
| 207 | |
| 208 | def __init__(self, |
| 209 | config, |
| 210 | batch_size, |
| 211 | start_step=2, |
| 212 | steps_per_output=None, |
| 213 | monitor_memory=False, |
| 214 | logging_fn=None, |
| 215 | sync_current_stream=False): |
| 216 | from deepspeed.utils import logger |
| 217 | self.config = config |
| 218 | self.start_time = 0 |
| 219 | self.end_time = 0 |
| 220 | self.started = False |
| 221 | self.batch_size = 1 if batch_size is None else batch_size |
| 222 | self.start_step = start_step |
| 223 | self.epoch_count = 0 |
| 224 | self.micro_step_count = 0 |
| 225 | self.global_step_count = 0 |
| 226 | self.total_elapsed_time = 0 |
| 227 | self.step_elapsed_time = 0 |
| 228 | self.steps_per_output = steps_per_output |
| 229 | self.monitor_memory = monitor_memory |
| 230 | self.logging = logging_fn |
| 231 | if self.logging is None: |
| 232 | self.logging = logger.info |
| 233 | self.initialized = False |
| 234 | # Reflow avoids a device-wide timer sync: waiting for its H2D side stream would delay |
| 235 | # the next GPU compute launch and reduce overlap with the CPU optimizer workers. |
| 236 | # Other runs keep the original full-device synchronization. |
| 237 | self.sync_current_stream = sync_current_stream |
| 238 | |
| 239 | if self.monitor_memory and importlib.util.find_spec("psutil") is None: |
| 240 | raise ImportError("Unable to import 'psutils', please install package") |
| 241 | |
| 242 | def update_epoch_count(self): |
| 243 | self.epoch_count += 1 |
| 244 | self.micro_step_count = 0 |
| 245 | |
| 246 | def _init_timer(self): |
| 247 | self.initialized = True |
| 248 | |
| 249 | def _synchronize(self): |
| 250 | if self.sync_current_stream: |
| 251 | get_accelerator().current_stream().synchronize() |
| 252 | else: |
| 253 | get_accelerator().synchronize() |
| 254 | |
| 255 | def start(self): |
| 256 | if not self.config.enabled: |
| 257 | return |
| 258 | self._init_timer() |
| 259 | self.started = True |
| 260 | if self.global_step_count >= self.start_step: |
| 261 | if self.config.synchronized: |
| 262 | self._synchronize() |
| 263 | self.start_time = time.time() |