Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
vmoens committed Dec 4, 2023
1 parent 86c239f commit 8cd6009
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
17 changes: 12 additions & 5 deletions tensordict/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3179,8 +3179,12 @@ def map(
num_workers (int, optional): the number of workers. Exclusive with ``pool``.
If none is provided, the number of workers will be set to the
number of cpus available.
chunksize (int, optional): The size of each chunk of data. If none
is provided, the number of chunks will equate the number
chunksize (int, optional): The size of each chunk of data.
A ``chunksize`` of 0 will unbind the tensordict along the
desired dimension and restack it after the function is applied,
whereas ``chunksize>0`` will split the tensordict and call
:func:`torch.cat` on the resulting list of tensordicts.
If none is provided, the number of chunks will equate the number
of workers. For very large tensordicts, such large chunks
may not fit in memory for the operation to be done and
more chunks may be needed to make the operation practically
Expand Down Expand Up @@ -3274,9 +3278,12 @@ def map(
raise ValueError(f"Got incompatible dimension {dim_orig}")

self_split = _split_tensordict(self, chunksize, num_chunks, num_workers, dim)
chunksize = 1
imap = pool.imap(fn, self_split, chunksize)
out = torch.cat(list(imap), dim)
call_chunksize = 1
imap = pool.imap(fn, self_split, call_chunksize)
if chunksize == 0:
out = torch.stack(list(imap), dim)
else:
out = torch.cat(list(imap), dim)
return out

# Functorch compatibility
Expand Down
2 changes: 2 additions & 0 deletions tensordict/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,8 @@ def _split_tensordict(td, chunksize, num_chunks, num_workers, dim):
num_chunks = min(td.shape[dim], num_chunks)
return td.chunk(num_chunks, dim=dim)
else:
if chunksize == 0:
return td.unbind(dim=dim)
chunksize = min(td.shape[dim], chunksize)
return td.split(chunksize, dim=dim)

Expand Down

0 comments on commit 8cd6009

Please sign in to comment.