| 1 | /* Copyright (c) 2025, Intel Corporation |
| 2 | * |
| 3 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | * copy of this software and associated documentation files (the "Software"), |
| 5 | * to deal in the Software without restriction, including without limitation |
| 6 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 | * and/or sell copies of the Software, and to permit persons to whom the |
| 8 | * Software is furnished to do so, subject to the following conditions: |
| 9 | * |
| 10 | * The above copyright notice and this permission notice shall be included |
| 11 | * in all copies or substantial portions of the Software. |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 17 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 18 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 19 | * OTHER DEALINGS IN THE SOFTWARE. |
| 20 | */ |
| 21 | //! |
| 22 | //! \file vp_graphset.cpp |
| 23 | //! \brief The header file of the base class of graph set |
| 24 | //! \details The graph set will include graph generation from binary. |
| 25 | //! It's responsible for setting up HW states and generating the SFC |
| 26 | //! commands. |
| 27 | //! |
| 28 | #include "vp_graph_manager.h" |
| 29 | #include "levelzero_npu_interface.h" |
| 30 | #include "vp_utils.h" |
| 31 | |
| 32 | using namespace vp; |
| 33 | |
| 34 | VpGraphManager::VpGraphManager(VpGraphSet *graphset, PMOS_INTERFACE osInterface, VpAllocator *allocator) : |
| 35 | m_graphset(graphset), m_osInterface(osInterface), m_allocator(allocator) |
| 36 | { |
| 37 | if (osInterface) |
| 38 | { |
| 39 | m_npuInterface = m_osInterface->npuInterface; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | MOS_STATUS VpGraphManager::GetGraphPackage(std::vector<AI_SINGLE_NPU_GRAPH_SETTING> &graphSettings, SwFilterPipe &executingPipe, GraphPackage *&graphPackage) |
| 44 | { |
| 45 | VP_PUBLIC_CHK_NULL_RETURN(m_graphset); |
| 46 | VP_PUBLIC_CHK_NULL_RETURN(m_npuInterface); |
| 47 | VP_PUBLIC_CHK_NULL_RETURN(m_allocator); |
| 48 | VP_PUBLIC_CHK_NULL_RETURN(m_npuInterface->pfnCreateFence); |
| 49 | VP_PUBLIC_CHK_NULL_RETURN(m_npuInterface->pfnCreateCmdList); |
| 50 | |
| 51 | VpGraphIDList graphIDs = {}; |
| 52 | for (AI_SINGLE_NPU_GRAPH_SETTING &graphSetting : graphSettings) |
| 53 | { |
| 54 | graphIDs.push_back(graphSetting.id); |
| 55 | } |
| 56 | auto handle = m_graphPackages.find(graphIDs); |
| 57 | if (handle == m_graphPackages.end()) |
| 58 | { |
| 59 | //Graph Package cannot be copied for it contains non-copiable member, so use piecewise_construct to create it directly in m_graphPackages |
| 60 | handle = m_graphPackages.emplace(std::piecewise_construct, std::forward_as_tuple(graphIDs), std::forward_as_tuple()).first; |
| 61 | VP_PUBLIC_CHK_NOT_FOUND_RETURN(handle, &m_graphPackages); |
| 62 | GraphPackage &package = handle->second; |
| 63 | |
| 64 | VP_PUBLIC_CHK_STATUS_RETURN(m_npuInterface->pfnCreateFence(m_npuInterface, package.GetNpuWLContext().Fence())); |
| 65 | for (AI_SINGLE_NPU_GRAPH_SETTING &graphSetting : graphSettings) |
| 66 | { |
| 67 | AI_NPU_BUFFER_ALLOCATION_MAP allocationMap = {}; |
| 68 | VP_PUBLIC_CHK_NULL_RETURN(graphSetting.pfnGetIntermediateSurfaceSetting); |
| 69 | VP_PUBLIC_CHK_STATUS_RETURN(graphSetting.pfnGetIntermediateSurfaceSetting(executingPipe, allocationMap)); |
| 70 | for (const auto &pair : allocationMap) |
| 71 | { |
| 72 | PVP_SURFACE surface = nullptr; |
| 73 | bool allocated = false; |
| 74 | VP_PUBLIC_CHK_STATUS_RETURN(m_allocator->ReAllocateNpuBuffer(surface, pair.second.surfaceName.c_str(), pair.second.size, allocated)); |
| 75 | package.IntermiedateSurfaces().insert(std::make_pair(pair.first, surface)); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | InitGraphListCmdPackage cmdPackage(m_npuInterface, &package); |
| 80 | for (AI_SINGLE_NPU_GRAPH_SETTING &graphSetting : graphSettings) |
| 81 | { |
| 82 | ze_command_list_handle_t cmdList = nullptr; |
| 83 | VP_PUBLIC_CHK_STATUS_RETURN(m_npuInterface->pfnCreateCmdList(m_npuInterface, cmdList)); |
| 84 | package.GetNpuWLContext().CmdList().push_back(cmdList); |
| 85 | |
| 86 | GraphHandle *graphHandle = nullptr; |
| 87 | VP_PUBLIC_CHK_STATUS_RETURN(m_graphset->GetGraphObject(graphSetting.id, graphHandle)); |
| 88 | VP_PUBLIC_CHK_NULL_RETURN(graphHandle); |
| 89 | |
| 90 | std::vector<void *> graphArgs = {}; |
| 91 | for (auto& arg : graphSetting.graphArgs) |
| 92 | { |
| 93 | auto argHandle = package.IntermiedateSurfaces().find(arg); |
| 94 | VP_PUBLIC_CHK_NOT_FOUND_RETURN(argHandle, &package.IntermiedateSurfaces()); |
| 95 | VP_PUBLIC_CHK_NULL_RETURN(argHandle->second); |
| 96 | VP_PUBLIC_CHK_NULL_RETURN(argHandle->second->zeNpuHostMem); |
| 97 | graphArgs.push_back(argHandle->second->zeNpuHostMem); |
| 98 | } |
| 99 | |
| 100 | cmdPackage.AddSingleLayerGraphArgs(graphArgs); |
| 101 | cmdPackage.AddGraphHandle(graphHandle); |
| 102 | } |
| 103 | // InitGraphListCmdPackage Submit() is doing GraphPackage initilization, the real graph execution submit is in NpuCmdPackageSpecific |
| 104 | VP_PUBLIC_CHK_STATUS_RETURN(m_osInterface->pfnSubmitPackage(m_osInterface, cmdPackage)) |
| 105 | } |
| 106 | graphPackage = &handle->second; |
| 107 | |
| 108 | return MOS_STATUS_SUCCESS; |
| 109 | } |
| 110 | |
| 111 | MOS_STATUS VpGraphManager::Destroy() |
| 112 | { |
| 113 | VP_PUBLIC_CHK_NULL_RETURN(m_allocator); |
| 114 | VP_PUBLIC_CHK_NULL_RETURN(m_npuInterface); |
| 115 | VP_PUBLIC_CHK_NULL_RETURN(m_npuInterface->pfnDestroyFence); |
| 116 | VP_PUBLIC_CHK_NULL_RETURN(m_npuInterface->pfnDestroyCmdList); |
| 117 | |
| 118 | for (auto& handle : m_graphPackages) |
| 119 | { |
| 120 | { |
| 121 | NpuWLContext &npuWLContext = handle.second.GetNpuWLContext(); |
| 122 | std::lock_guard<std::mutex> lock(npuWLContext.Mutex()); |
| 123 | npuWLContext.Initialized() = false; |
| 124 | if (npuWLContext.Thread().joinable()) |
| 125 | { |
| 126 | npuWLContext.Thread().join(); |
| 127 | } |
| 128 | if (MOS_STATUS_SUCCESS != m_npuInterface->pfnDestroyFence(m_npuInterface, npuWLContext.Fence())) |
| 129 | { |
| 130 | VP_PUBLIC_ASSERTMESSAGE("Destroy Fence fail"); |
| 131 | } |
| 132 | for (ze_command_list_handle_t &cmdList : npuWLContext.CmdList()) |
| 133 | { |
| 134 | if (MOS_STATUS_SUCCESS != m_npuInterface->pfnDestroyCmdList(m_npuInterface, cmdList)) |
| 135 | { |
| 136 | VP_PUBLIC_ASSERTMESSAGE("Destroy Npu Cmd List fail"); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | for (const auto &surfaceHandle : handle.second.IntermiedateSurfaces()) |
| 142 | { |
| 143 | PVP_SURFACE surface = surfaceHandle.second; |
| 144 | if (MOS_STATUS_SUCCESS != m_allocator->DestroyNpuBuffer(surface)) |
| 145 | { |
| 146 | VP_PUBLIC_ASSERTMESSAGE("Destroy Npu Buffer fail"); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | m_graphPackages.clear(); |
| 151 | |
| 152 | return MOS_STATUS_SUCCESS; |
| 153 | } |
| 154 | |
| 155 | MOS_STATUS InitGraphListCmdPackage::Submit() |
| 156 | { |
| 157 | VP_PUBLIC_CHK_NULL_RETURN(m_graphPackage); |
| 158 | VP_PUBLIC_CHK_NULL_RETURN(m_npuInterface); |
| 159 | VP_PUBLIC_CHK_NULL_RETURN(m_npuInterface->pfnAppendGraph); |
| 160 | |
| 161 | m_graphPackage->GetNpuWLContext().Thread() = std::thread([this]() { |
| 162 | MOS_STATUS status = MOS_STATUS_SUCCESS; |
| 163 | do |
| 164 | { |
| 165 | //waiting for all graph handles created successfully from IR/BLOB |
| 166 | for (auto &graphHandle : m_graphHandles) |
| 167 | { |
| 168 | if (graphHandle == nullptr) |
| 169 | { |
| 170 | status = MOS_STATUS_NULL_POINTER; |
| 171 | VP_PUBLIC_ASSERTMESSAGE("Graph Handle is nullptr"); |
| 172 | break; |
| 173 | } |
| 174 | std::unique_lock<std::mutex> graphHandleLock(graphHandle->Mutex()); |
| 175 | if (graphHandle->Condition().wait_for(graphHandleLock, std::chrono::milliseconds(graphHandle->GetTimeoutMs()), [=] { return graphHandle->Initialized() || graphHandle->Failed(); })) |
| 176 | { |
| 177 | if (graphHandle->Failed()) |
| 178 | { |
| 179 | status = MOS_STATUS_UNINITIALIZED; |
| 180 | VP_PUBLIC_ASSERTMESSAGE("Graph Handle initialize failed, cannot continue to initialize Graph Package"); |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | status = MOS_STATUS_UNINITIALIZED; |
| 187 | VP_PUBLIC_ASSERTMESSAGE("Timeout %d ms reached, Graph Handle still not finish Initialization", graphHandle->GetTimeoutMs()); |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | if (status != MOS_STATUS_SUCCESS) |
| 192 | { |
| 193 | break; |
| 194 | } |
| 195 | // binding resources and graph handles into cmd list |
| 196 | for (uint32_t i = 0; i < m_graphPackage->GetNpuWLContext().CmdList().size(); ++i) |
| 197 | { |
| 198 | if (m_multiLayerGraphArgs.size() <= i) |
| 199 | { |
| 200 | status = MOS_STATUS_INVALID_PARAMETER; |
| 201 | VP_PUBLIC_ASSERTMESSAGE("Graph Arguments in multiLayerGraphArgs is not enough"); |
| 202 | break; |
| 203 | } |
| 204 | if (m_graphHandles.size() <= i) |
| 205 | { |
| 206 | status = MOS_STATUS_INVALID_PARAMETER; |
| 207 | VP_PUBLIC_ASSERTMESSAGE("Graph Handle in graphHandles is not enough"); |
| 208 | break; |
| 209 | } |
| 210 | status = m_npuInterface->pfnAppendGraph(m_npuInterface, m_graphPackage->GetNpuWLContext().CmdList().at(i), m_graphHandles.at(i)->Graph(), m_multiLayerGraphArgs.at(i)); |
| 211 | if (status != MOS_STATUS_SUCCESS) |
| 212 | { |
| 213 | VP_PUBLIC_ASSERTMESSAGE("Initialize Graph Package failed %d", status); |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | } while (false); |
| 218 | |
| 219 | { |
| 220 | std::lock_guard<std::mutex> lock(m_graphPackage->GetNpuWLContext().Mutex()); |
| 221 | if (status == MOS_STATUS_SUCCESS) |
| 222 | { |
| 223 | m_graphPackage->GetNpuWLContext().Initialized() = true; |
| 224 | m_graphPackage->GetNpuWLContext().Failed() = false; |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | m_graphPackage->GetNpuWLContext().Failed() = true; |
| 229 | m_graphPackage->GetNpuWLContext().Initialized() = false; |
| 230 | } |
| 231 | } |
| 232 | m_graphPackage->GetNpuWLContext().Condition().notify_all(); |
| 233 | m_releaseable = true; |
| 234 | }); |
| 235 | |
| 236 | return MOS_STATUS_SUCCESS; |
| 237 | } |
| 238 | |
| 239 | std::unique_ptr<CmdPackage> InitGraphListCmdPackage::Clone() const |
| 240 | { |
| 241 | return std::make_unique<InitGraphListCmdPackage>(*this); |
| 242 | } |