Skip to content
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

Alternative to np.repeat? #258

Closed
harryjulian opened this issue Dec 22, 2023 · 11 comments
Closed

Alternative to np.repeat? #258

harryjulian opened this issue Dec 22, 2023 · 11 comments
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@harryjulian
Copy link

I tried to use .repeat to repeat a 1D array, but found it's missing from MLX at the moment.

I tried making a similar function, making use of a list for the cloning, but MLX doesn't seem to like this either.

def mlx_repeat(x, n):
    return mx.array([x for _ in range(n)])

# "ValueError: Invalid type in array initialization<class 'mlx.core.array'>."

Any ideas on the easiest / most efficient way to compute a repeat of an array with MLX at the moment?

@awni
Copy link
Member

awni commented Dec 22, 2023

@harryjulian you are looking for the concatenate op. We also have a stack if that's useful.

Would be great to put a repeat as a core op. Maybe someone could contribute it!

@awni awni added enhancement New feature or request good first issue Good for newcomers labels Dec 22, 2023
@Bahaatbb
Copy link
Contributor

@awni Hey sorry to bother you, but I would love to contribute to this can you please move me in the right direction, somewhat new to this.
are we expecting the same behavior as numpy.tile function?

@pandyaved98
Copy link

@awni I also like to work on the same. Can you please provide a bit more context so I can add the relevant code? Thanks in advance!

@awni
Copy link
Member

awni commented Dec 22, 2023

Here is the function we'd like to implement: https://numpy.org/doc/stable/reference/generated/numpy.repeat.html

So the same behavior as that

@beverm2391
Copy link

Would an operation like this need to be implemented under the hood or just on top of the existing Python bindings?

@awni
Copy link
Member

awni commented Dec 22, 2023

The way to do it is as a C++ op (which uses other C++ ops like reshape, concatenate, etc) with a binding to python. We don't implement functions in pure python in mlx.core

@Bahaatbb Bahaatbb mentioned this issue Dec 24, 2023
4 tasks
@0x1orz
Copy link

0x1orz commented Dec 27, 2023

To repeat the instances of nn.Module is also in common use. Will the operator be build-in?

@awni
Copy link
Member

awni commented Dec 27, 2023

To repeat the instances of nn.Module is also in common use.

@0x1orz What do you mean by that? Can you give an example?

@0x1orz
Copy link

0x1orz commented Dec 27, 2023

@awni
In PyTorch, I reused a module copy like:

from copy import deepcopy
repeat = lambda module, n: [deepcopy(module) for _ in range(n)]

from torch import nn
layer = nn.Linear(2,3)
layers = repeat(layer, 2)
for layer in layers:
    layer.reset_parameters()

while in MLX,

import  mlx.nn as mlx_nn
mlx_layer = mlx_nn.Linear(2,3)
mlx_layers = repeat(mlx_layer, 2)

that raised TypeError: cannot pickle 'mlx.core.array' object.

@awni
Copy link
Member

awni commented Dec 27, 2023

Ah that makes sense. Since that's such a different thing from mlx.core.repeat let's make a separate issue for it. Something like "Enable nn.Module with Python copy" (which might require making them pickle-able)

@awni
Copy link
Member

awni commented Jan 2, 2024

We added repeat in #278. Will be in the next release.

@awni awni closed this as completed Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

6 participants