mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
This patch adds support for sycl kernels build via `torch.utils.cpp_extension.load`, `torch.utils.cpp_extension.load_inline` and (new) `class SyclExtension` APIs. Files having `.sycl` extension are considered to have sycl kernels and are compiled with `icpx` (dpc++ sycl compiler from Intel). Files with other extensions, `.cpp`, `.cu`, are handled as before. API supports building sycl along with other file types into single extension. Note that `.sycl` file extension is a PyTorch convention for files containing sycl code which I propose to adopt. We did follow up with compiler team to introduce such file extension in the compiler, but they are opposed to this. At the same time discussion around sycl file extension and adding sycl language support into such tools as cmake is ongoing. Eventually cmake also considers to introduce some file extension convention for sycl. I hope we can further influence cmake and compiler communities to broader adopt `.sycl` file extension. By default SYCL kernels are compiled for all Intel GPU devices for which pytorch native aten SYCL kernels are compiled. At the moment `pvc,xe-lpg`. This behavior can be overridden by setting `TORCH_XPU_ARCH_LIST` environment variables to the comma separated list of desired devices to compile for. Fixes: #132944 CC: @gujinghui @EikanWang @fengyuan14 @guangyey @jgong5 Pull Request resolved: https://github.com/pytorch/pytorch/pull/132945 Approved by: https://github.com/albanD, https://github.com/guangyey, https://github.com/malfet Co-authored-by: Nikita Shulga <2453524+malfet@users.noreply.github.com>
62 lines
2.1 KiB
Python
62 lines
2.1 KiB
Python
# mypy: allow-untyped-defs
|
|
import collections
|
|
|
|
|
|
Entry = collections.namedtuple('Entry', 'version, hash')
|
|
|
|
|
|
def update_hash(seed, value):
|
|
# Good old boost::hash_combine
|
|
# https://www.boost.org/doc/libs/1_35_0/doc/html/boost/hash_combine_id241013.html
|
|
return seed ^ (hash(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2))
|
|
|
|
|
|
def hash_source_files(hash_value, source_files):
|
|
for filename in source_files:
|
|
with open(filename, 'rb') as file:
|
|
hash_value = update_hash(hash_value, file.read())
|
|
return hash_value
|
|
|
|
|
|
def hash_build_arguments(hash_value, build_arguments):
|
|
for group in build_arguments:
|
|
if group:
|
|
for argument in group:
|
|
hash_value = update_hash(hash_value, argument)
|
|
return hash_value
|
|
|
|
|
|
class ExtensionVersioner:
|
|
def __init__(self):
|
|
self.entries = {}
|
|
|
|
def get_version(self, name):
|
|
entry = self.entries.get(name)
|
|
return None if entry is None else entry.version
|
|
|
|
def bump_version_if_changed(self,
|
|
name,
|
|
source_files,
|
|
build_arguments,
|
|
build_directory,
|
|
with_cuda,
|
|
with_sycl,
|
|
is_python_module,
|
|
is_standalone):
|
|
hash_value = 0
|
|
hash_value = hash_source_files(hash_value, source_files)
|
|
hash_value = hash_build_arguments(hash_value, build_arguments)
|
|
hash_value = update_hash(hash_value, build_directory)
|
|
hash_value = update_hash(hash_value, with_cuda)
|
|
hash_value = update_hash(hash_value, with_sycl)
|
|
hash_value = update_hash(hash_value, is_python_module)
|
|
hash_value = update_hash(hash_value, is_standalone)
|
|
|
|
entry = self.entries.get(name)
|
|
if entry is None:
|
|
self.entries[name] = entry = Entry(0, hash_value)
|
|
elif hash_value != entry.hash:
|
|
self.entries[name] = entry = Entry(entry.version + 1, hash_value)
|
|
|
|
return entry.version
|