Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for cpp_extensions to compile standalone executable #47862

Closed
wants to merge 9 commits into from

Conversation

robieta
Copy link

@robieta robieta commented Nov 12, 2020

Stack from ghstack:

Differential Revision: D25199265

@dr-ci
Copy link

dr-ci bot commented Nov 12, 2020

💊 CI failures summary and remediations

As of commit e778589 (more details on the Dr. CI page):


None of the CI failures appear to be your fault 💚



🚧 1 fixed upstream failure:

These were probably caused by upstream breakages that were already fixed.

Please rebase on the viable/strict branch (expand for instructions)

Since your merge base is older than viable/strict, run these commands:

git fetch https://github.com/pytorch/pytorch viable/strict
git rebase FETCH_HEAD

Check out the recency history of this "viable master" tracking branch.


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 32 times.

@@ -23,6 +23,13 @@


IS_WINDOWS = sys.platform == 'win32'
LIB_EXT = 'pyd' if IS_WINDOWS else 'so'
EXEC_EXT = 'exe' if IS_WINDOWS else 'o'
Copy link
Contributor

Choose a reason for hiding this comment

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

exec extension is o on linux? Really?

Copy link
Author

Choose a reason for hiding this comment

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

I wound up just omitting it altogether. (Which I guess is the most UNIX-y thing to do.)

if IS_WINDOWS:
if is_standalone:
ldflags = extra_ldflags
elif IS_WINDOWS:
Copy link
Contributor

Choose a reason for hiding this comment

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

nittiest of nits: it would feel better if it was:

if is_standalone:
  ...
else:
  if IS_WINDOWS:
    ...
  else:
    ...

as this communicates better that there aren't three cases, there are two cases, and a subdivision in the second case.

Copy link
Author

@robieta robieta Nov 14, 2020

Choose a reason for hiding this comment

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

Fair point, though my cyclomatic complexity alarm went off for the double nest. I wound up defining SHARED_FLAG since platform is import constant, and then just did ldflags = ([] if is_standalone else [SHARED_FLAG]) + extra_ldflags. A cynic might argue that I'm being too cute / python-y, but hopefully that conveys the intent of "either it's standalone, or slap a --plays_well_with_others stamp on it.)

def _import_module_from_library(module_name, path, is_python_module):
def _import_module_from_library(module_name, path, is_python_module, is_standalone):
if is_standalone:
if IS_WINDOWS and TORCH_LIB_PATH not in os.getenv('PATH', ''):
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically this is wrong, because if I have f'{TORCH_LIB_PATH}/blah/blah in PATH it will incorrectly detect that the PATH is present when it actually isn't. I also get a bit nervous about forward slash and backward slash confusion here.

Copy link
Author

Choose a reason for hiding this comment

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

Good point. I added .split(';') so now it should actually do the right thing. There shouldn't be any forward-backward slash shenaniganry since I use os.path.blah for everything. (Right?)

Copy link
Collaborator

Choose a reason for hiding this comment

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

It will be strictly safe if you use os.path.samefile, which automatically handles cases and slashes. e.g.

>>> os.path.samefile('C:\\Windows', 'c:/windows')
True

But it requires the existence of the paths. So maybe you'll need to use it like os.path.exists(a) and os.path.exists(b) and os.path.samefile(a, b).

extra_ldflags.append(f'/LIBPATH:{TORCH_LIB_PATH}')
if not is_standalone:
extra_ldflags.append('torch_python.lib')
extra_ldflags.append(f'/LIBPATH:{python_lib_path}')
Copy link
Contributor

Choose a reason for hiding this comment

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

By the way, one could conceivable want a standalone executable that still has the Python interpreter. This use case would be when the top level runner is a C++ program, but then calls into the Python runtime. Pretty obscure, but it's a valid configuration. (No action needed.)

stdout=subprocess.PIPE,
)
self.assertEqual(r.returncode, 0)
self.assertEqual(
Copy link
Contributor

Choose a reason for hiding this comment

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

expect test?

Copy link
Author

Choose a reason for hiding this comment

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

In this case no, because:

  1. expecttest doesn't handle the loop properly. (Tries to double update.)
  2. The replacement is completely dedented.

I tried it (factoring the run bits so there were two literal tests) and it was just ugly and awkward. If there were more test cases or the output was less trivial, yeah I'd convert it to an expect test.

Copy link
Contributor

@ezyang ezyang left a comment

Choose a reason for hiding this comment

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

just nits

@facebook-github-bot
Copy link
Contributor

@robieta merged this pull request in 07f038a.

@facebook-github-bot facebook-github-bot deleted the gh/robieta/2/head branch December 5, 2020 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants