Comments (3)
Ok, cool.
Anyway, wdyt of allowing custom backend?
too much hassle to pass backend each time. Don't see this as a common use-case, so this won't be a part of package
There are a number of places to achieve this behavior should you really need it:
- If you don't like existing backend - you can monkey-patch it.
- you can just copy-paste function in
my_backend_einops
and replace backend resolution with backend you like: https://github.com/arogozhnikov/einops/blob/master/einops/einops.py#L515
I end up implementing something similar to your implementation.
Great, I think this is the most reasonable approach.
closing this as you have a working solution
from einops.
Hi @samsja ,
you make wrong assumptions about how torch works and subsequently how einops works.
- when it is possible to return a view, einops returns a view
from einops import rearrange
import torch
x = torch.zeros([2, 3, 4, 5])
y = rearrange(x, 'b h w c -> b c h w')
x.data_ptr() == y.data_ptr() # outputs True
- einops allocates space for result only if that's unavoidable, because result can't be represented as a view to the input
y = rearrange(x, 'b h w c -> b (c h) w')
x.data_ptr() == y.data_ptr() # outputs False
should there be a need for rearrange_view
, here is the optimal implementation:
def rearrange_view(x, pattern):
result = rearrange(x, pattern)
assert x.data_ptr() == result.data_ptr(), "result can't be a view to the input"
return result
But from practitioner's perspective it is not helpful - if output can't be a view to the input, you still want to compute it.
from einops.
Thanks for your response.
when it is possible to return a view, einops returns a view
einops allocates space for result only if that's unavoidable, because result can't be represented as a view to the input
Yes I am completely aware of this two points. I totally understand that rearrange will not copy memory if it don't have to (this is as well pytorch reshape behavior). Yet there is still a difference of utility between view and reshape. View will throw an error if it cannot do without copying memory while reshape will silently create a new tensor.
But from practitioner's perspective it is not helpful - if output can't be a view to the input, you still want to compute it.
This is arguable. In my case I specifically want this granularity and be totally sure that I am not copying anything.
I end up implementing something similar to your implementation. I guess that is fine for my use case, tho not totally optimal since you still create a new tensor, whereas view will fail before creating it. If I were working with really big tensor I could end with OOM even if I catch the assert error. Whereas torch.view no copy would have be done if I catch the view error.
Anyway, wdyt of allowing custom backend ? I can totally understand that such rearrange_view
is not in einops philosophy, tho it would be nice to our own thing with the backend !
Thanks in advance
from einops.
Related Issues (20)
- [BUG] Please explain in the README how to run tests HOT 1
- [BUG] 5 tests failed HOT 1
- Test test_torch_layer fails: RuntimeError: required keyword attribute 'value' has the wrong type HOT 3
- einops compatible with ONNX export? HOT 3
- [Feature suggestion] All/Any Reduction HOT 2
- [BUG] get_backend is not thread-safe HOT 3
- einops.layers.torch.Rearrange does not accept a list[torch.Tensor] as an input HOT 1
- [Feature suggestion] Support composition/decomposition of axes in `einsum` HOT 2
- *** AttributeError: 'Rearrange' object has no attribute 'recipe'[BUG] HOT 1
- [BUG] batchsize of dataloading
- [BUG] error when import einops HOT 1
- [BUG] Einops repeat throws device error during torchscripting HOT 1
- [Feature suggestion] apple mlx support
- [BUG] einops.repeat returns value with type Never HOT 3
- Add support for keras3 HOT 2
- [Feature suggestion] fixup/support anonymous axes in `parse_shape` HOT 2
- [BUG] `einsum` with `ii->i` raises an unknow axis error. HOT 1
- [Feature suggestion] package downloaded from conda-forge seems missing some functions HOT 1
- Passing a float in `repeat` as a dimension size prevents correct usage afterwards HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from einops.