_flags.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """Internal feature flags for torch.onnx.
  2. NOTE: These flags are experimental only. Any flag here can be removed at any
  3. time without notice.
  4. """
  5. import logging
  6. import os
  7. logger = logging.getLogger(__name__)
  8. def _load_boolean_flag(
  9. name: str,
  10. *,
  11. this_will: str,
  12. deprecated: bool = False,
  13. default: bool = False,
  14. ) -> bool:
  15. """Load a boolean flag from environment variable.
  16. Args:
  17. name: The name of the environment variable.
  18. this_will: A string that describes what this flag will do.
  19. deprecated: Whether this flag is deprecated.
  20. default: The default value if envvar not defined.
  21. """
  22. undefined = os.getenv(name) is None
  23. state = os.getenv(name) == "1"
  24. if state:
  25. if deprecated:
  26. logger.error(
  27. "Experimental flag %s is deprecated. Please remove it from your environment.",
  28. name,
  29. )
  30. else:
  31. logger.warning(
  32. "Experimental flag %s is enabled. This will %s.", name, this_will
  33. )
  34. if undefined:
  35. state = default
  36. return state
  37. ENABLE_DRAFT_EXPORT: bool = _load_boolean_flag(
  38. "TORCH_ONNX_ENABLE_DRAFT_EXPORT",
  39. this_will="enable torch.export.draft_export as a strategy for capturing models",
  40. default=False,
  41. )
  42. PREFER_DEFERRED_RUNTIME_ASSERTS_OVER_GUARDS: bool = _load_boolean_flag(
  43. "TORCH_ONNX_PREFER_DEFERRED_RUNTIME_ASSERTS_OVER_GUARDS",
  44. this_will="set prefer_deferred_runtime_asserts_over_guards when calling torch.export",
  45. default=True,
  46. )