Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Load NDArray only to GPU if GPU is present (#16432)
Browse files Browse the repository at this point in the history
* Load NDArray only to GPU if GPU is present

* Add test
  • Loading branch information
leezu authored and szha committed Oct 17, 2019
1 parent de524bb commit a4ea4a8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/ndarray/ndarray.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,13 @@ bool NDArray::Load(dmlc::Stream *strm) {
*this = std::move(temp); return true;
} else {
#if MXNET_USE_CUDA
*this = temp.Copy(ctx); return true;
int device_count = -1;
cudaGetDeviceCount(&device_count);
if (device_count > 0) {
*this = temp.Copy(ctx); return true;
} else {
*this = std::move(temp); return true;
}
#else
*this = std::move(temp); return true;
#endif
Expand Down
16 changes: 16 additions & 0 deletions tests/python/unittest/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,22 @@ def test_large_int_rounding():
assert np.all(a == large_integer)


def test_load_saved_gpu_array_when_no_gpus_are_present():
# State obtained with mx.nd.arange(1, ctx=mx.gpu()).__getstate__()
# State needs to be exported manually, as running above command will only
# work if a gpu is present.
ndarray_state = {
'handle':
bytearray(
b'\xc9\xfa\x93\xf9\x00\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
)
}
array = mx.nd.arange(1)
# Test that MXNDArrayLoadFromRawBytes works even if we have built with Cuda
# but there are no GPUs
array.__setstate__(ndarray_state)


if __name__ == '__main__':
import nose
nose.runmodule()

0 comments on commit a4ea4a8

Please sign in to comment.