|
| 1 | +# The nestedtensor package [prototype](https://pytorch.org/blog/pytorch-feature-classification-changes/#prototype) |
| 2 | + |
| 3 | +If you are here because you ran into a runtime error due to a missing feature or some kind of bug, please [open an issue and fill in the appropiate template](https://github.com/pytorch/nestedtensor/issues/new/choose). If you have general feedback about this prototype [you can use our suggested template](https://github.com/pytorch/nestedtensor/issues/new?assignees=&labels=&template=prototype-feedback.md&title=) or just open a free-form issue if you like. Thank you for contributing to this project! |
| 4 | + |
| 5 | +## Tutorials |
| 6 | + |
| 7 | +If you are new to this project, we recommend you take a look at our [whirlwind introduction](https://colab.research.google.com/github/pytorch/nestedtensor/blob/master/tutorials/notebooks/basic.ipynb) to get started. |
| 8 | + |
| 9 | +## Operator support |
| 10 | + |
| 11 | +Please see [the list of currently supported operators](https://github.com/pytorch/nestedtensor/blob/master/nestedtensor/csrc/README.md) and [open an issue](https://github.com/pytorch/nestedtensor/issues/new/choose) if you find you need one for your project that's not listed. |
| 12 | + |
| 13 | +## Binaries |
| 14 | + |
| 15 | +The nestedtensor project is built on top of a torch fork for improved interoperability and also ships with torchvision binaries that were built against this fork. To use NestedTensors you need to install this version of torch, which is frequently rebased upon PyTorch's [viable/strict](https://github.com/pytorch/pytorch/tree/viable/strict) branch (most recent master where all tests pass). |
| 16 | + |
| 17 | +| Version | Python | CUDA | Wheels | |
| 18 | +| --- | ---- | ------ | ---- | |
| 19 | +| 0.1.1 | 3.6 | CPU-only | [torch](https://download.pytorch.org/nestedtensor/whl/nightly/cpu/py3.6/torch-1.8.0_nestedtensor_0.1.1_cpu-cp36-cp36m-linux_x86_64.whl), [nestedtensor](https://download.pytorch.org/nestedtensor/whl/nightly/cpu/py3.6/nestedtensor-0.1.1_cpu-cp36-cp36m-linux_x86_64.whl), [torchvision](https://download.pytorch.org/nestedtensor/whl/nightly/cpu/py3.6/torchvision-0.1.1_cpu-cp36-cp36m-linux_x86_64.whl) | |
| 20 | +| 0.1.1 | 3.7 | CPU-only | [torch](https://download.pytorch.org/nestedtensor/whl/nightly/cpu/py3.7/torch-1.8.0_nestedtensor_0.1.1_cpu-cp37-cp37m-linux_x86_64.whl), [nestedtensor](https://download.pytorch.org/nestedtensor/whl/nightly/cpu/py3.7/nestedtensor-0.1.1_cpu-cp37-cp37m-linux_x86_64.whl), [torchvision](https://download.pytorch.org/nestedtensor/whl/nightly/cpu/py3.7/torchvision-0.1.1_cpu-cp37-cp37m-linux_x86_64.whl) | |
| 21 | +| 0.1.1 | 3.8 | CPU-only | [torch](https://download.pytorch.org/nestedtensor/whl/nightly/cpu/py3.8/torch-1.8.0_nestedtensor_0.1.1_cpu-cp38-cp38m-linux_x86_64.whl), [nestedtensor](https://download.pytorch.org/nestedtensor/whl/nightly/cpu/py3.8/nestedtensor-0.1.1_cpu-cp38-cp38m-linux_x86_64.whl), [torchvision](https://download.pytorch.org/nestedtensor/whl/nightly/cpu/py3.8/torchvision-0.1.1_cpu-cp38-cp38m-linux_x86_64.whl) | |
| 22 | + |
| 23 | +## Why consider using this? / Dealing with dynamic shapes |
| 24 | + |
| 25 | +In general we batch data for efficiency, but usually batched kernels need, or greatly benefit from, regular, statically-shaped data. |
| 26 | + |
| 27 | +One way of dealing with dynamic shapes then, is via padding and masking. |
| 28 | +[Various](https://github.com/pytorch/fairseq/blob/54b934417d95baa1b0076089c61bde32728e34cf/fairseq/data/audio/raw_audio_dataset.py#L92) |
| 29 | +[projects](https://github.com/facebookresearch/ParlAI/blob/8200396cdd08cfd26b01fe52b4a3bd0654081182/parlai/agents/drqa/utils.py#L143) |
| 30 | +[construct](https://github.com/facebookresearch/detr/blob/4e1a9281bc5621dcd65f3438631de25e255c4269/util/misc.py#L306) |
| 31 | +[masks](https://github.com/pytorch/vision/blob/24f16a338391d6f45aa6291c48eb6d5513771631/references/detection/utils.py#L102) |
| 32 | +[that](https://github.com/pytorch/audio/blob/3250d3df168c956389bd16956aa458ce111570d0/examples/pipeline_wav2letter/datasets.py#L90), together with a data Tensor, are used as a representation for lists of dynamically shaped Tensors. |
| 33 | + |
| 34 | +Obviously this is inefficient from a memory and compute perspective if the Tensors within this list are sufficiently diverse. |
| 35 | + |
| 36 | +You can also trace through the codebase where these masks are used and observe the kind of code this approach often leads to. See for example [universal_sentence_embedding](https://github.com/facebookresearch/ParlAI/blob/8200396cdd08cfd26b01fe52b4a3bd0654081182/parlai/agents/drqa/utils.py#L143). |
| 37 | + |
| 38 | +Otherwise we also have |
| 39 | +[one-off](https://pytorch.org/docs/master/generated/torch.nn.utils.rnn.pack_padded_sequence.html?highlight=pack_padded_sequence) |
| 40 | +[operator](https://pytorch.org/docs/master/generated/torch.nn.CrossEntropyLoss.html#torch.nn.CrossEntropyLoss) |
| 41 | +[support](https://pytorch.org/docs/master/generated/torch.nn.MultiheadAttention.html#torch.nn.MultiheadAttention) |
| 42 | +[in](https://pytorch.org/docs/master/generated/torch.nn.EmbeddingBag.html#torch.nn.EmbeddingBag) |
| 43 | +PyTorch that aims to support dynamic shapes via extra arguments such as a |
| 44 | +[padding index](https://pytorch.org/docs/master/generated/torch.nn.CrossEntropyLoss.html#torch.nn.CrossEntropyLoss). |
| 45 | +Of course, while these functions are fast and sometimes memory efficient, they don't provide a consistent interface. |
| 46 | + |
| 47 | +Other users simply gave up and started writing [for-loops](https://github.com/pytorch/vision/blob/1aef87d01eec2c0989458387fa04baebcc86ea7b/torchvision/models/detection/transform.py#L97), or discovered that batching didn't help. |
| 48 | + |
| 49 | +We want to have a single abstraction that is consistent, fast, memory efficient and readable and the nestedtensor project aims to provide that. |
| 50 | + |
| 51 | +## How does nestedtensor help here? |
| 52 | + |
| 53 | +NestedTensors are a generalization of torch Tensors which eases working with data of different shapes and lengths. |
| 54 | +In a nutshell, Tensors have scalar entries (e.g. floats) and NestedTensors have Tensor entries. However, note that |
| 55 | +a NestedTensor is still a Tensor. That means it needs to have a single dimension, single dtype, single device and single layout. |
| 56 | + |
| 57 | + Tensor entry constraints: |
| 58 | + - Each Tensor constituent is of the dtype, layout and device of the containing NestedTensor. |
| 59 | + - The dimension of a constituent Tensor must be less than the dimension of the NestedTensor. |
| 60 | + - An empty NestedTensor is of dimension zero. |
| 61 | + |
| 62 | +## Prototype classification |
| 63 | + |
| 64 | +The nestedtensor package is a prototype intended for early stage feedback and testing. It is on the road to a beta classification, but there is no definitive timeline yet. See [PyTorch feature classification](https://pytorch.org/docs/stable/index.html) for what prototype, beta and stale means. |
| 65 | + |
| 66 | +## Supported platforms |
| 67 | + |
| 68 | +It is developed [against a fork](https://github.com/cpuhrsch/pytorchnestedtensor) of PyTorch to enable cutting-edge features such as improved performance or better `torch.vmap` integration. |
| 69 | + |
| 70 | +Developers will thus need to build from source, but users can use the binary we will start shipping soon ([see the related issue](https://github.com/pytorch/nestedtensor/issues/262)). |
| 71 | + |
| 72 | +If you want to use the binaries you need to run on Linux, use Python 3.8+ and have a CUDA-11 toolkit installed. |
| 73 | + |
| 74 | +If you want to build from source you can probably get it to work on many platforms, but supporting other platforms won't take priority over Linux. We're happy to review community contributions that achieve this however. |
| 75 | + |
| 76 | +## Dependencies |
| 77 | + |
| 78 | +- pytorch (installed from nestedtensor/third_party/pytorch submodule) |
| 79 | +- torchvision (needed for examples and tests) |
| 80 | +- ipython (needed for examples) |
| 81 | +- notebook (needed for examples) |
| 82 | + |
| 83 | +## Build for development |
| 84 | + |
| 85 | +Get the source |
| 86 | + |
| 87 | +``` |
| 88 | +git clone --recursive https://github.com/pytorch/nestedtensor |
| 89 | +cd nestedtensor |
| 90 | +# if you are updating an existing checkout |
| 91 | +git submodule sync |
| 92 | +git submodule update --init --recursive |
| 93 | +``` |
| 94 | + |
| 95 | +Install the build tools |
| 96 | + |
| 97 | +``` |
| 98 | +conda install numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing_extensions future six requests |
| 99 | +conda install -c pytorch magma-cuda110 |
| 100 | +``` |
| 101 | + |
| 102 | +Build from scratch |
| 103 | +``` |
| 104 | +./clean_build_with_submodule.sh |
| 105 | +``` |
| 106 | + |
| 107 | +Incremental builds |
| 108 | +``` |
| 109 | +./build_with_submodule.sh |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | +## Contribution |
| 114 | +The project is under active development. If you have a suggestions or found a bug, please file an issue! |
0 commit comments