Upload dnmetis test tool for NPU inference

This commit is contained in:
chengchunlei
2020-10-24 11:44:17 +08:00
committed by Gitee
parent 15981373b9
commit 6fb0b3b154
9 changed files with 5447 additions and 0 deletions
@@ -0,0 +1,36 @@
Copyright (c) 2016 The Pybind Development Team, All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
You are under no obligation whatsoever to provide any bug fixes, patches, or
upgrades to the features, functionality or performance of the source code
("Enhancements") to anyone; however, if you choose to make your Enhancements
available either publicly, or directly to the author of this software, without
imposing a separate written license agreement for such Enhancements, then you
hereby grant the following license: a non-exclusive, royalty-free perpetual
license to install, use, modify, prepare derivative works, incorporate into
other computer software, distribute, and sublicense such enhancements or
derivative works thereof, in binary and source code form.
@@ -0,0 +1,83 @@
## dnmetis_backend
It contains one AclBackend(C++), It can be called by DNMetis when tester tests NPU perf&accuracy
The third-party C++ Backends can be easily added
1.install dnmetis_backend
1.python setup.py install
2.Install:
/usr/local/python3.7.5/lib/python3.7/site-packages/setuptools/dist.py:474: UserWarning: Normalizing 'V1.0.2' to '1.0.2'
normalized_version,
running install
running bdist_egg
running egg_info
creating dnmetis_backend.egg-info
writing dnmetis_backend.egg-info/PKG-INFO
writing dependency_links to dnmetis_backend.egg-info/dependency_links.txt
writing top-level names to dnmetis_backend.egg-info/top_level.txt
writing manifest file 'dnmetis_backend.egg-info/SOURCES.txt'
reading manifest file 'dnmetis_backend.egg-info/SOURCES.txt'
writing manifest file 'dnmetis_backend.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_ext
creating tmp
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/usr/local/python3.7.5/include/python3.7m -c /tmp/tmp4o2xo183.cpp -o tmp/tmp4o2xo183.o -std=c++14
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/usr/local/python3.7.5/include/python3.7m -c /tmp/tmptazg09at.cpp -o tmp/tmptazg09at.o -fvisibility=hidden
building 'dnmetis_backend' extension
creating build
creating build/temp.linux-x86_64-3.7
creating build/temp.linux-x86_64-3.7/src
creating build/temp.linux-x86_64-3.7/backend
creating build/temp.linux-x86_64-3.7/backend/built-in
creating build/temp.linux-x86_64-3.7/backend/built-in/src
creating build/temp.linux-x86_64-3.7/backend/custom
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DVERSION_INFO="1.0.2" -I/home/c00506053/dnmetis/backend_C++/dnmetis_backend/.eggs/pybind11-2.5.0-py3.7.egg/pybind11/include -I./inc/ -I./backend/inc -I./backend/built-in -I./backend/built-in/inc -I./backend/custom -I./backend/custom/inc -I/usr/local/Ascend/acllib/include/ -I/usr/local/python3.7.5/include/python3.7m -c src/main.cpp -o build/temp.linux-x86_64-3.7/src/main.o -w -O0 -fpermissive -std=c++14 -fvisibility=hidden
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DVERSION_INFO="1.0.2" -I/home/c00506053/dnmetis/backend_C++/dnmetis_backend/.eggs/pybind11-2.5.0-py3.7.egg/pybind11/include -I./inc/ -I./backend/inc -I./backend/built-in -I./backend/built-in/inc -I./backend/custom -I./backend/custom/inc -I/usr/local/Ascend/acllib/include/ -I/usr/local/python3.7.5/include/python3.7m -c src/Config.cpp -o build/temp.linux-x86_64-3.7/src/Config.o -w -O0 -fpermissive -std=c++14 -fvisibility=hidden
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DVERSION_INFO="1.0.2" -I/home/c00506053/dnmetis/backend_C++/dnmetis_backend/.eggs/pybind11-2.5.0-py3.7.egg/pybind11/include -I./inc/ -I./backend/inc -I./backend/built-in -I./backend/built-in/inc -I./backend/custom -I./backend/custom/inc -I/usr/local/Ascend/acllib/include/ -I/usr/local/python3.7.5/include/python3.7m -c backend/BaseBackend.cpp -o build/temp.linux-x86_64-3.7/backend/BaseBackend.o -w -O0 -fpermissive -std=c++14 -fvisibility=hidden
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DVERSION_INFO="1.0.2" -I/home/c00506053/dnmetis/backend_C++/dnmetis_backend/.eggs/pybind11-2.5.0-py3.7.egg/pybind11/include -I./inc/ -I./backend/inc -I./backend/built-in -I./backend/built-in/inc -I./backend/custom -I./backend/custom/inc -I/usr/local/Ascend/acllib/include/ -I/usr/local/python3.7.5/include/python3.7m -c backend/BackendFactory.cpp -o build/temp.linux-x86_64-3.7/backend/BackendFactory.o -w -O0 -fpermissive -std=c++14 -fvisibility=hidden
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DVERSION_INFO="1.0.2" -I/home/c00506053/dnmetis/backend_C++/dnmetis_backend/.eggs/pybind11-2.5.0-py3.7.egg/pybind11/include -I./inc/ -I./backend/inc -I./backend/built-in -I./backend/built-in/inc -I./backend/custom -I./backend/custom/inc -I/usr/local/Ascend/acllib/include/ -I/usr/local/python3.7.5/include/python3.7m -c backend/built-in/aclbackend.cpp -o build/temp.linux-x86_64-3.7/backend/built-in/aclbackend.o -w -O0 -fpermissive -std=c++14 -fvisibility=hidden
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DVERSION_INFO="1.0.2" -I/home/c00506053/dnmetis/backend_C++/dnmetis_backend/.eggs/pybind11-2.5.0-py3.7.egg/pybind11/include -I./inc/ -I./backend/inc -I./backend/built-in -I./backend/built-in/inc -I./backend/custom -I./backend/custom/inc -I/usr/local/Ascend/acllib/include/ -I/usr/local/python3.7.5/include/python3.7m -c backend/built-in/src/model_process.cpp -o build/temp.linux-x86_64-3.7/backend/built-in/src/model_process.o -w -O0 -fpermissive -std=c++14 -fvisibility=hidden
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DVERSION_INFO="1.0.2" -I/home/c00506053/dnmetis/backend_C++/dnmetis_backend/.eggs/pybind11-2.5.0-py3.7.egg/pybind11/include -I./inc/ -I./backend/inc -I./backend/built-in -I./backend/built-in/inc -I./backend/custom -I./backend/custom/inc -I/usr/local/Ascend/acllib/include/ -I/usr/local/python3.7.5/include/python3.7m -c backend/built-in/src/sample_process.cpp -o build/temp.linux-x86_64-3.7/backend/built-in/src/sample_process.o -w -O0 -fpermissive -std=c++14 -fvisibility=hidden
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DVERSION_INFO="1.0.2" -I/home/c00506053/dnmetis/backend_C++/dnmetis_backend/.eggs/pybind11-2.5.0-py3.7.egg/pybind11/include -I./inc/ -I./backend/inc -I./backend/built-in -I./backend/built-in/inc -I./backend/custom -I./backend/custom/inc -I/usr/local/Ascend/acllib/include/ -I/usr/local/python3.7.5/include/python3.7m -c backend/built-in/src/utils.cpp -o build/temp.linux-x86_64-3.7/backend/built-in/src/utils.o -w -O0 -fpermissive -std=c++14 -fvisibility=hidden
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -DVERSION_INFO="1.0.2" -I/home/c00506053/dnmetis/backend_C++/dnmetis_backend/.eggs/pybind11-2.5.0-py3.7.egg/pybind11/include -I./inc/ -I./backend/inc -I./backend/built-in -I./backend/built-in/inc -I./backend/custom -I./backend/custom/inc -I/usr/local/Ascend/acllib/include/ -I/usr/local/python3.7.5/include/python3.7m -c backend/custom/trtbackend.cpp -o build/temp.linux-x86_64-3.7/backend/custom/trtbackend.o -w -O0 -fpermissive -std=c++14 -fvisibility=hidden
creating build/lib.linux-x86_64-3.7
g++ -pthread -shared build/temp.linux-x86_64-3.7/src/main.o build/temp.linux-x86_64-3.7/src/Config.o build/temp.linux-x86_64-3.7/backend/BaseBackend.o build/temp.linux-x86_64-3.7/backend/BackendFactory.o build/temp.linux-x86_64-3.7/backend/built-in/aclbackend.o build/temp.linux-x86_64-3.7/backend/built-in/src/model_process.o build/temp.linux-x86_64-3.7/backend/built-in/src/sample_process.o build/temp.linux-x86_64-3.7/backend/built-in/src/utils.o build/temp.linux-x86_64-3.7/backend/custom/trtbackend.o -L/usr/local/Ascend/acllib/lib64/ -L/usr/local/python3.7.5/lib -lascendcl -lpython3.7m -o build/lib.linux-x86_64-3.7/dnmetis_backend.cpython-37m-x86_64-linux-gnu.so -O0
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
copying build/lib.linux-x86_64-3.7/dnmetis_backend.cpython-37m-x86_64-linux-gnu.so -> build/bdist.linux-x86_64/egg
creating stub loader for dnmetis_backend.cpython-37m-x86_64-linux-gnu.so
byte-compiling build/bdist.linux-x86_64/egg/dnmetis_backend.py to dnmetis_backend.cpython-37.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying dnmetis_backend.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dnmetis_backend.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dnmetis_backend.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dnmetis_backend.egg-info/not-zip-safe -> build/bdist.linux-x86_64/egg/EGG-INFO
copying dnmetis_backend.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
creating dist
creating 'dist/dnmetis_backend-1.0.2-py3.7-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing dnmetis_backend-1.0.2-py3.7-linux-x86_64.egg
removing '/usr/local/python3.7.5/lib/python3.7/site-packages/dnmetis_backend-1.0.2-py3.7-linux-x86_64.egg' (and everything under it)
creating /usr/local/python3.7.5/lib/python3.7/site-packages/dnmetis_backend-1.0.2-py3.7-linux-x86_64.egg
Extracting dnmetis_backend-1.0.2-py3.7-linux-x86_64.egg to /usr/local/python3.7.5/lib/python3.7/site-packages
dnmetis-backend 1.0.2 is already the active version in easy-install.pth
Installed /usr/local/python3.7.5/lib/python3.7/site-packages/dnmetis_backend-1.0.2-py3.7-linux-x86_64.egg
Processing dependencies for dnmetis-backend==1.0.2
Finished processing dependencies for dnmetis-backend==1.0.2
@@ -0,0 +1,198 @@
//Config.h
#pragma once
#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <sstream>
/*
* \brief Generic configuration Class
*
*/
class Config {
// Data
protected:
std::string m_Delimiter; //!< separator between key and value
std::string m_Comment; //!< separator between value and comments
std::map<std::string,std::string> m_Contents; //!< extracted keys and values
typedef std::map<std::string,std::string>::iterator mapi;
typedef std::map<std::string,std::string>::const_iterator mapci;
// Methods
public:
static Config *instance;
static Config *setInstance(char* cfg){
if(instance == nullptr){
instance = new Config(cfg);
}
return instance;
}
static Config *getInstance(){
return instance;
}
Config( std::string filename,std::string delimiter = "=",std::string comment = "#" );
Config();
template<class T> T Read( const std::string& in_key ) const; //!<Search for key and read value or optional default value, call as read<T>
template<class T> T Read( const std::string& in_key, const T& in_value ) const;
template<class T> bool ReadInto( T& out_var, const std::string& in_key ) const;
template<class T>
bool ReadInto( T& out_var, const std::string& in_key, const T& in_value ) const;
bool FileExist(std::string filename);
void ReadFile(std::string filename,std::string delimiter = "=",std::string comment = "#" );
// Check whether key exists in configuration
bool KeyExists( const std::string& in_key ) const;
// Modify keys and values
template<class T> void Add( const std::string& in_key, const T& in_value );
void Remove( const std::string& in_key );
// Check or change configuration syntax
std::string GetDelimiter() const { return m_Delimiter; }
std::string GetComment() const { return m_Comment; }
std::string SetDelimiter( const std::string& in_s )
{ std::string old = m_Delimiter; m_Delimiter = in_s; return old; }
std::string SetComment( const std::string& in_s )
{ std::string old = m_Comment; m_Comment = in_s; return old; }
// Write or read configuration
friend std::ostream& operator<<( std::ostream& os, const Config& cf );
friend std::istream& operator>>( std::istream& is, Config& cf );
protected:
template<class T> static std::string T_as_string( const T& t );
template<class T> static T string_as_T( const std::string& s );
static void Trim( std::string& inout_s );
// Exception types
public:
struct File_not_found {
std::string filename;
File_not_found( const std::string& filename_ = std::string() )
: filename(filename_) {} };
struct Key_not_found { // thrown only by T read(key) variant of read()
std::string key;
Key_not_found( const std::string& key_ = std::string() )
: key(key_) {} };
};
/* static */
template<class T>
std::string Config::T_as_string( const T& t )
{
// Convert from a T to a string
// Type T must support << operator
std::ostringstream ost;
ost << t;
return ost.str();
}
/* static */
template<class T>
T Config::string_as_T( const std::string& s )
{
// Convert from a string to a T
// Type T must support >> operator
T t;
std::istringstream ist(s);
ist >> t;
return t;
}
/* static */
template<>
inline std::string Config::string_as_T<std::string>( const std::string& s )
{
// Convert from a string to a string
// In other words, do nothing
return s;
}
/* static */
template<>
inline bool Config::string_as_T<bool>( const std::string& s )
{
// Convert from a string to a bool
// Interpret "false", "F", "no", "n", "0" as false
// Interpret "true", "T", "yes", "y", "1", "-1", or anything else as true
bool b = true;
std::string sup = s;
for( std::string::iterator p = sup.begin(); p != sup.end(); ++p )
*p = toupper(*p); // make string all caps
if( sup==std::string("FALSE") || sup==std::string("F") ||
sup==std::string("NO") || sup==std::string("N") ||
sup==std::string("0") || sup==std::string("NONE") )
b = false;
return b;
}
template<class T>
T Config::Read( const std::string& key ) const
{
// Read the value corresponding to key
mapci p = m_Contents.find(key);
if( p == m_Contents.end() ) throw Key_not_found(key);
return string_as_T<T>( p->second );
}
template<class T>
T Config::Read( const std::string& key, const T& value ) const
{
// Return the value corresponding to key or given default value
// if key is not found
mapci p = m_Contents.find(key);
if( p == m_Contents.end() ) return value;
return string_as_T<T>( p->second );
}
template<class T>
bool Config::ReadInto( T& var, const std::string& key ) const
{
// Get the value corresponding to key and store in var
// Return true if key is found
// Otherwise leave var untouched
mapci p = m_Contents.find(key);
bool found = ( p != m_Contents.end() );
if( found ) var = string_as_T<T>( p->second );
return found;
}
template<class T>
bool Config::ReadInto( T& var, const std::string& key, const T& value ) const
{
// Get the value corresponding to key and store in var
// Return true if key is found
// Otherwise set var to given default
mapci p = m_Contents.find(key);
bool found = ( p != m_Contents.end() );
if( found )
var = string_as_T<T>( p->second );
else
var = value;
return found;
}
template<class T>
void Config::Add( const std::string& in_key, const T& value )
{
// Add a key with given value
std::string v = T_as_string( value );
std::string key=in_key;
trim(key);
trim(v);
m_Contents[key] = v;
return;
}
@@ -0,0 +1,101 @@
/**
* @file common.h
*
* Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#pragma once
#include <iostream>
#include<vector>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include "half.hpp"
#include "Config.h"
#define LOG 2
#define INFO_LOG(fmt, args...) if(Config::getInstance()->Read("backend_loglevel", 0)>=3) fprintf(stdout, "[INFO] " fmt "\n", ##args)
#define WARN_LOG(fmt, args...) if(Config::getInstance()->Read("backend_loglevel", 0)>=2) fprintf(stdout, "[WARN] " fmt "\n", ##args)
#define ERROR_LOG(fmt, args...) if(Config::getInstance()->Read("backend_loglevel", 0)>=1) fprintf(stdout, "[ERROR] " fmt "\n", ##args)
namespace py = pybind11;
// half_float::half behaviors like float, but with different precision
using float16 = half_float::half;
NAMESPACE_BEGIN(pybind11)
NAMESPACE_BEGIN(detail)
template <>
struct type_caster<float16> {
public:
PYBIND11_TYPE_CASTER(float16, _("float16"));
using float_caster = type_caster<float>;
bool load(handle src, bool convert) {
float_caster caster;
if (caster.load(src, convert)) {
this->value = float16(float(caster)); // Implicit cast defined by `type_caster`.
return true;
}
return false;
}
static handle cast(float16 src, return_value_policy policy, handle parent) {
return float_caster::cast(float(src), policy, parent);
}
};
constexpr int NPY_FLOAT16 = 23;
template <>
struct npy_format_descriptor<float16> {
static pybind11::dtype dtype() {
handle ptr = npy_api::get().PyArray_DescrFromType_(NPY_FLOAT16);
return reinterpret_borrow<pybind11::dtype>(ptr);
}
static std::string format() {
// following: https://docs.python.org/3/library/struct.html#format-characters
return "e";
}
static constexpr auto name() {
return _("float16");
}
};
NAMESPACE_END(detail)
NAMESPACE_END(pybind11)
typedef enum Result {
SUCCESS = 0,
FAILED = 1
} Result;
enum FrameworkType {
CAFFE = 0,
MINDSPORE = 1,
TENSORFLOW = 3,
ANDROID_NN,
ACL,
TRT,
HIAI_ENGINE,
FRAMEWORK_RESERVED,
};
struct Output_buf {
void *ptr = nullptr; // Pointer to the underlying storage
int64_t itemsize = 0; // Size of individual items in bytes
int64_t size = 0; // Total number of entries
std::string format; // For homogeneous buffers, this should be set to format_descriptor<T>::format()
int64_t ndim = 0; // Number of dimensions
std::vector<int64_t> shape; // Shape of the tensor (1 entry per dimension)
std::vector<int64_t> strides; // Number of bytes between adjacent entries (for each per dimension)
bool readonly = false; // flag to indicate if the underlying storage may be written to
};
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,140 @@
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
import sys
import setuptools
__version__ = 'V1.0.2'
class get_pybind_include(object):
"""Helper class to determine the pybind11 include path
The purpose of this class is to postpone importing pybind11
until it is actually installed, so that the ``get_include()``
method can be invoked. """
def __str__(self):
import pybind11
return pybind11.get_include()
ext_modules = [
Extension(
'dnmetis_backend',
# Sort input source files to ensure bit-for-bit reproducible builds
# (https://github.com/pybind/dnmetis_backend/pull/53)
#sorted(['src/main.cpp','src/model_process.cpp','src/sample_process.cpp','src/utils.cpp']),
sources=[
'src/main.cpp',
'src/Config.cpp',
'backend/BaseBackend.cpp',
'backend/BackendFactory.cpp',
'backend/built-in/aclbackend.cpp',
'backend/built-in/src/model_process.cpp',
'backend/built-in/src/sample_process.cpp',
'backend/built-in/src/utils.cpp',
'backend/custom/trtbackend.cpp',
],
include_dirs=[
# Path to pybind11 headers
get_pybind_include(),
'./inc/',
'./backend/inc',
'./backend/built-in',
'./backend/built-in/inc',
'./backend/custom',
'./backend/custom/inc',
'/home/HwHiAiUser/Ascend/ascend-toolkit/20.10.0.B023//acllib/include/',
],
library_dirs=['/home/HwHiAiUser/Ascend/ascend-toolkit/20.10.0.B023//acllib/lib64/',],
libraries=['ascendcl',],
language='c++'
),
]
# cf http://bugs.python.org/issue26689
def has_flag(compiler, flagname):
"""Return a boolean indicating whether a flag name is supported on
the specified compiler.
"""
import tempfile
import os
with tempfile.NamedTemporaryFile('w', suffix='.cpp', delete=False) as f:
f.write('int main (int argc, char **argv) { return 0; }')
fname = f.name
try:
compiler.compile([fname], extra_postargs=[flagname])
except setuptools.distutils.errors.CompileError:
return False
finally:
try:
os.remove(fname)
except OSError:
pass
return True
def cpp_flag(compiler):
"""Return the -std=c++[11/14/17] compiler flag.
The newer version is prefered over c++11 (when it is available).
"""
flags = ['-std=c++14', '-std=c++17','-std=c++11']
for flag in flags:
if has_flag(compiler, flag):
return flag
raise RuntimeError('Unsupported compiler -- at least C++11 support '
'is needed!')
class BuildExt(build_ext):
"""A custom build extension for adding compiler-specific options."""
c_opts = {
'msvc': ['/EHsc'],
'unix': ['-w','-O0', '-fpermissive'],
}
l_opts = {
'msvc': [],
'unix': ['-O0'],
}
if sys.platform == 'darwin':
darwin_opts = ['-stdlib=libc++', '-mmacosx-version-min=10.7']
c_opts['unix'] += darwin_opts
l_opts['unix'] += darwin_opts
def build_extensions(self):
ct = self.compiler.compiler_type
opts = self.c_opts.get(ct, [])
link_opts = self.l_opts.get(ct, [])
if ct == 'unix':
opts.append(cpp_flag(self.compiler))
if has_flag(self.compiler, '-fvisibility=hidden'):
opts.append('-fvisibility=hidden')
for ext in self.extensions:
ext.define_macros = [('VERSION_INFO', '"{}"'.format(self.distribution.get_version()))]
ext.extra_compile_args = opts
ext.extra_link_args = link_opts
build_ext.build_extensions(self)
setup(
name='dnmetis_backend',
version=__version__,
author='chegulu',
author_email='chegulu@xxx.com',
url='https://chegulu',
description='A test tool project using pybind11',
long_description='',
ext_modules=ext_modules,
setup_requires=['pybind11==2.5.0'],
#data_files=[('config', ['cfg/config.txt'])],
cmdclass={'build_ext': BuildExt},
zip_safe=False,
)
@@ -0,0 +1,156 @@
// Config.cpp
#include "Config.h"
using namespace std;
Config::Config( string filename, string delimiter,
string comment )
: m_Delimiter(delimiter), m_Comment(comment)
{
// Construct a Config, getting keys and values from given file
std::ifstream in( filename.c_str() );
if( !in ) throw File_not_found( filename );
in >> (*this);
}
Config::Config()
: m_Delimiter( string(1,'=') ), m_Comment( string(1,'#') )
{
// Construct a Config without a file; empty
}
bool Config::KeyExists( const string& key ) const
{
// Indicate whether key is found
mapci p = m_Contents.find( key );
return ( p != m_Contents.end() );
}
/* static */
void Config::Trim( string& inout_s )
{
// Remove leading and trailing whitespace
static const char whitespace[] = " \n\t\v\r\f";
inout_s.erase( 0, inout_s.find_first_not_of(whitespace) );
inout_s.erase( inout_s.find_last_not_of(whitespace) + 1U );
}
std::ostream& operator<<( std::ostream& os, const Config& cf )
{
// Save a Config to os
for( Config::mapci p = cf.m_Contents.begin();
p != cf.m_Contents.end();
++p )
{
os << p->first << " " << cf.m_Delimiter << " ";
os << p->second << std::endl;
}
return os;
}
void Config::Remove( const string& key )
{
// Remove key and its value
m_Contents.erase( m_Contents.find( key ) );
return;
}
std::istream& operator>>( std::istream& is, Config& cf )
{
// Load a Config from is
// Read in keys and values, keeping internal whitespace
typedef string::size_type pos;
const string& delim = cf.m_Delimiter; // separator
const string& comm = cf.m_Comment; // comment
const pos skip = delim.length(); // length of separator
string nextline = ""; // might need to read ahead to see where value ends
while( is || nextline.length() > 0 )
{
// Read an entire line at a time
string line;
if( nextline.length() > 0 )
{
line = nextline; // we read ahead; use it now
nextline = "";
}
else
{
std::getline( is, line );
}
// Ignore comments
line = line.substr( 0, line.find(comm) );
// Parse the line if it contains a delimiter
pos delimPos = line.find( delim );
if( delimPos < string::npos )
{
// Extract the key
string key = line.substr( 0, delimPos );
line.replace( 0, delimPos+skip, "" );
// See if value continues on the next line
// Stop at blank line, next line with a key, end of stream,
// or end of file sentry
bool terminate = false;
while( !terminate && is )
{
std::getline( is, nextline );
terminate = true;
string nlcopy = nextline;
Config::Trim(nlcopy);
if( nlcopy == "" ) continue;
nextline = nextline.substr( 0, nextline.find(comm) );
if( nextline.find(delim) != string::npos )
continue;
nlcopy = nextline;
Config::Trim(nlcopy);
if( nlcopy != "" ) line += "\n";
line += nextline;
terminate = false;
}
// Store key and value
Config::Trim(key);
Config::Trim(line);
cf.m_Contents[key] = line; // overwrites if key is repeated
}
}
return is;
}
bool Config::FileExist(std::string filename)
{
bool exist= false;
std::ifstream in( filename.c_str() );
if( in )
exist = true;
return exist;
}
void Config::ReadFile( string filename, string delimiter,
string comment )
{
m_Delimiter = delimiter;
m_Comment = comment;
std::ifstream in( filename.c_str() );
if( !in ) throw File_not_found( filename );
in >> (*this);
}
@@ -0,0 +1,150 @@
#include <pybind11/pybind11.h>
#include "BackendFactory.h"
#include "BaseBackend.h"
//#include "common.h"
#include "utils.h"
#include <string>
using namespace std;
bool warmup=true;
long deviceTime=0;
std::shared_ptr<BaseBackend> backend;
Config * Config::instance = nullptr;
int backend_setconfig(char* cfg)
{
Config::setInstance(cfg);
}
int backend_load(int backend_type,char* omModelPath,char* binfile)
{
backend = BackendFactory::Instance()->CreateBaseBackend(FrameworkType(backend_type));
if(backend == nullptr)
{
ERROR_LOG("FAILED, Not found the test backend, type:%d.", FrameworkType(backend_type));
return FAILED;
}
backend->init(omModelPath,binfile);
printf("[INFO] AclBackend init OK\n");
backend->load(omModelPath,binfile);
printf("[INFO] AclBackend load OK\n");
}
//py::array backend_predict(int type, char* omModelPath, py::array binfile)
vector<py::array> backend_predict(int type, char* omModelPath, py::array binfile)
{
if(warmup)
{
printf("[INFO] start warmup AclBackend predict\n");
//warmup=false;
}
INFO_LOG("start backend_predict is %d", Utils::getCurrentTime());
std::vector<Output_buf> result_buf;
//INFO_LOG("binfile.nbytes is %d", binfile.nbytes());
deviceTime = 0;
backend->predict(omModelPath, binfile.mutable_data(), binfile.nbytes(),result_buf, deviceTime);
INFO_LOG("Pure device execute time is %f ms", deviceTime);
if(warmup)
{
printf("[INFO] end warmup AclBackend predict\n");
warmup=false;
}
INFO_LOG("end backend_predict is %d", Utils::getCurrentTime());
vector<py::array> vec_result;
for(int i =0 ; i<result_buf.size();i++)
{
std::string str;
if(!result_buf[i].format.compare("uint8"))
str=py::format_descriptor<uint8_t>::format();
if(!result_buf[i].format.compare("int8"))
str=py::format_descriptor<int8_t>::format();
if(!result_buf[i].format.compare("float"))
str=py::format_descriptor<float>::format();
if(!result_buf[i].format.compare("float16"))
str=py::format_descriptor<float16>::format();
if(!result_buf[i].format.compare("int64"))
str=py::format_descriptor<int64_t>::format();
if(!result_buf[i].format.compare("uint64"))
str=py::format_descriptor<uint64_t>::format();
py::buffer_info tmp=py::buffer_info(
result_buf[i].ptr,
(ssize_t)result_buf[i].itemsize, //itemsize
str,
(ssize_t)result_buf[i].ndim,// ndim
result_buf[i].shape, // shape
result_buf[i].strides //strides
);
py::dtype dt = py::dtype(str);
py::array result = py::array(dt,tmp.shape, tmp.strides, tmp.ptr);
vec_result.push_back(result);
}
return vec_result;
//return result;
}
long backend_get_device_time()
{
return deviceTime;
}
int backend_unload(int type,char* omModelPath,char* binfile)
{
backend->unload(omModelPath,binfile);
printf("[INFO] AclBackend unload OK\n");
}
namespace py = pybind11;
PYBIND11_MODULE(dnmetis_backend, m) {
m.doc() = R"pbdoc(
Pybind11 example plugin
-----------------------
.. currentmodule:: dnmetis_backend
.. autosummary::
:toctree: _generate
add
subtract
)pbdoc";
/*m.def("backend_main", &backend_main, R"pbdoc(
backend
)pbdoc");*/
m.def("backend_setconfig", &backend_setconfig, R"pbdoc(
backend
)pbdoc");
m.def("backend_load", &backend_load, R"pbdoc(
backend
)pbdoc");
m.def("backend_predict", &backend_predict, R"pbdoc(
backend
)pbdoc");
m.def("backend_get_device_time", &backend_get_device_time, R"pbdoc(
backend
)pbdoc");
m.def("backend_unload", &backend_unload, R"pbdoc(
backend
)pbdoc");
m.def("add", [](int i, int j) { return i + j; }, R"pbdoc(
add
)pbdoc");
m.def("subtract", [](int i, int j) { return i - j; }, R"pbdoc(
subtract
)pbdoc");
#ifdef VERSION_INFO
m.attr("__version__") = VERSION_INFO;
#else
m.attr("__version__") = "dev";
#endif
}
@@ -0,0 +1,8 @@
import dnmetis_backend as m
print(m.__version__)
assert m.add(1, 2) == 3
print("Test add Ok!")
assert m.subtract(1, 2) == -1
print("Test subtract Ok!")
print("Test Ok!")