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

ImportError: cannot import name 'TypeAlias' from 'typing' on macOS #322

Closed
asimilon opened this issue Sep 2, 2022 · 7 comments
Closed

Comments

@asimilon
Copy link

asimilon commented Sep 2, 2022

After following the steps in README-Mac-MPS.md I get the following error:

(ldm) richelmes@M1mini:~/various/stable-diffusion
main 16:05 $ python scripts/dream.py --full_precision           
* Initializing, be patient...

Traceback (most recent call last):
  File "/Users/richelmes/various/stable-diffusion/scripts/dream.py", line 553, in <module>
    main()
  File "/Users/richelmes/various/stable-diffusion/scripts/dream.py", line 41, in main
    from ldm.simplet2i import T2I
  File "/Users/richelmes/various/stable-diffusion/ldm/simplet2i.py", line 29, in <module>
    from ldm.models.diffusion.ksampler import KSampler
  File "/Users/richelmes/various/stable-diffusion/ldm/models/diffusion/ksampler.py", line 2, in <module>
    import k_diffusion as K
  File "/Users/richelmes/various/stable-diffusion/src/k-diffusion/k_diffusion/__init__.py", line 1, in <module>
    from . import augmentation, config, evaluation, external, gns, layers, models, sampling, utils
  File "/Users/richelmes/various/stable-diffusion/src/k-diffusion/k_diffusion/external.py", line 6, in <module>
    from . import sampling, utils
  File "/Users/richelmes/various/stable-diffusion/src/k-diffusion/k_diffusion/sampling.py", line 10, in <module>
    from typing import Optional, Callable, TypeAlias
ImportError: cannot import name 'TypeAlias' from 'typing' (/opt/homebrew/anaconda3/envs/ldm/lib/python3.9/typing.py)

I tried:

pip uninstall typing_extensions
pip install typing_extensions==4.0.0
Found existing installation: typing_extensions 4.3.0
Uninstalling typing_extensions-4.3.0:
  Would remove:
    /opt/homebrew/anaconda3/envs/ldm/lib/python3.9/site-packages/typing_extensions-4.3.0.dist-info/*
    /opt/homebrew/anaconda3/envs/ldm/lib/python3.9/site-packages/typing_extensions.py
Proceed (Y/n)? y
  Successfully uninstalled typing_extensions-4.3.0
Collecting typing_extensions==4.0.0
  Using cached typing_extensions-4.0.0-py3-none-any.whl (22 kB)
Installing collected packages: typing_extensions
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
qudida 0.0.4 requires opencv-python-headless>=4.0.1, which is not installed.
albumentations 1.2.1 requires opencv-python-headless>=4.1.1, which is not installed.
Successfully installed typing_extensions-4.0.0

but still get the same error.

This was all working for me before pulling the latest version today, but I removed the ldm anaconda environment to be sure that everything was up to date and ran into problems, so I nuked everything and tried to install from scratch.

@ghost
Copy link

ghost commented Sep 2, 2022

I'm also having this issue

@jnordberg
Copy link

jnordberg commented Sep 2, 2022

I worked around this by upgrading to python 3.10.

diff --git a/environment-mac.yaml b/environment-mac.yaml
index 44cd1ef..6742a1f 100644
--- a/environment-mac.yaml
+++ b/environment-mac.yaml
@@ -1,11 +1,11 @@
-name: ldm
+name: ldm310
 channels:
   - pytorch-nightly
   - conda-forge
 dependencies:
-  - python==3.9.13
-  - pip==22.2.2
-
+  - python=3.10
+  - pip
+
   # pytorch-nightly, left unpinned
   - pytorch
   - torchmetrics
(END)

@psychedelicious
Copy link
Collaborator

TypeAlias is only supported as of python 3.10: https://docs.python.org/3/whatsnew/3.10.html (search in page for 'TypeAlias')

So I think this is was caused by the project reverting to python 3.9, which occurred here: #283

A new PR is pending to move back to 3.10: #301

@magnusviri
Copy link
Contributor

This is an error on a different repo. This patch will fix it temporarily (I hope the code I'm pasting works right).

patch -u src/k-diffusion/k_diffusion/sampling.py
--- sampling.py-orig	2022-09-02 07:01:41.000000000 -0600
+++ sampling.py	2022-09-02 07:02:13.000000000 -0600
@@ -7,7 +7,11 @@
 from torch import Tensor
 from torchdiffeq import odeint
 from tqdm.auto import trange, tqdm
-from typing import Optional, Callable, TypeAlias
+from typing import Optional, Callable
+try:
+    from typing import TypeAlias
+except ImportError:
+    from typing_extensions import TypeAlias

 from . import utils

@magnusviri
Copy link
Contributor

This bug was first noticed in this issue.

@magnusviri
Copy link
Contributor

@jnordberg your version changes the env name to ldm310, which will probably confuse people. For a minute I thought that was my comment so I edited to fix it, but after saving the fix I realized it was someone else's comment! Sorry for that, I've restored it to the way you had it.

@jnordberg
Copy link

@magnusviri Ah ok, I was wondering what was going on there 😂

FWIW I'm not seeing any performance regression on python 3.10 on a 16g M1 MacBook Air

With 3.9 and the typing_extensions patch: 3.94s/it
With 3.10: 3.84s/it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants