Skip to content

[NFC] Create a python dir for common utilities in build_tools - #10065

Closed
pzread wants to merge 6 commits into
iree-org:mainfrom
pzread:build-tools-util
Closed

pzread wants to merge 6 commits into
iree-org:mainfrom
pzread:build-tools-util

Conversation

@pzread

@pzread pzread commented Aug 11, 2022 •

Copy link
Copy Markdown
Contributor

Backgrounds
Currently under build_tools we have executable python scripts in multiple directories (benchmarks, docker, ...). They contain some duplicate utility functions, which can be factored out into the common python modules.

The problem is that these scripts don't have a proper search path to find the common python modules, since they aren't packed as a python package and installed. Right now they can only import the modules in the same and child directories, so there is no way to share the python modules with the scripts in other directories.

Proposal
This PR adds a directory build_tools/python for all common python modules. To solve the issue of search path, I use a hacky way to modify the search path at the beginning of each executable script. I hope there is a better way to do this 🙂

sys.path.append(
    next(
        # Construct the path: <iree>/build_tools/python
        str(parent / "python")
        for parent in Path(__file__).parents
        # Search for the build_tools dir in the parents
        if parent.name == "build_tools"))

We can still run those scripts anywhere without setting the PYTHONPATH, which is convenient for developers and CI.

Another good reason to do this is that we can freely put the executable scripts in sub-directories. Right now we have all benchmark scripts right under build_tools/benchmarks because they need to import build_tools/benchmarks/common. With this change, they can be put into sub-directories, for example: benchmarks/run_tools, benchmarks/report_tools, ...

@pzread pzread changed the title [DO NOT SUBMIT] Test utility directory [NFC] Create a python dir for common utilities in build tools Aug 11, 2022
@pzread pzread added the infrastructure Relating to build systems, CI, or testing label Aug 11, 2022
@pzread
pzread requested a review from stellaraccident August 11, 2022 04:34
@pzread
pzread marked this pull request as ready for review August 11, 2022 04:36
@pzread pzread changed the title [NFC] Create a python dir for common utilities in build tools [NFC] Create a python dir for common utilities in build_tools Aug 11, 2022

from common.benchmark_definition import CompilationInfo, CompilationResults, CompilationStatistics, ModuleComponentSizes, get_git_commit_hash
# Add build_tools python dir to the search path.
sys.path.append(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this is not nice. Is there another way to avoid having this? What about

  • Putting build_tools as the common parent module?, or
  • Moving Python scripts in benchmarks under the python directory?

@pzread pzread Aug 11, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is the part I need suggestions for. AFAIK, if you run python build_tools/benchmarks/xyz.py, the script won't be able to find the modules above the directory build_tools/benchmarks, because they are not in the search path.

To the second option, we have python scripts outside benchmarks that want to share code. So unless we put all the scripts under the python directory, it won't solve the problem. And even if we agree to put all scripts under python directory, all executable scripts will need to be right under python; otherwise they still can't find the common modules in their parent directories. So we can't sort these scripts into different sub-directories.

Some alternative ways I know are:

  • Pack the build tools scripts into a python package. But this will require everyone including CI to install the package before running the tools (and we have CMake rules depending on these scripts).
  • Require everyone to run scripts with python -m. If you run python -m build_tools.benchmarks.xyz, the xyz.py will be able to use relative paths to import the modules under build_tools or build_tools/python.
  • Require everyone to run scripts with PYTHONPATH=build_tools/python in the env.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of all of these, I hate the last one the least, but I actually think that what you have now is not too bad. One way or another we have to put information about the local file system into Python, either via an environment variable or by Python figuring it out itself

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I don't have better suggestions anyway so unblocking this from my side. :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @stellaraccident , do you have some suggestions on this? : )

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of thing is pretty par for the course for in-place, in-repo utilities.

My guidance:

  • If there are just going to be a couple of these (ever), then just do the path munging at the top of each.
  • Otherwise, commit to only having each "executable" python script be a trampoline that does two things: a) munge the path, b) import the tool, c) delegate to the tool's main().

If going to the trouble of organizing this, I'd recommend the second option. Your scripts will have some repetition, but they will basically be only trampolines, so they should all be roughly the same.

import sys
from pathlib import Path
# Add build_tools python dir to the search path.
sys.path.append(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You most likely want sys.path.insert(0, ...) (you want the local path to take precedence.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might just be old-school, but I usually write this kind of thing as:

sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "python"))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is indeed the old school way. pathlib is recommended now :-)

You could spell this with pathlib though:

sys.path.insert(0, pathlib.Path(__file__).parent / "../python")

Personally, I do like something that is robust regardless of the exact path of the file itself. Another option is shelling out to git to find the root of the repository.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"recommended" is a strong word when it comes to these parts of python.

Given all of the things that can break, I just swallow my objections and make it path dependent. When it is wrong, out is obvious and it didn't presume things it shouldn't.

@GMNGeoffrey

Copy link
Copy Markdown
Member

What's the status on this? It shows up on my review queue, but I'm not sure if we decided on another approach

@pzread

pzread commented Oct 4, 2022 •

Copy link
Copy Markdown
Contributor Author

Abandoned this one as we started the refactor with #10415

@pzread pzread closed this Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

infrastructure Relating to build systems, CI, or testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants