-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement gather operation, which selects elements along an axis for … #25
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,8 @@ | |
import orchpy as op | ||
|
||
__all__ = ["BLOCK_SIZE", "DistArray", "assemble", "zeros", "ones", "copy", | ||
"eye", "triu", "tril", "blockwise_dot", "dot", "block_column", "block_row"] | ||
"eye", "triu", "tril", "blockwise_dot", "dot", "block_column", | ||
"block_row", "gather", "shape"] | ||
|
||
BLOCK_SIZE = 10 | ||
|
||
|
@@ -202,3 +203,23 @@ def block_row(a, row): | |
result = DistArray() | ||
result.construct(shape, a.objrefs[row, :]) | ||
return result | ||
|
||
@op.distributed([np.ndarray], [List[tuple]]) | ||
def shape(objrefs): | ||
shapes = [single.shape(objref) for objref in objrefs] | ||
return [op.pull(shape) for shape in shapes] | ||
|
||
@op.distributed([np.ndarray, np.ndarray, int], [np.ndarray]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Methods in "dist" should operate on distributed arrays. Shouldn't this also be called "slice"? Why not use the signature |
||
def gather(objrefs, indices, axis): | ||
perm = indices.argsort() | ||
sorted_indices = indices[perm] | ||
shapes = op.pull(shape(objrefs)) | ||
cumsizes = np.zeros(len(shapes) + 1, dtype="int64") # + 1 for leading zero | ||
np.array([s[axis] for s in shapes]).cumsum(out=cumsizes[1:]) | ||
ranges = np.searchsorted(sorted_indices, cumsizes[1:]) | ||
idx = 0 | ||
results = [] | ||
for i, nextidx in enumerate(ranges): | ||
results.append(single.gather(objrefs[i], sorted_indices[idx:nextidx] - cumsizes[i])) | ||
idx = nextidx | ||
return np.vstack([op.pull(r) for r in results])[indices] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
import random, linalg | ||
from core import zeros, zeros_like, ones, eye, dot, vstack, hstack, subarray, copy, tril, triu | ||
from core import zeros, zeros_like, ones, eye, dot, vstack, hstack, subarray, copy, tril, triu, gather, shape |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,3 +49,11 @@ def tril(a): | |
@op.distributed([np.ndarray], [np.ndarray]) | ||
def triu(a): | ||
return np.triu(a) | ||
|
||
@op.distributed([np.ndarray, np.ndarray], [np.ndarray]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is just a slice operation, and it'd be ideal if we could parallel the numpy slicing syntax. One approach (probably not a good idea) is to override |
||
def gather(a, indices): | ||
return a[indices] | ||
|
||
@op.distributed([np.ndarray], [tuple]) | ||
def shape(a): | ||
return a.shape |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really want to implement this method? It doesn't parallel anything in the numpy API. Also, if we do provide it, it should be in "single" not "dist", since it operates on numpy arrays.