diff --git a/clean/FORMATS.md b/clean/FORMATS.md index adc064e3..1f37fb82 100644 --- a/clean/FORMATS.md +++ b/clean/FORMATS.md @@ -467,6 +467,17 @@ see "Context teardown and re-creation"). Other keys go to the network runtime's `k_input=16|f32` (model K's input pass, below); the create flag `WG_CREATE_HDR` reaches it as `WgRtConfig.hdr`; timing log parts then named exposure, input, network, output). +The D3D12 executor makes the network's allocations twice: once to measure the arena they need, in the default arena +(`WG_D3D12_ARENA_MB`, 2560 MiB), then in an arena of exactly that size (executor_d3d12.cpp `prepare_device`). A network +that does not fit the default arena is measured again in a 4095 MiB arena, the largest (logged `the network does not fit +the default arena (...); measuring again in a 4095 MiB arena`, then `measured in the 4095 MiB arena: the network needs N +MiB; the D3D12 executor runs it in an arena of that size` or `measuring again in a 4095 MiB arena failed (...)`); not +when `WG_D3D12_ARENA_MB` is set. Model L at 3840x2160 native needs 2738 MiB and runs on the D3D12 executor this way +(clean/l/records/issue182_create_destroy_record.json; issue #182). If the second measure fails too, the context goes to +the HIP executor (`executor dx12 (default on RDNA 4) could not take this context (...); the HIP executor runs it`), and +after it is destroyed the process keeps HIP's instance and the pooled imported arena until it ends, as "Context +teardown and re-creation" describes (the same record's control: +1419 MiB after every destroy, constant). + ### Shared resources (created at create on the game's device, imported into HIP) Two slots (used alternately by the frames whose network is enqueued). All ten buffers (twelve with `motion_hi`) live in ONE committed buffer, diff --git a/clean/l/records/issue182_create_destroy_record.json b/clean/l/records/issue182_create_destroy_record.json new file mode 100644 index 00000000..87e0db54 --- /dev/null +++ b/clean/l/records/issue182_create_destroy_record.json @@ -0,0 +1,208 @@ +{ + "what": "Issue #182: process GPU memory after an upscaler is created and destroyed (5 cycles, no resize), Model L and Model M at 3840x2160 output, native and Quality, with the D3D12 executor's arena re-measure", + "harness": "clean/fsr/tests/test_dll_d3d12.cpp resize_real (WG_TEST_RESIZE_STEPS=0, WG_TEST_RESIZE_CYCLES=5, settle 800 ms): DXGI QueryVideoMemoryInfo, local segment, this process only; the runtime-reported figure is the front end's FFX_API_QUERY_DESC_TYPE_UPSCALE_GPU_MEMORY_USAGE", + "setup": { + "gpu": "AMD Radeon RX 9070 XT (gfx1201), 16191 MiB dedicated", + "base": "this branch merged with main (merge cabcd4c)", + "runtime": "this branch, clean/runner/build.py --model-l --no-model-k, D3D12 kernels build_dx12.py --model-l (69 kernels)", + "front_end": "clean/fsr with WG_MODEL_L=ON, WG_MODEL_K=OFF (test_dll_d3d12 and its front-end copy)", + "runtime_dll_sha256": "9204b29fa4ba92111235a61473aa0b2f412195aa739a914c957b607c2296ddb0", + "front_end_dll_sha256": "b89ceea05b3dd1de875f366266a0bf88bddc7bf3d307bc24f7b06b252529eb55", + "models": "synthetic files from the repository's generators (Model M: synthetic_model.synthetic_tensors(3) with its Swin weights scaled so every stage takes the clamp-free kernels, as a real model's do; Model L: l_frame_check's synthetic model, seed 89); memory depends on the network's shapes, not on the values", + "gpu_runs": "one at a time, each through tools/gpu_slot.py --cap-s 170" + }, + "runs": [ + { + "run": "l4k-native", + "model": "L", + "output": "3840x2160", + "mode": "native (render 3840x2160)", + "what": "fix", + "cycles": 5, + "process_local_mib_before_any_context": 204, + "process_growth_mib_per_cycle_peak": [ + 2742, + 2742, + 2742, + 2742, + 2742 + ], + "runtime_reported_mib": [ + 2739 + ], + "growth_mib_after_each_destroy": [ + 2, + 2, + 2, + 2, + 2 + ], + "executor_per_cycle": [ + "dx12", + "dx12", + "dx12", + "dx12", + "dx12" + ], + "remeasured_in_4095_mib_arena": 5, + "d3d12_arena_need_mib": [ + 2738 + ], + "hip_fallback_lines": 0, + "pooled_arena_kept_lines": 0, + "test_passed": true + }, + { + "run": "l4k-q", + "model": "L", + "output": "3840x2160", + "mode": "Quality (render 2560x1440)", + "what": "fix", + "cycles": 5, + "process_local_mib_before_any_context": 135, + "process_growth_mib_per_cycle_peak": [ + 1444, + 1444, + 1444, + 1444, + 1444 + ], + "runtime_reported_mib": [ + 1441 + ], + "growth_mib_after_each_destroy": [ + 2, + 2, + 2, + 2, + 2 + ], + "executor_per_cycle": [ + "dx12", + "dx12", + "dx12", + "dx12", + "dx12" + ], + "remeasured_in_4095_mib_arena": 0, + "d3d12_arena_need_mib": [], + "hip_fallback_lines": 0, + "pooled_arena_kept_lines": 0, + "test_passed": true + }, + { + "run": "m4k-native", + "model": "M", + "output": "3840x2160", + "mode": "native (render 3840x2160)", + "what": "fix", + "cycles": 5, + "process_local_mib_before_any_context": 204, + "process_growth_mib_per_cycle_peak": [ + 1593, + 1593, + 1593, + 1593, + 1593 + ], + "runtime_reported_mib": [ + 1590 + ], + "growth_mib_after_each_destroy": [ + 2, + 2, + 2, + 2, + 2 + ], + "executor_per_cycle": [ + "dx12", + "dx12", + "dx12", + "dx12", + "dx12" + ], + "remeasured_in_4095_mib_arena": 0, + "d3d12_arena_need_mib": [], + "hip_fallback_lines": 0, + "pooled_arena_kept_lines": 0, + "test_passed": true + }, + { + "run": "m4k-q", + "model": "M", + "output": "3840x2160", + "mode": "Quality (render 2560x1440)", + "what": "fix", + "cycles": 5, + "process_local_mib_before_any_context": 135, + "process_growth_mib_per_cycle_peak": [ + 936, + 936, + 936, + 936, + 936 + ], + "runtime_reported_mib": [ + 933 + ], + "growth_mib_after_each_destroy": [ + 2, + 2, + 2, + 2, + 2 + ], + "executor_per_cycle": [ + "dx12", + "dx12", + "dx12", + "dx12", + "dx12" + ], + "remeasured_in_4095_mib_arena": 0, + "d3d12_arena_need_mib": [], + "hip_fallback_lines": 0, + "pooled_arena_kept_lines": 0, + "test_passed": true + }, + { + "run": "l4k-native-arena2560", + "model": "L", + "output": "3840x2160", + "mode": "native (render 3840x2160)", + "what": "control: same build, WG_D3D12_ARENA_MB=2560 (no re-measure: the behaviour before this change)", + "cycles": 5, + "process_local_mib_before_any_context": 204, + "process_growth_mib_per_cycle_peak": [ + 4230, + 4230, + 4230, + 4230, + 4230 + ], + "runtime_reported_mib": [ + 3433 + ], + "growth_mib_after_each_destroy": [ + 1419, + 1419, + 1419, + 1419, + 1419 + ], + "executor_per_cycle": [ + "hip", + "hip", + "hip", + "hip", + "hip" + ], + "remeasured_in_4095_mib_arena": 0, + "d3d12_arena_need_mib": [], + "hip_fallback_lines": 5, + "pooled_arena_kept_lines": 5, + "test_passed": true + } + ] +} diff --git a/clean/runtime/interop/executor_d3d12.cpp b/clean/runtime/interop/executor_d3d12.cpp index 4d29adf5..fdc90515 100644 --- a/clean/runtime/interop/executor_d3d12.cpp +++ b/clean/runtime/interop/executor_d3d12.cpp @@ -23,6 +23,7 @@ // into an RG32F texture of ours, which the network then reads as its render-size motion texture. #include #include +#include #include #include #include @@ -39,6 +40,10 @@ namespace wgi { namespace { +// The largest native D3D12 arena (WG_D3D12_ARENA_MB's upper bound, 4095 MiB): the arena that measures a network +// larger than the default one. +constexpr uint64_t kMeasureMaxBytes = uint64_t(4095) << 20; + template void release_ref(T*& p) { if (p) p->Release(); @@ -94,15 +99,44 @@ class D3d12Executor : public Executor { const std::string build = cfg_.build_dir ? cfg_.build_dir : "."; // Pass 1 measures the arena the network needs (the default arena is large); pass 2 makes the same allocations in // an arena of exactly that size. The allocation sequence of runtime_create is deterministic. + // A network larger than the default arena (Model L at 4K native: about 3.4 GiB) is measured again in the largest + // arena (kMeasureMaxBytes, WG_D3D12_ARENA_MB's upper bound) unless WG_D3D12_ARENA_MB is set. Without this the + // context went to the HIP executor, whose process keeps HIP's instance and the pooled imported arena after the + // context is destroyed (clean/FORMATS.md "Context teardown and re-creation"; issue #182). uint64_t need = 0; { + const char* arena_env = std::getenv("WG_D3D12_ARENA_MB"); + const bool may_grow = !arena_env || !*arena_env; std::string e; - std::unique_ptr dev = wgrt::make_d3d12_device_on(device, queue_, build, &e); - if (!dev) return *err = "D3D12 device: " + e, false; - wgrt::Runtime* rt = wgrt::runtime_create(c, std::move(dev), &e, true, true); - if (!rt) return *err = "D3D12 runtime: " + e, false; + wgrt::Runtime* rt = nullptr; + bool grown = false; + const std::string max_mib = std::to_string(kMeasureMaxBytes >> 20); + for (uint64_t arena : {uint64_t(0), kMeasureMaxBytes}) { + e.clear(); + std::unique_ptr dev = wgrt::make_d3d12_device_on(device, queue_, build, &e, arena); + if (!dev) { + if (grown && log_) + log_(true, "measuring again in a " + max_mib + " MiB arena failed (" + e + ")"); + return *err = "D3D12 device: " + e, false; + } + rt = wgrt::runtime_create(c, std::move(dev), &e, true, true); + if (rt || !may_grow || arena || e.find("native D3D12 arena exhausted") == std::string::npos) break; + grown = true; + if (log_) + log_(false, "the network does not fit the default arena (" + e + "); measuring again in a " + + max_mib + " MiB arena"); + } + if (!rt) { + if (grown && log_) + log_(true, "measuring again in a " + max_mib + " MiB arena failed (" + e + ")"); + return *err = "D3D12 runtime: " + e, false; + } need = wgrt::d3d12_ext(wgrt::runtime_device(rt))->arena_high_water(); wgrt::runtime_destroy(rt); + if (grown && log_) + log_(false, "measured in the " + max_mib + " MiB arena: the network needs " + + std::to_string((need + (1u << 20) - 1) >> 20) + " MiB; the D3D12 executor runs it in an arena " + "of that size"); } std::string e; std::unique_ptr dev = wgrt::make_d3d12_device_on(device, queue_, build, &e, need + (1u << 20));