{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# For tips on running notebooks in Google Colab, see\n# https://codelin.vip/beginner/colab\n%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Prototype) MaskedTensor Sparsity\n=================================\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Before working on this tutorial, please make sure to review our\n[MaskedTensor Overview tutorial\n\\]{.title-ref}.\n\nIntroduction\n============\n\nSparsity has been an area of rapid growth and importance within PyTorch;\nif any sparsity terms are confusing below, please refer to the [sparsity\ntutorial](https://pytorch.org/docs/stable/sparse.html) for additional\ndetails.\n\nSparse storage formats have been proven to be powerful in a variety of\nways. As a primer, the first use case most practitioners think about is\nwhen the majority of elements are equal to zero (a high degree of\nsparsity), but even in cases of lower sparsity, certain formats (e.g.\nBSR) can take advantage of substructures within a matrix.\n\n```{=html}\n
NOTE:
\n```\n```{=html}\n
\n```\n```{=html}\n

At the moment, MaskedTensor supports COO and CSR tensors with plans to support additional formats(such as BSR and CSC) in the future. If you have any requests for additional formats,please file a feature request here!

\n```\n```{=html}\n
\n```\nPrinciples\n==========\n\nWhen creating a `MaskedTensor`{.interpreted-text role=\"class\"} with\nsparse tensors, there are a few principles that must be observed:\n\n1. `data` and `mask` must have the same storage format, whether that\\'s\n `torch.strided`{.interpreted-text role=\"attr\"},\n `torch.sparse_coo`{.interpreted-text role=\"attr\"}, or\n `torch.sparse_csr`{.interpreted-text role=\"attr\"}\n2. `data` and `mask` must have the same size, indicated by\n `size()`{.interpreted-text role=\"func\"}\n\nSparse COO tensors\n==================\n\nIn accordance with Principle \\#1, a sparse COO MaskedTensor is created\nby passing in two sparse COO tensors, which can be initialized by any of\nits constructors, for example\n`torch.sparse_coo_tensor`{.interpreted-text role=\"func\"}.\n\nAs a recap of [sparse COO\ntensors](https://pytorch.org/docs/stable/sparse.html#sparse-coo-tensors),\nthe COO format stands for \\\"coordinate format\\\", where the specified\nelements are stored as tuples of their indices and the corresponding\nvalues. That is, the following are provided:\n\n- `indices`: array of size `(ndim, nse)` and dtype `torch.int64`\n- `values`: array of size [(nse,)]{.title-ref} with any integer or\n floating point dtype\n\nwhere `ndim` is the dimensionality of the tensor and `nse` is the number\nof specified elements.\n\nFor both sparse COO and CSR tensors, you can construct a\n`MaskedTensor`{.interpreted-text role=\"class\"} by doing either:\n\n1. `masked_tensor(sparse_tensor_data, sparse_tensor_mask)`\n2. `dense_masked_tensor.to_sparse_coo()` or\n `dense_masked_tensor.to_sparse_csr()`\n\nThe second method is easier to illustrate so we\\'ve shown that below,\nbut for more on the first and the nuances behind the approach, please\nread the `Sparse COO Appendix `{.interpreted-text\nrole=\"ref\"}.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import torch\nfrom torch.masked import masked_tensor\nimport warnings\n\n# Disable prototype warnings and such\nwarnings.filterwarnings(action='ignore', category=UserWarning)\n\nvalues = torch.tensor([[0, 0, 3], [4, 0, 5]])\nmask = torch.tensor([[False, False, True], [False, False, True]])\nmt = masked_tensor(values, mask)\nsparse_coo_mt = mt.to_sparse_coo()\n\nprint(\"mt:\\n\", mt)\nprint(\"mt (sparse coo):\\n\", sparse_coo_mt)\nprint(\"mt data (sparse coo):\\n\", sparse_coo_mt.get_data())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sparse CSR tensors\n==================\n\nSimilarly, `MaskedTensor`{.interpreted-text role=\"class\"} also supports\nthe [CSR (Compressed Sparse\nRow)](https://pytorch.org/docs/stable/sparse.html#sparse-csr-tensor)\nsparse tensor format. Instead of storing the tuples of the indices like\nsparse COO tensors, sparse CSR tensors aim to decrease the memory\nrequirements by storing compressed row indices. In particular, a CSR\nsparse tensor consists of three 1-D tensors:\n\n- `crow_indices`: array of compressed row indices with size\n `(size[0] + 1,)`. This array indicates which row a given entry in\n values lives in. The last element is the number of specified\n elements, while [crow\\_indices\\[i+1\\] -\n crow\\_indices\\[i\\]]{.title-ref} indicates the number of specified\n elements in row i.\n- `col_indices`: array of size `(nnz,)`. Indicates the column indices\n for each value.\n- `values`: array of size `(nnz,)`. Contains the values of the CSR\n tensor.\n\nOf note, both sparse COO and CSR tensors are in a\n[beta](https://pytorch.org/docs/stable/index.html) state.\n\nBy way of example:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mt_sparse_csr = mt.to_sparse_csr()\n\nprint(\"mt (sparse csr):\\n\", mt_sparse_csr)\nprint(\"mt data (sparse csr):\\n\", mt_sparse_csr.get_data())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Supported Operations\n====================\n\nUnary\n-----\n\nAll [unary\noperators](https://pytorch.org/docs/master/masked.html#unary-operators)\nare supported, e.g.:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mt.sin()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Binary\n======\n\n[Binary\noperators](https://pytorch.org/docs/master/masked.html#unary-operators)\nare also supported, but the input masks from the two masked tensors must\nmatch. For more information on why this decision was made, please find\nour [MaskedTensor: Advanced Semantics\ntutorial](https://pytorch.org/tutorials/prototype/maskedtensor_advanced_semantics.html).\n\nPlease find an example below:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "i = [[0, 1, 1],\n [2, 0, 2]]\nv1 = [3, 4, 5]\nv2 = [20, 30, 40]\nm = torch.tensor([True, False, True])\n\ns1 = torch.sparse_coo_tensor(i, v1, (2, 3))\ns2 = torch.sparse_coo_tensor(i, v2, (2, 3))\nmask = torch.sparse_coo_tensor(i, m, (2, 3))\n\nmt1 = masked_tensor(s1, mask)\nmt2 = masked_tensor(s2, mask)\n\nprint(\"mt1:\\n\", mt1)\nprint(\"mt2:\\n\", mt2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"torch.div(mt2, mt1):\\n\", torch.div(mt2, mt1))\nprint(\"torch.mul(mt1, mt2):\\n\", torch.mul(mt1, mt2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reductions\n==========\n\nFinally,\n[reductions](https://pytorch.org/docs/master/masked.html#reductions) are\nsupported:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mt" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"mt.sum():\\n\", mt.sum())\nprint(\"mt.sum(dim=1):\\n\", mt.sum(dim=1))\nprint(\"mt.amin():\\n\", mt.amin())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "MaskedTensor Helper Methods\n===========================\n\nFor convenience, `MaskedTensor`{.interpreted-text role=\"class\"} has a\nnumber of methods to help convert between the different layouts and\nidentify the current layout:\n\nSetup:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "v = [[3, 0, 0],\n [0, 4, 5]]\nm = [[True, False, False],\n [False, True, True]]\n\nmt = masked_tensor(torch.tensor(v), torch.tensor(m))\nmt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`MaskedTensor.to_sparse_coo()`{.interpreted-text role=\"meth\"} /\n`MaskedTensor.to_sparse_csr()`{.interpreted-text role=\"meth\"} /\n`MaskedTensor.to_dense()`{.interpreted-text role=\"meth\"} to help convert\nbetween the different layouts.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "mt_sparse_coo = mt.to_sparse_coo()\nmt_sparse_csr = mt.to_sparse_csr()\nmt_dense = mt_sparse_coo.to_dense()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`MaskedTensor.is_sparse`{.interpreted-text role=\"meth\"} \\-- this will\ncheck if the `MaskedTensor`{.interpreted-text role=\"class\"}\\'s layout\nmatches any of the supported sparse layouts (currently COO and CSR).\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"mt_dense.is_sparse: \", mt_dense.is_sparse)\nprint(\"mt_sparse_coo.is_sparse: \", mt_sparse_coo.is_sparse)\nprint(\"mt_sparse_csr.is_sparse: \", mt_sparse_csr.is_sparse)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`MaskedTensor.is_sparse_coo()`{.interpreted-text role=\"meth\"}\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"mt_dense.is_sparse_coo(): \", mt_dense.is_sparse_coo())\nprint(\"mt_sparse_coo.is_sparse_coo: \", mt_sparse_coo.is_sparse_coo())\nprint(\"mt_sparse_csr.is_sparse_coo: \", mt_sparse_csr.is_sparse_coo())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`MaskedTensor.is_sparse_csr()`{.interpreted-text role=\"meth\"}\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"mt_dense.is_sparse_csr(): \", mt_dense.is_sparse_csr())\nprint(\"mt_sparse_coo.is_sparse_csr: \", mt_sparse_coo.is_sparse_csr())\nprint(\"mt_sparse_csr.is_sparse_csr: \", mt_sparse_csr.is_sparse_csr())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Appendix\n========\n\nSparse COO Construction {#sparse-coo-appendix}\n-----------------------\n\nRecall in our `original example `{.interpreted-text\nrole=\"ref\"}, we created a `MaskedTensor`{.interpreted-text role=\"class\"}\nand then converted it to a sparse COO MaskedTensor with\n`MaskedTensor.to_sparse_coo`{.interpreted-text role=\"meth\"}.\n\nAlternatively, we can also construct a sparse COO MaskedTensor directly\nby passing in two sparse COO tensors:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "values = torch.tensor([[0, 0, 3], [4, 0, 5]]).to_sparse()\nmask = torch.tensor([[False, False, True], [False, False, True]]).to_sparse()\nmt = masked_tensor(values, mask)\n\nprint(\"values:\\n\", values)\nprint(\"mask:\\n\", mask)\nprint(\"mt:\\n\", mt)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Instead of using `torch.Tensor.to_sparse`{.interpreted-text\nrole=\"meth\"}, we can also create the sparse COO tensors directly, which\nbrings us to a warning:\n\n```{=html}\n
WARNING:
\n```\n```{=html}\n
\n```\n```{=html}\n

When using a function like MaskedTensor.to_sparse_coo (analogous to Tensor.to_sparse),if the user does not specify the indices like in the above example,then the 0 values will be \"unspecified\" by default.

\n```\n```{=html}\n
\n```\nBelow, we explicitly specify the 0\\'s:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "i = [[0, 1, 1],\n [2, 0, 2]]\nv = [3, 4, 5]\nm = torch.tensor([True, False, True])\nvalues = torch.sparse_coo_tensor(i, v, (2, 3))\nmask = torch.sparse_coo_tensor(i, m, (2, 3))\nmt2 = masked_tensor(values, mask)\n\nprint(\"values:\\n\", values)\nprint(\"mask:\\n\", mask)\nprint(\"mt2:\\n\", mt2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that `mt` and `mt2` look identical on the surface, and in the vast\nmajority of operations, will yield the same result. But this brings us\nto a detail on the implementation:\n\n`data` and `mask` \\-- only for sparse MaskedTensors \\-- can have a\ndifferent number of elements (`nnz`{.interpreted-text role=\"func\"}) **at\ncreation**, but the indices of `mask` must then be a subset of the\nindices of `data`. In this case, `data` will assume the shape of `mask`\nby `data = data.sparse_mask(mask)`; in other words, any of the elements\nin `data` that are not `True` in `mask` (that is, not specified) will be\nthrown away.\n\nTherefore, under the hood, the data looks slightly different; `mt2` has\nthe \\\"4\\\" value masked out and `mt` is completely without it. Their\nunderlying data has different shapes, which would make operations like\n`mt + mt2` invalid.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print(\"mt data:\\n\", mt.get_data())\nprint(\"mt2 data:\\n\", mt2.get_data())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Sparse CSR Construction {#sparse-csr-appendix}\n=======================\n\nWe can also construct a sparse CSR MaskedTensor using sparse CSR\ntensors, and like the example above, this results in a similar treatment\nunder the hood.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "crow_indices = torch.tensor([0, 2, 4])\ncol_indices = torch.tensor([0, 1, 0, 1])\nvalues = torch.tensor([1, 2, 3, 4])\nmask_values = torch.tensor([True, False, False, True])\n\ncsr = torch.sparse_csr_tensor(crow_indices, col_indices, values, dtype=torch.double)\nmask = torch.sparse_csr_tensor(crow_indices, col_indices, mask_values, dtype=torch.bool)\nmt = masked_tensor(csr, mask)\n\nprint(\"mt:\\n\", mt)\nprint(\"mt data:\\n\", mt.get_data())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conclusion\n==========\n\nIn this tutorial, we have introduced how to use\n`MaskedTensor`{.interpreted-text role=\"class\"} with sparse COO and CSR\nformats and discussed some of the subtleties under the hood in case\nusers decide to access the underlying data structures directly. Sparse\nstorage formats and masked semantics indeed have strong synergies, so\nmuch so that they are sometimes used as proxies for each other (as we\nwill see in the next tutorial). In the future, we certainly plan to\ninvest and continue developing in this direction.\n\nFurther Reading\n===============\n\nTo continue learning more, you can find our [Efficiently writing\n\\\"sparse\\\" semantics for Adagrad with MaskedTensor\ntutorial](https://pytorch.org/tutorials/prototype/maskedtensor_adagrad.html)\nto see an example of how MaskedTensor can simplify existing workflows\nwith native masking semantics.\n" ] } ], "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.10.12" } }, "nbformat": 4, "nbformat_minor": 0 }