-
-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fixed ncnn input name bug * Refactored ncnn net to cache like ort session
- Loading branch information
1 parent
82c9fc3
commit 0935ddc
Showing
3 changed files
with
52 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from __future__ import annotations | ||
from ncnn_vulkan import ncnn | ||
from weakref import WeakKeyDictionary | ||
|
||
from .exec_options import ExecutionOptions | ||
from .ncnn_model import NcnnModel | ||
|
||
|
||
def create_ncnn_net(model: NcnnModel, exec_options: ExecutionOptions) -> ncnn.Net: | ||
net = ncnn.Net() | ||
|
||
# Use vulkan compute | ||
net.opt.use_vulkan_compute = True | ||
net.set_vulkan_device(exec_options.ncnn_gpu_index) | ||
|
||
# Load model param and bin | ||
net.load_param_mem(model.write_param()) | ||
net.load_model_mem(model.weights_bin) | ||
|
||
# Attribute not needed anymore, save memory | ||
model.weights_bin = b"" | ||
|
||
return net | ||
|
||
|
||
__session_cache: WeakKeyDictionary[NcnnModel, ncnn.Net] = WeakKeyDictionary() | ||
|
||
|
||
def get_ncnn_net(model: NcnnModel, exec_options: ExecutionOptions) -> ncnn.Net: | ||
cached = __session_cache.get(model) | ||
if cached is None: | ||
cached = create_ncnn_net(model, exec_options) | ||
__session_cache[model] = cached | ||
return cached |