From 40773f87b6c4d64f28e78b1c4010172f4b16e714 Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Mon, 3 Jun 2024 09:10:20 -0700 Subject: [PATCH] [IR] Expose convenience functions and set module name as public (#1581) We modify the `__module__` property for all public apis so that their modules appear correctly in documentation. Added test similar to PyTorch that checks for public apis in the IR and fixed errors. Example errors: ``` ====================================================================== FAIL: test_correct_module_names (__main__.TestPublicApiNamespace.test_correct_module_names) An API is considered public, if its `__module__` starts with `onnxscript.ir` ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/justinchu/dev/onnxscript/tests/ir/public_api_test.py", line 182, in test_correct_module_names self.assertTrue(not failure_list, msg) AssertionError: False is not true : Make sure that everything that is public is expected (in particular that the module has a properly populated `__all__` attribute) and that everything that is supposed to be public does look public (it does not start with `_` and has a `__module__` that is properly populated). Full list: # onnxscript.ir.serde.deserialize_dimension: - Is NOT public: it is not inside the module's (`onnxscript.ir.serde`) `__all__` - Does look public: it does look public because it follows the rules from the doc above (does not start with `_` and has a proper `__module__`). - You can do either of these two things to fix this problem: - To make it public: add it from the modules's (`onnxscript.ir.serde`) `__all__` - To make it NOT look public: make its name start with `_` # onnxscript.ir.serde.deserialize_metadata_props: - Is NOT public: it is not inside the module's (`onnxscript.ir.serde`) `__all__` - Does look public: it does look public because it follows the rules from the doc above (does not start with `_` and has a proper `__module__`). - You can do either of these two things to fix this problem: - To make it public: add it from the modules's (`onnxscript.ir.serde`) `__all__` - To make it NOT look public: make its name start with `_` ``` --- onnxscript/ir/__init__.py | 10 ++ onnxscript/ir/_convenience.py | 2 +- onnxscript/ir/_name_authority.py | 4 + onnxscript/ir/_type_casting.py | 4 + onnxscript/ir/_type_casting_test.py | 4 + onnxscript/ir/convenience.py | 32 +++++ onnxscript/ir/passes/__init__.py | 10 ++ onnxscript/ir/serde.py | 4 +- tests/ir/public_api_test.py | 189 ++++++++++++++++++++++++++++ 9 files changed, 257 insertions(+), 2 deletions(-) create mode 100644 onnxscript/ir/convenience.py create mode 100644 tests/ir/public_api_test.py diff --git a/onnxscript/ir/__init__.py b/onnxscript/ir/__init__.py index 3c872b195..fa58bc296 100644 --- a/onnxscript/ir/__init__.py +++ b/onnxscript/ir/__init__.py @@ -136,3 +136,13 @@ ValueProtocol, ) from onnxscript.ir.serde import TensorProtoTensor, from_proto, to_proto + + +def __set_module() -> None: + """Set the module of all functions in this module to this public module.""" + global_dict = globals() + for name in __all__: + global_dict[name].__module__ = __name__ + + +__set_module() diff --git a/onnxscript/ir/_convenience.py b/onnxscript/ir/_convenience.py index 14127e353..f0c41b109 100644 --- a/onnxscript/ir/_convenience.py +++ b/onnxscript/ir/_convenience.py @@ -5,7 +5,7 @@ """Convenience methods for constructing and manipulating the IR. This is an internal only module. We should choose to expose some of the methods -after they are proven to be useful. +in convenience.py after they are proven to be useful. """ from __future__ import annotations diff --git a/onnxscript/ir/_name_authority.py b/onnxscript/ir/_name_authority.py index 895433564..d89d57023 100644 --- a/onnxscript/ir/_name_authority.py +++ b/onnxscript/ir/_name_authority.py @@ -1,3 +1,7 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +# -------------------------------------------------------------------------- """Auxiliary class for managing names in the IR.""" from __future__ import annotations diff --git a/onnxscript/ir/_type_casting.py b/onnxscript/ir/_type_casting.py index a043854ef..0dc300627 100644 --- a/onnxscript/ir/_type_casting.py +++ b/onnxscript/ir/_type_casting.py @@ -1,3 +1,7 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +# -------------------------------------------------------------------------- """Numpy utilities for non-native type operation.""" # TODO(justinchuby): Upstream the logic to onnx diff --git a/onnxscript/ir/_type_casting_test.py b/onnxscript/ir/_type_casting_test.py index 3109f75bc..c7ca82eb5 100644 --- a/onnxscript/ir/_type_casting_test.py +++ b/onnxscript/ir/_type_casting_test.py @@ -1,3 +1,7 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +# -------------------------------------------------------------------------- import unittest import numpy as np diff --git a/onnxscript/ir/convenience.py b/onnxscript/ir/convenience.py new file mode 100644 index 000000000..cd09ccad9 --- /dev/null +++ b/onnxscript/ir/convenience.py @@ -0,0 +1,32 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +# -------------------------------------------------------------------------- +"""Convenience methods for constructing and manipulating the IR.""" + +from __future__ import annotations + +__all__ = [ + "convert_attribute", + "convert_attributes", + "replace_all_uses_with", +] + +from onnxscript.ir._convenience import ( + convert_attribute, + convert_attributes, + replace_all_uses_with, +) + +# NOTE: Do not implement any other functions in this module. +# implement them in the _convenience module and import them here instead. + + +def __set_module() -> None: + """Set the module of all functions in this module to this public module.""" + global_dict = globals() + for name in __all__: + global_dict[name].__module__ = __name__ + + +__set_module() diff --git a/onnxscript/ir/passes/__init__.py b/onnxscript/ir/passes/__init__.py index e2ce59183..14a364027 100644 --- a/onnxscript/ir/passes/__init__.py +++ b/onnxscript/ir/passes/__init__.py @@ -23,3 +23,13 @@ PostconditionError, PreconditionError, ) + + +def __set_module() -> None: + """Set the module of all functions in this module to this public module.""" + global_dict = globals() + for name in __all__: + global_dict[name].__module__ = __name__ + + +__set_module() diff --git a/onnxscript/ir/serde.py b/onnxscript/ir/serde.py index 08f5206ee..b1237b30d 100644 --- a/onnxscript/ir/serde.py +++ b/onnxscript/ir/serde.py @@ -21,8 +21,10 @@ # Deserialization "from_proto", "deserialize_attribute", + "deserialize_dimension", "deserialize_function", "deserialize_graph", + "deserialize_metadata_props", "deserialize_model", "deserialize_node", "deserialize_opset_import", @@ -132,7 +134,7 @@ def to_proto( | _protocols.AttributeProtocol | _protocols.ReferenceAttributeProtocol | _protocols.TensorProtocol - | onnx.TypeProto + | _protocols.TypeProtocol | _protocols.GraphViewProtocol, ) -> Any: """Serialize an IR object to a proto.""" diff --git a/tests/ir/public_api_test.py b/tests/ir/public_api_test.py new file mode 100644 index 000000000..1247db9e9 --- /dev/null +++ b/tests/ir/public_api_test.py @@ -0,0 +1,189 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +# -------------------------------------------------------------------------- +# Adapted from +# https://github.com/pytorch/pytorch/blob/b505e8647547f029d0f7df408ee5f2968f757f89/test/test_public_bindings.py#L523 +# Original code PyTorch license https://github.com/pytorch/pytorch/blob/main/LICENSE +# Modifications Copyright (c) Microsoft Corporation. All rights reserved. +from __future__ import annotations + +import importlib +import itertools +import os +import pathlib +import pkgutil +import unittest +from typing import Iterable + +import onnxscript.ir + +IR_NAMESPACE = "onnxscript.ir" + + +def _find_all_importables(pkg): + """Find all importables in the project. + Return them in order. + """ + return sorted( + set( + itertools.chain.from_iterable( + _discover_path_importables(pathlib.Path(p), pkg.__name__) for p in pkg.__path__ + ), + ), + ) + + +def _discover_path_importables(pkg_path: os.PathLike, pkg_name: str) -> Iterable[str]: + """Yield all importables under a given path and package. + This is like pkgutil.walk_packages, but does *not* skip over namespace + packages. Taken from https://stackoverflow.com/questions/41203765/init-py-required-for-pkgutil-walk-packages-in-python3 + """ + for dir_path, _, file_names in os.walk(pkg_path): + pkg_dir_path = pathlib.Path(dir_path) + + if pkg_dir_path.parts[-1] == "__pycache__": + continue + + if all(pathlib.Path(_).suffix != ".py" for _ in file_names): + continue + + rel_pt = pkg_dir_path.relative_to(pkg_path) + pkg_pref = ".".join((pkg_name, *rel_pt.parts)) + yield from ( + pkg_path + for _, pkg_path, _ in pkgutil.walk_packages( + (str(pkg_dir_path),), + prefix=f"{pkg_pref}.", + ) + ) + + +def _is_mod_public(modname: str) -> bool: + split_strs = modname.split(".") + return all(not (elem.startswith("_") or "_test" in elem) for elem in split_strs) + + +def _validate_module(modname: str, failure_list: list[str]) -> None: + mod = importlib.import_module(modname) + if not _is_mod_public(modname): + return + + # verifies that each public API has the correct module name and naming semantics + def check_one_element(elem, modname, mod, *, is_public, is_all): + obj = getattr(mod, elem) + elem_module = getattr(obj, "__module__", None) + # Only used for nice error message below + why_not_looks_public = "" + if elem_module is None: + why_not_looks_public = "because it does not have a `__module__` attribute" + elem_modname_starts_with_mod = ( + elem_module is not None + and elem_module.startswith(IR_NAMESPACE) + and "._" not in elem_module + ) + if not why_not_looks_public and not elem_modname_starts_with_mod: + why_not_looks_public = ( + f"because its `__module__` attribute (`{elem_module}`) is not within the " + f"onnxscript.ir library or does not start with the submodule where it is defined (`{modname}`)" + ) + # elem's name must NOT begin with an `_` and it's module name + # SHOULD start with it's current module since it's a public API + looks_public = not elem.startswith("_") and elem_modname_starts_with_mod + if not why_not_looks_public and not looks_public: + why_not_looks_public = f"because it starts with `_` (`{elem}`)" + + if is_public != looks_public: + if is_public: + why_is_public = ( + f"it is inside the module's (`{modname}`) `__all__`" + if is_all + else "it is an attribute that does not start with `_` on a module that " + "does not have `__all__` defined" + ) + fix_is_public = ( + f"remove it from the modules's (`{modname}`) `__all__`" + if is_all + else f"either define a `__all__` for `{modname}` or add a `_` at the beginning of the name" + ) + else: + assert is_all + why_is_public = f"it is not inside the module's (`{modname}`) `__all__`" + fix_is_public = f"add it from the modules's (`{modname}`) `__all__`" + + if looks_public: + why_looks_public = ( + "it does look public because it follows the rules from the doc above " + "(does not start with `_` and has a proper `__module__`)." + ) + fix_looks_public = "make its name start with `_`" + else: + why_looks_public = why_not_looks_public + if not elem_modname_starts_with_mod: + fix_looks_public = ( + "make sure the `__module__` is properly set and points to a submodule " + f"of `{modname}`" + ) + else: + fix_looks_public = "remove the `_` at the beginning of the name" + + failure_list.append(f"# {modname}.{elem}:") + is_public_str = "" if is_public else " NOT" + failure_list.append(f" - Is{is_public_str} public: {why_is_public}") + looks_public_str = "" if looks_public else " NOT" + failure_list.append(f" - Does{looks_public_str} look public: {why_looks_public}") + # Swap the str below to avoid having to create the NOT again + failure_list.append( + " - You can do either of these two things to fix this problem:" + ) + failure_list.append(f" - To make it{looks_public_str} public: {fix_is_public}") + failure_list.append( + f" - To make it{is_public_str} look public: {fix_looks_public}" + ) + + if hasattr(mod, "__all__"): + public_api = mod.__all__ + all_api = dir(mod) + for elem in all_api: + check_one_element(elem, modname, mod, is_public=elem in public_api, is_all=True) + else: + all_api = dir(mod) + for elem in all_api: + if not elem.startswith("_"): + check_one_element(elem, modname, mod, is_public=True, is_all=False) + + +class TestPublicApiNamespace(unittest.TestCase): + tested_modules = (IR_NAMESPACE, *(_find_all_importables(onnxscript.ir))) + + def test_correct_module_names(self): + """ + An API is considered public, if its `__module__` starts with `onnxscript.ir` + and there is no name in `__module__` or the object itself that starts with "_". + Each public package should either: + - (preferred) Define `__all__` and all callables and classes in there must have their + `__module__` start with the current submodule's path. Things not in `__all__` should + NOT have their `__module__` start with the current submodule. + - (for simple python-only modules) Not define `__all__` and all the elements in `dir(submod)` must have their + `__module__` that start with the current submodule. + """ + failure_list = [] + + for modname in self.tested_modules: + _validate_module(modname, failure_list) + + msg = ( + "Make sure that everything that is public is expected (in particular that the module " + "has a properly populated `__all__` attribute) and that everything that is supposed to be public " + "does look public (it does not start with `_` and has a `__module__` that is properly populated)." + ) + + msg += "\n\nFull list:\n" + msg += "\n".join(failure_list) + + # empty lists are considered false in python + self.assertTrue(not failure_list, msg) + + +if __name__ == "__main__": + unittest.main()