惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
T
Threat Research - Cisco Blogs
N
News and Events Feed by Topic
Hacker News: Ask HN
Hacker News: Ask HN
Project Zero
Project Zero
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 叶小钗
Security Latest
Security Latest
Spread Privacy
Spread Privacy
aimingoo的专栏
aimingoo的专栏
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
U
Unit 42
Cyberwarzone
Cyberwarzone
小众软件
小众软件
Scott Helme
Scott Helme
Engineering at Meta
Engineering at Meta
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
A
About on SuperTechFans
爱范儿
爱范儿
S
Schneier on Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Latest news
Latest news
GbyAI
GbyAI
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
PCI Perspectives
PCI Perspectives
Jina AI
Jina AI
AI
AI
NISL@THU
NISL@THU
I
Intezer
G
GRAHAM CLULEY
B
Blog
S
Secure Thoughts
IT之家
IT之家
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
Y
Y Combinator Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
V2EX - 技术
V2EX - 技术
Recorded Future
Recorded Future
Hacker News - Newest:
Hacker News - Newest: "LLM"

hsfzxjy 的博客

解决 VSCode + CMake + MSVC 编译器信息乱码的问题 使用 3090 部署 1.58bit 动态量化版 DeepSeek R1 671b 如何在 VS Code DevContainer 中配置 HTTP 代理 如何在跳板机背后的服务器上使用 VS Code Remote - Containers Cohesive Digests for Ints and Floats Rust 中的隐匿概念 —— Place(位置) 美术馆 一尺之槌,日取其半,1075日而竭 老生常谈:使用 Cloudflare 自选 IP 加速站点访问 辩义 State、Nation 与 Country 将 Base64 编码的数据快速转换为 Uint8Array 折腾 NPU·第0章 —— Intel NPU 概述与 Level-Zero 新增域名 monad.run CSS 中为特定字符设置不同字体 Arbitary Lifetime Transmutation via Rust Unsoundness Dijkstra 算法的延伸 Manacher 回文计数算法 硬卧 Go Fact: Zero-sized Field at the Rear of a Struct Has Non-zero Size Display *big.Rat Losslessly and Smartly in Golang 代码的仪式 Building Electron From Scratch 中式亲属称谓研究之一:构建半群 Some Notes on Kotlin Coroutines Git sparse-checkout and partial clones for Mega-Repos 辩义“封建” Diving from the CUDA Error 804 into a bug of libnvidia-container Modern Cryptography, GPG and Integration with Git(hub) Move the Root Partition of Ubuntu A New Programmer Kicks a Roadblock Git-based Dependencies in Dart and Go Reversy Naming 人类一败涂地 Invalid Golang Pointers Can Bite You Even If You Don't Dereference Side Project(副业) A Flaw of Promoting Complex Trait Bounds in Rust Initialize Process Pool Worker with Individual Value Rust - Python FFI From Scratch [Extending Hexo For My Site] Part 1 [Extending Hexo For My Site] Part 0 Debug a 'torch.tensor(1).cuda()' hanging 不自由的互联网 Retrieve Contents over HTTP without curl or wget [Unravelling mocona] Part 1 - Verbosity or Anti-Pattern [Unravelling mocona] Part 0 - Preface Understanding pickle in Python Rough Notes on Deploying Vaultwarden & NextCloud Bookmarks 语言狂热者与实用主义者 Demystify the randomness in CUDA kernels Performant Bulk Mutations in IndexedDB Auto Rebuild .pyx Files with pyximport Cython and Threads Obtain a Random Available TCP Port with Bash Information Theory: KL Divergence Information Theory: Entropy and Mutual Information 铁板烧 西郊线 Proof of the Gumbel Max Trick Option::as_ref Rc, RefCell and Interior Mutability Visualizing Correlation 三月十日杂感 三月一日杂感 二月十一日杂感 一月二十六日杂感 SS Configuration 一月七日杂感 四月·病 Haskell 笔记:State Monad Haskell 笔记:Monad 引论 Haskell 笔记:Applicative Haskell 笔记:Category Theory and Functor Haskell 笔记:data, type, newtype Haskell 笔记:folds 使用 Aria2 在 Ubuntu 中下载百度云资源 从伪并行的 Python 多线程说起 一个 Reentrant Error 引发的对 Python 信号机制的探索和思考 Linux 文件权限 HSFZMUN 4.0 部署小记 午后雨·科大 最后的雨夜·广州 揭秘·变态的平方根倒数算法 神坑·Python 装饰类无限递归 Python“黑魔法”之 Encoding & Decoding Ubuntu 重新映射键盘布局 为什么我要翻墙 Python“黑魔法”之 Generator Coroutines 数学美 之 判断线段相交的最简方法 除夕杂感 17 行代码实现的简易 Javascript 字符串模板 Python“黑魔法”之 Meta Classes 诗集 生活,需要被“发现” 家书·十八岁成人礼 炫技?还是需求? 【译】响应式图片的现状 【译】“为什么有这么多的编程语言?” Wisecity 商赛总结——也谈前端自动化测试 记一次 DoS 诈骗网站的经历 那一年,我们望向星空
折腾 NPU·第1章 —— 搭建 Level Zero 开发环境
2024-06-22 · via hsfzxjy 的博客

在上一章 折腾 NPU·第0章 —— Intel NPU 概述与 Level-Zero 中,我们了解了 NPU 的概念以及 NPU 技术栈。本章将介绍如何在 VS Code 中搭建 Level-Zero 开发环境,以便我们在后续章节中编写 NPU 程序。

编译/构建工具准备

在 Windows 系统中,我们需要安装 VS Build Tools 和 CMake 作为编译和构建工具。CMake 可以通过 winget 安装:

winget install Kitware.CMake

VS Build Tools 则可以从 这里 下载,其包含了 MSVC 编译器 MSBuild 构建工具,类似于 Linux 中的 GCC 和 GNU Make。在安装时,我们需要勾选以下组件:

  • MSVC v143 - VS 2022 C++ x64/x86 build tools
  • Windows 11 SDK
  • C++ (v143) Universal Windows Platform tools
  • C++ CMake tools for Windows
  • MSVC v143 - VS 2022 C++ x64/x86 Spectre-mitigated libs

由于 Level Zero 编译使用了 /Qspectre 选项,列表中的最后一个组件是必选的,否则会有编译错误。如果不慎遗漏了某些组件,后续也可以通过重新运行 VS Installer 进行添加。

VS Code 准备

为了方便开发,我们需要在 VS Code 中安装以下插件:

  • CMake Tools (ms-vscode.cmake-tools)
  • C/C++ (ms-vscode.cpptools)

初始化项目

构建工具准备完成后,我们需要创建一个新的 CMake 项目 OneAPIDemo。该项目包含两个简单的文件:

cmake_minimum_required(VERSION 3.10)
project(OneAPIDemo)

set(CMAKE_CXX_STANDARD 17)

add_executable(OneAPIDemo src/main.cpp)

随后,在 VS Code 中执行 CMake: Select a Kit,选择 Visual Studio Build Tools 2022 Release - x86_amd64,并按 Ctrl + F5 尝试运行。如果到此一切正常,我们的项目就已经准备好了。

当然,我们也可以不依赖 VS Code 的 GUI 操作,而是通过命令行来构建项目。具体说来,我们可以在项目根目录下执行:

cmake --build build -G "Visual Studio 17 2022"
cmake --build build --config Debug

以上两条命令分别对应于 Select a KitCtrl + F5 的两个步骤。在构建完成后,我们同样可以在 build/Debug 目录下找到生成的可执行文件 OneAPIDemo.exe。在调试时,VSCode 的快捷方式更加方便,但在 CI/CD 等场景下,命令行构建则更加适用,因此我们有必要了解其背后具体执行的命令。

准备 Level Zero 依赖

这里我们采用 Git Submodule 的方式引入 Level Zero 依赖。在项目根目录下执行:

git init
git submodule add https://github.com/oneapi-src/level-zero
git submodule add https://github.com/intel/level-zero-npu-extensions

这样一来,整个项目变成了自包含的形式,即项目自带所有的依赖库,其他人也因此可以快速构建项目,而不必担心依赖库的安装问题。下载完依赖后,我们需要在 CMakeLists.txt 中添加以下内容:

cmake_minimum_required(VERSION 3.10)
project(OneAPIDemo)

set(CMAKE_CXX_STANDARD 17)
add_subdirectory(./level-zero EXCLUDE_FROM_ALL)

add_executable(OneAPIDemo src/main.cpp)
target_include_directories(OneAPIDemo PUBLIC ./level-zero/include)
target_include_directories(OneAPIDemo PUBLIC ./level-zero-npu-extensions)
target_link_libraries(OneAPIDemo ze_loader)
cmake_minimum_required(VERSION 3.10)
project(OneAPIDemo)

set(CMAKE_CXX_STANDARD 17)

add_executable(OneAPIDemo src/main.cpp)
cmake_minimum_required(VERSION 3.10)
project(OneAPIDemo)

set(CMAKE_CXX_STANDARD 17)
add_subdirectory(./level-zero EXCLUDE_FROM_ALL)

add_executable(OneAPIDemo src/main.cpp)
target_include_directories(OneAPIDemo PUBLIC ./level-zero/include)
target_include_directories(OneAPIDemo PUBLIC ./level-zero-npu-extensions)
target_link_libraries(OneAPIDemo ze_loader)

测试程序

为测试 Level Zero 是否能正常工作,我们修改 src/main.cpp

#include <iostream>
#include <ze_api.h>


#define CHECK(result) \
do { \
auto _result = result; \
if (_result != ZE_RESULT_SUCCESS) { \
std::cerr << "failed at " << __FILE__ << ":" << __LINE__ \
<< "Code: " << _result << std::endl; \
exit(1); \
} \
} while (0)

int main() {
CHECK(zeInit(ZE_INIT_FLAG_VPU_ONLY | ZE_INIT_FLAG_GPU_ONLY));
uint32_t driverCount = 0;
CHECK(zeDriverGet(&driverCount, nullptr));
std::cout << "Driver count: " << driverCount << std::endl;
return 0;
}

如果看到输出 Driver count: 2,则说明 Level Zero 调用成功了!在这个程序中,我们调用了 zeInitzeDriverGet 两个函数。前者用于初始化 Level Zero,后者用于获取当前系统中的驱动数量。由于 Thinkbook 14p 2024 同时搭载了 GPU 和 NPU,Level Zero 能检测到的驱动数为 2

总结

这一章介绍了如何在 VS Code 中搭建 Level-Zero 开发环境,并通过一个简单的程序测试了 Level Zero 的调用。在下一章中,我们将继续介绍 Level Zero 的设计架构和 API,进一步观察 NPU 的功能使用。


作者:hsfzxjy
链接:
许可:CC BY-NC-ND 4.0.
著作权归作者所有。本文不允许被用作商业用途,非商业转载请注明出处。