run_tests.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """
  2. Runs tests that are appropriate for framework.
  3. """
  4. import os
  5. import sys
  6. from subprocess import Popen
  7. from pathlib import Path
  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. usage = f"""
  33. Usage: python -m einops.tests.run_tests <frameworks> [--pip-install]
  34. Example: python -m einops.tests.run_tests numpy pytorch --pip-install
  35. Available frameworks: {list(framework_name2installation)}
  36. When --pip-install is set, auto-installs requirements with pip.
  37. (make sure which pip points to right pip)
  38. """
  39. if len(frameworks) == 0:
  40. print(usage)
  41. return
  42. else:
  43. synonyms = {
  44. "tf": "tensorflow",
  45. "pytorch": "torch",
  46. "paddlepaddle": "paddle",
  47. }
  48. frameworks = [synonyms.get(f, f) for f in frameworks]
  49. wrong_frameworks = [f for f in frameworks if f not in framework_name2installation]
  50. if wrong_frameworks:
  51. print(usage)
  52. raise RuntimeError(f"Unrecognized frameworks: {wrong_frameworks}")
  53. if pip_install_is_set:
  54. print("Install testing infra")
  55. other_dependencies = ["pytest"]
  56. assert 0 == run("pip install {} --progress-bar off -q".format(" ".join(other_dependencies)))
  57. for framework in frameworks:
  58. print(f"Installing {framework}")
  59. pip_instructions = framework_name2installation[framework]
  60. assert 0 == run("pip install {} --progress-bar off -q".format(" ".join(pip_instructions)))
  61. # we need to inform testing script which frameworks to use
  62. # this is done by setting an envvar EINOPS_TEST_BACKENDS
  63. from einops.tests import unparse_backends
  64. envvar_name, envvar_value = unparse_backends(backend_names=frameworks)
  65. return_code = run(
  66. "python -m pytest .",
  67. **{envvar_name: envvar_value},
  68. )
  69. assert return_code == 0
  70. if __name__ == "__main__":
  71. main()