huggingface / candle

Minimalist ML framework for Rust
Apache License 2.0
15.57k stars 924 forks source link

Tensor unfold? #1416

Open grzuy opened 10 months ago

grzuy commented 10 months ago

Hi there,

Any tips on how would one could achieve something similar/close to pytorch's https://pytorch.org/docs/stable/generated/torch.Tensor.unfold.html?

Something built on top of narrow maybe?

Thanks!

EricLBuehler commented 10 months ago

If you know the number of dims of the input tensor, perhaps you can do the following:

mokeyish commented 10 months ago

You can try to implement unfold in this candle-ext

mokeyish commented 10 months ago

It seems that unfold calculates a new shape and then reshapes it? It may also need permute.

LaurentMazare commented 10 months ago

You should avoid doing to_vec for such ops as much as possible, this breaks backprop so training isn't possible when using it and has some large overhead when running on gpus/accelerated devices (and even on cpu). Instead you probably want to generate a tensor with the indexes of the target positions based on the unfold parameters and call gather or some equivalent function on the source tensor using these indexes.

grzuy commented 10 months ago

Thanks all for the feedback :+1:

EricLBuehler commented 4 months ago

@grzuy, did you have success implementing this?

grzuy commented 4 months ago

@grzuy, did you have success implementing this?

Hi @EricLBuehler , Didn't try in the end.

EricLBuehler commented 4 months ago

@grzuy I actually just wrote some code to do it on the CPU in the end.