doglilly2/soe / antigravity /GPU_Setup_Colab.ipynb
doglilly2's picture
download
raw
5.84 kB
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"gpuType": "T4"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 🚀 GPU Setup สำหรับ Google Colab\n",
"\n",
"---\n",
"\n",
"## ⚙️ ขั้นตอนก่อนรันโค้ด (ทำครั้งเดียว)\n",
"\n",
"1. ไปที่เมนู **Runtime** → **Change runtime type**\n",
"2. ที่ **Hardware accelerator** เลือก **T4 GPU**\n",
"3. กด **Save**\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🔍 Cell 1: ตรวจสอบสถานะ GPU ด้วย nvidia-smi"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"# ตรวจสอบสถานะ GPU\n",
"!nvidia-smi"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🔍 Cell 2: ตรวจสอบ GPU ผ่าน PyTorch"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"import torch\n",
"\n",
"# ตรวจสอบว่ามี GPU หรือไม่\n",
"gpu_available = torch.cuda.is_available()\n",
"print(f\"✅ GPU พร้อมใช้งาน: {gpu_available}\")\n",
"\n",
"if gpu_available:\n",
" print(f\"🖥️ ชื่อ GPU : {torch.cuda.get_device_name(0)}\")\n",
" print(f\"💾 VRAM ทั้งหมด : {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB\")\n",
" print(f\"🔢 จำนวน GPU : {torch.cuda.device_count()}\")\n",
" print(f\"📌 CUDA Version : {torch.version.cuda}\")\n",
"else:\n",
" print(\"❌ ไม่พบ GPU กรุณาตั้งค่า Runtime → Change runtime type → T4 GPU\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🔍 Cell 3: ตรวจสอบ RAM ของ GPU"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"# ดูรายละเอียด VRAM\n",
"!nvidia-smi --query-gpu=name,memory.total,memory.free,memory.used --format=csv,noheader,nounits"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ⚡ Cell 4: ทดสอบความเร็ว GPU vs CPU"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"import torch\n",
"import time\n",
"\n",
"size = 10000\n",
"a = torch.randn(size, size)\n",
"b = torch.randn(size, size)\n",
"\n",
"# ทดสอบ CPU\n",
"start = time.time()\n",
"c_cpu = torch.mm(a, b)\n",
"cpu_time = time.time() - start\n",
"print(f\"🖥️ CPU ใช้เวลา: {cpu_time:.3f} วินาที\")\n",
"\n",
"# ทดสอบ GPU\n",
"if torch.cuda.is_available():\n",
" a_gpu = a.cuda()\n",
" b_gpu = b.cuda()\n",
"\n",
" # Warm up\n",
" _ = torch.mm(a_gpu, b_gpu)\n",
" torch.cuda.synchronize()\n",
"\n",
" start = time.time()\n",
" c_gpu = torch.mm(a_gpu, b_gpu)\n",
" torch.cuda.synchronize()\n",
" gpu_time = time.time() - start\n",
"\n",
" print(f\"🚀 GPU ใช้เวลา: {gpu_time:.3f} วินาที\")\n",
" print(f\"⚡ GPU เร็วกว่า CPU: {cpu_time / gpu_time:.1f}x\")\n",
"else:\n",
" print(\"❌ ไม่มี GPU สำหรับการทดสอบ\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 📊 Cell 5: ตรวจสอบ VRAM แบบ Real-time"
]
},
{
"cell_type": "code",
"metadata": {},
"source": [
"import torch\n",
"\n",
"def show_gpu_memory():\n",
" if torch.cuda.is_available():\n",
" allocated = torch.cuda.memory_allocated(0) / 1024**2\n",
" reserved = torch.cuda.memory_reserved(0) / 1024**2\n",
" total = torch.cuda.get_device_properties(0).total_memory / 1024**2\n",
" free = total - reserved\n",
" print(f\"📦 VRAM ที่ใช้อยู่ : {allocated:.1f} MB\")\n",
" print(f\"🔒 VRAM ที่จอง : {reserved:.1f} MB\")\n",
" print(f\"✅ VRAM ที่เหลือ : {free:.1f} MB\")\n",
" print(f\"💾 VRAM ทั้งหมด : {total:.1f} MB\")\n",
" else:\n",
" print(\"❌ ไม่พบ GPU\")\n",
"\n",
"show_gpu_memory()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"## 💡 Tips เพิ่มเติม\n",
"\n",
"| คำสั่ง | ความหมาย |\n",
"|--------|----------|\n",
"| `!nvidia-smi` | ดูสถานะ GPU ทั้งหมด |\n",
"| `torch.cuda.empty_cache()` | ล้าง VRAM Cache |\n",
"| `torch.cuda.is_available()` | เช็คว่ามี GPU ไหม |\n",
"| `.cuda()` | ย้าย Tensor ไปที่ GPU |\n",
"| `.cpu()` | ย้าย Tensor กลับมา CPU |\n",
"\n",
"> **หมายเหตุ:** Colab Free มีเวลา GPU จำกัด หากต้องการเวลานานขึ้น ให้อัปเกรดเป็น **Colab Pro**"
]
}
]
}

Xet Storage Details

Size:
5.84 kB
·
Xet hash:
3ae8a7b6aea400c942668aee96f4dbf5e6ca6cec27c4c582c0127134d73f2e9c

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.