Fork the cuda rng state, perform operations, and exit with the original state.
(self, name=_MODEL_PARALLEL_RNG_TRACKER_NAME)
| 188 | |
| 189 | @contextlib.contextmanager |
| 190 | def fork(self, name=_MODEL_PARALLEL_RNG_TRACKER_NAME): |
| 191 | """Fork the cuda rng state, perform operations, and exit with |
| 192 | the original state.""" |
| 193 | # Check if we have added the state |
| 194 | if name not in self.states_: |
| 195 | print(name, self.states_) |
| 196 | raise Exception("cuda rng state {} is not added".format(name)) |
| 197 | # Store current rng state. |
| 198 | orig_cuda_rng_state = torch.cuda.get_rng_state() |
| 199 | # Set rng state to the desired one |
| 200 | _set_cuda_rng_state(self.states_[name]) |
| 201 | # Do the stuff we wanted to do. |
| 202 | try: |
| 203 | yield |
| 204 | finally: |
| 205 | # Update the current rng state for later use. |
| 206 | self.states_[name] = torch.cuda.get_rng_state() |
| 207 | # And set the state to the original state we started with. |
| 208 | _set_cuda_rng_state(orig_cuda_rng_state) |
| 209 | |
| 210 | |
| 211 | # RNG tracker object. |
no test coverage detected