feat: 添加MPS支持示例并重置执行计数
Browse files添加新的mps.ipynb文件展示如何在Apple Silicon GPU上使用PyTorch的MPS加速
重置conversation.ipynb中的执行计数为null
- conversation.ipynb +1 -1
- mps.ipynb +119 -0
conversation.ipynb
CHANGED
|
@@ -33,7 +33,7 @@
|
|
| 33 |
},
|
| 34 |
{
|
| 35 |
"cell_type": "code",
|
| 36 |
-
"execution_count":
|
| 37 |
"id": "e0022b61",
|
| 38 |
"metadata": {},
|
| 39 |
"outputs": [
|
|
|
|
| 33 |
},
|
| 34 |
{
|
| 35 |
"cell_type": "code",
|
| 36 |
+
"execution_count": null,
|
| 37 |
"id": "e0022b61",
|
| 38 |
"metadata": {},
|
| 39 |
"outputs": [
|
mps.ipynb
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 2,
|
| 6 |
+
"id": "3e83459e",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stdout",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"MPS available: True\n",
|
| 14 |
+
"Tensor created on device: mps:0\n",
|
| 15 |
+
"tensor([[1., 1., 1.],\n",
|
| 16 |
+
" [1., 1., 1.]], device='mps:0')\n"
|
| 17 |
+
]
|
| 18 |
+
}
|
| 19 |
+
],
|
| 20 |
+
"source": [
|
| 21 |
+
"import torch\n",
|
| 22 |
+
"\n",
|
| 23 |
+
"# 检查 MPS 是否可用\n",
|
| 24 |
+
"print(f\"MPS available: {torch.backends.mps.is_available()}\")\n",
|
| 25 |
+
"\n",
|
| 26 |
+
"# 如果可用,可以进行一个简单的测试\n",
|
| 27 |
+
"if torch.backends.mps.is_available():\n",
|
| 28 |
+
" device = torch.device(\"mps\")\n",
|
| 29 |
+
" x = torch.ones(2, 3, device=device)\n",
|
| 30 |
+
" print(f\"Tensor created on device: {x.device}\")\n",
|
| 31 |
+
" print(x)"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"cell_type": "code",
|
| 36 |
+
"execution_count": 3,
|
| 37 |
+
"id": "1b11398e",
|
| 38 |
+
"metadata": {},
|
| 39 |
+
"outputs": [
|
| 40 |
+
{
|
| 41 |
+
"name": "stdout",
|
| 42 |
+
"output_type": "stream",
|
| 43 |
+
"text": [
|
| 44 |
+
"✅ 检测到 Apple Silicon GPU (MPS),加速已就绪!\n",
|
| 45 |
+
"\n",
|
| 46 |
+
"👇 创建的张量内容如下:\n",
|
| 47 |
+
"tensor([[-1.1299, 0.7376, 0.9677],\n",
|
| 48 |
+
" [ 1.1327, 0.1566, -1.4688]], device='mps:0')\n",
|
| 49 |
+
"\n",
|
| 50 |
+
"📍 当前张量所在的设备是: mps:0\n"
|
| 51 |
+
]
|
| 52 |
+
}
|
| 53 |
+
],
|
| 54 |
+
"source": [
|
| 55 |
+
"import torch\n",
|
| 56 |
+
"\n",
|
| 57 |
+
"# 1. 自动检测设备:如果支持 MPS 则使用 mps,否则使用 cpu\n",
|
| 58 |
+
"if torch.backends.mps.is_available():\n",
|
| 59 |
+
" device = torch.device(\"mps\")\n",
|
| 60 |
+
" print(\"✅ 检测到 Apple Silicon GPU (MPS),加速已就绪!\")\n",
|
| 61 |
+
"else:\n",
|
| 62 |
+
" device = torch.device(\"cpu\")\n",
|
| 63 |
+
" print(\"⚠️ MPS 不可用,当前使用 CPU 运行。\")\n",
|
| 64 |
+
"\n",
|
| 65 |
+
"# 2. 在指定设备上创建一个随机张量 (形状为 2x3)\n",
|
| 66 |
+
"# 这里的 torch.randn 会生成符合标准正态分布的随机数\n",
|
| 67 |
+
"x = torch.randn(2, 3, device=device)\n",
|
| 68 |
+
"\n",
|
| 69 |
+
"# 3. 打印张量\n",
|
| 70 |
+
"print(\"\\n👇 创建的张量内容如下:\")\n",
|
| 71 |
+
"print(x)\n",
|
| 72 |
+
"\n",
|
| 73 |
+
"# 4. 额外验证:打印设备信息,确保它真的在 mps 上\n",
|
| 74 |
+
"print(f\"\\n📍 当前张量所在的设备是: {x.device}\")"
|
| 75 |
+
]
|
| 76 |
+
},
|
| 77 |
+
{
|
| 78 |
+
"cell_type": "code",
|
| 79 |
+
"execution_count": 4,
|
| 80 |
+
"id": "3a7269bb",
|
| 81 |
+
"metadata": {},
|
| 82 |
+
"outputs": [
|
| 83 |
+
{
|
| 84 |
+
"name": "stdout",
|
| 85 |
+
"output_type": "stream",
|
| 86 |
+
"text": [
|
| 87 |
+
"tensor([[ 0.2961, 0.5715, 0.3610],\n",
|
| 88 |
+
" [ 0.6116, 2.6332, -0.5906]])\n"
|
| 89 |
+
]
|
| 90 |
+
}
|
| 91 |
+
],
|
| 92 |
+
"source": [
|
| 93 |
+
"x = torch.randn(2, 3)\n",
|
| 94 |
+
"print(x)\n"
|
| 95 |
+
]
|
| 96 |
+
}
|
| 97 |
+
],
|
| 98 |
+
"metadata": {
|
| 99 |
+
"kernelspec": {
|
| 100 |
+
"display_name": "hgface",
|
| 101 |
+
"language": "python",
|
| 102 |
+
"name": "python3"
|
| 103 |
+
},
|
| 104 |
+
"language_info": {
|
| 105 |
+
"codemirror_mode": {
|
| 106 |
+
"name": "ipython",
|
| 107 |
+
"version": 3
|
| 108 |
+
},
|
| 109 |
+
"file_extension": ".py",
|
| 110 |
+
"mimetype": "text/x-python",
|
| 111 |
+
"name": "python",
|
| 112 |
+
"nbconvert_exporter": "python",
|
| 113 |
+
"pygments_lexer": "ipython3",
|
| 114 |
+
"version": "3.13.12"
|
| 115 |
+
}
|
| 116 |
+
},
|
| 117 |
+
"nbformat": 4,
|
| 118 |
+
"nbformat_minor": 5
|
| 119 |
+
}
|