[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
This commit is contained in:
Sam Toyer 2019-07-16 22:37:57 -07:00 committed by Eric Liang
parent 3e0ad11ae0
commit 214f09d969
2 changed files with 9 additions and 2 deletions

View file

@ -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:

View file

@ -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