Repository navigation
add support for tile #317
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
add support for tile #317
Changes from all commits
fc14b6a
c77a77b
583d373
5a84e8b
d9a6b54
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 |
|---|---|---|
|
|
@@ -104,6 +104,7 @@ Operations | |
| take_along_axis | ||
| tan | ||
| tanh | ||
| tile | ||
| tensordot | ||
| transpose | ||
| tri | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -753,6 +753,43 @@ array repeat(const array& arr, int repeats, StreamOrDevice s) { | |
| return repeat(flatten(arr, s), repeats, 0, s); | ||
| } | ||
|
|
||
| /** Replicating array a specified number of times along each axis */ | ||
| array tile(const array& a, std::vector<int> repeats, StreamOrDevice s) { | ||
| if (repeats.size() == 0) { | ||
| return a; | ||
| } | ||
|
|
||
| for (size_t i = 0; i < repeats.size(); i++) { | ||
| if (repeats[i] < 0) { | ||
| throw std::invalid_argument( | ||
| "[tile] Negative repeat value found at index " + std::to_string(i)); | ||
| } | ||
| } | ||
|
|
||
| int d = repeats.size(); | ||
| array arr = copy(a, s); | ||
|
Member
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. Copy is not needed here, you can simply do |
||
|
|
||
| if (d < arr.ndim()) { | ||
| repeats.insert(repeats.begin(), arr.ndim() - d, 1); | ||
| } else if (d > arr.ndim()) { | ||
| arr = expand_dims(arr, std::vector<int>(d - arr.ndim(), 0), s); | ||
| } | ||
|
|
||
| for (size_t i = 0; i < arr.shape().size(); i++) { | ||
| if (repeats[i] != 1) { | ||
| arr = expand_dims(arr, i, s); | ||
| std::vector<int> new_shape(arr.shape()); | ||
| new_shape[i] *= repeats[i]; | ||
| arr = broadcast_to(arr, new_shape, s); | ||
| new_shape[i] = -1; | ||
| new_shape.erase(new_shape.begin() + i + 1); | ||
| arr = reshape(arr, new_shape, s); | ||
| } | ||
| } | ||
|
Member
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 is already much better than before, however again each of these reshapes will be an actual copy and there is no need to launch many. Basically this is like calling The better way to do it is to keep 3 vectors.
After that the implementation is as simple as the following: |
||
|
|
||
| return arr; | ||
| } | ||
|
|
||
| /** Pad an array with a constant value */ | ||
| array pad( | ||
| const array& a, | ||
|
|
||
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.
Extremely nitpicky, feel free to ignore. Using an
ostringstreamwould be better as it would allow you to also provide the whole repeats vector. Something like: