#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION) #pragma once #include namespace c10::xpu { /* * XPUEvent are movable not copyable wrappers around SYCL event. XPUEvent are * constructed lazily when first recorded. It has a device, and this device is * acquired from the first recording stream. Later streams that record the event * must match the same device. * * IPC (inter-process communication) for event handles is supported. If * reconstructed from a handle or ipc_handle() is called, the XPUEvent is * initialized eagerly instead of lazily. */ struct XPUEvent { // Constructors XPUEvent(bool enable_timing = false, bool enable_ipc = false) noexcept : enable_timing_{enable_timing}, enable_ipc_{enable_ipc} {} #if SYCL_COMPILER_VERSION >= 20260200 XPUEvent( DeviceIndex device_index, const sycl::ext::oneapi::experimental::ipc::handle_data_t& handle_data) : device_index_(device_index) { #ifdef _WIN32 TORCH_CHECK(false, "XPU IPC events are not supported on Windows."); #endif auto& device = c10::xpu::get_raw_device(device_index); // IPC is only supported for reusable events. reusable_ = device.has(sycl::aspect::ext_oneapi_ipc_event); TORCH_CHECK( reusable_, "XPUEvent reconstructed from an IPC handle must be reusable."); event_ = std::make_unique( sycl::ext::oneapi::experimental::ipc::event::open( handle_data, c10::xpu::get_device_context())); const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace(); if (C10_UNLIKELY(interp)) { (*interp)->trace_gpu_event_creation( c10::kXPU, reinterpret_cast(event_.get())); } } #endif ~XPUEvent() { if (isCreated()) { const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace(); if (C10_UNLIKELY(interp)) { (*interp)->trace_gpu_event_deletion( c10::kXPU, reinterpret_cast(event_.get())); } } } C10_DISABLE_COPY_AND_ASSIGN(XPUEvent); XPUEvent(XPUEvent&& other) = default; XPUEvent& operator=(XPUEvent&& other) = default; operator sycl::event&() const { return event(); } std::optional device() const { if (isCreated()) { return c10::Device(c10::kXPU, device_index_); } else { return std::nullopt; } } inline bool isCreated() const { return (event_.get() != nullptr); } DeviceIndex device_index() const { return device_index_; } sycl::event& event() const { return *event_; } bool query() const { using namespace sycl::info; if (!isCreated()) { return true; } return event().get_info() == event_command_status::complete; } void record() { record(getCurrentXPUStream()); } void recordOnce(const XPUStream& stream) { if (!isCreated()) { record(stream); } } void record(const XPUStream& stream) { const bool first_record = !isCreated(); if (first_record) { createEvent(stream.device_index()); } TORCH_CHECK( device_index_ == stream.device_index(), "Event device ", device_index_, " does not match recording stream's device ", stream.device_index(), "."); if (reusable_) { #if SYCL_COMPILER_VERSION >= 20260200 sycl::ext::oneapi::experimental::enqueue_signal_event( stream.queue(), *event_); #endif } else { assignEvent(stream.queue()); } const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace(); if (C10_UNLIKELY(interp)) { if (first_record) { (*interp)->trace_gpu_event_creation( c10::kXPU, reinterpret_cast(event_.get())); } (*interp)->trace_gpu_event_record( c10::kXPU, reinterpret_cast(event_.get()), reinterpret_cast(&stream.queue())); } } void block(const XPUStream& stream) { if (isCreated()) { if (reusable_) { #if SYCL_COMPILER_VERSION >= 20260200 sycl::ext::oneapi::experimental::enqueue_wait_event( stream.queue(), *event_); #endif } else { std::vector event_list{event()}; // Make this stream wait until event_ is completed. stream.queue().ext_oneapi_submit_barrier(event_list); } const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace(); if (C10_UNLIKELY(interp)) { (*interp)->trace_gpu_event_wait( c10::kXPU, reinterpret_cast(event_.get()), reinterpret_cast(&stream.queue())); } } } double elapsed_time(const XPUEvent& other) const { TORCH_CHECK( isCreated() && other.isCreated(), "Both events must be recorded before calculating elapsed time."); TORCH_CHECK( query() && other.query(), "Both events must be completed before calculating elapsed time."); TORCH_CHECK( enable_timing_ && other.enable_timing_, "Both events must be created with argument 'enable_timing=True'."); using namespace sycl::info::event_profiling; // Block until both of the recorded events are completed. uint64_t end_time_ns = other.event().get_profiling_info(); uint64_t start_time_ns = event().get_profiling_info(); // Return the elapsed time in milliseconds. return 1e-6 * (static_cast(end_time_ns) - static_cast(start_time_ns)); } void synchronize() const { if (isCreated()) { const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace(); if (C10_UNLIKELY(interp)) { (*interp)->trace_gpu_event_synchronization( c10::kXPU, reinterpret_cast(event_.get())); } event().wait_and_throw(); } } #if SYCL_COMPILER_VERSION >= 20260200 sycl::ext::oneapi::experimental::ipc::handle_data_t ipc_handle() { TORCH_CHECK( enable_ipc_, "XPUEvent ipc_handle() requires the event to be constructed with enable_ipc=True."); if (!isCreated()) { createEvent(c10::xpu::current_device()); const c10::impl::PyInterpreter* interp = c10::impl::GPUTrace::get_trace(); if (C10_UNLIKELY(interp)) { (*interp)->trace_gpu_event_creation( c10::kXPU, reinterpret_cast(event_.get())); } } TORCH_CHECK(reusable_, "XPUEvent must be reusable to support IPC."); // Reject re-exporting an event that was itself imported from an IPC handle. TORCH_CHECK( event().ext_oneapi_ipc_enabled(), "XPUEvent ipc_handle() requires the event to be constructed with enable_ipc=True."); return sycl::ext::oneapi::experimental::ipc::event::get(*event_).data(); } #endif private: void assignEvent(sycl::queue& queue) { event_.reset(); if (enable_timing_) { event_ = std::make_unique( sycl::ext::oneapi::experimental::submit_profiling_tag(queue)); } else { event_ = std::make_unique(queue.ext_oneapi_submit_barrier()); } } void createEvent(c10::DeviceIndex device_index) { device_index_ = device_index; TORCH_CHECK( !enable_ipc_ || !enable_timing_, "XPUEvent cannot have both IPC and timing enabled."); #ifdef _WIN32 TORCH_CHECK(!enable_ipc_, "XPU IPC events are not supported on Windows."); #endif #if SYCL_COMPILER_VERSION >= 20260200 if (enable_ipc_) { auto& device = c10::xpu::get_raw_device(device_index_); TORCH_CHECK( device.has(sycl::aspect::ext_oneapi_ipc_event), "Requires the ext_oneapi_ipc_event extension, " "which is not supported on this device. ", "Please upgrade to a newer driver."); } #else TORCH_CHECK( !enable_ipc_, "XPU IPC events require SYCL compiler 2026.2 or later."); #endif // Only IPC-enabled events are backed by a reusable sycl::event, which // requires SYCL compiler 2026.2 or later. Reusable events conflict with // SYCL host_task, so we restrict reusability to IPC events to preserve // backward compatibility. reusable_ = enable_ipc_; #if SYCL_COMPILER_VERSION >= 20260200 if (reusable_) { namespace syclex = sycl::ext::oneapi::experimental; event_ = std::make_unique(syclex::make_event( c10::xpu::get_device_context(), syclex::properties{ syclex::enable_ipc{enable_ipc_}, syclex::enable_profiling{enable_timing_}})); } #endif } bool enable_timing_ = false; bool enable_ipc_ = false; bool reusable_ = false; c10::DeviceIndex device_index_ = -1; std::unique_ptr event_; }; } // namespace c10::xpu #else #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined." #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)