Skip to content

Instantly share code, notes, and snippets.

@dnlcrl
Created May 2, 2018 16:36
Show Gist options
  • Save dnlcrl/4245f297a178aebc5754b7dcd4b8ac31 to your computer and use it in GitHub Desktop.
Save dnlcrl/4245f297a178aebc5754b7dcd4b8ac31 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import torch\n",
"a = torch.arange(6000000).reshape(2000, 3000)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"TRANSPOSE"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.9 µs ± 88.6 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%timeit torch.einsum('ij->ji', [a])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.22 µs ± 83.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
]
}
],
"source": [
"%timeit a.t()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"SUM"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.87 ms ± 72.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit torch.einsum('ij->', [a])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.69 ms ± 97.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit a.sum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"COLUMN SUM"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25.4 ms ± 767 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit torch.einsum('ij->j', [a])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.88 ms ± 71.4 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit torch.sum(a, 0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"ROW SUM"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.95 ms ± 360 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit torch.einsum('ij->i', [a])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.66 ms ± 106 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit torch.sum(a, 1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"MATRIX-MATRIX MULTIPLICATION"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"a = torch.arange(6000000).reshape(2000, 3000)\n",
"b = torch.arange(15000000).reshape(3000, 5000)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"176 ms ± 4.79 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit torch.einsum('ik,kj->ij', [a, b])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"189 ms ± 11.6 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit torch.mm(a, b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"MATRIX-VECTOR MULTIPLICATION"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"a = torch.arange(6000000).reshape(2000, 3000)\n",
"b = torch.arange(3000)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"840 µs ± 14.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit torch.einsum('ik,k->i', [a, b])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"798 µs ± 4.41 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit torch.mv(a,b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"DOT PRODUCT"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"vector"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"a = torch.arange(3000)\n",
"b = torch.arange(3000,6000) "
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"42 µs ± 135 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%timeit torch.einsum('i,i->', [a, b])"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.93 µs ± 36.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%timeit torch.dot(a,b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"matrix"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"a = torch.arange(6000000).reshape(2000, 3000)\n",
"b = torch.arange(6000000,12000000).reshape(2000, 3000)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"42.2 ms ± 493 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit torch.einsum('ij,ij->', [a, b])"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2.88 ms ± 27.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit torch.dot(a.view(-1),b.view(-1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"HADAMARD PRODUCT"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"a = torch.arange(6000000).reshape(2000, 3000)\n",
"b = torch.arange(6000000,12000000).reshape(2000, 3000)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.74 ms ± 83 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit torch.einsum('ij,ij->ij', [a, b])"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.75 ms ± 158 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit a*b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"OUTER PRODUCT"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"a = torch.arange(3000)\n",
"b = torch.arange(3000,7000)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"17.9 ms ± 333 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit torch.einsum('i,j->ij', [a, b])"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"16.1 ms ± 806 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit torch.ger(a, b)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"BATCH MATRIX MULTIPLICATION"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"a = torch.randn(300,2000,5000)\n",
"b = torch.randn(300,5000,3000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%timeit torch.einsum('ijk,ikl->ijl', [a, b])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%timeit a.bmm(b)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment