def swith_rng_state(rng_state): orig_cuda_rng_state = paddle.get_cuda_rng_state() paddle.set_cuda_rng_state(rng_state) try: yield finally: paddle.set_cuda_rng_state(orig_cuda_rng_state)
def rng_state(self, name=MODEL_PARALLEL_RNG): if name not in self.states_: raise ValueError('state {} does not exist'.format(name)) orig_cuda_rng_state = paddle.get_cuda_rng_state() paddle.set_cuda_rng_state(self.states_[name]) try: yield finally: self.states_[name] = paddle.get_cuda_rng_state() paddle.set_cuda_rng_state(orig_cuda_rng_state)
def add(self, name, seed): if seed in self.seeds_: raise ValueError('seed {} already exists'.format(seed)) self.seeds_.add(seed) if name in self.states_: raise ValueError('state {} already exists'.format(name)) orig_rng_state = paddle.get_cuda_rng_state() paddle.seed(seed) self.states_[name] = paddle.get_cuda_rng_state() paddle.set_cuda_rng_state(orig_rng_state)
def _swith_rng_state_tracker(rng_state, tracker): orig_cuda_rng_state = paddle.get_cuda_rng_state() orig_cuda_rng_tracker = get_rng_state_tracker().get_states_tracker() paddle.set_cuda_rng_state(rng_state) get_rng_state_tracker().set_states_tracker(tracker) try: yield finally: paddle.set_cuda_rng_state(orig_cuda_rng_state) get_rng_state_tracker().set_states_tracker(orig_cuda_rng_tracker)
def test_gen_dropout_dygraph(self): gen = paddle.seed(12343) fluid.enable_dygraph() gen.manual_seed(111111111) st = paddle.get_cuda_rng_state() x = fluid.layers.uniform_random([2, 10], dtype="float32", min=0.0, max=1.0) x_again = fluid.layers.uniform_random([2, 10], dtype="float32", min=0.0, max=1.0) x_third = fluid.layers.uniform_random([2, 10], dtype="float32", min=0.0, max=1.0) print("x: {}".format(x.numpy())) print("x_again: {}".format(x_again.numpy())) x = x + x_again + x_third y = fluid.layers.dropout(x, 0.5) paddle.set_cuda_rng_state(st) x1 = fluid.layers.uniform_random([2, 10], dtype="float32", min=0.0, max=1.0) x1_again = fluid.layers.uniform_random([2, 10], dtype="float32", min=0.0, max=1.0) x1_third = fluid.layers.uniform_random([2, 10], dtype="float32", min=0.0, max=1.0) x1 = x1 + x1_again + x1_third y1 = fluid.layers.dropout(x1, 0.5) y_np = y.numpy() y1_np = y1.numpy() if core.is_compiled_with_cuda(): print(">>>>>>> dropout dygraph >>>>>>>") self.assertTrue(np.allclose(y_np, y1_np))
def test_generator_gaussian_random_dygraph(self): """Test Generator seed.""" fluid.enable_dygraph() paddle.seed(12312321111) x = fluid.layers.gaussian_random([120], dtype="float32") st1 = paddle.get_cuda_rng_state() x1 = fluid.layers.gaussian_random([120], dtype="float32") paddle.set_cuda_rng_state(st1) x2 = fluid.layers.gaussian_random([120], dtype="float32") paddle.seed(12312321111) x3 = fluid.layers.gaussian_random([120], dtype="float32") x_np = x.numpy() x1_np = x1.numpy() x2_np = x2.numpy() x3_np = x3.numpy() if core.is_compiled_with_cuda(): print(">>>>>>> gaussian random dygraph >>>>>>>") self.assertTrue(np.allclose(x1_np, x2_np)) self.assertTrue(np.allclose(x_np, x3_np))