|
21 | 21 | "metadata": {}, |
22 | 22 | "outputs": [], |
23 | 23 | "source": [ |
24 | | - "!pip install -q oxideon numpy torch" |
| 24 | + "!pip install -q oxideon" |
25 | 25 | ] |
26 | 26 | }, |
27 | 27 | { |
|
36 | 36 | "x = ox.Tensor.arange(0.0, 24.0, 1.0).reshape([2, 3, 4])\n", |
37 | 37 | "v = x.transpose(0, 2)\n", |
38 | 38 | "assert v.shares_storage(x)\n", |
39 | | - "print(\"v shape:\", v.shape(), \"shares_storage:\", v.shares_storage(x))\n", |
| 39 | + "print(\"v shape:\", list(v.shape), \"shares_storage:\", v.shares_storage(x))\n", |
40 | 40 | "\n", |
41 | 41 | "# 2. Einops-style rearrange & channels_last\n", |
42 | 42 | "img = ox.randn([2, 8, 4, 4])\n", |
43 | 43 | "nhwc = img.rearrange(\"b c h w -> b h w c\")\n", |
44 | 44 | "cl = img.to_channels_last()\n", |
45 | | - "print(\"rearranged shape:\", nhwc.shape(), \"is_channels_last:\", cl.is_channels_last())" |
| 45 | + "print(\"rearranged shape:\", list(nhwc.shape), \"is_channels_last:\", cl.is_channels_last())" |
46 | 46 | ] |
47 | 47 | }, |
48 | 48 | { |
|
98 | 98 | "metadata": {}, |
99 | 99 | "outputs": [], |
100 | 100 | "source": [ |
101 | | - "# 5. Quick CPU benchmark vs PyTorch (AdamW + Softmax + LayerNorm)\n", |
| 101 | + "# 5. Zero-copy NumPy / DLPack interop & quick CPU benchmark vs PyTorch\n", |
102 | 102 | "import time\n", |
103 | | - "import torch\n", |
104 | 103 | "\n", |
105 | 104 | "x_np = ox.randn([32, 128, 768]).numpy()\n", |
106 | 105 | "xo = ox.from_numpy(x_np)\n", |
107 | | - "xt = torch.from_numpy(x_np)\n", |
108 | | - "\n", |
109 | 106 | "for _ in range(3):\n", |
110 | 107 | " _ = xo.softmax(-1)\n", |
111 | | - " _ = torch.softmax(xt, -1)\n", |
112 | | - "\n", |
113 | 108 | "t0 = time.perf_counter()\n", |
114 | 109 | "for _ in range(20):\n", |
115 | 110 | " _ = xo.softmax(-1)\n", |
116 | 111 | "ox_ms = (time.perf_counter() - t0) / 20 * 1000\n", |
117 | 112 | "\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\")" |
| 113 | + "try:\n", |
| 114 | + " import torch\n", |
| 115 | + " xt = torch.utils.dlpack.from_dlpack(xo)\n", |
| 116 | + " for _ in range(3):\n", |
| 117 | + " _ = torch.softmax(xt, -1)\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\")\n", |
| 123 | + "except ImportError:\n", |
| 124 | + " print(f\"softmax [32,128,768]: Oxideon={ox_ms:.2f} ms (install torch to compare)\")" |
123 | 125 | ] |
124 | 126 | } |
125 | 127 | ], |
|
0 commit comments