Fork the cuda rng state, perform operations, and exit with the original state.
(self, name=_MODEL_PARALLEL_RNG_TRACKER_NAME)
| 167 | |
| 168 | @contextlib.contextmanager |
| 169 | def fork(self, name=_MODEL_PARALLEL_RNG_TRACKER_NAME): |
| 170 | """Fork the cuda rng state, perform operations, and exit with |
| 171 | the original state.""" |
| 172 | # Check if we have added the state |
| 173 | if name not in self.states_: |
| 174 | raise Exception('cuda rng state {} is not added'.format(name)) |
| 175 | # Store current rng state. |
| 176 | orig_cuda_rng_state = torch.cuda.get_rng_state() |
| 177 | # Set rng state to the desired one |
| 178 | _set_cuda_rng_state(self.states_[name]) |
| 179 | # Do the stuff we wanted to do. |
| 180 | try: |
| 181 | yield |
| 182 | finally: |
| 183 | # Update the current rng state for later use. |
| 184 | self.states_[name] = torch.cuda.get_rng_state() |
| 185 | # And set the state to the original state we started with. |
| 186 | _set_cuda_rng_state(orig_cuda_rng_state) |
| 187 | |
| 188 | |
| 189 | # RNG tracker object. |