Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Convert models to formats supported by mace/ncnn via pnnx #1

Merged
merged 19 commits into from
Sep 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 8 additions & 131 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,132 +1,9 @@
# find src/ tools/ tests/ examples/ benchmark/ -type f -name '*.c' -o -name '*.cpp' -o -name '*.h' | xargs -i clang-format -i {}

# need clang-format >= 10.0

AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
# AlignConsecutiveBitFields: true
AlignConsecutiveDeclarations: false
AlignConsecutiveMacros: true
AlignEscapedNewlines: Left
# AlignOperands: AlignAfterOperator
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: true
# AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: true
AfterUnion: true
AfterExternBlock: false
BeforeCatch: true
BeforeElse: true
# BeforeLambdaBody: false
# BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: false
BreakAfterJavaFieldAnnotations: true
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakStringLiterals: false
ColumnLimit: 0
# CommentPragmas:
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: false
---
BasedOnStyle: Google
---
Language: Cpp
Cpp11BracedListStyle: true
Standard: Cpp11
DerivePointerAlignment: false
# DisableFormat:
# ExperimentalAutoDetectBinPacking:
FixNamespaceComments: true
# ForEachMacros:
IncludeBlocks: Regroup
# IncludeCategories:
# IncludeIsMainRegex:
# IncludeIsMainSourceRegex:
# IndentCaseBlocks: false
IndentCaseLabels: false
# IndentExternBlock: NoIndent
IndentGotoLabels: false
IndentPPDirectives: None
IndentWidth: 4
# IndentWrappedFunctionNames: 4
# InsertTrailingCommas: None
# JavaImportGroups:
# JavaScriptQuotes
# JavaScriptWrapImports:
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
# MacroBlockBegin:
# MacroBlockEnd:
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
# NamespaceMacros:
# ObjCBinPackProtocolList:
# ObjCBlockIndentWidth:
# ObjCBreakBeforeNestedBlockParam:
# ObjCSpaceAfterProperty:
# ObjCSpaceBeforeProtocolList:
# PenaltyBreakAssignment:
# PenaltyBreakBeforeFirstCallParameter:
# PenaltyBreakComment:
# PenaltyBreakFirstLessLess:
# PenaltyBreakString:
# PenaltyBreakTemplateDeclaration:
# PenaltyExcessCharacter:
# PenaltyReturnTypeOnItsOwnLine:
PointerAlignment: Left
# RawStringFormats:
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: c++03
#StatementMacros:
TabWidth: 4
# TypenameMacros:
UseCRLF: false
UseTab: Never
PointerAlignment: Right
---
4 changes: 2 additions & 2 deletions .github/workflows/release-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ jobs:
brew uninstall --ignore-dependencies libomp

- name: Build wheels
uses: pypa/cibuildwheel@v2.8.1
uses: pypa/cibuildwheel@v2.9.0
env:
CIBW_ARCHS_MACOS: x86_64 universal2 arm64
CIBW_ARCHS_LINUX: x86_64 i686
Expand Down Expand Up @@ -103,7 +103,7 @@ jobs:
platforms: all

- name: Build wheels
uses: pypa/cibuildwheel@v2.8.1
uses: pypa/cibuildwheel@v2.9.0
env:
CIBW_ARCHS_LINUX: ${{ matrix.arch }}
CIBW_BUILD: ${{ matrix.build }}
Expand Down
48 changes: 48 additions & 0 deletions benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4322,3 +4322,51 @@ cooling_down = 0
yolo-fastestv2 min = 3.33 max = 3.73 avg = 3.44
vision_transformer min = 214.91 max = 229.91 avg = 220.82
```

### Intel Celeron M 420 (Yonah 1.60 GHz x 1)

Tested on `Debian GNU/Linux 11 (bullseye) i686` with `cmake -DNCNN_RUNTIME_CPU=OFF -DNCNN_AVX=OFF -DNCNN_AVX2=OFF -DNCNN_AVX512=OFF-DNCNN_BUILD_TESTS=ON ..`.

```
mouri@Mouri-Laptop-2:~/ncnn/benchmark$ ./../build/benchmark/benchncnn
loop_count = 4
num_threads = 1
powersave = 0
gpu_device = -1
cooling_down = 1
squeezenet min = 289.23 max = 301.83 avg = 292.90
squeezenet_int8 min = 442.82 max = 457.21 avg = 446.89
mobilenet min = 549.62 max = 561.20 avg = 554.78
mobilenet_int8 min = 823.92 max = 837.70 avg = 830.52
mobilenet_v2 min = 341.72 max = 353.77 avg = 345.34
mobilenet_v3 min = 267.68 max = 282.08 avg = 273.10
shufflenet min = 151.66 max = 153.02 avg = 152.24
shufflenet_v2 min = 161.54 max = 163.38 avg = 162.13
mnasnet min = 322.66 max = 336.91 avg = 326.86
proxylessnasnet min = 356.63 max = 368.79 avg = 360.66
efficientnet_b0 min = 489.92 max = 505.11 avg = 497.32
efficientnetv2_b0 min = 618.16 max = 632.02 avg = 622.82
regnety_400m min = 414.83 max = 428.42 avg = 419.28
blazeface min = 38.56 max = 40.05 avg = 39.05
googlenet min = 1022.54 max = 1037.53 avg = 1029.48
googlenet_int8 min = 1493.35 max = 1495.46 avg = 1494.31
resnet18 min = 803.32 max = 818.27 avg = 812.49
resnet18_int8 min = 1188.26 max = 1200.88 avg = 1192.56
alexnet min = 613.78 max = 623.88 avg = 619.99
vgg16 min = 4465.44 max = 4478.12 avg = 4474.16
vgg16_int8 min = 6042.40 max = 6114.37 avg = 6077.07
resnet50 min = 2517.75 max = 2528.42 avg = 2522.83
resnet50_int8 min = 3746.28 max = 3771.09 avg = 3756.88
squeezenet_ssd min = 585.56 max = 636.01 avg = 602.62
squeezenet_ssd_int8 min = 822.43 max = 968.77 avg = 862.33
mobilenet_ssd min = 1116.98 max = 1139.17 avg = 1127.65
mobilenet_ssd_int8 min = 1665.03 max = 1670.55 avg = 1668.37
mobilenet_yolo min = 2638.61 max = 2666.54 avg = 2652.26
mobilenetv2_yolov3 min = 1248.56 max = 1255.98 avg = 1251.22
yolov4-tiny min = 1507.31 max = 1525.56 avg = 1514.66
nanodet_m min = 386.41 max = 400.63 avg = 391.21
yolo-fastest-1.1 min = 159.97 max = 164.53 avg = 161.41
yolo-fastestv2 min = 134.29 max = 135.47 avg = 134.70
vision_transformer min = 22201.32 max = 22510.75 avg = 22315.09
FastestDet min = 146.94 max = 148.50 avg = 147.44
```
6 changes: 6 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# How to install

```bash
python3 setup.py bdist_wheel
pip install ./dist/*.whl
```
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[build-system]
requires = [
"setuptools>=42",
"wheel",
"ninja; sys_platform != 'win32'",
"cmake>=3.12",
# "wheel",
# "ninja; sys_platform != 'win32'",
# "cmake>=3.12",
]
build-backend = "setuptools.build_meta"
81 changes: 8 additions & 73 deletions python/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,30 +244,15 @@ PYBIND11_MODULE(ncnn, m)

.def(py::init([](py::buffer const b) {
py::buffer_info info = b.request();

if (info.ndim > 4)
{
std::stringstream ss;
ss << "convert numpy.ndarray to ncnn.Mat only dims <=4 support now, but given " << info.ndim;
pybind11::pybind11_fail(ss.str());
}

size_t elemsize = 4u;
if (info.format == py::format_descriptor<double>::format())
{
elemsize = 8u;
}
if (info.format == py::format_descriptor<float>::format() || info.format == py::format_descriptor<int>::format())
{
elemsize = 4u;
}
else if (info.format == "e")
{
elemsize = 2u;
}
else if (info.format == py::format_descriptor<int8_t>::format() || info.format == py::format_descriptor<uint8_t>::format())
{
elemsize = 1u;
}
size_t elemsize = info.itemsize;

Mat* v = nullptr;
if (info.ndim == 1)
Expand Down Expand Up @@ -296,66 +281,16 @@ PYBIND11_MODULE(ncnn, m)
// so we set the cstep as numpy's cstep
v->cstep = (int)info.shape[3] * (int)info.shape[2] * (int)info.shape[1];
}
return v;
return std::unique_ptr<Mat>(v);
}),
py::arg("array"))
.def_buffer([](Mat& m) -> py::buffer_info {
if (m.elemsize != 1 && m.elemsize != 2 && m.elemsize != 4)
{
std::stringstream ss;
ss << "convert ncnn.Mat to numpy.ndarray only elemsize 1, 2, 4 support now, but given " << m.elemsize;
pybind11::pybind11_fail(ss.str());
}
if (m.elempack != 1)
{
std::stringstream ss;
ss << "convert ncnn.Mat to numpy.ndarray only elempack 1 support now, but given " << m.elempack;
pybind11::pybind11_fail(ss.str());
}
std::string format = get_mat_format(m);
std::vector<py::ssize_t> shape;
std::vector<py::ssize_t> strides;
if (m.dims == 1)
{
shape.push_back(m.w);
strides.push_back(m.elemsize);
}
else if (m.dims == 2)
{
shape.push_back(m.h);
shape.push_back(m.w);
strides.push_back(m.w * m.elemsize);
strides.push_back(m.elemsize);
}
else if (m.dims == 3)
{
shape.push_back(m.c);
shape.push_back(m.h);
shape.push_back(m.w);
strides.push_back(m.cstep * m.elemsize);
strides.push_back(m.w * m.elemsize);
strides.push_back(m.elemsize);
}
else if (m.dims == 4)
{
shape.push_back(m.c);
shape.push_back(m.d);
shape.push_back(m.h);
shape.push_back(m.w);
strides.push_back(m.cstep * m.elemsize);
strides.push_back(m.w * m.h * m.elemsize);
strides.push_back(m.w * m.elemsize);
strides.push_back(m.elemsize);
}
return py::buffer_info(
m.data, /* Pointer to buffer */
m.elemsize, /* Size of one scalar */
format, /* Python struct-style format descriptor */
m.dims, /* Number of dimensions */
shape, /* Buffer dimensions */
strides /* Strides (in bytes) for each index */
);
return to_buffer_info(m);
})
.def("numpy", [](py::object obj, const std::string& format="") -> py::array {
auto* m = obj.cast<Mat*>();
return py::array(to_buffer_info(*m, format), obj);
}, py::arg("format")="")
//.def("fill", (void (Mat::*)(int))(&Mat::fill), py::arg("v"))
.def("fill", (void (Mat::*)(float))(&Mat::fill), py::arg("v"))
.def("clone", &Mat::clone, py::arg("allocator") = nullptr)
Expand Down
Loading