Skip to content

Commit df37132

Browse files
authored
Merge pull request #1482 from qingqing01/data_convert
Fix dataprovider_converter when using only one GPU
2 parents 91f13e4 + 566e5f1 commit df37132

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

paddle/api/PaddleAPI.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ void setUseGpu(bool useGpu);
4747
/// Return true if this py_paddle is compiled in GPU Version
4848
bool isGpuVersion();
4949

50+
/// Return FLAGS_trainer_count
51+
int getTrainerCount();
52+
5053
/// The Error of IO Operation. Such as file not found, etc.
5154
class IOError {};
5255

paddle/api/Util.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,7 @@ bool isGpuVersion() {
5454
#endif
5555
}
5656

57+
int getTrainerCount() { return FLAGS_trainer_count; }
58+
5759
static_assert(NUM_PARAMETER_TYPES == paddle::NUM_PARAMETER_TYPES,
5860
"The Parameter Type should be same in core/api and core/common");

paddle/py_paddle/dataprovider_converter.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ def __init__(self, input_type, pos):
2626
if not isinstance(self.input_type, dp2.InputType):
2727
raise ValueError("input type should be dataprovider2.InputType")
2828
self.pos = pos
29+
# data_in_gpu is used to indicate whether to create argument on GPU
30+
# or not in GPU mode. Now if using one thread (trainer_count=1),
31+
# trainer uses NeuralNetwork which needs to create argument on GPU
32+
# before calling forward function. So, set data_in_gpu to True.
33+
# Otherwise, trainer uses MultiGradientMachine which will transfer
34+
# data from CPU to GPU in the forward function, set data_in_gpu to
35+
# False in this case.
36+
self.data_in_gpu = swig_paddle.isUsingGpu(
37+
) and swig_paddle.getTrainerCount() == 1
2938

3039
def scan(self, dat):
3140
pass
@@ -53,7 +62,8 @@ def finish_scan(self, argument):
5362
assert isinstance(argument, swig_paddle.Arguments)
5463
if self.__mat__.dtype != numpy.float32:
5564
self.__mat__ = self.__mat__.astype(numpy.float32)
56-
m = swig_paddle.Matrix.createDenseFromNumpy(self.__mat__, True, False)
65+
m = swig_paddle.Matrix.createDenseFromNumpy(self.__mat__, True,
66+
self.data_in_gpu)
5767
argument.setSlotValue(self.pos, m)
5868

5969

@@ -75,10 +85,13 @@ def extend_cols(self, dat):
7585

7686
def finish_scan(self, argument):
7787
assert isinstance(argument, swig_paddle.Arguments)
78-
m = swig_paddle.Matrix.createSparse(self.__height__,
79-
self.input_type.dim,
80-
len(self.__cols__),
81-
len(self.__value__) == 0)
88+
m = swig_paddle.Matrix.createSparse(
89+
self.__height__,
90+
self.input_type.dim,
91+
len(self.__cols__),
92+
len(self.__value__) == 0,
93+
False, # trans
94+
False) # TODO supoort GPU
8295
assert isinstance(m, swig_paddle.Matrix)
8396
m.sparseCopyFrom(self.__rows__, self.__cols__, self.__value__)
8497
argument.setSlotValue(self.pos, m)
@@ -102,7 +115,7 @@ def scan(self, dat):
102115
self.__ids__.append(dat)
103116

104117
def finish_scan(self, argument):
105-
ids = swig_paddle.IVector.create(self.__ids__)
118+
ids = swig_paddle.IVector.create(self.__ids__, self.data_in_gpu)
106119
assert isinstance(argument, swig_paddle.Arguments)
107120
argument.setSlotIds(self.pos, ids)
108121

python/paddle/v2/tests/test_data_feeder.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,8 @@ def test_multiple_features_tuple(self):
235235

236236
if __name__ == '__main__':
237237
api.initPaddle("--use_gpu=0")
238-
unittest.main()
238+
suite = unittest.TestLoader().loadTestsFromTestCase(DataFeederTest)
239+
unittest.TextTestRunner().run(suite)
240+
if api.isGpuVersion():
241+
api.setUseGpu(True)
242+
unittest.main()

0 commit comments

Comments
 (0)