|
| 1 | +""" |
| 2 | + instantiate(graph::HIPGraph)::HIPGraphExec |
| 3 | +
|
| 4 | +Instantiate captured graph making it executable with [`launch`](@ref). |
| 5 | +""" |
| 6 | +instantiate |
| 7 | + |
| 8 | +""" |
| 9 | + capture(f::Function; flags = hipStreamCaptureModeGlobal, throw_error::Bool = true)::Union{Nothing, HIPGraph} |
| 10 | +
|
| 11 | +Capture fiven function `f` to a graph. |
| 12 | +If successful, returns a captured graph that needs to be [`instantiate`](@ref)'d to obtain executable graph. |
| 13 | +""" |
| 14 | +capture |
| 15 | + |
| 16 | +function unchecked_hipStreamEndCapture(stream, pGraph) |
| 17 | + AMDGPU.prepare_state() |
| 18 | + @gcsafe_ccall(libhip.hipStreamEndCapture(stream::hipStream_t, pGraph::Ptr{hipGraph_t})::hipError_t) |
| 19 | +end |
| 20 | + |
| 21 | +mutable struct HIPGraph |
| 22 | + handle::hipGraph_t |
| 23 | + |
| 24 | + function HIPGraph(flags = hipStreamCaptureModeGlobal) |
| 25 | + handle_ref = Ref{hipGraph_t}() |
| 26 | + hipGraphCreate(handle_ref, flags) |
| 27 | + |
| 28 | + obj = new(handle_ref[]) |
| 29 | + finalizer(obj) do obj |
| 30 | + hipGraphDestroy(obj) |
| 31 | + end |
| 32 | + return obj |
| 33 | + end |
| 34 | + |
| 35 | + global function capture(f::Function; flags = hipStreamCaptureModeGlobal, throw_error::Bool = true)::Union{Nothing, HIPGraph} |
| 36 | + gc_state = GC.enable(false) |
| 37 | + stream = AMDGPU.stream() |
| 38 | + try |
| 39 | + hipStreamBeginCapture(stream, flags) |
| 40 | + f() |
| 41 | + finally |
| 42 | + handle_ref = Ref{hipGraph_t}() |
| 43 | + st = unchecked_hipStreamEndCapture(stream, handle_ref) |
| 44 | + GC.enable(gc_state) |
| 45 | + |
| 46 | + if st == hipErrorStreamCaptureInvalidated && !throw_error |
| 47 | + return nothing |
| 48 | + elseif st != hipSuccess |
| 49 | + throw(HIPError(st)) |
| 50 | + end |
| 51 | + |
| 52 | + obj = new(handle_ref[]) |
| 53 | + finalizer(hipGraphDestroy, obj) |
| 54 | + return obj |
| 55 | + end |
| 56 | + return nothing |
| 57 | + end |
| 58 | +end |
| 59 | + |
| 60 | +Base.unsafe_convert(::Type{hipGraph_t}, graph::HIPGraph) = graph.handle |
| 61 | + |
| 62 | +mutable struct HIPGraphExec |
| 63 | + handle::hipGraphExec_t |
| 64 | + |
| 65 | + global function instantiate(graph::HIPGraph) |
| 66 | + handle_ref = Ref{hipGraphExec_t}() |
| 67 | + hipGraphInstantiateWithFlags(handle_ref, graph, 0) |
| 68 | + obj = new(handle_ref[]) |
| 69 | + |
| 70 | + finalizer(obj) do obj |
| 71 | + hipGraphExecDestroy(obj) |
| 72 | + end |
| 73 | + return obj |
| 74 | + end |
| 75 | +end |
| 76 | + |
| 77 | +Base.unsafe_convert(::Type{hipGraphExec_t}, exec::HIPGraphExec) = exec.handle |
| 78 | + |
| 79 | +""" |
| 80 | + launch(exec::HIPGraphExec, stream::HIPStream = AMDGPU.stream()) |
| 81 | +
|
| 82 | +Launch executable graph on a given stream. |
| 83 | +""" |
| 84 | +function launch(exec::HIPGraphExec, stream::HIPStream = AMDGPU.stream()) |
| 85 | + hipGraphLaunch(exec, stream) |
| 86 | +end |
| 87 | + |
| 88 | +""" |
| 89 | + update(exec::HIPGraphExec, graph::HIPGraph; throw_error::Bool = true)::Bool |
| 90 | +
|
| 91 | +Given executable graph, perform update with graph. |
| 92 | +Return `true` if successful, `false` otherwise. |
| 93 | +
|
| 94 | +If `throw_error=false` allows avoiding throwing an exception if update was not successful. |
| 95 | +""" |
| 96 | +function update(exec::HIPGraphExec, graph::HIPGraph; throw_error::Bool = true)::Bool |
| 97 | + error_node = Ref{hipGraphNode_t}() |
| 98 | + update_res_ref = Ref{hipGraphExecUpdateResult}() |
| 99 | + hipGraphExecUpdate(exec, graph, error_node, update_res_ref) |
| 100 | + |
| 101 | + update_res = update_res_ref[] |
| 102 | + if update_res != hipGraphExecUpdateSuccess |
| 103 | + throw_error && error("Failed to update HIPGraphExec: `$(update_res)`.") |
| 104 | + return false |
| 105 | + end |
| 106 | + return true |
| 107 | +end |
| 108 | + |
| 109 | +function capture_status(stream::HIPStream) |
| 110 | + status_ref = Ref{hipStreamCaptureStatus}() |
| 111 | + id_ref = Ref{Culonglong}() |
| 112 | + hipStreamGetCaptureInfo(stream, status_ref, id_ref) |
| 113 | + status = status_ref[] |
| 114 | + return (; status, id=(status == hipStreamCaptureStatusActive) ? id_ref[] : nothing) |
| 115 | +end |
| 116 | + |
| 117 | +""" |
| 118 | + is_capturing(stream::HIPStream = AMDGPU.stream())::Bool |
| 119 | +
|
| 120 | +For a given `stream` check if capturing for a graph is performed. |
| 121 | +""" |
| 122 | +function is_capturing(stream::HIPStream = AMDGPU.stream())::Bool |
| 123 | + capture_status(stream).status == hipStreamCaptureStatusActive |
| 124 | +end |
| 125 | + |
| 126 | +""" |
| 127 | + graph = AMDGPU.@captured begin |
| 128 | + # code to capture in a graph. |
| 129 | + end |
| 130 | +
|
| 131 | +Macro to capture a given expression in a graph & execute it. |
| 132 | +Returns captured graph, that can be relaunched with [`launch`](@ref) or updated with [`update`](@ref). |
| 133 | +
|
| 134 | +If capture fails (e.g. due to JIT), attempts recovery, compilation and re-capture. |
| 135 | +""" |
| 136 | +macro captured(ex) |
| 137 | + quote |
| 138 | + executed = false |
| 139 | + GC.enable(false) |
| 140 | + graph = try |
| 141 | + capture(; throw_error=false) do |
| 142 | + $(esc(ex)) |
| 143 | + end |
| 144 | + finally |
| 145 | + GC.enable(true) |
| 146 | + end |
| 147 | + |
| 148 | + if graph === nothing |
| 149 | + # If the capture failed, this may have been due to JIT compilation. |
| 150 | + # execute the body out of capture, and try capturing again. |
| 151 | + $(esc(ex)) |
| 152 | + |
| 153 | + # Don't tolerate capture failures now so that the user will be informed. |
| 154 | + GC.enable(false) |
| 155 | + graph = try |
| 156 | + capture() do |
| 157 | + $(esc(ex)) |
| 158 | + end |
| 159 | + catch |
| 160 | + rethrow() |
| 161 | + finally |
| 162 | + GC.enable(true) |
| 163 | + end |
| 164 | + executed = true |
| 165 | + end |
| 166 | + |
| 167 | + exec = instantiate(graph) |
| 168 | + executed || launch(exec) |
| 169 | + exec |
| 170 | + end |
| 171 | +end |
0 commit comments