-
Notifications
You must be signed in to change notification settings - Fork 80
Add test_sycl_extension from test_cpp_extensions_aot for XPU #2927
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
Open
astachowiczhabana
wants to merge
11
commits into
main
Choose a base branch
from
astachowiczhabana/issue2574
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c08ecdd
Add test_sycl_extension from test_cpp_extensions_aot for XPU
a9d697b
Address review: narrow exception handling, defer sys.path mutation
bdcb5ca
Merge branch 'main' into astachowiczhabana/issue2574
astachowiczhabana fc66b34
Merge branch 'main' into astachowiczhabana/issue2574
mengfei25 5bc9d1e
Add dedicated Windows CI step for CPP Extensions AOT SYCL UT
61e507c
Merge branch 'main' into astachowiczhabana/issue2574
astachowiczhabana 6be44d5
Add dedicated Windows CI step for CPP Extensions AOT SYCL UT
d579695
Address review: move SYCL UT step before Run Test XPU UT, trigger on …
e4799f9
Fix Windows AOT build: set DISTUTILS_USE_SDK=1 when VC env is active
adc0bdb
Remove unnecessary test from skip_list_common.py
3aa7996
Merge branch 'main' into astachowiczhabana/issue2574
astachowiczhabana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| # Owner(s): ["module: cpp-extensions"] | ||
|
|
||
| import os | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import unittest | ||
|
|
||
| import torch | ||
| import torch.testing._internal.common_utils as common | ||
| from torch.testing._internal.common_utils import TEST_XPU | ||
|
|
||
| # Resolved in setUpClass; points to pytorch/test/ in the CI layout. | ||
| _test_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../../../test") | ||
| _cpp_extensions_dir = os.path.join(_test_dir, "cpp_extensions") | ||
|
|
||
|
|
||
| def _build_cpp_extensions(): | ||
| """Build AOT cpp extensions from pytorch/test/cpp_extensions/. | ||
|
|
||
| Mirrors the build logic from pytorch/test/run_test.py's | ||
| _test_cpp_extensions_aot helper, but only builds the parts needed | ||
| for the SYCL extension test. | ||
| """ | ||
| build_dir = os.path.join(_cpp_extensions_dir, "build") | ||
| if os.path.exists(build_dir): | ||
| shutil.rmtree(build_dir) | ||
|
|
||
| shell_env = os.environ.copy() | ||
| shell_env["USE_NINJA"] = "1" | ||
|
|
||
| install_cmd = [ | ||
| sys.executable, | ||
| "-m", | ||
| "pip", | ||
| "install", | ||
| "--no-build-isolation", | ||
| ".", | ||
| "--root", | ||
| "./install", | ||
| ] | ||
|
|
||
| return_code = subprocess.call(install_cmd, cwd=_cpp_extensions_dir, env=shell_env) | ||
| if return_code != 0: | ||
| raise RuntimeError( | ||
| f"Failed to build cpp extensions (exit code {return_code}). " | ||
| f"Build dir: {_cpp_extensions_dir}" | ||
| ) | ||
|
|
||
| # Add the installed packages to sys.path so they can be imported | ||
| install_directories = [] | ||
| for root, directories, _ in os.walk(os.path.join(_cpp_extensions_dir, "install")): | ||
| for directory in directories: | ||
| if "-packages" in directory: | ||
| install_directories.append(os.path.join(root, directory)) | ||
|
|
||
| for d in install_directories: | ||
| if d not in sys.path: | ||
| sys.path.insert(0, d) | ||
|
|
||
|
|
||
| def _check_sycl_extension_available(): | ||
| try: | ||
| import torch_test_cpp_extension.sycl # noqa: F401 | ||
|
|
||
| return True | ||
| except ModuleNotFoundError as e: | ||
| # Only treat the extension module itself being absent as "not available". | ||
| # Re-raise other missing-module errors (e.g. a broken dependency) so the | ||
| # underlying failure is visible instead of being silently masked. | ||
| if getattr(e, "name", None) in ( | ||
| "torch_test_cpp_extension", | ||
| "torch_test_cpp_extension.sycl", | ||
| ): | ||
| return False | ||
| raise | ||
|
|
||
|
|
||
| @torch.testing._internal.common_utils.markDynamoStrictTest | ||
| class TestCppExtensionAOT(common.TestCase): | ||
| """Tests ahead-of-time SYCL cpp extensions on XPU.""" | ||
|
|
||
| _sycl_extension_built = False | ||
|
|
||
| @classmethod | ||
| def setUpClass(cls): | ||
| super().setUpClass() | ||
| if not TEST_XPU: | ||
| return | ||
| # Defer sys.path mutation until we actually need it | ||
| if _test_dir not in sys.path: | ||
| sys.path.insert(0, _test_dir) | ||
| if _check_sycl_extension_available(): | ||
| cls._sycl_extension_built = True | ||
| return | ||
| # Build the AOT cpp extensions; let failures surface as test errors | ||
| _build_cpp_extensions() | ||
| cls._sycl_extension_built = _check_sycl_extension_available() | ||
astachowiczhabana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @unittest.skipIf(not TEST_XPU, "XPU not found") | ||
| def test_sycl_extension(self): | ||
| if not self._sycl_extension_built: | ||
| self.skipTest("torch_test_cpp_extension.sycl not built") | ||
astachowiczhabana marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import torch_test_cpp_extension.sycl as sycl_extension | ||
|
|
||
| x = torch.zeros(100, device="xpu", dtype=torch.float32) | ||
| y = torch.zeros(100, device="xpu", dtype=torch.float32) | ||
|
|
||
| z = sycl_extension.sigmoid_add(x, y).cpu() | ||
|
|
||
| # 2 * sigmoid(0) = 2 * 0.5 = 1 | ||
| self.assertEqual(z, torch.ones_like(z)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| common.run_tests() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.