From 214f09d969480279930994cabbcc2a75535cc6ca Mon Sep 17 00:00:00 2001 From: Sam Toyer Date: Tue, 16 Jul 2019 22:37:57 -0700 Subject: [PATCH] [rllib] Make RLLib handle zero-length observation arrays (#5208) * [rllib] Make _summarize handle zero-len arrays Fixes #5207 * [rllib] Make aligned_array() handle empty arrays * [rllib] Conform with old yapf --- python/ray/rllib/utils/debug.py | 5 ++++- python/ray/rllib/utils/memory.py | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/python/ray/rllib/utils/debug.py b/python/ray/rllib/utils/debug.py index 0f636b0f0..2903d83c6 100644 --- a/python/ray/rllib/utils/debug.py +++ b/python/ray/rllib/utils/debug.py @@ -78,7 +78,10 @@ def _summarize(obj): elif isinstance(obj, tuple): return tuple(_summarize(x) for x in obj) elif isinstance(obj, np.ndarray): - if obj.dtype == np.object: + if obj.size == 0: + return _StringValue("np.ndarray({}, dtype={})".format( + obj.shape, obj.dtype)) + elif obj.dtype == np.object: return _StringValue("np.ndarray({}, dtype={}, head={})".format( obj.shape, obj.dtype, _summarize(obj[0]))) else: diff --git a/python/ray/rllib/utils/memory.py b/python/ray/rllib/utils/memory.py index a51a18402..78bab8380 100644 --- a/python/ray/rllib/utils/memory.py +++ b/python/ray/rllib/utils/memory.py @@ -56,7 +56,11 @@ def aligned_array(size, dtype, align=64): empty = np.empty(n + (align - 1), dtype=np.uint8) data_align = empty.ctypes.data % align offset = 0 if data_align == 0 else (align - data_align) - output = empty[offset:offset + n].view(dtype) + if n == 0: + # stop np from optimising out empty slice reference + output = empty[offset:offset + 1][0:0].view(dtype) + else: + output = empty[offset:offset + n].view(dtype) assert len(output) == size, len(output) assert output.ctypes.data % align == 0, output.ctypes.data