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

[cpp_extension][inductor] Fix sleef windows depends. (#128770) #128811

Merged

Conversation

xuhancn
Copy link
Collaborator

@xuhancn xuhancn commented Jun 17, 2024

Issue:

During I'm working on enable inductor on PyTorch Windows, I found the sleef lib dependency issue. image

Analysis:

After we enabled SIMD on PyTorch Windows(#118980 ), the sleef functions are called from VEC headers. It bring the sleef to the dependency.

Here is a different between Windows and Linux OS.

Linux :

Linux is default export its functions, so libtorch_cpu.so static link to sleef.a, and then It also export sleef's functions. image

Windows:

Windows is by default not export its functions, and have many limitation to export functions, reference: #80604 We can't package sleef functions via torch_cpu.dll like Linux.

Solution:

Acturally, we also packaged sleef static lib as a part of release. We just need to help user link to sleef.lib, it should be fine.

  1. Add sleef to cpp_builder for inductor.
  2. Add sleef to cpp_extension for C++ extesion.

Pull Request resolved: #128770
Approved by: https://github.com/jgong5, https://github.com/jansel

cc @peterjc123 @mszhanyi @skyline75489 @nbcsm @vladimir-aubrecht @iremyux @Blackhex @cristianPanaite @malfet @zou3519 @jbschlosser @jgong5 @mingfeima @XiaobingSuper @sanchitintel @ashokei @jingxu10 @voznesenskym @penguinwu @EikanWang @Guobing-Chen @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @peterbell10 @ipiszy @yf225 @chenyang78 @kadeng @muchulee8 @ColinPeppler @amjames @desertfire @chauhang

Copy link

pytorch-bot bot commented Jun 17, 2024

🔗 Helpful Links

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

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

❌ 8 New Failures, 1 Unrelated Failure

As of commit f0189bd with merge base b66e3f0 (image):

NEW FAILURES - The following jobs have failed:

UNSTABLE - The following job failed but was likely due to flakiness present on trunk and has been marked as unstable:

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

@xuhancn xuhancn marked this pull request as ready for review June 17, 2024 06:19
@xuhancn xuhancn added module: windows Windows support for PyTorch ciflow/binaries_libtorch Trigger binary build and upload jobs for libtorch on the PR intel This tag is for PR from Intel ciflow/trunk Trigger trunk jobs on your pull request labels Jun 17, 2024
@xuhancn xuhancn requested review from jgong5 and jansel June 17, 2024 06:42
@xuhancn xuhancn added the topic: not user facing topic category label Jun 17, 2024
@xuhancn
Copy link
Collaborator Author

xuhancn commented Jun 18, 2024

@pytorchbot rebase -b release/2.4

@pytorchmergebot
Copy link
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/release/2.4. Check the current status here

@pytorchmergebot
Copy link
Collaborator

Tried to rebase and push PR #128811, but it was already up to date. Try rebasing against main by issuing:
@pytorchbot rebase -b main

@malfet
Copy link
Contributor

malfet commented Jun 19, 2024

Do we have any testing for the feature?

@xuhancn xuhancn added module: cpp-extensions Related to torch.utils.cpp_extension module: cpp Related to C++ API labels Jun 19, 2024
@xuhancn
Copy link
Collaborator Author

xuhancn commented Jun 19, 2024

@pytorchmergebot rebase -b release/2.4

@pytorchmergebot
Copy link
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/release/2.4. Check the current status here

@pytorchmergebot
Copy link
Collaborator

Successfully rebased xu_fix_windows_sleef_depends_24 onto refs/remotes/origin/release/2.4, please pull locally before adding more changes (for example, via git checkout xu_fix_windows_sleef_depends_24 && git pull --rebase)

@pytorchmergebot pytorchmergebot force-pushed the xu_fix_windows_sleef_depends_24 branch from 3026516 to 7e87d87 Compare June 19, 2024 18:47
@xuhancn
Copy link
Collaborator Author

xuhancn commented Jun 19, 2024

Do we have any testing for the feature?

  1. the origin PR is passed all PreCI with cpp-extension and binaries_libtorch tag, I believe it passed the libtorch and cpp_extension UTs.
  2. I double comfirmed with cmake config file,
    append_torchlib_if_found(sleef asmjit)
    add sleef as dependency is existing.

@xuhancn
Copy link
Collaborator Author

xuhancn commented Jun 21, 2024

Hi @malfet , @atalman

I write a small UT for this PR:

vec.cpp

#include <torch/extension.h>
#if defined(CPU_CAPABILITY_AVX512) || defined(CPU_CAPABILITY_AVX2) || defined(CPU_CAPABILITY_ZVECTOR) || defined(CPU_CAPABILITY_NEON)
#include <ATen/cpu/vec/functional.h>
#include <ATen/cpu/vec/vec.h>
#endif

#if defined(__GNUC__)
#define __at_align__ __attribute__((aligned(64)))
#elif defined(_WIN32)
#define __at_align__ __declspec(align(64))
#else
#define __at_align__
#endif
__at_align__ float in_out_ptr0[16] = {0.0};

extern "C" void __avx_chk_kernel() {
    auto tmp0 = at::vec::Vectorized<float>(1);
    auto tmp1 = tmp0.exp();
    tmp1.store(in_out_ptr0);
}

PYBIND11_MODULE(vec, m) {
  m.def("__avx_chk_kernel", &__avx_chk_kernel, "");
}

setup.py

from setuptools import setup

from torch.utils.cpp_extension import (
    CppExtension,
    BuildExtension,
)

setup(
    name="vec",
    ext_modules=[
        CppExtension(
            "vec",
            [
                "vec.cpp",
            ],
            extra_compile_args=['/arch:AVX2', '/DCPU_CAPABILITY_AVX2'],
        )
    ],
    cmdclass={"build_ext": BuildExtension.with_options(use_ninja=True)},
)

reproduce steps:

python setup.py install

result on release/2.4 branch:
image

result on xuhancn:xu_fix_windows_sleef_depends_24 this branch:
image

@xuhancn
Copy link
Collaborator Author

xuhancn commented Jun 21, 2024

@pytorchmergebot rebase -b release/2.4

@pytorchmergebot
Copy link
Collaborator

@pytorchbot started a rebase job onto refs/remotes/origin/release/2.4. Check the current status here

# Issue:
During I'm working on enable inductor on PyTorch Windows, I found the sleef lib dependency issue.
<img width="1011" alt="image" src="https://github.com/pytorch/pytorch/assets/8433590/423bd854-3c5f-468f-9a64-a392d9b514e3">

# Analysis:
After we enabled SIMD on PyTorch Windows(pytorch#118980 ), the sleef functions are called from VEC headers. It bring the sleef to the dependency.

Here is a different between Windows and Linux OS.
## Linux :
Linux is default export its functions, so libtorch_cpu.so static link to sleef.a, and then It also export sleef's functions.
<img width="647" alt="image" src="https://github.com/pytorch/pytorch/assets/8433590/00ac536c-33fc-4943-a435-25590508840d">

## Windows:
Windows is by default not export its functions, and have many limitation to export functions, reference: pytorch#80604
We can't package sleef functions via torch_cpu.dll like Linux.

# Solution:
Acturally, we also packaged sleef static lib as a part of release. We just need to help user link to sleef.lib, it should be fine.
1. Add sleef to cpp_builder for inductor.
2. Add sleef to cpp_extension for C++ extesion.

Pull Request resolved: pytorch#128770
Approved by: https://github.com/jgong5, https://github.com/jansel
@pytorchmergebot
Copy link
Collaborator

Successfully rebased xu_fix_windows_sleef_depends_24 onto refs/remotes/origin/release/2.4, please pull locally before adding more changes (for example, via git checkout xu_fix_windows_sleef_depends_24 && git pull --rebase)

@pytorchmergebot pytorchmergebot force-pushed the xu_fix_windows_sleef_depends_24 branch from 7e87d87 to f0189bd Compare June 21, 2024 17:19
@xuhancn
Copy link
Collaborator Author

xuhancn commented Jun 24, 2024

@atalman Any comments?

@atalman atalman merged commit 04e98d3 into pytorch:release/2.4 Jun 26, 2024
263 of 272 checks passed
@xuhancn xuhancn deleted the xu_fix_windows_sleef_depends_24 branch June 26, 2024 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ciflow/binaries_libtorch Trigger binary build and upload jobs for libtorch on the PR ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request intel This tag is for PR from Intel module: cpp Related to C++ API module: cpp-extensions Related to torch.utils.cpp_extension module: inductor module: windows Windows support for PyTorch open source topic: not user facing topic category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants