-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82a8bf0
commit b328d9d
Showing
6 changed files
with
213 additions
and
43 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,48 @@ | ||
from .. import default_backend, get_backend | ||
from .. import events | ||
|
||
|
||
class TensorViewer(object): | ||
def __init__(self, indices): | ||
# self.partition_indices has the "target" indices | ||
# of the stitched vector. In order to .gather() | ||
# an concatennation of source arrays into the | ||
# desired form, one needs to gather on the "sorted" | ||
# indices | ||
# >>> source = np.asarray([9,8,7,6]) | ||
# >>> target = np.asarray([2,1,3,0]) | ||
# >>> source[target.argsort()] | ||
# array([6, 8, 9, 7]) | ||
|
||
self.partition_indices = indices | ||
a = default_backend.astensor( | ||
default_backend.concatenate(self.partition_indices), dtype='int' | ||
) | ||
self._sorted_indices = default_backend.tolist(a.argsort()) | ||
|
||
self._precompute() | ||
events.subscribe('tensorlib_changed')(self._precompute) | ||
|
||
def _precompute(self): | ||
tensorlib, _ = get_backend() | ||
self.sorted_indices = tensorlib.astensor(self._sorted_indices) | ||
|
||
def stitch(self, data): | ||
tensorlib, _ = get_backend() | ||
assert len(self.partition_indices) == len(data) | ||
|
||
data = tensorlib.concatenate(data, axis=-1) | ||
data = tensorlib.einsum('...j->j...', data) | ||
stitched = tensorlib.gather(data, tensorlib.astensor(self.sorted_indices)) | ||
stitched = tensorlib.einsum('...j->j...', stitched) | ||
return stitched | ||
|
||
def split(self, data): | ||
tensorlib, _ = get_backend() | ||
data = tensorlib.astensor(data) | ||
data = tensorlib.einsum('...j->j...', tensorlib.astensor(data)) | ||
split = [ | ||
tensorlib.einsum('...j->j...', tensorlib.gather(data, idx)) | ||
for idx in self.partition_indices | ||
] | ||
return split |
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