|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Oxideon Quickstart\n", |
| 8 | + "\n", |
| 9 | + "Run **Oxideon** (`pip install oxideon`) directly in Google Colab or Jupyter:\n", |
| 10 | + "- Zero-copy strided views & higher-order autograd (`create_graph=True`)\n", |
| 11 | + "- Training a neural network with `AdamW` & `LayerNorm`\n", |
| 12 | + "- Zero-copy NumPy / DLPack interop & Einops `rearrange`\n", |
| 13 | + "- Quick CPU speed check vs PyTorch\n", |
| 14 | + "\n", |
| 15 | + "Repo: https://github.com/voidstackloop/oxideon" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": null, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "!pip install -q oxideon numpy torch" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": null, |
| 30 | + "metadata": {}, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "import oxideon as ox\n", |
| 34 | + "\n", |
| 35 | + "# 1. Zero-copy strided views\n", |
| 36 | + "x = ox.Tensor.arange(0.0, 24.0, 1.0).reshape([2, 3, 4])\n", |
| 37 | + "v = x.transpose(0, 2)\n", |
| 38 | + "assert v.shares_storage(x)\n", |
| 39 | + "print(\"v shape:\", v.shape(), \"shares_storage:\", v.shares_storage(x))\n", |
| 40 | + "\n", |
| 41 | + "# 2. Einops-style rearrange & channels_last\n", |
| 42 | + "img = ox.randn([2, 8, 4, 4])\n", |
| 43 | + "nhwc = img.rearrange(\"b c h w -> b h w c\")\n", |
| 44 | + "cl = img.to_channels_last()\n", |
| 45 | + "print(\"rearranged shape:\", nhwc.shape(), \"is_channels_last:\", cl.is_channels_last())" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": null, |
| 51 | + "metadata": {}, |
| 52 | + "outputs": [], |
| 53 | + "source": [ |
| 54 | + "# 3. Higher-order autograd (1st, 2nd, and 3rd derivatives)\n", |
| 55 | + "x = ox.Tensor.from_slice([3.0], [1], requires_grad=True)\n", |
| 56 | + "y = x.powf(4.0)\n", |
| 57 | + "dy = ox.grad([y], [x], create_graph=True)[0]\n", |
| 58 | + "d2y = ox.grad([dy], [x], create_graph=True)[0]\n", |
| 59 | + "d3y = ox.grad([d2y], [x])[0]\n", |
| 60 | + "print(\"y=x^4 at x=3 -> dy:\", dy.to_list(), \"d2y:\", d2y.to_list(), \"d3y:\", d3y.to_list())" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": null, |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "# 4. Train a compact MLP with AdamW\n", |
| 70 | + "class MLP(ox.nn.Module):\n", |
| 71 | + " def __init__(self, d=64):\n", |
| 72 | + " super().__init__()\n", |
| 73 | + " self.fc1 = ox.nn.Linear(16, d)\n", |
| 74 | + " self.ln = ox.nn.LayerNorm(d)\n", |
| 75 | + " self.fc2 = ox.nn.Linear(d, 4)\n", |
| 76 | + "\n", |
| 77 | + " def forward(self, x):\n", |
| 78 | + " return self.fc2(self.ln(self.fc1(x)).gelu())\n", |
| 79 | + "\n", |
| 80 | + "model = MLP()\n", |
| 81 | + "opt = ox.optim.AdamW(model.parameters(), lr=1e-2)\n", |
| 82 | + "xs = ox.randn([32, 16])\n", |
| 83 | + "ys = ox.randn([32, 4])\n", |
| 84 | + "\n", |
| 85 | + "for step in range(40):\n", |
| 86 | + " opt.zero_grad()\n", |
| 87 | + " pred = model(xs)\n", |
| 88 | + " loss = ((pred - ys) * (pred - ys)).mean()\n", |
| 89 | + " loss.backward()\n", |
| 90 | + " opt.step()\n", |
| 91 | + " if step in (0, 19, 39):\n", |
| 92 | + " print(f\"step {step:2d}: loss = {loss.item():.6f}\")" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": null, |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [], |
| 100 | + "source": [ |
| 101 | + "# 5. Quick CPU benchmark vs PyTorch (AdamW + Softmax + LayerNorm)\n", |
| 102 | + "import time\n", |
| 103 | + "import torch\n", |
| 104 | + "\n", |
| 105 | + "x_np = ox.randn([32, 128, 768]).numpy()\n", |
| 106 | + "xo = ox.from_numpy(x_np)\n", |
| 107 | + "xt = torch.from_numpy(x_np)\n", |
| 108 | + "\n", |
| 109 | + "for _ in range(3):\n", |
| 110 | + " _ = xo.softmax(-1)\n", |
| 111 | + " _ = torch.softmax(xt, -1)\n", |
| 112 | + "\n", |
| 113 | + "t0 = time.perf_counter()\n", |
| 114 | + "for _ in range(20):\n", |
| 115 | + " _ = xo.softmax(-1)\n", |
| 116 | + "ox_ms = (time.perf_counter() - t0) / 20 * 1000\n", |
| 117 | + "\n", |
| 118 | + "t0 = time.perf_counter()\n", |
| 119 | + "for _ in range(20):\n", |
| 120 | + " _ = torch.softmax(xt, -1)\n", |
| 121 | + "pt_ms = (time.perf_counter() - t0) / 20 * 1000\n", |
| 122 | + "print(f\"softmax [32,128,768]: Oxideon={ox_ms:.2f} ms, PyTorch={pt_ms:.2f} ms\")" |
| 123 | + ] |
| 124 | + } |
| 125 | + ], |
| 126 | + "metadata": { |
| 127 | + "kernelspec": { |
| 128 | + "display_name": "Python 3", |
| 129 | + "language": "python", |
| 130 | + "name": "python3" |
| 131 | + }, |
| 132 | + "language_info": { |
| 133 | + "name": "python", |
| 134 | + "version": "3.11.0" |
| 135 | + } |
| 136 | + }, |
| 137 | + "nbformat": 4, |
| 138 | + "nbformat_minor": 4 |
| 139 | +} |
0 commit comments