[MLIR][Python] remove PyYAML as a dep (#169145)

PyYAML is not an actual use-time/runtime dependency of our bindings. It
is necessary only if someone wants to regenerate
`LinalgNamedStructuredOps.yaml`:
93097b2d47/mlir/tools/mlir-linalg-ods-gen/update_core_linalg_named_ops.sh.in (L29)

This PR does the minimal refactor to remove the need during actual run/use time.
This commit is contained in:
Maksim Levental
2025-11-22 12:56:39 -05:00
committed by GitHub
parent b00c620b35
commit 216e85bdda
2 changed files with 45 additions and 27 deletions

View File

@@ -5,22 +5,25 @@
import sys
def multiline_str_representer(dumper, data):
if len(data.splitlines()) > 1:
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
else:
return dumper.represent_scalar("tag:yaml.org,2002:str", data)
try:
import yaml
from yaml import YAMLObject as _YAMLObject, add_representer
add_representer(str, multiline_str_representer)
except ModuleNotFoundError as e:
raise ModuleNotFoundError(
f"This tool requires PyYAML but it was not installed. "
f"Recommend: {sys.executable} -m pip install PyYAML"
) from e
__all__ = [
"yaml_dump",
"yaml_dump_all",
"YAMLObject",
]
class _YAMLObject:
pass
class YAMLObject(yaml.YAMLObject):
class YAMLObject(_YAMLObject):
@classmethod
def to_yaml(cls, dumper, self):
"""Default to a custom dictionary mapping."""
@@ -33,21 +36,34 @@ class YAMLObject(yaml.YAMLObject):
return yaml_dump(self)
def multiline_str_representer(dumper, data):
if len(data.splitlines()) > 1:
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
else:
return dumper.represent_scalar("tag:yaml.org,2002:str", data)
yaml.add_representer(str, multiline_str_representer)
def yaml_dump(data, sort_keys=False, **kwargs):
return yaml.dump(data, sort_keys=sort_keys, **kwargs)
try:
import yaml
return yaml.dump(data, sort_keys=sort_keys, **kwargs)
except ModuleNotFoundError as e:
raise ModuleNotFoundError(
f"This tool requires PyYAML but it was not installed. "
f"Recommend: {sys.executable} -m pip install PyYAML"
) from e
def yaml_dump_all(data, sort_keys=False, explicit_start=True, **kwargs):
return yaml.dump_all(
data, sort_keys=sort_keys, explicit_start=explicit_start, **kwargs
)
try:
import yaml
return yaml.dump_all(
data, sort_keys=sort_keys, explicit_start=explicit_start, **kwargs
)
except ModuleNotFoundError as e:
raise ModuleNotFoundError(
f"This tool requires PyYAML but it was not installed. "
f"Recommend: {sys.executable} -m pip install PyYAML"
) from e
__all__ = [
"yaml_dump",
"yaml_dump_all",
"YAMLObject",
]

View File

@@ -1,7 +1,9 @@
# BUILD dependencies
nanobind>=2.9, <3.0
numpy>=1.19.5, <=2.1.2
pybind11>=2.10.0, <=2.13.6
PyYAML>=5.4.0, <=6.0.1
typing_extensions>=4.12.2
# RUN dependencies
numpy>=1.19.5, <=2.1.2
ml_dtypes>=0.1.0, <=0.6.0; python_version<"3.13" # provides several NumPy dtype extensions, including the bf16
ml_dtypes>=0.5.0, <=0.6.0; python_version>="3.13"
typing_extensions>=4.12.2