forked from ray-project/ray
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rllib] Use 64-byte aligned memory when concatenating arrays (ray-pro…
- Loading branch information
Showing
4 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import numpy as np | ||
|
||
|
||
def aligned_array(size, dtype, align=64): | ||
"""Returns an array of a given size that is 64-byte aligned. | ||
The returned array can be efficiently copied into GPU memory by TensorFlow. | ||
""" | ||
|
||
n = size * dtype.itemsize | ||
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) | ||
|
||
assert len(output) == size, len(output) | ||
assert output.ctypes.data % align == 0, output.ctypes.data | ||
return output | ||
|
||
|
||
def concat_aligned(items): | ||
"""Concatenate arrays, ensuring the output is 64-byte aligned. | ||
We only align float arrays; other arrays are concatenated as normal. | ||
This should be used instead of np.concatenate() to improve performance | ||
when the output array is likely to be fed into TensorFlow. | ||
""" | ||
|
||
if len(items) == 0: | ||
return [] | ||
elif len(items) == 1: | ||
# we assume the input is aligned. In any case, it doesn't help | ||
# performance to force align it since that incurs a needless copy. | ||
return items[0] | ||
elif (isinstance(items[0], np.ndarray) | ||
and items[0].dtype in [np.float32, np.float64, np.uint8]): | ||
dtype = items[0].dtype | ||
flat = aligned_array(sum(s.size for s in items), dtype) | ||
batch_dim = sum(s.shape[0] for s in items) | ||
new_shape = (batch_dim, ) + items[0].shape[1:] | ||
output = flat.reshape(new_shape) | ||
assert output.ctypes.data % 64 == 0, output.ctypes.data | ||
np.concatenate(items, out=output) | ||
return output | ||
else: | ||
return np.concatenate(items) |