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

Load NDArray only to GPU if GPU is present #16432

Merged
merged 2 commits into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1988,6 +1988,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()