Skip to content

Commit 766799e

Browse files
committed
fix device id and add get/set_cuda_rng_state api
1 parent 158c5fc commit 766799e

File tree

8 files changed

+39
-36
lines changed

8 files changed

+39
-36
lines changed

paddle/fluid/framework/generator.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ const std::shared_ptr<Generator>& GetDefaultCUDAGenerator(int64_t device_id) {
4343
cuda_device_flags.resize(num_cuda_devices);
4444
default_cuda_generators.resize(num_cuda_devices);
4545
});
46-
platform::Place place;
47-
if (device_id == -1)
46+
if (device_id == -1) {
47+
platform::Place place;
4848
device_id = BOOST_GET_CONST(platform::CUDAPlace, place).GetDeviceId();
49+
}
50+
4951
std::call_once(cuda_device_flags[device_id], [device_id]() {
5052
default_cuda_generators[device_id] =
5153
std::make_shared<Generator>(GetRandomSeed(), device_id);

paddle/fluid/operators/dropout_op.cu

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ class GPUDropoutKernel : public framework::OpKernel<T> {
186186
context.Attr<bool>("fix_seed") ? context.Attr<int>("seed") : rnd();
187187
}
188188

189-
auto gen_cuda = framework::GetDefaultCUDAGenerator(-1);
189+
int device_id = BOOST_GET_CONST(platform::CUDAPlace, context.GetPlace())
190+
.GetDeviceId();
191+
auto gen_cuda = framework::GetDefaultCUDAGenerator(device_id);
190192
if (gen_cuda->GetIsInitPy() && (!context.Attr<bool>("fix_seed"))) {
191193
auto seed_offset = gen_cuda->IncrementOffset(1);
192194
RandomGeneratorWithGenerator<T, uint8_t><<<grid, threads, 0, stream>>>(

paddle/fluid/operators/gaussian_random_op.cu

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ class GPUGaussianRandomKernel : public framework::OpKernel<T> {
6464
T* data = tensor->mutable_data<T>(context.GetPlace());
6565

6666
int64_t size = tensor->numel();
67-
auto gen_cuda = framework::GetDefaultCUDAGenerator(-1);
67+
68+
int device_id =
69+
BOOST_GET_CONST(platform::CUDAPlace, context.GetPlace()).GetDeviceId();
70+
auto gen_cuda = framework::GetDefaultCUDAGenerator(device_id);
6871

6972
if (gen_cuda->GetIsInitPy() && seed_flag) {
7073
auto seed_offset = gen_cuda->IncrementOffset(1);
@@ -102,7 +105,10 @@ class GPUGaussianRandomBatchSizeLikeKernel : public framework::OpKernel<T> {
102105
T std = static_cast<T>(context.Attr<float>("std"));
103106
thrust::counting_iterator<unsigned int> index_sequence_begin(0);
104107
int64_t size = tensor->numel();
105-
auto gen_cuda = framework::GetDefaultCUDAGenerator(-1);
108+
109+
int device_id =
110+
BOOST_GET_CONST(platform::CUDAPlace, context.GetPlace()).GetDeviceId();
111+
auto gen_cuda = framework::GetDefaultCUDAGenerator(device_id);
106112

107113
if (gen_cuda->GetIsInitPy() && seed_flag) {
108114
auto seed_offset = gen_cuda->IncrementOffset(1);

paddle/fluid/operators/truncated_gaussian_random_op.cu

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ class GPUTruncatedGaussianRandomKernel : public framework::OpKernel<T> {
9797
thrust::counting_iterator<unsigned int> index_sequence_begin(0);
9898
int64_t size = tensor->numel();
9999

100-
auto gen_cuda = framework::GetDefaultCUDAGenerator(-1);
100+
int device_id =
101+
BOOST_GET_CONST(platform::CUDAPlace, context.GetPlace()).GetDeviceId();
102+
auto gen_cuda = framework::GetDefaultCUDAGenerator(device_id);
103+
101104
if (gen_cuda->GetIsInitPy() && seed_flag) {
102105
auto seed_offset = gen_cuda->IncrementOffset(1);
103106
int offset_step = 100;

paddle/fluid/operators/uniform_random_op.cu

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ class GPUUniformRandomKernel : public framework::OpKernel<T> {
138138
T diag_val = static_cast<T>(context.Attr<float>("diag_val"));
139139
thrust::counting_iterator<unsigned int> index_sequence_begin(0);
140140
int64_t size = tensor->numel();
141-
int64_t device_id = -1;
141+
int device_id =
142+
BOOST_GET_CONST(platform::CUDAPlace, context.GetPlace()).GetDeviceId();
142143
auto gen_cuda = framework::GetDefaultCUDAGenerator(device_id);
143144
if (gen_cuda->GetIsInitPy() && seed_flag) {
144145
auto seed_offset = gen_cuda->IncrementOffset(1);

python/paddle/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@
217217
from .tensor.search import nonzero #DEFINE_ALIAS
218218
from .tensor.search import sort #DEFINE_ALIAS
219219
from .framework.random import manual_seed #DEFINE_ALIAS
220-
from .framework.random import get_cuda_state #DEFINE_ALIAS
221-
from .framework.random import set_cuda_state #DEFINE_ALIAS
220+
from .framework.random import get_cuda_rng_state #DEFINE_ALIAS
221+
from .framework.random import set_cuda_rng_state #DEFINE_ALIAS
222222
from .framework import Variable #DEFINE_ALIAS
223223
from .framework import ParamAttr #DEFINE_ALIAS
224224
from .framework import create_global_var #DEFINE_ALIAS

python/paddle/fluid/tests/unittests/test_cuda_random_seed.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ def test_gen_dropout_dygraph(self):
3636
fluid.enable_dygraph()
3737

3838
gen.manual_seed(111111111)
39-
st = paddle.get_cuda_state()
40-
#x_data = np.arange(1, 101).reshape(2, 50).astype("float32")
41-
#x = paddle.to_variable(x_data)
39+
st = paddle.get_cuda_rng_state()
40+
4241
x = fluid.layers.uniform_random(
4342
[2, 10], dtype="float32", min=0.0, max=1.0)
4443
x_again = fluid.layers.uniform_random(
@@ -49,12 +48,9 @@ def test_gen_dropout_dygraph(self):
4948
print("x_again: {}".format(x_again.numpy()))
5049
x = x + x_again + x_third
5150
y = fluid.layers.dropout(x, 0.5)
52-
#gen.manual_seed(111111111)
53-
# gen.set_state(st)
54-
paddle.set_cuda_state(st)
55-
#gen = paddle.manual_seed(123431)
56-
#x1 = np.arange(1,101).reshape(2,50).astype("float32")
57-
#x1 = paddle.to_variable(x_data)
51+
52+
paddle.set_cuda_rng_state(st)
53+
5854
x1 = fluid.layers.uniform_random(
5955
[2, 10], dtype="float32", min=0.0, max=1.0)
6056
x1_again = fluid.layers.uniform_random(
@@ -65,8 +61,7 @@ def test_gen_dropout_dygraph(self):
6561
y1 = fluid.layers.dropout(x1, 0.5)
6662
y_np = y.numpy()
6763
y1_np = y1.numpy()
68-
#print(y_np)
69-
#print(y1_np)
64+
7065
if core.is_compiled_with_cuda():
7166
print(">>>>>>> dropout dygraph >>>>>>>")
7267
self.assertTrue(np.allclose(y_np, y1_np))
@@ -77,20 +72,16 @@ def test_generator_gaussian_random_dygraph(self):
7772

7873
paddle.manual_seed(12312321111)
7974
x = fluid.layers.gaussian_random([120], dtype="float32")
80-
st1 = paddle.get_cuda_state()
75+
st1 = paddle.get_cuda_rng_state()
8176
x1 = fluid.layers.gaussian_random([120], dtype="float32")
82-
paddle.set_cuda_state(st1)
77+
paddle.set_cuda_rng_state(st1)
8378
x2 = fluid.layers.gaussian_random([120], dtype="float32")
8479
paddle.manual_seed(12312321111)
8580
x3 = fluid.layers.gaussian_random([120], dtype="float32")
8681
x_np = x.numpy()
8782
x1_np = x1.numpy()
8883
x2_np = x2.numpy()
8984
x3_np = x3.numpy()
90-
#print("x: {}".format(x_np))
91-
#print("x1: {}".format(x1_np))
92-
#print("x2: {}".format(x2_np))
93-
#print("x3: {}".format(x3_np))
9485

9586
if core.is_compiled_with_cuda():
9687
print(">>>>>>> gaussian random dygraph >>>>>>>")
@@ -124,7 +115,7 @@ def test_gen_TruncatedNormal_initializer(self):
124115
fluid.disable_dygraph()
125116

126117
gen = paddle.manual_seed(123123143)
127-
cur_state = paddle.get_cuda_state()
118+
cur_state = paddle.get_cuda_rng_state()
128119

129120
startup_program = fluid.Program()
130121
train_program = fluid.Program()

python/paddle/framework/random.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import paddle.fluid as fluid
1717
from paddle.fluid import core
1818

19-
__all__ = ['manual_seed', 'get_cuda_state', 'set_cuda_state']
19+
__all__ = ['manual_seed', 'get_cuda_rng_state', 'set_cuda_rng_state']
2020

2121

2222
def manual_seed(seed):
@@ -51,7 +51,7 @@ def manual_seed(seed):
5151
return core.default_cpu_generator().manual_seed(seed)
5252

5353

54-
def get_cuda_state():
54+
def get_cuda_rng_state():
5555
"""
5656
5757
Get random state of cuda generators.
@@ -66,8 +66,7 @@ def get_cuda_state():
6666
.. code-block:: python
6767
6868
import paddle
69-
gen = paddle.manual_seed(102)
70-
sts = paddle.get_cuda_state()
69+
sts = paddle.get_cuda_rng_state()
7170
7271
"""
7372
state_list = []
@@ -78,13 +77,13 @@ def get_cuda_state():
7877
return state_list
7978

8079

81-
def set_cuda_state(state_list):
80+
def set_cuda_rng_state(state_list):
8281
"""
8382
8483
Sets generator state for all cuda generators
8584
8685
Args:
87-
state_list(list): The cuda states to set back to cuda generators. state_list is obtained from get_cuda_state().
86+
state_list(list): The cuda states to set back to cuda generators. state_list is obtained from get_cuda_rng_state().
8887
8988
Returns:
9089
None
@@ -93,9 +92,8 @@ def set_cuda_state(state_list):
9392
.. code-block:: python
9493
9594
import paddle
96-
gen = paddle.manual_seed(102)
97-
sts = paddle.get_cuda_state()
98-
paddle.set_cuda_state(sts)
95+
sts = paddle.get_cuda_rng_state()
96+
paddle.set_cuda_rng_state(sts)
9997
10098
"""
10199
if core.is_compiled_with_cuda():

0 commit comments

Comments
 (0)