Repository navigation
Conversation
angeloskath
left a comment
There was a problem hiding this comment.
Thanks for adding this. However, tile should be implemented using broadcast_to and reshape rather than repeat to avoid doing multiple unnecessary copies.
See #318 for an improved repeat implementation that could be used as a guide for a tile.
|
@angeloskath Hey, Happy new year 🎉. Note: I change the implementation code to match python code. |
angeloskath
left a comment
There was a problem hiding this comment.
Sorry for being late to review it. I left some comments, I think after these it should be mergeable. Thanks!
| } | ||
|
|
||
| int d = repeats.size(); | ||
| array arr = copy(a, s); |
There was a problem hiding this comment.
Copy is not needed here, you can simply do array arr = a;.
| new_shape.erase(new_shape.begin() + i + 1); | ||
| arr = reshape(arr, new_shape, s); | ||
| } | ||
| } |
There was a problem hiding this comment.
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 repeat() many times.
The better way to do it is to keep 3 vectors.
- a reshape vector which will do all the expands at once (start with a.shape() and add 1s where needed)
- a broadcast to vector which will have the number of repeats where 1s were added in the above
- final reshape vector where the repeats are merged to the axis to their left
After that the implementation is as simple as the following:
auto out = reshape(a, expand_shape, s);
out = broadcast_to(out, repeat_shape, s);
out = reshape(out, final_shape, s);
| 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)); |
There was a problem hiding this comment.
Extremely nitpicky, feel free to ignore. Using an ostringstream would be better as it would allow you to also provide the whole repeats vector. Something like:
std::ostringstream msg;
msg << "[tile] Repeats argument cannot contain negative values but "
<< repeats << " was provided.";
throw std::invalid_argument(msg.str());
|
is this pr stale ? |
|
Sorry, I haven't had the chance to get back to this, I think did what is required @dc-dc-dc . |
Proposed changes
np.tilefunction in mlx as a opmlx.core.tileChecklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changes