Skip to content

Instantly share code, notes, and snippets.

@sjchoi86
Created February 26, 2026 16:52
Show Gist options
  • Select an option

  • Save sjchoi86/f9b37dfddc02dcfb39adcf4f73031e7a to your computer and use it in GitHub Desktop.

Select an option

Save sjchoi86/f9b37dfddc02dcfb39adcf4f73031e7a to your computer and use it in GitHub Desktop.
foundation_model_notebooks
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "deb52a72-f025-4b14-8f57-a8be049fb76c",
"metadata": {},
"source": [
"#### Check current device availability"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "28b5f9e3-2c3b-4807-a18a-123f801b9b92",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"torch:[2.10.0]\n"
]
}
],
"source": [
"import time\n",
"import torch\n",
"print (\"torch:[%s]\"%(torch.__version__))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "493a7567-45cf-49b2-a834-0ecc26d667cb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ready.\n"
]
}
],
"source": [
"def bench_matmul(dev,n=2048,warmup=10,iters=50):\n",
" dtype = torch.float32 if dev==\"cpu\" else torch.float16\n",
"\n",
" a = torch.randn((n,n),device=dev,dtype=dtype)\n",
" b = torch.randn((n,n),device=dev,dtype=dtype)\n",
"\n",
" for _ in range(warmup):\n",
" _ = a @ b\n",
" if dev==\"cuda\":\n",
" torch.cuda.synchronize()\n",
" elif dev==\"mps\":\n",
" torch.mps.synchronize()\n",
"\n",
" t0 = time.time()\n",
" for _ in range(iters):\n",
" _ = a @ b\n",
" if dev==\"cuda\":\n",
" torch.cuda.synchronize()\n",
" elif dev==\"mps\":\n",
" torch.mps.synchronize()\n",
" t1 = time.time()\n",
"\n",
" dt = (t1-t0)/float(iters)\n",
" tflops = (2.0*(n**3)/dt)/1e12\n",
" return dt,tflops\n",
"\n",
"print (\"Ready.\")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "6019d7d5-5f02-47bc-988a-81fa23511bb8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cpu:[True]\n",
"cuda:[False]\n",
"mps:[True]\n",
"mps_is_built:[True]\n"
]
}
],
"source": [
"# Check availability\n",
"cpu_ok = True\n",
"cuda_ok = torch.cuda.is_available()\n",
"mps_ok = (hasattr(torch.backends,\"mps\") and torch.backends.mps.is_available())\n",
"\n",
"print(\"cpu:[%s]\"%cpu_ok)\n",
"print(\"cuda:[%s]\"%cuda_ok)\n",
"print(\"mps:[%s]\"%mps_ok)\n",
"\n",
"if cuda_ok:\n",
" print(\"cuda_device_count:[%d]\"%torch.cuda.device_count())\n",
" print(\"cuda_device_name_0:[%s]\"%torch.cuda.get_device_name(0))\n",
"\n",
"if mps_ok:\n",
" print(\"mps_is_built:[%s]\"%torch.backends.mps.is_built())"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "be217dff-267a-4c7f-810a-3b38065e3a4e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"n = [2048]\n",
"FLOPs per matmul ≈ 2*n^3 = [17179869184]\n",
"TFLOPs ≈ (FLOPs / avg_time) / 1e12\n",
"[cpu] avg=[0.005192]s approx=[3.309] TFLOPs\n",
"[mps] avg=[0.001160]s approx=[14.805] TFLOPs\n"
]
}
],
"source": [
"n = 2048\n",
"flops = 2*(n**3)\n",
"print(\"n = [%d]\"%n)\n",
"print(\"FLOPs per matmul ≈ 2*n^3 = [%d]\"%flops)\n",
"print(\"TFLOPs ≈ (FLOPs / avg_time) / 1e12\")\n",
"\n",
"if cpu_ok:\n",
" dt,tflops = bench_matmul(\"cpu\",n=n,warmup=3,iters=10)\n",
" print(\"[cpu] avg=[%.6f]s approx=[%.3f] TFLOPs\" % (dt,tflops))\n",
"if cuda_ok:\n",
" dt,tflops = bench_matmul(\"cuda\",n=n,warmup=10,iters=50)\n",
" print(\"[cuda] avg=[%.6f]s approx=[%.3f] TFLOPs\" % (dt,tflops))\n",
"if mps_ok:\n",
" dt,tflops = bench_matmul(\"mps\",n=n,warmup=10,iters=50)\n",
" print(\"[mps] avg=[%.6f]s approx=[%.3f] TFLOPs\" % (dt,tflops))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f8af04a6-71e7-4260-b42e-e0ecebabef42",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.19"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment