Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 1 addition & 64 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ option(BUILD_BENCHMARK "Build benchmarks in 'benchmark' directory" ON)
option(BUILD_DOCUMENTATION "Build the documentation which is online available" OFF)
option(BUILD_EXAMPLE "Build the examples" OFF)

set(IPEX_VERSION "2.1.30" CACHE STRING "The version of Intel Extension for PyTorch")
set(TARGET_DEVICE "BMG" CACHE STRING "Valid values {PVC, ACM, BMG}")

set(ACM_DEVICES ACM)
Expand All @@ -32,76 +31,14 @@ string( TOUPPER ${TARGET_DEVICE} TARGET_DEVICE_UPPERCASE)
if (TARGET_DEVICE_UPPERCASE IN_LIST XE_DEVICES)
message(STATUS "Building for PVC/Xe2")
add_definitions(-DTARGET_DEVICE=0)

elseif (TARGET_DEVICE_UPPERCASE IN_LIST ACM_DEVICES)
message(STATUS "Building for Alchemist")
add_definitions(-DTARGET_DEVICE=1)
else()
message(ERROR "Please set a valid device by adding -DTARGET_DEVICE=<value>, where <value> is one of \"PVC\", \"BMG\", or \"ACM\"")
endif()

message(WARNING "cmake build system is still WIP.")

# Check if either BUILD_PYBIND or BUILD_TORCH_TEST is ON
if(BUILD_PYBIND OR BUILD_TORCH_TEST OR BUILD_TORCH_BENCHMARK)
# Check if libtorch directory exists
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/extern/libtorch")
# Notify that we are downloading the necessary libtorch files
message(STATUS "libtorch not found in ${PROJECT_SOURCE_DIR}/extern/. Downloading...")

# Determine the appropriate filenames based on the IPEX_VERSION
if(IPEX_VERSION VERSION_EQUAL "2.1.30")
set(LIBTORCH_FILENAME "libtorch-cxx11-abi-shared-with-deps-2.1.0.post2.zip")
set(LIBINTEL_EXT_FILENAME "libintel-ext-pt-cxx11-abi-2.1.30.post0.run")
elseif(IPEX_VERSION VERSION_EQUAL "2.1.20")
set(LIBTORCH_FILENAME "libtorch-cxx11-abi-shared-with-deps-2.1.0.post0.zip")
set(LIBINTEL_EXT_FILENAME "libintel-ext-pt-cxx11-abi-2.1.20+xpu.run")
elseif(IPEX_VERSION VERSION_EQUAL "2.1.10")
set(LIBTORCH_FILENAME "libtorch-cxx11-abi-shared-with-deps-2.1.0a0.zip")
set(LIBINTEL_EXT_FILENAME "libintel-ext-pt-cxx11-abi-2.1.10+xpu.run")
else()
message(FATAL_ERROR "Unsupported IPEX_VERSION: ${IPEX_VERSION}")
endif()

# Set the URLs for the files to be downloaded using the determined filenames
set(LIBTORCH_URL "https://intel-extension-for-pytorch.s3.amazonaws.com/libipex/xpu/${LIBTORCH_FILENAME}")
set(LIBINTEL_EXT_URL "https://intel-extension-for-pytorch.s3.amazonaws.com/libipex/xpu/${LIBINTEL_EXT_FILENAME}")

# Download libtorch
file(DOWNLOAD "${LIBTORCH_URL}" "${CMAKE_CURRENT_BINARY_DIR}/${LIBTORCH_FILENAME}"
STATUS download_status_libtorch)
list(GET download_status_libtorch 0 download_result_libtorch)
if(NOT download_result_libtorch EQUAL 0)
message(FATAL_ERROR "Failed to download libtorch. Please download it manually with:\nwget ${LIBTORCH_URL}\nand follow install instructions for cppsdk: https://intel.github.io/intel-extension-for-pytorch/index.html#installation?platform=gpu&version=v2.1.10%2Bxpu")
endif()

# Download libintel extension
file(DOWNLOAD "${LIBINTEL_EXT_URL}" "${CMAKE_CURRENT_BINARY_DIR}/${LIBINTEL_EXT_FILENAME}"
STATUS download_status_libintel_ext)
list(GET download_status_libintel_ext 0 download_result_libintel_ext)
if(NOT download_result_libintel_ext EQUAL 0)
message(FATAL_ERROR "Failed to download libintel extension. Please download it manually with:\nwget ${LIBINTEL_EXT_URL}\nand follow install instructions for cppsdk: https://intel.github.io/intel-extension-for-pytorch/index.html#installation?platform=gpu&version=v2.1.10%2Bxpu")
endif()

# Unzip the downloaded libtorch file
execute_process(
COMMAND ${CMAKE_COMMAND} -E tar xvf "${CMAKE_CURRENT_BINARY_DIR}/${LIBTORCH_FILENAME}"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/extern/"
)

# Execute the installer for the libintel extension
execute_process(
COMMAND bash "${CMAKE_CURRENT_BINARY_DIR}/${LIBINTEL_EXT_FILENAME}" install "${PROJECT_SOURCE_DIR}/extern/libtorch/"
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/extern/"
)

# After unpacking and installation, clean up the downloaded files if needed.
file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/${LIBTORCH_FILENAME}")
file(REMOVE "${CMAKE_CURRENT_BINARY_DIR}/${LIBINTEL_EXT_FILENAME}")
else()
message(STATUS "libtorch already exists in ${PROJECT_SOURCE_DIR}/extern/.")
endif()

endif()

add_subdirectory(source)

Expand Down
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,24 +108,23 @@ conda create -n tiny-dpcpp-nn python=3.10 -y
conda activate tiny-dpcpp-nn
```

Install the latest [ipex](https://intel.github.io/intel-extension-for-pytorch/index.html#installation) via
[Install PyTorch (at the point of writing 2.7)](https://pytorch.org/docs/stable/notes/get_start_xpu.html):

```bash
python -m pip install torch==2.1.0.post2 torchvision==0.16.0.post2 torchaudio==2.1.0.post2 intel-extension-for-pytorch==2.1.30+xpu oneccl_bind_pt==2.1.300+xpu --extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
```
pip install -r requirements.txt
```

Verify that drivers and PyTorch are installed correctly:

*Note* please ensure that the IPEX version (2.1.30 in this example) is the same as used in `IPEX_VERSION` in `tiny-dpcpp-nn/CMakeLists.txt`
```
python -c "import torch; print(torch.xpu.is_available())"

Install the module (if no `TARGET_DEVICE` is set, the target_device in setup.py is set to `ARC`. Currently `PVC` and `ARC` is supported):
```bash
cd dpcpp_bindings
TARGET_DEVICE=ARC pip install -e .
```

Finally, to test the sample scripts and tests, install the requirements:
```bash
cd python && pip install -r requirements.txt
```
### Test the install
To test that the installation was successful, you can do the following four tests.

Expand All @@ -152,7 +151,7 @@ To have all tests, run:
cmake -DTARGET_DEVICE="PVC" -DBUILD_TORCH_TEST="ON" ..
```

After all tests are build into `build/`, you can run `cd build/ && make tests` to verfiy that the setup is correct. Please note that we provide tests for both the core `dpcpp` implementation and the `libtorch` wrapper implementation.
After all tests are build into `build/`, you cvan run `cd build/ && make tests` to verfiy that the setup is correct. Please note that we provide tests for both the core `dpcpp` implementation and the `libtorch` wrapper implementation.

To test whether the pytorch bindings were installed correctly, please run

Expand Down
12 changes: 4 additions & 8 deletions dpcpp_bindings/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
# Include headers

find_package(IntelSYCL REQUIRED)
find_package(Torch REQUIRED PATHS ${TORCH_CMAKE_PREFIX_PATH})
set(CMAKE_PREFIX_PATH "${TORCH_CMAKE_PREFIX_PATH}" CACHE PATH "Path to Torch")
find_package(Torch REQUIRED)
# Add include directories

set(LIBTORCH_DIR "${PROJECT_SOURCE_DIR}/extern/libtorch/share/cmake/")
list(APPEND CMAKE_PREFIX_PATH "${LIBTORCH_DIR}")

find_package(IPEX REQUIRED)

include_directories(${TORCH_INCLUDE_DIRS})
# include_directories(${TORCH_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)
# Set source files for the Python bindings
set(PYBIND_SOURCES
Expand All @@ -24,7 +20,7 @@ pybind11_add_module(tiny_dpcpp_nn_pybind_module ${PYBIND_SOURCES})
find_library(TORCH_PYTHON_LIBRARY torch_python PATHS "${TORCH_CMAKE_PREFIX_PATH}/../../lib" REQUIRED)

# Link any required libraries
target_link_libraries(tiny_dpcpp_nn_pybind_module PRIVATE network ${TORCH_LIBRARIES} ${TORCH_PYTHON_LIBRARY} ${TORCH_IPEX_LIBRARIES})
target_link_libraries(tiny_dpcpp_nn_pybind_module PRIVATE network ${TORCH_LIBRARIES} ${TORCH_PYTHON_LIBRARY})
target_include_directories(tiny_dpcpp_nn_pybind_module PRIVATE "${PROJECT_SOURCE_DIR}/extern/json" "${PROJECT_SOURCE_DIR}/extern/pybind11_json")
add_sycl_to_target(TARGET tiny_dpcpp_nn_pybind_module SOURCES ${PYBIND_SOURCES})

Expand Down
3 changes: 3 additions & 0 deletions dpcpp_bindings/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools>=62.6.0", "wheel", "torch", "ninja"]
build-backend = "setuptools.build_meta"
8 changes: 8 additions & 0 deletions dpcpp_bindings/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--index-url https://download.pytorch.org/whl/xpu
--extra-index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/
torch==2.7.0
torchvision==0.22.0
torchaudio==2.7.0
ninja==1.11.1.1
# intel-extension-for-pytorch==2.7.10+xpu
# oneccl_bind_pt==2.7.0+xpu
54 changes: 10 additions & 44 deletions dpcpp_bindings/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def build_extension(self, ext: CMakeExtension) -> None:
# Set Python_EXECUTABLE instead if you use PYBIND11_FINDPYTHON
# EXAMPLE_VERSION_INFO shows you how to pass a value into the C++ code
# from Python.
target_device = os.environ.get("TARGET_DEVICE", "ARC")
target_device = os.environ.get("TARGET_DEVICE", "PVC")

# Validate TARGET_DEVICE
if target_device not in {"ARC", "PVC"}:
Expand Down Expand Up @@ -73,49 +73,15 @@ def build_extension(self, ext: CMakeExtension) -> None:
# In this example, we pass in the version to C++. You might not need to.
cmake_args += [f"-DEXAMPLE_VERSION_INFO={self.distribution.get_version()}"]

if self.compiler.compiler_type != "msvc":
# Using Ninja-build since it a) is available as a wheel and b)
# multithreads automatically. MSVC would require all variables be
# exported for Ninja to pick it up, which is a little tricky to do.
# Users can override the generator with CMAKE_GENERATOR in CMake
# 3.15+.
if not cmake_generator or cmake_generator == "Ninja":
try:
import ninja

ninja_executable_path = Path(ninja.BIN_DIR) / "ninja"
cmake_args += [
"-GNinja",
f"-DCMAKE_MAKE_PROGRAM:FILEPATH={ninja_executable_path}",
]
except ImportError:
pass

else:
# Single config generators are handled "normally"
single_config = any(x in cmake_generator for x in {"NMake", "Ninja"})

# CMake allows an arch-in-generator style for backward compatibility
contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"})

# Specify the arch if using MSVC generator, but only if it doesn't
# contain a backward-compatibility arch spec already in the
# generator name.
if not single_config and not contains_arch:
cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]]

# Multi-config generators have a different way to specify configs
if not single_config:
cmake_args += [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}"
]
build_args += ["--config", cfg]

if sys.platform.startswith("darwin"):
# Cross-compile support for macOS - respect ARCHFLAGS if set
archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", ""))
if archs:
cmake_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))]
# Using ninja
if not cmake_generator or cmake_generator == "Ninja":
import ninja

ninja_executable_path = Path(ninja.BIN_DIR) / "ninja"
cmake_args += [
"-GNinja",
f"-DCMAKE_MAKE_PROGRAM:FILEPATH={ninja_executable_path}",
]

# Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level
# across all generators.
Expand Down
2 changes: 1 addition & 1 deletion dpcpp_bindings/tiny_dpcpp_nn/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import time

from tiny_dpcpp_nn_pybind_module import (
from .tiny_dpcpp_nn_pybind_module import (
Activation,
create_network,
create_encoding,
Expand Down
18 changes: 18 additions & 0 deletions include/common/tensor_legacy_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <torch/torch.h>

namespace xpu {
namespace dpcpp {

torch::Tensor fromUSM(void *ptr, torch::Dtype dtype, std::vector<long> sizes, bool clone = true) {
const torch::TensorOptions &options =
torch::TensorOptions().dtype(dtype).device(torch::kXPU);
if (clone) {
return torch::from_blob(ptr, sizes, options).clone();
}
else {
return torch::from_blob(ptr, sizes, options);
}
}

} // namespace dpcpp
} // namespace xpu
2 changes: 1 addition & 1 deletion include/encodings/frequency.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class FrequencyEncoding : public Encoding<T> {
const uint32_t outputs_per_input = n_frequencies * 2;

float result = 0.0f;
for (int k = 0; k < outputs_per_input; ++k) {
for (int k = 0; k < int(outputs_per_input); ++k) {
result += (float)dL_doutput(i, j * outputs_per_input + k) * dy_dx[i * n_dims_to_encode * outputs_per_input + j * outputs_per_input + k];
}
loc_dL_dinput(i, j) = result;
Expand Down
2 changes: 1 addition & 1 deletion include/encodings/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void kernel_grid_backward_input(const size_t num_elements,
dL_dx(i, dim) = 0.0f;
}

for (int k = 0; k < num_grid_features; ++k) {
for (int k = 0; k < int(num_grid_features); ++k) {
const float dL_dy_local = (float)dL_dy_rm(i, k);

for (uint32_t dim = 0; dim < N_POS_DIMS; ++dim) {
Expand Down
30 changes: 17 additions & 13 deletions include/network/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,19 @@ template <typename T> class Network : public NetworkBase<T> {

Network(sycl::queue &q, const int n_hidden_layers, const int input_width, const int network_width,
const int output_width, const WeightInitMode mode, const bool use_bias)
: m_q(q), input_width_(PadWidths(input_width, network_width)),
output_width_(PadWidths(output_width, network_width)), original_input_width_(input_width),
original_output_width_(output_width), n_hidden_layers_(NonNegative(n_hidden_layers)),
network_width_(NonNegative(network_width)),
m_weights_matrices(get_n_hidden_layers() + 1, get_input_width(), get_network_width(), get_network_width(),
: m_q(q),
input_width_(PadWidths(input_width, network_width)),
output_width_(PadWidths(output_width, network_width)),
original_output_width_(output_width),
original_input_width_(input_width),
n_hidden_layers_(NonNegative(n_hidden_layers)),
network_width_(NonNegative(network_width)),
use_bias_(use_bias),
m_weights_matrices(get_n_hidden_layers() + 1, get_input_width(), get_network_width(), get_network_width(),
get_network_width(), get_network_width(), get_output_width(), m_q),
m_weightsT_matrices(get_n_hidden_layers() + 1, get_network_width(), get_input_width(), get_network_width(),
get_network_width(), get_output_width(), get_network_width(), m_q),
use_bias_(use_bias) {
m_weightsT_matrices(get_n_hidden_layers() + 1, get_network_width(), get_input_width(), get_network_width(),
get_network_width(), get_output_width(), get_network_width(), m_q)
{

SanityCheck();
initialize_weights_matrices(input_width, output_width, mode);
Expand Down Expand Up @@ -135,14 +139,14 @@ template <typename T> class Network : public NetworkBase<T> {
throw std::runtime_error(errorMessage);
}

if (get_input_width() > network_width_) {
if (int(get_input_width()) > network_width_) {
std::string errorMessage = "Input width of " + std::to_string(get_input_width()) +
" is not supported. Value must be <= network width (" +
std::to_string(network_width_) + ").";
throw std::runtime_error(errorMessage);
}

if (get_output_width() > network_width_) {
if (int(get_output_width()) > network_width_) {
std::string errorMessage = "Input width of " + std::to_string(get_output_width()) +
" is not supported. Value must be <= network width (" +
std::to_string(network_width_) + ").";
Expand All @@ -158,14 +162,14 @@ template <typename T> class Network : public NetworkBase<T> {
if (network_width_ != 16 && network_width_ != 32 && network_width_ != 64 && network_width_ != 128)
throw std::invalid_argument("Network width has to be a power of 2 between 16 and 128.");

if (network_width_ != get_input_width() || network_width_ != get_output_width())
if (network_width_ != int(get_input_width()) || network_width_ != int(get_output_width()))
throw std::invalid_argument("Only networks with same input, layer and output width are allowed.");
}

///@brief Helper function which sets values of the weights matrices to 0 if
/// the actual input/output width was padded to the network-allowed input/output width.
void ZeroWeightsPadding(const int unpadded_input_width, const int unpadded_output_width) {
if (unpadded_input_width > get_input_width() || unpadded_output_width > get_output_width())
if (unpadded_input_width > int(get_input_width()) || unpadded_output_width > int(get_output_width()))
throw std::invalid_argument("Padded weights width cannot be less than the unpadded.");

/// we need to copy everything here since we do not want to have an implicit copy of 'this'
Expand Down Expand Up @@ -213,7 +217,7 @@ template <typename T> class Network : public NetworkBase<T> {
}

// output matrix set columns to 0
const int output_matrix_pos = m_weights_matrices.GetNumberOfMatrices() - 1;
// const int output_matrix_pos = m_weights_matrices.GetNumberOfMatrices() - 1;
if (unpadded_output_width != padded_output_width) {
DeviceMatrixView<T> weights = m_weights_matrices.Back();
m_q.parallel_for(
Expand Down
Loading