| 1 | ## supported platform |
| 2 | |
| 3 | * Y = known work |
| 4 | * ? = shall work, not confirmed |
| 5 | * / = not applied |
| 6 | |
| 7 | | |windows|linux|android|mac|ios| |
| 8 | |---|---|---|---|---|---| |
| 9 | |intel|Y|Y|?|?|/| |
| 10 | |amd|Y|Y|/|?|/| |
| 11 | |nvidia|Y|Y|?|/|/| |
| 12 | |qcom|/|/|Y|/|/| |
| 13 | |apple|/|/|/|?|Y| |
| 14 | |arm|/|?|?|/|/| |
| 15 | |
| 16 | ## enable vulkan compute support |
| 17 | ``` |
| 18 | $ sudo dnf install vulkan-devel |
| 19 | $ cmake -DNCNN_VULKAN=ON .. |
| 20 | ``` |
| 21 | |
| 22 | ## enable vulkan compute inference |
| 23 | ```cpp |
| 24 | ncnn::Net net; |
| 25 | net.opt.use_vulkan_compute = 1; |
| 26 | ``` |
| 27 | |
| 28 | ## proper allocator usage |
| 29 | ```cpp |
| 30 | ncnn::VkAllocator* blob_vkallocator = vkdev.acquire_blob_allocator(); |
| 31 | ncnn::VkAllocator* staging_vkallocator = vkdev.acquire_blob_allocator(); |
| 32 | |
| 33 | net.opt.blob_vkallocator = blob_vkallocator; |
| 34 | net.opt.workspace_vkallocator = blob_vkallocator; |
| 35 | net.opt.staging_vkallocator = staging_vkallocator; |
| 36 | |
| 37 | // .... |
| 38 | |
| 39 | // after inference |
| 40 | vkdev.reclaim_blob_allocator(blob_vkallocator); |
| 41 | vkdev.reclaim_staging_allocator(staging_vkallocator); |
| 42 | ``` |
| 43 | |
| 44 | ## select gpu device |
| 45 | ```cpp |
| 46 | // get gpu count |
| 47 | int gpu_count = ncnn::get_gpu_count(); |
| 48 | |
| 49 | // set specified vulkan device before loading param and model |
| 50 | net.set_vulkan_device(0); // use device-0 |
| 51 | net.set_vulkan_device(1); // use device-1 |
| 52 | ``` |
| 53 | |
| 54 | ## zero-copy on unified memory device |
| 55 | ```cpp |
| 56 | ncnn::VkMat blob_gpu; |
| 57 | ncnn::Mat mapped = blob_gpu.mapped(); |
| 58 | |
| 59 | // use mapped.data directly |
| 60 | ``` |
| 61 | |
| 62 | ## hybrid cpu/gpu inference |
| 63 | ```cpp |
| 64 | ncnn::Extractor ex_cpu = net.create_extractor(); |
| 65 | ncnn::Extractor ex_gpu = net.create_extractor(); |
| 66 | ex_cpu.set_vulkan_compute(false); |
| 67 | ex_gpu.set_vulkan_compute(true); |
| 68 | |
| 69 | #pragma omp parallel sections |
| 70 | { |
| 71 | #pragma omp section |
| 72 | { |
| 73 | ex_cpu.input(); |
| 74 | ex_cpu.extract(); |
| 75 | } |
| 76 | #pragma omp section |
| 77 | { |
| 78 | ex_gpu.input(); |
| 79 | ex_gpu.extract(); |
| 80 | } |
| 81 | } |
| 82 | ``` |
| 83 | |
| 84 | ## zero-copy gpu inference chaining |
| 85 | ```cpp |
| 86 | ncnn::Extractor ex1 = net1.create_extractor(); |
| 87 | ncnn::Extractor ex2 = net2.create_extractor(); |
| 88 | |
| 89 | ncnn::VkCompute cmd(&vkdev); |
| 90 | |
| 91 | ncnn::VkMat conv1; |
| 92 | ncnn::VkMat conv2; |
| 93 | ncnn::VkMat conv3; |
| 94 | |
| 95 | ex1.input("conv1", conv1); |
| 96 | ex1.extract("conv2", conv2, cmd); |
| 97 | |
| 98 | ex2.input("conv2", conv2); |
| 99 | ex2.extract("conv3", conv3, cmd); |
| 100 | |
| 101 | cmd.submit(); |
| 102 | |
| 103 | cmd.wait(); |
| 104 | |
| 105 | ``` |
| 106 | |
| 107 | ## batch inference |
| 108 | ```cpp |
| 109 | int max_batch_size = vkdev->info.compute_queue_count(); |
| 110 | |
| 111 | ncnn::Mat inputs[1000]; |
| 112 | ncnn::Mat outputs[1000]; |
| 113 | |
| 114 | #pragma omp parallel for num_threads(max_batch_size) |
| 115 | for (int i=0; i<1000; i++) |
| 116 | { |
| 117 | ncnn::Extractor ex = net1.create_extractor(); |
| 118 | ex.input("data", inputs[i]); |
| 119 | ex.extract("prob", outputs[i]); |
| 120 | } |
| 121 | ``` |
| 122 | |
| 123 | ## control storage and arithmetic precision |
| 124 | |
| 125 | disable all lower-precision optimizations, get full fp32 precision |
| 126 | |
| 127 | ```cpp |
| 128 | ncnn::Net net; |
| 129 | net.opt.use_fp16_packed = false; |
| 130 | net.opt.use_fp16_storage = false; |
| 131 | net.opt.use_fp16_arithmetic = false; |
| 132 | net.opt.use_int8_storage = false; |
| 133 | net.opt.use_int8_arithmetic = false; |
| 134 | ``` |
| 135 | |
| 136 | ## debugging tips |
| 137 | ```cpp |
| 138 | #define ENABLE_VALIDATION_LAYER 1 // modify to 1 in gpu.cpp |
| 139 | ``` |
| 140 | |
| 141 | ## add vulkan compute support to layer |
| 142 | 1. add vulkan shader in src/layer/shader/ |
| 143 | |
| 144 | 2. upload model weight data in Layer::upload_model() |
| 145 | |
| 146 | 3. setup pipeline in Layer::create_pipeline() |
| 147 | |
| 148 | 4. destroy pipeline in Layer::destroy_pipeline() |
| 149 | |
| 150 | 5. record command in Layer::forward() |
| 151 | |
| 152 | ## add optimized shader path |
| 153 | 1. add vulkan shader in src/layer/shader/ named XXX_abc.comp |
| 154 | |
| 155 | 2. create pipeline with "XXX_abc" |
| 156 | |
| 157 | 3. record command using XXX_abc pipeline |
| 158 | |
| 159 | ## low-level op api |
| 160 | 1. create layer |
| 161 | |
| 162 | 2. load param and load model |
| 163 | |
| 164 | 3. upload model |
| 165 | |
| 166 | 4. create pipeline |
| 167 | |
| 168 | 5. new command |
| 169 | |
| 170 | 6. record |
| 171 | |
| 172 | 7. submit and wait |
| 173 | |