diff --git a/README.md b/README.md index ddbfc8b..e2b89ab 100644 --- a/README.md +++ b/README.md @@ -428,66 +428,66 @@ test_flip = make_test("flip", flip, flip_spec, add_sizes=["i"]) # run_test(test_flip) ``` -## Puzzle 12 - compress +## Puzzle 12 - pad_to -Compute [compress](https://numpy.org/doc/stable/reference/generated/numpy.compress.html) - keep only masked entries (left-aligned). +Compute pad_to - eliminate or add 0s to change size of vector. ```python -def compress_spec(g, v, out): - j = 0 - for i in range(len(g)): - if g[i]: - out[j] = v[i] - j += 1 - -def compress(g: TT["i", bool], v: TT["i"], i:int) -> TT["i"]: +def pad_to_spec(a, out): + for i in range(min(len(out), len(a))): + out[i] = a[i] + + +def pad_to(a: TT["i"], i: int, j: int) -> TT["j"]: raise NotImplementedError -test_compress = make_test("compress", compress, compress_spec, add_sizes=["i"]) +test_pad_to = make_test("pad_to", pad_to, pad_to_spec, add_sizes=["i", "j"]) ``` -![svg](Tensor%20Puzzlers_files/Tensor%20Puzzlers_44_0.svg) +![svg](Tensor%20Puzzlers_files/Tensor%20Puzzlers_47_0.svg) ```python -# run_test(test_compress) +# run_test(test_pad_to) ``` -## Puzzle 13 - pad_to +## Puzzle 13 - compress -Compute pad_to - eliminate or add 0s to change size of vector. +Compute [compress](https://numpy.org/doc/stable/reference/generated/numpy.compress.html) - keep only masked entries (left-aligned). ```python -def pad_to_spec(a, out): - for i in range(min(len(out), len(a))): - out[i] = a[i] - - -def pad_to(a: TT["i"], i: int, j: int) -> TT["j"]: +def compress_spec(g, v, out): + j = 0 + for i in range(len(g)): + if g[i]: + out[j] = v[i] + j += 1 + +def compress(g: TT["i", bool], v: TT["i"], i:int) -> TT["i"]: raise NotImplementedError -test_pad_to = make_test("pad_to", pad_to, pad_to_spec, add_sizes=["i", "j"]) +test_compress = make_test("compress", compress, compress_spec, add_sizes=["i"]) ``` -![svg](Tensor%20Puzzlers_files/Tensor%20Puzzlers_47_0.svg) +![svg](Tensor%20Puzzlers_files/Tensor%20Puzzlers_44_0.svg) ```python -# run_test(test_pad_to) +# run_test(test_compress) ``` ## Puzzle 14 - sequence_mask diff --git a/Tensor Puzzlers.ipynb b/Tensor Puzzlers.ipynb index 8c2019b..c7c7005 100644 --- a/Tensor Puzzlers.ipynb +++ b/Tensor Puzzlers.ipynb @@ -1567,10 +1567,70 @@ }, { "cell_type": "markdown", - "id": "fa4469eb", - "metadata": {}, + "id": "6c1517a4119f7045", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "## Puzzle 12 - pad_to\n", + "\n", + "\n", + "Compute pad_to - eliminate or add 0s to change size of vector." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ae9bf2c156964e0a", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], + "source": [ + "def pad_to_spec(a, out):\n", + " for i in range(min(len(out), len(a))):\n", + " out[i] = a[i]\n", + "\n", + "\n", + "def pad_to(a: TT[\"i\"], i: int, j: int) -> TT[\"j\"]:\n", + " raise NotImplementedError\n", + "\n", + "\n", + "test_pad_to = make_test(\"pad_to\", pad_to, pad_to_spec, add_sizes=[\"i\", \"j\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c5be502f294385f4", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "outputs": [], "source": [ - "## Puzzle 12 - compress\n", + "# run_test(test_pad_to)" + ] + }, + { + "cell_type": "markdown", + "id": "5b4ab83cd83cc3", + "metadata": { + "collapsed": false, + "jupyter": { + "outputs_hidden": false + } + }, + "source": [ + "## Puzzle 13 - compress\n", "\n", "\n", "Compute [compress](https://numpy.org/doc/stable/reference/generated/numpy.compress.html) - keep only masked entries (left-aligned)." @@ -1647,85 +1707,6 @@ "# run_test(test_compress)" ] }, - { - "cell_type": "markdown", - "id": "0e944d79", - "metadata": { - "lines_to_next_cell": 2 - }, - "source": [ - "## Puzzle 13 - pad_to\n", - "\n", - "\n", - "Compute pad_to - eliminate or add 0s to change size of vector." - ] - }, - { - "cell_type": "code", - "execution_count": 30, - "id": "52988720", - "metadata": { - "execution": { - "iopub.execute_input": "2022-07-13T00:15:30.064726Z", - "iopub.status.busy": "2022-07-13T00:15:30.064418Z", - "iopub.status.idle": "2022-07-13T00:15:31.162878Z", - "shell.execute_reply": "2022-07-13T00:15:31.162268Z" - }, - "id": "-DsZHgOTroVN" - }, - "outputs": [ - { - "data": { - "image/svg+xml": [ - "pad_toatarget" - ], - "text/plain": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], - "source": [ - "def pad_to_spec(a, out):\n", - " for i in range(min(len(out), len(a))):\n", - " out[i] = a[i]\n", - "\n", - "\n", - "def pad_to(a: TT[\"i\"], i: int, j: int) -> TT[\"j\"]:\n", - " raise NotImplementedError\n", - "\n", - "\n", - "test_pad_to = make_test(\"pad_to\", pad_to, pad_to_spec, add_sizes=[\"i\", \"j\"])" - ] - }, - { - "cell_type": "code", - "execution_count": 31, - "id": "f8e5aee3", - "metadata": { - "execution": { - "iopub.execute_input": "2022-07-13T00:15:31.166060Z", - "iopub.status.busy": "2022-07-13T00:15:31.165749Z", - "iopub.status.idle": "2022-07-13T00:15:31.169131Z", - "shell.execute_reply": "2022-07-13T00:15:31.168580Z" - } - }, - "outputs": [], - "source": [ - "# run_test(test_pad_to)" - ] - }, { "cell_type": "markdown", "id": "aee589be", @@ -2283,12 +2264,27 @@ "def repeat(a: TT[\"i\"], d: TT[1]) -> TT[\"d\", \"i\"]:\n", " raise NotImplementedError\n", "\n", - "test_repeat = make_test(\"repeat\", repeat, repeat_spec, constraint=constraint_set)\n", - "\n", + "test_repeat = make_test(\"repeat\", repeat, repeat_spec, constraint=constraint_set)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4633aa19-3b1b-46fa-a8de-d9644017d212", + "metadata": {}, + "outputs": [], + "source": [ + "run_test(test_repeat)" + ] + }, + { + "cell_type": "markdown", + "id": "48faa010-3410-4c56-97a7-a8c3cffe31ec", + "metadata": {}, + "source": [ + "## Puzzle 21 - bucketize\n", "\n", - "# ## Puzzle 21 - bucketize\n", - "#\n", - "# Compute [bucketize](https://pytorch.org/docs/stable/generated/torch.bucketize.html)" + "Compute [bucketize](https://pytorch.org/docs/stable/generated/torch.bucketize.html)" ] }, { @@ -2351,11 +2347,27 @@ "test_bucketize = make_test(\"bucketize\", bucketize, bucketize_spec,\n", " constraint=constraint_set)\n", "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fb653685-584e-4766-8f7e-022bbc548585", + "metadata": {}, + "outputs": [], + "source": [ + "run_test(test_bucketize)" + ] + }, + { + "cell_type": "markdown", + "id": "ab451e9e-ace5-40b7-b711-cb602cf9c91a", + "metadata": {}, + "source": [ + "## Speed Run Mode!\n", "\n", - "#\n", - "# # Speed Run Mode!\n", - "#\n", - "# What is the smallest you can make each of these?" + "What is the smallest you can make each of these?" ] }, { @@ -2414,9 +2426,9 @@ "cell_metadata_filter": "id,-all" }, "kernelspec": { - "display_name": "tensor_puzzles", + "display_name": "Python 3 (ipykernel)", "language": "python", - "name": "tensor_puzzles" + "name": "python3" }, "language_info": { "codemirror_mode": { @@ -2428,7 +2440,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.10" + "version": "3.11.6" } }, "nbformat": 4,