-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathappendix-a.html
More file actions
430 lines (374 loc) · 16.5 KB
/
Copy pathappendix-a.html
File metadata and controls
430 lines (374 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="AI学习笔记 - 从零开始系统学习人工智能,涵盖前置基础、核心原理、工程实践、应用工具、进阶展望">
<meta name="keywords" content="AI, 人工智能, 机器学习, 深度学习, Python, 数学基础">
<meta name="author" content="AI Learner">
<title>附录A:GPU与算力指南 - AI学习笔记</title>
<!-- Google Fonts - Inter -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
<!-- Font Awesome Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
<!-- KaTeX - 轻量级数学公式渲染 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js"
onload="renderMathInElement(document.body, {
delimiters: [
{left: '$$', right: '$$', display: true},
{left: '\\[', right: '\\]', display: true},
{left: '\\(', right: '\\)', display: false},
{left: '$', right: '$', display: false}
],
throwOnError: false
});"></script>
<!-- Prism.js 代码高亮 - tomorrow-night 主题 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/prism.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-python.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-bash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js"></script>
<!-- 自定义样式表 -->
<link rel="stylesheet" href="../../css/main.css">
<link rel="stylesheet" href="../../css/dark-mode.css">
<link rel="stylesheet" href="../../css/mobile-optimizations.css">
<!-- 内联CSS样式 -->
<!-- AI-Notes共享样式 -->
<link rel="stylesheet" href="../assets/ai-notes.css">
</head>
<body>
<!-- ==================== 导航栏 ==================== -->
<nav class="ai-nav">
<a href="../../index.html" class="ai-nav-brand">
<i class="fas fa-brain"></i>
AI学习笔记
</a>
<ul class="ai-nav-links" id="navLinks">
<li><a href="../../index.html"><i class="fas fa-home"></i> 首页</a></li>
<li><a href="ch15.html"><i class="fas fa-robot"></i> Agent</a></li>
<li><a href="ch16.html"><i class="fas fa-tools"></i> 工具</a></li>
<li><a href="ch17.html"><i class="fas fa-book-open"></i> 知识库</a></li>
<li><a href="ch18.html"><i class="fas fa-layer-group"></i> 记忆</a></li>
<li><a href="ch19.html"><i class="fas fa-comment-dots"></i> 提示</a></li>
<li><a href="ch20.html"><i class="fas fa-database"></i> 数据</a></li>
<li><a href="ch21.html"><i class="fas fa-exclamation-circle"></i> 局限</a></li>
<li><a href="ch22.html"><i class="fas fa-toolbox"></i> 平台</a></li>
<li><a href="ch23.html"><i class="fas fa-history"></i> 简史</a></li>
<li><a href="ch24.html"><i class="fas fa-rocket"></i> 实战</a></li>
</ul>
<button class="ai-nav-toggle" id="navToggle" aria-label="切换导航">
<i class="fas fa-bars"></i>
</button>
</nav>
<!-- ==================== Hero Section ==================== -->
<section class="ai-hero">
<h1><i class="fas fa-brain"></i> AI 学习笔记</h1>
<p>附录A:GPU与算力指南</p>
<div class="ai-hero-badges">
<span class="ai-hero-badge"><i class="fas fa-book"></i> 系统学习</span>
<span class="ai-hero-badge"><i class="fas fa-code"></i> 实践导向</span>
</div>
</section>
<!-- ==================== 主内容区域 ==================== -->
<div class="content-wrapper">
<!-- ========== 主内容 ========== -->
<main class="main-content">
<nav class="ai-chapter-nav">
<div class="breadcrumb">
<a href="../../index.html"><i class="fas fa-home"></i> 首页</a>
<span class="separator">/</span>
<span class="current">附录A:GPU与算力指南</span>
</div>
<div class="chapter-nav-links">
<a href="ch23.html" class="prev-chapter"><i class="fas fa-arrow-left"></i> 上一章</a>
<a href="appendix-b.html" class="next-chapter">下一章 <i class="fas fa-arrow-right"></i></a>
</div>
</nav>
<!-- 附录A:GPU与算力指南 -->
<!-- ============================================================ -->
<h2 class="ai-section-title"><i class="fas fa-microchip"></i> 附录A:GPU与算力指南</h2>
<div class="concept-section">
<h2><i class="fas fa-tachometer-alt"></i> 算力是什么?</h2>
<p><span class="keyword-highlight">算力</span>(Computing Power)是指计算机进行数值计算的能力。在AI领域,算力是训练和运行模型的基础资源。</p>
<div class="concept-box">
<h4><i class="fas fa-info-circle"></i> 算力单位</h4>
<p><strong>FLOPS</strong>(Floating Point Operations Per Second,每秒浮点运算次数)是衡量算力的标准单位:</p>
<ul>
<li><strong>MFLOPS</strong>:百万次/秒(10^6)</li>
<li><strong>GFLOPS</strong>:十亿次/秒(10^9)</li>
<li><strong>TFLOPS</strong>:万亿次/秒(10^12)</li>
<li><strong>PFLOPS</strong>:千万亿次/秒(10^15)</li>
</ul>
<p>一块NVIDIA A100 GPU的算力约为 <strong>312 TFLOPS</strong>(FP16),而训练GPT-3需要约 <strong>3.14 × 10^23 FLOPS</strong> 的总计算量。</p>
</div>
<h3>CPU vs GPU对比</h3>
<table class="comparison-table">
<tr>
<th>维度</th>
<th>CPU</th>
<th>GPU</th>
</tr>
<tr>
<td><strong>核心数</strong></td>
<td>少量强大核心(4-64个)</td>
<td>大量简单核心(数千个)</td>
</tr>
<tr>
<td><strong>擅长任务</strong></td>
<td>串行计算、逻辑控制</td>
<td>大规模并行计算</td>
</tr>
<tr>
<td><strong>内存带宽</strong></td>
<td>较低(~50 GB/s)</td>
<td>极高(~2 TB/s)</td>
</tr>
<tr>
<td><strong>适用场景</strong></td>
<td>通用计算、操作系统</td>
<td>AI训练/推理、图形渲染</td>
</tr>
</table>
<div class="concept-box">
<h4><i class="fas fa-lightbulb"></i> 类比理解</h4>
<p><strong>CPU</strong>就像一位<strong>大学教授</strong>:知识渊博,能解决各种复杂问题,但一次只能辅导一个学生。<br/>
<strong>GPU</strong>就像<strong>1000个小学生</strong>:每个人只能做简单的加减乘除,但1000个人同时做,总速度远超教授。AI训练需要做大量简单的矩阵运算,正好适合GPU的并行能力。</p>
</div>
<div class="concept-box">
<h4><i class="fas fa-question-circle"></i> 为什么AI需要GPU?</h4>
<p>AI模型训练涉及大量的矩阵乘法运算,这些运算可以并行执行。GPU拥有数千个计算核心,可以同时处理大量运算,比CPU快数十倍甚至上百倍。没有GPU,训练现代大模型可能需要数年甚至数十年。</p>
</div>
<h3>主流GPU产品</h3>
<table class="comparison-table">
<tr>
<th>GPU型号</th>
<th>显存</th>
<th>FP16算力</th>
<th>适用场景</th>
</tr>
<tr>
<td><strong>NVIDIA H100</strong></td>
<td>80GB HBM3</td>
<td>~990 TFLOPS</td>
<td>大模型训练(旗舰级)</td>
</tr>
<tr>
<td><strong>NVIDIA A100</strong></td>
<td>40/80GB HBM2e</td>
<td>~312 TFLOPS</td>
<td>大模型训练/推理(主流)</td>
</tr>
<tr>
<td><strong>NVIDIA RTX 4090</strong></td>
<td>24GB GDDR6X</td>
<td>~83 TFLOPS</td>
<td>本地推理、小模型微调</td>
</tr>
</table>
<h3>显存的重要性</h3>
<p><span class="keyword-highlight">显存(VRAM)</span>是GPU上用于存储模型参数和中间计算结果的内存。显存大小直接决定了能运行多大的模型。</p>
<div class="concept-box">
<h4><i class="fas fa-question-circle"></i> 为什么显存很重要?</h4>
<p>模型参数需要加载到显存中才能进行计算。例如,一个7B参数的FP16模型需要约14GB显存,加上KV Cache等运行开销,至少需要16GB显存。如果显存不足,模型就无法运行,或者需要使用量化等压缩技术。</p>
</div>
<h3>显存优化技术</h3>
<ul>
<li><strong>量化</strong>:降低参数精度(FP16→INT8→INT4),减少显存占用</li>
<li><strong>梯度检查点</strong>:用计算换显存,减少训练时的显存峰值</li>
<li><strong>LoRA/QLoRA</strong>:只训练少量额外参数,大幅减少训练显存需求</li>
<li><strong>分布式训练</strong>:将模型分布到多张GPU上,突破单卡显存限制</li>
</ul>
</div>
<div class="concept-section">
<h2><i class="fas fa-code"></i> 代码实践</h2>
<p>以下代码展示了如何检测GPU可用性,以及进行简单的<strong>GPU vs CPU性能对比</strong>。这些代码帮助你了解PyTorch如何利用GPU加速计算。</p>
<h3>示例1:PyTorch GPU检测</h3>
<pre><code class="language-python">
import torch
print("=" * 50)
print("🔍 PyTorch GPU 检测工具")
print("=" * 50)
# 1. 检查PyTorch版本
print(f"\n📦 PyTorch 版本:{torch.__version__}")
# 2. 检查CUDA是否可用
print(f"\n🔥 CUDA 是否可用:{torch.cuda.is_available()}")
if torch.cuda.is_available():
# 3. CUDA版本
print(f"🔥 CUDA 版本:{torch.version.cuda}")
# 4. GPU数量
gpu_count = torch.cuda.device_count()
print(f"\n🎮 GPU 数量:{gpu_count}")
# 5. 遍历每个GPU
for i in range(gpu_count):
props = torch.cuda.get_device_properties(i)
print(f"\n 🎮 GPU {i}: {torch.cuda.get_device_name(i)}")
print(f" 总显存:{props.total_memory / 1024**3:.1f} GB")
print(f" 计算能力:{props.major}.{props.minor}")
print(f" 多处理器数量:{props.multi_processor_count}")
# 6. 当前使用的GPU
print(f"\n📍 当前默认 GPU:{torch.cuda.current_device()}")
else:
print("\n⚠️ 未检测到可用的GPU,将使用CPU进行计算。")
print(" 提示:如需GPU加速,请安装CUDA版本的PyTorch。")
# 7. 快速测试:创建张量
print("\n🧪 快速测试:")
if torch.cuda.is_available():
x = torch.rand(3, 3).cuda()
print(f" GPU 张量:{x.device}")
else:
x = torch.rand(3, 3)
print(f" CPU 张量:{x.device}")
print(f" 张量内容:\n{x}")
</code></pre>
<div class="concept-box">
<h4><i class="fas fa-lightbulb"></i> 运行结果(有GPU时)</h4>
<pre><code>
==================================================
🔍 PyTorch GPU 检测工具
==================================================
📦 PyTorch 版本:2.1.0
🔥 CUDA 是否可用:True
🔥 CUDA 版本:12.1
🎮 GPU 数量:1
🎮 GPU 0: NVIDIA GeForce RTX 4090
总显存:24.0 GB
计算能力:8.9
多处理器数量:128
📍 当前默认 GPU:0
🧪 快速测试:
GPU 张量:cuda:0
张量内容:
tensor([[0.1234, 0.5678, 0.9012],
[0.3456, 0.7890, 0.2345],
[0.6789, 0.0123, 0.4567]], device='cuda:0')
</code></pre>
</div>
<h3>示例2:GPU vs CPU 性能对比</h3>
<pre><code class="language-python">
import torch
import time
print("=" * 50)
print("⚡ GPU vs CPU 性能对比")
print("=" * 50)
# 定义矩阵大小(越大越能看出差距)
size = 5000
# 创建随机矩阵
A_cpu = torch.randn(size, size)
B_cpu = torch.randn(size, size)
# ===== CPU 计算 =====
print(f"\n🖥️ CPU 计算(矩阵 {size}×{size})")
start = time.time()
C_cpu = torch.matmul(A_cpu, B_cpu)
cpu_time = time.time() - start
print(f" 耗时:{cpu_time:.3f} 秒")
# ===== GPU 计算 =====
if torch.cuda.is_available():
A_gpu = A_cpu.cuda()
B_gpu = B_cpu.cuda()
# 预热(第一次GPU调用通常较慢)
_ = torch.matmul(A_gpu, B_gpu)
torch.cuda.synchronize()
print(f"\n🎮 GPU 计算(矩阵 {size}×{size})")
start = time.time()
C_gpu = torch.matmul(A_gpu, B_gpu)
torch.cuda.synchronize() # 等待GPU完成
gpu_time = time.time() - start
print(f" 耗时:{gpu_time:.3f} 秒")
# 计算加速比
speedup = cpu_time / gpu_time
print(f"\n🚀 GPU 加速比:{speedup:.1f}x")
print(f" GPU 比 CPU 快了 {speedup:.1f} 倍!")
# 验证结果一致性
C_gpu_cpu = C_gpu.cpu()
max_diff = (C_cpu - C_gpu_cpu).abs().max()
print(f"\n✅ 结果一致性检查:最大差异 {max_diff:.6f}")
else:
print("\n⚠️ 未检测到GPU,跳过GPU性能对比。")
</code></pre>
<div class="concept-box">
<h4><i class="fas fa-lightbulb"></i> 运行结果(RTX 4090 示例)</h4>
<pre><code>
==================================================
⚡ GPU vs CPU 性能对比
==================================================
🖥️ CPU 计算(矩阵 5000×5000)
耗时:2.845 秒
🎮 GPU 计算(矩阵 5000×5000)
耗时:0.023 秒
🚀 GPU 加速比:123.7x
GPU 比 CPU 快了 123.7 倍!
✅ 结果一致性检查:最大差异 0.000031
</code></pre>
<p>(注:实际加速比因硬件配置而异,通常大型矩阵运算GPU可比CPU快数十到数百倍)</p>
</div>
<p>这两个示例展示了GPU在AI计算中的核心价值:</p>
<ul>
<li><strong>GPU检测</strong>:了解你的计算环境,确认CUDA和PyTorch是否正确配置</li>
<li><strong>性能对比</strong>:直观感受GPU在大规模矩阵运算中的加速效果</li>
</ul>
<p>在深度学习中,模型训练和推理都依赖大量的矩阵运算,因此GPU是AI开发的必备硬件。</p>
</div>
<!-- ============================================================ -->
</main>
</div>
<!-- 页脚 -->
<!-- ============================================================ -->
<footer class="footer">
<div class="footer-content">
<div class="footer-section">
<h3><i class="fas fa-graduation-cap"></i> AI学习笔记</h3>
<p>从零开始系统学习人工智能</p>
<p>涵盖前置基础、核心原理、工程实践、应用工具、进阶展望</p>
</div>
<div class="footer-section">
<h3><i class="fas fa-bookmark"></i> 内容导航</h3>
<p>第1-11章:基础与核心(见 ai-notes-core.html)</p>
<p>第12-23章:工程实践与进阶(本文件)</p>
<p>附录A:GPU与算力指南</p>
<p>附录B:术语表</p>
</div>
<div class="footer-section">
<h3><i class="fas fa-info-circle"></i> 说明</h3>
<p>本笔记仅供学习参考,内容如有错误欢迎指正。</p>
<p>AI技术发展迅速,部分信息可能随时间变化。</p>
</div>
</div>
<div class="footer-bottom">
<p>AI学习笔记 © 2024 | 持续更新中</p>
</div>
</footer>
</div><!-- /ai-notes-container -->
<!-- Scripts -->
<script src="../../js/mobile-menu.js"></script>
<script src="../../js/dark-mode.js"></script>
<script>
// 目录滚动高亮
(function() {
const tocLinks = document.querySelectorAll('.toc-sidebar a');
const sections = [];
tocLinks.forEach(link => {
const id = link.getAttribute('href');
if (id && id.startsWith('#')) {
const el = document.querySelector(id);
if (el) sections.push({ link, el });
}
});
function updateActive() {
const scrollY = window.scrollY + 100;
let current = sections[0];
for (const s of sections) {
if (s.el.offsetTop <= scrollY) current = s;
}
tocLinks.forEach(l => l.classList.remove('active'));
if (current) current.link.classList.add('active');
}
window.addEventListener('scroll', updateActive);
updateActive();
})();
</script>
</body>
</html>