intel/npu-nn-cost-model
关于
Library for modelling performance costs of different Neural Network workloads on NPU devices
系统与基础设施实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文VPUNN cost model
A NN-Based Cost Model for VPU Devices. For additional information about model setup and training, please refer this paper
If you find this work useful, please cite the following paper:
@article{DBLP:journals/corr/abs-2205-04586,
doi = {10.48550/ARXIV.2205.04586},
url = {https://arxiv.org/abs/2205.04586},
author = {Hunter, Ian Frederick Vigogne Goodbody and Palla, Alessandro and Nagy, Sebastian Eusebiu and Richmond, Richard and McAdoo, Kyle},
title = {Towards Optimal VPU Compiler Cost Modeling by using Neural Networks to Infer Hardware Performances},
publisher = {arXiv},
year = {2022},
copyright = {arXiv.org perpetual, non-exclusive license}
}
Setup
GCC version should be > 9. You can check your GCC version by running gcc --version and g++ --version
If you do not set CC and CXX environment variables, which gcc and which g++ are used by default.
Compile the library by typing cmake -H. -Bbuild && cmake --build build
General architecture diagram of target interactions, using command cmake -DCBLAS_LIB=openblas -DVPUNN_BUILD_HTTP_CLIENT=ON -DVPUNN_BUILD_APPS=ON -DCMAKE_BUILD_TYPE=Coverage -H. -Bbuild is presented here:

@TODO: environment compatible with newer compiler versions (gcc>=10, clang >10 )
Use Intel oneAPI MKL
Install oneAPI base Toolkit (instructions). oneAPI is massive so feel free to install only the Math Kernel Library library.
If you have troubles with proxy, please export no_proxy=127.0.0.1 in order to bypass any no_proxy env vs *.intel.com urls
To enable MKL you need to source this file /opt/intel/oneapi/setvars.sh to set the appropriate environment variables. Look here on how to get started with VSC
Select BLAS library
You can select which BLAS library to use (assume you have MKL installed) and the threading mode by using the following cmake variables
-DCBLAS_LIB=<value>(options:mklfor oneMKL andopenblasfor OpenBLAS)-DMKL_THREADING=<value>(options:tbbfor oneAPI Threading Building Blocks andsequentialfor no threading)
Using the cost model: C++
To use the VPUN cost model in a cmake project is quite simple. An example of a CMakeLists.txt file is shown below
include_directories(${CMAKE_BINARY_DIR}/include)
include_directories(${FLATBUFFERS_SRC_DIR}/include)
...
target_link_libraries(<your exe or lib> inference)
The following example code explains how to instantiate the cost model and how to run a simple query for a 3x3s1 convolution
#include "vpu_cost_model.h"
auto model = VPUNN::VPUCostModel(model_path);
auto dpu_cycles = model.DPU({VPUNN::VPUDevice::VPU_2_7,
VPUNN::Operation::CONVOLUTION,
{VPUNN::VPUTensor(56, 56, 16, 1, VPUNN::DataType::UINT8)}, // input dimensions
{VPUNN::VPUTensor(56, 56, 16, 1, VPUNN::DataType::UINT8)}, // output dimensions
{3, 3}, //kernels
{1, 1}, //strides
{1, 1}, //padding
VPUNN::ExecutionMode::CUBOID_16x16} // execution mode
);
The example folder contains few examples on how to build and use the cost model in a C++ project. The following list is a WIP of the supported example:
workload_mode_selection:- Selecting the optimal MPE mode for a VPU_2_0 workload
- Choosing the optimal workload split strategy amound multiple ones
Install
To test the install option, for find_package(VPUNN) functionality.
git clone <repo-url> <repo-name>
cd <repo-name>
cmake -S . -B build
cmake --build build -j
cmake --install build --prefix <install-path> [--config <config-type-for-multiconfig-systems (windows)>]
cd <your-custom-repo>
cmake -S . -B build -DCMAKE_PREFIX_PATH=<install-path>
cmake --build build -j
Using the cost model: Python
To install the Python package, run the following command from the root of the repository:
pip install .
This will use scikit-build to compile the C++ extension (bindings) and install the Python package.
It is recommended to do this in a python virtual environment.
Developer guide
Git hooks
All developers should install the git hooks that are tracked in the .githooks directory. We use the pre-commit framework for hook management. The recommended way of installing it is using pip:
pip install pre-commit
The hooks can then be installed into your local clone using:
pre-commit install --allow-missing-config
--allow-missing-config is an optional argument that will allow users to have the hooks installed and be functional even if using an older branch that does not have them tracked. A warning will be displayed for such cases when the hooks are ran.
If you want to manually run all pre-commit hooks on a repository, run pre-commit run --all-files. To run individual hooks use pre-commit run <hook_id>.
Uninstalling the hooks can be done using
pre-commit uninstall
Testing the library
Cost model test (C++)
Tests uses Google test suite for automatizing tests
To run the test suite: ctest --test-dir build/tests/cpp/
Example: running only cost model integration test: ./build/bin/test_cost_model (or ./build/bin/Release/test_cost_model.exe for a Visual Studio Release build).
All C++ tests in a test executable use the same global utest_log logger. It is created once per test process and is configured before the test suite starts. Tests should use utest_log for test logging rather than creating another global test logger.
Test logger
All C++ tests in a test executable share one VPUNN_unit_tests::utest_log::GlobalUTestLogger instance per process. It writes message text to standard output; it does not add a timestamp, logger name, severity prefix, or newline. The caller controls line breaks. Use this logger for test diagnostics rather than creating another process-wide test logger.
Include logger/logger.h for the severity helpers, Stream, Level, and the native fmt/spdlog formatting overloads. The names below are shown fully qualified where that helps distinguish namespaces; inside namespace VPUNN_unit_tests, utest_log can be used directly.
Logging styles
The stream-style helpers accept the usual std::ostream values and manipulators. A temporary Stream buffers one message and submits it when the full expression ends, normally at the semicolon:
VPUNN_unit_tests::utest_log::info() << "value=" << 42 << '\n';
VPUNN_unit_tests::utest_log::debug() << std::hex << 255 << '\n';
The available helpers are trace(), debug(), info(), warn(), err(), and critical(). The severity filter is applied when the buffered message is submitted. Stream helpers do not add a newline or flush at each insertion; include a newline when a line break is wanted.
For formatted messages, the same helper names have an overload taking a compile-time checked fmt/spdlog format string and arguments:
VPUNN_unit_tests::utest_log::info("value={}, name={}", 42, "example");
VPUNN_unit_tests::utest_log::warn("layer {} exceeded {} cycles", layer_id, limit);
Use {} fields and fmt format specifications. This overload sends the formatted message directly to the shared logger. For a multi-line message, include \n in the format string or argument.
For std::cout-style expressions, include logger/tout.h. tout is declared in VPUNN_unit_tests::utest_log and uses TOut::get_default_level(), which is always debug:
tests/cpp/common/performance.cpp uses tout unqualified after using namespace utest_log;.
VPUNN_unit_tests::utest_log::tout << "value=" << 42 << '\n';
To select a severity for the whole statement, put a Level value first:
VPUNN_unit_tests::utest_log::tout << VPUNN_unit_tests::utest_log::Level::info << "details\n";
Include logger/log_level_scope.h to temporarily change the shared level inside a test. The guard restores the prior level when it leaves scope, including when scopes are nested:
TEST(LoggerExample, DebugDetails) {
VPUNN_unit_tests::utest_log::LogLevelScope level{
VPUNN_unit_tests::utest_log::Level::debug};
VPUNN_unit_tests::utest_log::debug() << "debug details\n";
}
Level::level_enum is an alias for spdlog::level::level_enum. Level provides trace, debug, info, warn, err, critical, and off constants, plus Level::from_str() for parsing a level name.
Formatting user types
Built-in fmt-supported types can be passed directly to the formatted helpers. logger/logger.h includes logger/logger_generic_formatter.h, which adapts stream-insertable types in the VPUNN and VPUNN_unit_tests namespaces so their operator<< representation can be used with {} and string format specifications:
namespace VPUNN_unit_tests {
enum class MyEnum { A, B };
std::ostream& operator<<(std::ostream& os, MyEnum value) {
return os << (value == MyEnum::A ? "A" : "B");
}
}
VPUNN_unit_tests::utest_log::info("value={}", VPUNN_unit_tests::MyEnum::A);
VPUNN_unit_tests::utest_log::info("[{:>4}]", VPUNN_unit_tests::MyEnum::B); // [ B]
The adapter must be discoverable by argument-dependent lookup in the type's associated namespace. For a type in another namespace, make the appropriate format_as visible there or provide an explicit fmt::formatter<T> specialization. A stream insertion operator declared only in an unrelated namespace is not enough.
VPUNN::EnumMap and VPUNN::EnumInverseMap are aliases of std::map, so they need explicit fmt::formatter specializations instead of the ADL-based stream adapter. In this test logger, those specializations are defined in logger/logger_vpunn_map_formatter.h and must be included explicitly; logger/logger.h does not include that header. VPUNN enums such as VPUNN::DataType and VPUNN::VPUDevice do not currently have stream insertion operators and are not directly formattable; convert them at the call site (for example, through VPUNN::mapToText<E>()) or add a formatter. An explicit fmt::formatter<T> takes precedence over the stream adapter.
Levels and configuration
The default threshold is info. Messages at the configured threshold or a more severe level are emitted; less severe messages are suppressed. off suppresses all messages. Valid names are trace, debug, info, warn, err, critical, and off; an invalid name is treated as off.
Set the process-wide level with --logger_level when invoking the test executable:
./build/bin/test_cost_model --logger_level debug
CTest does not forward this custom option to the executable, so use LOGGER_LEVEL when running through CTest:
LOGGER_LEVEL=debug ctest --test-dir build/tests/cpp/
The command-line option takes precedence over the environment variable. If neither is supplied, the level remains info. The level is configured before GoogleTest runs. LogLevelScope is intended for the current GoogleTest execution model, where test bodies run sequentially in one process. Do not use overlapping scopes or change the shared level concurrently from multiple test bodies. Worker threads may log while a scope is active, but they all observe that same process-wide threshold. Separate CTest processes each have their own logger and level.
Stream lifetime
utest_log::Stream is an implementation type normally obtained from the helpers or tout. It is non-copyable and supports move construction (but not move assignment) so temporary streams can be returned safely. Keep it temporary: it emits its buffered content in its destructor, so a static or long-lived stream can submit after the shared logger's lifetime. Prefer info(), debug(), and the other temporary helpers.
E2E Python test
pytest tests/python/test_e2e.py -v
Code coverage
To generate Code coverage report you need to enable it in CMake
cmake -DCMAKE_BUILD_TYPE=Coverage .. && make coverage -j
This commands generate a coverage folder into the build one with all the coverage information
Dependencies:
- Gcov-9 and Gcovr tools are needed in order to generate the report
- Only GCC is supported (no WASM/Visual Studio)
Notice about configurations not covered by training, or with greater errors.
NPU2.0
Not Available
NPU2.7
ISI=CLUSTERING + OWT=2 : replaced at runtime with SOK. runtime should be the same, no input halo used
Elementwise + ISI=SOK : replaced at runtime with clustering + owt=1, time is a little undervalued, but its the best approximation available
CM_CONV (compress convolution) + InputChannels=1
SOH (HALO) split with Kernel =1 has probably not been part of training, doesn't make sense to have kernel=1 and input halo.NN predictions are problematic. : replaced at runtime with Clustering.
SOH Halo split , at least when H is small, K small, produces much bigger results than SOH Overlapped. This is not realistic, might be a NN limitation. See VPULayerCostModelTest.Unet_perf_SOH_SOK_after_SOHO
Output write tiles is limited to 2. EG also when used as mock for NPU4.0 where more than 2 tiles are present and used for split.
NPU2.7 splits by H with Halo were trained to NN using the memory tensor instead of the general rule for compute tensor (memory tensor is smaller with half a kernel in general). Calling NN with compute tensor introduces errors by reporting smaller values. To get corrected values (closer to Ground Truth) when generating the descriptor for NNs with interface 11 and SOH isi strategy, we are using not the input tensor, but a computed memory input tensor that mimics the one used at training
NPU4.0 (in development)
Reusing:when using the 2.7 trained version as mock please read the NPU2.7 section above.
- DW_CONV (depthwise convolution)with kernel 3x3 is optimized in NPU4.0, but not in NPU2.7. The NN reported runtime is adjusted with a factor depending on datatype, channels and kernel size Trained NN for 4.0:
- WIP
Known problems:
- NPU2.7: NN was not trained to discriminate the sporadic high runtime for swizzling. EISXW-98656 not solved (ELt wise add with big profiled CLUSTERING, but small SOH) Test: RuntimeELT_CONV_SOH_SOK_EISXW_98656. Elementwise accepts (at NN run) SWizzling ON or OFF but has to be the same for all in/out/wts all 0 (OFF), all 5(ON) combinations not trained. To consider: training of NN with swizzlings combinations (profiling shows runtime is different)
SHAVE operators available
Shave version interface 1 (the old one) will be deleted in the near future, do not use it. SHAVE v2 interface is active.
Details of any operator can be obtained by calling: ShaveOpExecutor::toString() method.
For most updated list of operators and their details see also the unit tests: TestSHAVE.SHAVE_v2_ListOfOperators, TestSHAVE.SHAVE_v2_ListOfOperatorsDetails_27,... .
For information about the profiled operators and extraparameters you can consult this document
Cost providers
The cost model is designed to be extensible. The cost providers are the classes that implement the cost model for a specific device. The cost providers are selected at runtime based on the device type. The following cost providers are available:
- NN based cost provider - is a learned performance model.
- Theoretical cost provider - is a simple mathematical model.
- "Oracle" cost provider - a LUT of measured performance for specific workloads.
- Profiled cost provider - it's an http service that can be queried to get the measured performance of a specific workload.
- Currently it supports only DPU costs and it can be configured using the following env. variables
ENABLE_VPUNN_PROFILING_SERVICE--TRUEto enable the profiling serviceVPUNN_PROFILING_SERVICE_BACKEND--siliconto use the RVP for profiling,vpuemto use VPUEM as a cost provider.VPUNN_PROFILING_SERVICE_HOST-- address of the profiling service host, default isirlccggpu04.ir.intel.comVPUNN_PROFILING_SERVICE_PORT-- port of the profiling service, default is5000
- Currently it supports only DPU costs and it can be configured using the following env. variables
To see a list of all queried workloads and which cost provider was used for each, set the environment variable ENABLE_VPUNN_DATA_SERIALIZATION to TRUE.
This will generate a couple of csv files in the directory where vpunn is used.
探索相关项目
加载中…