Comments (3)
Hmmm, good point, does wrapping _backends.items()
into list solves the problem?
from einops.
I don't think so. Assume two threads T1 and T2 using the same backend cannot find the backend in the dict.
If T1 then imports the respective backend, T2 may encounter
if BackendSubclass.framework_name not in _backends:
therefore not finding the backend that has already been imported by T1.
A simple solution would be to introduce a lock, something like
def get_backend(tensor) -> 'AbstractBackend':
"""
Takes a correct backend (e.g. numpy backend if tensor is numpy.ndarray) for a tensor.
If needed, imports package and creates backend
"""
for framework_name, backend in _backends.items():
if backend.is_appropriate_type(tensor):
return backend
with lock:
# Try to find backend again
for framework_name, backend in _backends.items():
if backend.is_appropriate_type(tensor):
return backend
# Find backend subclasses recursively
backend_subclasses = []
backends = AbstractBackend.__subclasses__()
while backends:
backend = backends.pop()
backends += backend.__subclasses__()
backend_subclasses.append(backend)
for BackendSubclass in backend_subclasses:
if _debug_importing:
print('Testing for subclass of ', BackendSubclass)
if BackendSubclass.framework_name not in _backends:
# check that module was already imported. Otherwise it can't be imported
if BackendSubclass.framework_name in sys.modules:
if _debug_importing:
print('Imported backend for ', BackendSubclass.framework_name)
backend = BackendSubclass()
_backends[backend.framework_name] = backend
if backend.is_appropriate_type(tensor):
return backend
raise RuntimeError('Tensor type unknown to einops {}'.format(type(tensor)))
from einops.
If T1 then imports the respective backend, T2 may encounter
if BackendSubclass.framework_name not in _backends: therefore not finding the backend that has already been imported by T1.
True, but not an issue: rest of function is idempontent, and no problem if backend is created twice.
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
- 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
- [Feature suggestion] Allow performing a view instead of a reshape HOT 3
- [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.