Skip to content

Define CMake args and environment variables simultaneously in install script #9494

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

Closed
wants to merge 1 commit into from

Conversation

jathu
Copy link
Contributor

@jathu jathu commented Mar 21, 2025

Summary

This script currently configures CMAKE_ARGS, that is eventually propagated to the underlying cmake build (via pip install). However, notice how for some args, we also define an equivalent envvar. This is primarily for the pip setup script:

executorch/setup.py

Lines 72 to 98 in 78ee0e6

class ShouldBuild:
"""Indicates whether to build various components."""
@staticmethod
def _is_env_enabled(env_var: str, default: bool = False) -> bool:
val = os.environ.get(env_var, None)
if val is None:
return default
if val in ("OFF", "0", ""):
return False
return True
@classmethod
def pybindings(cls) -> bool:
return cls._is_env_enabled("EXECUTORCH_BUILD_PYBIND", default=False)
@classmethod
def training(cls) -> bool:
return cls._is_env_enabled("EXECUTORCH_BUILD_TRAINING", default=False)
@classmethod
def llama_custom_ops(cls) -> bool:
return cls._is_env_enabled("EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT", default=True)
@classmethod
def flatc(cls) -> bool:
return cls._is_env_enabled("EXECUTORCH_BUILD_FLATC", default=True)

So there is an implicit expectation that if you want to access these definitions in the setup script, you need to make sure to also define the envvar along with the cmake args. Well, it took me more than an hour to figure out why my cmake args wasn't being recognized in the setup script 🤦 . So, to make sure consistency — let's force setting args and envvars simultaneously.

Test plan

build + check CMakeCache.txt to ensure flags are set

# Expected: EXECUTORCH_BUILD_PYBIND=ON EXECUTORCH_BUILD_XNNPACK=ON EXECUTORCH_BUILD_COREML=OFF
$ rm -rf pip-out dist && ./install_executorch.sh

# Expected: EXECUTORCH_BUILD_PYBIND=ON EXECUTORCH_BUILD_XNNPACK=OFF EXECUTORCH_BUILD_COREML=ON
$ rm -rf pip-out dist && ./install_executorch.sh --pybind coreml

# Expected: EXECUTORCH_BUILD_PYBIND=ON EXECUTORCH_BUILD_XNNPACK=ON EXECUTORCH_BUILD_COREML=ON
$ rm -rf pip-out dist && ./install_executorch.sh --pybind xnnpack coreml

cc @larryliu0820 @lucylq

@jathu jathu added module: build/install Issues related to the cmake and buck2 builds, and to installing ExecuTorch ciflow/trunk topic: not user facing labels Mar 21, 2025
@jathu jathu requested a review from swolchok March 21, 2025 17:09
Copy link

pytorch-bot bot commented Mar 21, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/9494

Note: Links to docs will display an error until the docs builds have been completed.

❌ 2 New Failures

As of commit 553bbba with merge base e22b21a (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Mar 21, 2025
@jathu jathu marked this pull request as ready for review March 21, 2025 17:12
Copy link
Contributor

@swolchok swolchok left a comment

Choose a reason for hiding this comment

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

This doesn't seem right to me. CMake pays attention to both environment variables and defined flags. We shouldn't have to set both. Can you give a specific motivating example?

@jathu
Copy link
Contributor Author

jathu commented Mar 21, 2025

This doesn't seem right to me. CMake pays attention to both environment variables and defined flags. We shouldn't have to set both. Can you give a specific motivating example?

The environment variable is not for CMake, it is used by the setup.py file we created — I included where we check for it in the description

@jathu jathu force-pushed the jathu/consistent-envvar-setter branch from 9d441a8 to 553bbba Compare March 21, 2025 17:23
@jathu jathu requested a review from swolchok March 21, 2025 18:16
@swolchok
Copy link
Contributor

This doesn't seem right to me. CMake pays attention to both environment variables and defined flags. We shouldn't have to set both. Can you give a specific motivating example?

The environment variable is not for CMake, it is used by the setup.py file we created — I included where we check for it in the description

OK, then I think the redundant part is putting the flags in CMAKE_ARGS? setup.py is already responsible for that, right?

@jathu
Copy link
Contributor Author

jathu commented Mar 21, 2025

This doesn't seem right to me. CMake pays attention to both environment variables and defined flags. We shouldn't have to set both. Can you give a specific motivating example?

The environment variable is not for CMake, it is used by the setup.py file we created — I included where we check for it in the description

OK, then I think the redundant part is putting the flags in CMAKE_ARGS? setup.py is already responsible for that, right?

Unfortunately, setup.py doesn't fully commit to that. For example, the reason it expects some some of these flags to be defined as an env var, is so it can easily infer if it is turned on then add additional logic like putting together pybinging. In this case, it does set the cmake flag again. It wouldn't know what environment variable should be defined as a cmake arg — we would need to make that explicit mapping. Which can just be difficult to maintain. So we should continue to let CMAKE_ARGS get set.

I guess we can remove the duplicate definition by splitting apart the CMAKE_ARGS in setup.py, and getting rid of the environment variable logic entirely. The only problem is this will now expect people to switch their workflow from:

# Before
$ EXECUTORCH_BUILD_PYBIND=ON python setup.py bdist_wheel

# After
$ CMAKE_ARGS="-DEXECUTORCH_BUILD_PYBIND=ON" python setup.py bdist_wheel

I was hesitant to change this since it might break people's current usage, and might require wider CI changes. Note that technically this diff should be neutral change because it maintains the same API — is just forces consistency of the two places of definition instead of doing it manually as it is being done now.

Thinking about it more, if we are okay with breaking people's current workflow of building wheels — I think I'd prefer to get rid of defining environment variables and just relying on CMAKE_ARGS. Thoughts?

@jathu jathu closed this Mar 25, 2025
@jathu jathu deleted the jathu/consistent-envvar-setter branch March 25, 2025 17:55
jathu added a commit that referenced this pull request Mar 25, 2025
### Summary

We seem to be using a combination of CMAKE_ARGS and environment
variables when creating wheels. Ultimately, CMake only uses the cmake
args, however we redefine some of these flags as env vars to help
`setup.py` determine if a certain feature is turned on. Specifically, it
looks for pybinding vars to bundle pybindings.

Let's remove this redundancy and just use the CMAKE_ARGS as the single
source of truth. For more details and other considerations, see
#9494 (abandoned).

Note that even in the wheel building jobs, we use cmake args instead of
environment variables to control features:


https://github.com/pytorch/executorch/blob/644b7ddf14180d97e348faa627f576e13d367d69/.ci/scripts/wheel/envvar_base.sh#L20


https://github.com/pytorch/executorch/blob/644b7ddf14180d97e348faa627f576e13d367d69/.ci/scripts/wheel/envvar_macos.sh#L14-L15

### Test plan

build + check CMakeCache.txt to ensure flags are set

```bash
# Expected: EXECUTORCH_BUILD_PYBIND=OFF EXECUTORCH_BUILD_XNNPACK=OFF EXECUTORCH_BUILD_COREML=OFF
$ rm -rf pip-out dist && ./install_executorch.sh --pybind off

# Expected: EXECUTORCH_BUILD_PYBIND=ON EXECUTORCH_BUILD_XNNPACK=ON EXECUTORCH_BUILD_COREML=OFF
$ rm -rf pip-out dist && ./install_executorch.sh

# Expected: EXECUTORCH_BUILD_PYBIND=ON EXECUTORCH_BUILD_XNNPACK=OFF EXECUTORCH_BUILD_COREML=ON
$ rm -rf pip-out dist && ./install_executorch.sh --pybind coreml

# Expected: EXECUTORCH_BUILD_PYBIND=ON EXECUTORCH_BUILD_XNNPACK=ON EXECUTORCH_BUILD_COREML=ON
$ rm -rf pip-out dist && ./install_executorch.sh --pybind xnnpack coreml

# Throws an error
$ rm -rf pip-out dist && ./install_executorch.sh --pybind coreml off
```


cc @larryliu0820 @lucylq
kirklandsign pushed a commit that referenced this pull request Apr 11, 2025
### Summary

We seem to be using a combination of CMAKE_ARGS and environment
variables when creating wheels. Ultimately, CMake only uses the cmake
args, however we redefine some of these flags as env vars to help
`setup.py` determine if a certain feature is turned on. Specifically, it
looks for pybinding vars to bundle pybindings.

Let's remove this redundancy and just use the CMAKE_ARGS as the single
source of truth. For more details and other considerations, see
#9494 (abandoned).

Note that even in the wheel building jobs, we use cmake args instead of
environment variables to control features:


https://github.com/pytorch/executorch/blob/644b7ddf14180d97e348faa627f576e13d367d69/.ci/scripts/wheel/envvar_base.sh#L20


https://github.com/pytorch/executorch/blob/644b7ddf14180d97e348faa627f576e13d367d69/.ci/scripts/wheel/envvar_macos.sh#L14-L15

### Test plan

build + check CMakeCache.txt to ensure flags are set

```bash
# Expected: EXECUTORCH_BUILD_PYBIND=OFF EXECUTORCH_BUILD_XNNPACK=OFF EXECUTORCH_BUILD_COREML=OFF
$ rm -rf pip-out dist && ./install_executorch.sh --pybind off

# Expected: EXECUTORCH_BUILD_PYBIND=ON EXECUTORCH_BUILD_XNNPACK=ON EXECUTORCH_BUILD_COREML=OFF
$ rm -rf pip-out dist && ./install_executorch.sh

# Expected: EXECUTORCH_BUILD_PYBIND=ON EXECUTORCH_BUILD_XNNPACK=OFF EXECUTORCH_BUILD_COREML=ON
$ rm -rf pip-out dist && ./install_executorch.sh --pybind coreml

# Expected: EXECUTORCH_BUILD_PYBIND=ON EXECUTORCH_BUILD_XNNPACK=ON EXECUTORCH_BUILD_COREML=ON
$ rm -rf pip-out dist && ./install_executorch.sh --pybind xnnpack coreml

# Throws an error
$ rm -rf pip-out dist && ./install_executorch.sh --pybind coreml off
```


cc @larryliu0820 @lucylq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/trunk CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: build/install Issues related to the cmake and buck2 builds, and to installing ExecuTorch topic: not user facing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants