From c2dee0d70f72bb6161f31c3e53470956016eb538 Mon Sep 17 00:00:00 2001 From: Hyeonseo Yang Date: Tue, 31 Dec 2024 13:36:31 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5=20Remove=20opencl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cpp/ipc_apple.h | 3 +- src/cpp/ipc_apple.mm | 19 +----- src/cpp/ipc_apple_opencl.mm | 127 ------------------------------------ src/cpp/ipc_apple_torch.cpp | 4 +- 4 files changed, 4 insertions(+), 149 deletions(-) delete mode 100644 src/cpp/ipc_apple_opencl.mm diff --git a/src/cpp/ipc_apple.h b/src/cpp/ipc_apple.h index 688d5e06..e37fa8af 100644 --- a/src/cpp/ipc_apple.h +++ b/src/cpp/ipc_apple.h @@ -1,8 +1,7 @@ #ifndef __IPC_APPLE_H__ #define __IPC_APPLE_H__ -#define USE_OPENCL_DL_PACK_TENSOR 0 -#define USE_CUSTOM_DL_PACK_TENSOR (HAS_TORCH) +#define USE_CUSTOM_DL_PACK_TENSOR 1 py::object mtl_tensor_from_mach_port(unsigned int machPort, int width, int height); diff --git a/src/cpp/ipc_apple.mm b/src/cpp/ipc_apple.mm index e666c72e..1a5c4097 100644 --- a/src/cpp/ipc_apple.mm +++ b/src/cpp/ipc_apple.mm @@ -6,8 +6,6 @@ namespace py = pybind11; #include "dlpack.h" #include "ipc_apple.h" -#include -#include #include #include #include @@ -17,6 +15,8 @@ // Sad news: PyTorch does not support from_dlpack for Metal tensors. // Therefore, we should create a OpenCL dlpack tensor from the IOSurface. +// However OpenCL is not supported in PyTorch! +// Use custom dlpack tensor for now. IOSurfaceRef getIOSurfaceFromMachPort(mach_port_t machPort) { mach_port_type_t portType; @@ -136,20 +136,6 @@ static void deleteDLManagedTensor(DLManagedTensor *self) { ); } -#if USE_OPENCL_DL_PACK_TENSOR - cl_context context = createOpenCLContext(); - DLManagedTensor *tensor = - createDLPackTensorFromOpenCL(context, ioSurface, width, height); - return py::reinterpret_steal(PyCapsule_New( - tensor, - "dltensor", - [](PyObject *capsule) { - DLManagedTensor *tensor = - (DLManagedTensor *)PyCapsule_GetPointer(capsule, "dltensor"); - tensor->deleter(tensor); - } - )); -#else DLManagedTensor *tensor = createDLPackTensorMetal(mtlBuffer, width, height); #if USE_CUSTOM_DL_PACK_TENSOR @@ -165,5 +151,4 @@ static void deleteDLManagedTensor(DLManagedTensor *self) { } )); #endif -#endif } diff --git a/src/cpp/ipc_apple_opencl.mm b/src/cpp/ipc_apple_opencl.mm deleted file mode 100644 index 0ec5c2b1..00000000 --- a/src/cpp/ipc_apple_opencl.mm +++ /dev/null @@ -1,127 +0,0 @@ -// Sad news: PyTorch does not support from_dlpack for Metal tensors. -// Therefore, we should create a OpenCL dlpack tensor from the IOSurface. -#include "ipc_apple.h" -#if USE_OPENCL_DL_PACK_TENSOR -#include -#include -#import -#define GL_SILENCE_DEPRECATION - -#include -#include -#include -#include -#include -#include -#include -#include - -static bool initializedOpenCL = false; - -static cl_context createOpenCLContext() { - if (initializedOpenCL) { - throw std::runtime_error("OpenCL context is already initialized."); - } - - cl_int err; - - // 1. Check available platforms - cl_uint numPlatforms; - err = clGetPlatformIDs(0, nullptr, &numPlatforms); - if (err != CL_SUCCESS || numPlatforms == 0) { - throw std::runtime_error("Failed to find any OpenCL platforms."); - } - - std::vector platforms(numPlatforms); - err = clGetPlatformIDs(numPlatforms, platforms.data(), nullptr); - if (err != CL_SUCCESS) { - throw std::runtime_error("Failed to get OpenCL platform IDs."); - } - - // 2. Check available devices, select the first GPU device - cl_platform_id selectedPlatform = platforms[0]; // select the first platform - cl_uint numDevices; - err = clGetDeviceIDs( - selectedPlatform, CL_DEVICE_TYPE_GPU, 0, nullptr, &numDevices - ); - if (err != CL_SUCCESS || numDevices == 0) { - throw std::runtime_error("Failed to find any OpenCL devices."); - } - - std::vector devices(numDevices); - err = clGetDeviceIDs( - selectedPlatform, - CL_DEVICE_TYPE_GPU, - numDevices, - devices.data(), - nullptr - ); - if (err != CL_SUCCESS) { - throw std::runtime_error("Failed to get OpenCL device IDs."); - } - - // 3. Create OpenCL context - cl_context_properties properties[] = { - CL_CONTEXT_PLATFORM, (cl_context_properties)selectedPlatform, 0, - }; - - cl_context context = - clCreateContext(properties, 1, &devices[0], nullptr, nullptr, &err); - if (err != CL_SUCCESS) { - throw std::runtime_error("Failed to create OpenCL context."); - } - initializedOpenCL = true; - return context; -} - -DLManagedTensor *createDLPackTensorFromOpenCL( - cl_context context, IOSurfaceRef ioSurface, size_t width, size_t height -) { - // create opencl image - cl_image_format format = { - CL_RGBA, // rgba channel order - CL_UNORM_INT8 // 8-bit unsigned normalized integer - }; - - cl_int errcode; - cl_mem clBuffer = clCreateImageFromIOSurface2DAPPLE( - context, CL_MEM_READ_WRITE, &format, width, height, ioSurface, &errcode - ); - - if (!clBuffer || errcode != CL_SUCCESS) { - throw std::runtime_error( - "Failed to create OpenCL image from IOSurface: " + - std::to_string(errcode) - ); - } - - DLManagedTensor *tensor = - (DLManagedTensor *)malloc(sizeof(DLManagedTensor)); - - tensor->dl_tensor.data = - reinterpret_cast(clBuffer); // set cl_mem as data - tensor->dl_tensor.ndim = 3; // 3D tensor (H x W x C) - tensor->dl_tensor.shape = (int64_t *)malloc(3 * sizeof(int64_t)); - tensor->dl_tensor.shape[0] = height; - tensor->dl_tensor.shape[1] = width; - tensor->dl_tensor.shape[2] = 4; // RGBA - tensor->dl_tensor.strides = nullptr; // Dense layout - tensor->dl_tensor.byte_offset = 0; - - tensor->dl_tensor.dtype = {kDLUInt, 8, 1}; // Unsigned 8-bit integer - tensor->dl_tensor.device = {kDLOpenCL, 0}; // OpenCL device - - // Set memory deleter - tensor->manager_ctx = new cl_mem{clBuffer}; // cl_mem context - tensor->deleter = [](DLManagedTensor *self) { - cl_mem *buffer = reinterpret_cast(self->manager_ctx); - clReleaseMemObject(*buffer); // OpenCL release - delete buffer; - - free(self->dl_tensor.shape); // release shape - free(self); // DLManagedTensor release - }; - - return tensor; -} -#endif \ No newline at end of file diff --git a/src/cpp/ipc_apple_torch.cpp b/src/cpp/ipc_apple_torch.cpp index 66253354..0d8caf4b 100644 --- a/src/cpp/ipc_apple_torch.cpp +++ b/src/cpp/ipc_apple_torch.cpp @@ -1,8 +1,6 @@ -#include "dlpack.h" -#include "ipc_apple.h" #include -#include #include +#include "ipc_apple.h" #if USE_CUSTOM_DL_PACK_TENSOR at::Tensor