run_tests.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """
  2. Runs tests that are appropriate for framework.
  3. """
  4. import os
  5. import sys
  6. from pathlib import Path
  7. from subprocess import Popen
  8. __author__ = "Alex Rogozhnikov"
  9. def run(cmd, **env):
  10. # keeps printing output when testing
  11. cmd = cmd.split(" ") if isinstance(cmd, str) else cmd
  12. print("running:", cmd)
  13. p = Popen(cmd, cwd=str(Path(__file__).parent), env={**os.environ, **env})
  14. p.communicate()
  15. return p.returncode
  16. def main():
  17. _executable, *args = sys.argv
  18. frameworks = [x for x in args if x != "--pip-install"]
  19. pip_install_is_set = "--pip-install" in args
  20. framework_name2installation = {
  21. "numpy": ["numpy"],
  22. "torch": ["torch --index-url https://download.pytorch.org/whl/cpu"],
  23. "jax": ["jax[cpu]", "flax"],
  24. "tensorflow": ["tensorflow"],
  25. "cupy": ["cupy"],
  26. # switch to stable paddlepaddle, because of https://github.com/PaddlePaddle/Paddle/issues/63927
  27. # "paddle": ["paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.cn/whl/linux/cpu-mkl/develop.html"],
  28. "paddle": ["paddlepaddle"],
  29. "oneflow": ["oneflow==0.9.0"],
  30. "pytensor": ["pytensor"],
  31. }
  32. if sys.platform == "darwin":
  33. framework_name2installation["mlx"] = ["mlx"]
  34. if sys.platform.startswith("linux"):
  35. framework_name2installation["mlx"] = ["mlx[cpu]"]
  36. usage = f"""
  37. Usage: python -m einops.tests.run_tests <frameworks> [--pip-install]
  38. Example: python -m einops.tests.run_tests numpy pytorch --pip-install
  39. Available frameworks: {list(framework_name2installation)}
  40. When --pip-install is set, auto-installs requirements with pip.
  41. (make sure which pip points to right pip)
  42. """
  43. if len(frameworks) == 0:
  44. print(usage)
  45. return
  46. else:
  47. synonyms = {
  48. "tf": "tensorflow",
  49. "pytorch": "torch",
  50. "paddlepaddle": "paddle",
  51. }
  52. frameworks = [synonyms.get(f, f) for f in frameworks]
  53. wrong_frameworks = [f for f in frameworks if f not in framework_name2installation]
  54. if wrong_frameworks:
  55. print(usage)
  56. raise RuntimeError(f"Unrecognized frameworks: {wrong_frameworks}")
  57. if pip_install_is_set:
  58. print("Install testing infra")
  59. other_dependencies = ["pytest"]
  60. assert 0 == run("pip install {} --progress-bar off -q".format(" ".join(other_dependencies)))
  61. for framework in frameworks:
  62. print(f"Installing {framework}")
  63. pip_instructions = framework_name2installation[framework]
  64. assert 0 == run("pip install {} --progress-bar off -q".format(" ".join(pip_instructions)))
  65. # we need to inform testing script which frameworks to use
  66. # this is done by setting an envvar EINOPS_TEST_BACKENDS
  67. from einops.tests import unparse_backends
  68. envvar_name, envvar_value = unparse_backends(backend_names=frameworks)
  69. return_code = run(
  70. "python -m pytest .",
  71. **{envvar_name: envvar_value},
  72. )
  73. assert return_code == 0
  74. if __name__ == "__main__":
  75. main()