Skip to content

Commit 216e85b

Browse files
authored
[MLIR][Python] remove PyYAML as a dep (llvm#169145)
PyYAML is not an actual use-time/runtime dependency of our bindings. It is necessary only if someone wants to regenerate `LinalgNamedStructuredOps.yaml`: https://github.com/llvm/llvm-project/blob/93097b2d47c87bf5eee0a2612d961c7a01831eab/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.
1 parent b00c620 commit 216e85b

File tree

2 files changed

+43
-25
lines changed

2 files changed

+43
-25
lines changed

mlir/python/mlir/dialects/linalg/opdsl/lang/yaml_helper.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55

66
import sys
77

8+
9+
def multiline_str_representer(dumper, data):
10+
if len(data.splitlines()) > 1:
11+
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
12+
else:
13+
return dumper.represent_scalar("tag:yaml.org,2002:str", data)
14+
15+
816
try:
9-
import yaml
17+
from yaml import YAMLObject as _YAMLObject, add_representer
18+
19+
add_representer(str, multiline_str_representer)
1020
except ModuleNotFoundError as e:
11-
raise ModuleNotFoundError(
12-
f"This tool requires PyYAML but it was not installed. "
13-
f"Recommend: {sys.executable} -m pip install PyYAML"
14-
) from e
1521

16-
__all__ = [
17-
"yaml_dump",
18-
"yaml_dump_all",
19-
"YAMLObject",
20-
]
22+
class _YAMLObject:
23+
pass
2124

2225

23-
class YAMLObject(yaml.YAMLObject):
26+
class YAMLObject(_YAMLObject):
2427
@classmethod
2528
def to_yaml(cls, dumper, self):
2629
"""Default to a custom dictionary mapping."""
@@ -33,21 +36,34 @@ def as_linalg_yaml(self):
3336
return yaml_dump(self)
3437

3538

36-
def multiline_str_representer(dumper, data):
37-
if len(data.splitlines()) > 1:
38-
return dumper.represent_scalar("tag:yaml.org,2002:str", data, style="|")
39-
else:
40-
return dumper.represent_scalar("tag:yaml.org,2002:str", data)
39+
def yaml_dump(data, sort_keys=False, **kwargs):
40+
try:
41+
import yaml
4142

43+
return yaml.dump(data, sort_keys=sort_keys, **kwargs)
44+
except ModuleNotFoundError as e:
45+
raise ModuleNotFoundError(
46+
f"This tool requires PyYAML but it was not installed. "
47+
f"Recommend: {sys.executable} -m pip install PyYAML"
48+
) from e
4249

43-
yaml.add_representer(str, multiline_str_representer)
4450

51+
def yaml_dump_all(data, sort_keys=False, explicit_start=True, **kwargs):
52+
try:
53+
import yaml
4554

46-
def yaml_dump(data, sort_keys=False, **kwargs):
47-
return yaml.dump(data, sort_keys=sort_keys, **kwargs)
55+
return yaml.dump_all(
56+
data, sort_keys=sort_keys, explicit_start=explicit_start, **kwargs
57+
)
58+
except ModuleNotFoundError as e:
59+
raise ModuleNotFoundError(
60+
f"This tool requires PyYAML but it was not installed. "
61+
f"Recommend: {sys.executable} -m pip install PyYAML"
62+
) from e
4863

4964

50-
def yaml_dump_all(data, sort_keys=False, explicit_start=True, **kwargs):
51-
return yaml.dump_all(
52-
data, sort_keys=sort_keys, explicit_start=explicit_start, **kwargs
53-
)
65+
__all__ = [
66+
"yaml_dump",
67+
"yaml_dump_all",
68+
"YAMLObject",
69+
]

mlir/python/requirements.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
# BUILD dependencies
12
nanobind>=2.9, <3.0
2-
numpy>=1.19.5, <=2.1.2
33
pybind11>=2.10.0, <=2.13.6
44
PyYAML>=5.4.0, <=6.0.1
5+
typing_extensions>=4.12.2
6+
# RUN dependencies
7+
numpy>=1.19.5, <=2.1.2
58
ml_dtypes>=0.1.0, <=0.6.0; python_version<"3.13" # provides several NumPy dtype extensions, including the bf16
69
ml_dtypes>=0.5.0, <=0.6.0; python_version>="3.13"
7-
typing_extensions>=4.12.2

0 commit comments

Comments
 (0)