Skip to content
This repository was archived by the owner on Nov 15, 2022. It is now read-only.

Commit bef337d

Browse files
authored
Move to __torch_function__ (#11)
1 parent 547b3da commit bef337d

23 files changed

+439
-944
lines changed

.circleci/config.yml

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,24 @@ version: 2
66
jobs:
77
build:
88
docker:
9-
# specify the version you desire here
10-
# use `-browsers` prefix for selenium tests, e.g. `3.6.1-browsers`
11-
- image: circleci/python:3.6.1
12-
13-
# Specify service dependencies here if necessary
14-
# CircleCI maintains a library of pre-built images
15-
# documented at https://circleci.com/docs/2.0/circleci-images/
16-
# - image: circleci/postgres:9.4
9+
- image: continuumio/miniconda3
1710

1811
working_directory: ~/repo
1912

2013
steps:
2114
- checkout
2215

23-
# Download and cache dependencies
24-
- restore_cache:
25-
keys:
26-
- v1-dependencies-{{ checksum "requirements.txt" }}
27-
# fallback to using the latest cache if no exact match is found
28-
- v1-dependencies-
29-
30-
- run:
31-
name: install dependencies
32-
command: |
33-
python3 -m venv venv
34-
. venv/bin/activate
35-
pip3 install -q -r requirements.txt
36-
pip3 install -q torch
37-
38-
- save_cache:
39-
paths:
40-
- ./venv
41-
key: v1-dependencies-{{ checksum "requirements.txt" }}
42-
4316
- run:
4417
name: run tests
4518
command: |
46-
. venv/bin/activate
47-
pip3 install .
19+
conda install pytorch cpuonly -c pytorch-nightly -q -y
20+
apt-get update
21+
apt-get install -y gcc
22+
apt-get install -y g++
23+
pip install .
4824
python test/test_nested_tensor_autograd.py
4925
python test/test_nested_tensor_class.py
5026
python test/test_nested_tensor_functional.py
5127
python test/test_nested_tensor_masking.py
5228
python test/test_nested_tensor_nary.py
5329
python test/test_nested_tensor_tensorwise.py
54-
55-
- store_artifacts:
56-
path: test-reports
57-
destination: test-reports

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# The nestedtensor package
22

3-
NOTE: PLEASE NOTE, NESTEDTENSOR IS UNDER ACTIVE DEVELOPMENT AND EVERYTHING HERE IS SUBJECT TO CHANGE.
3+
NOTE: nestedtensor is under active development and various aspects may change.
4+
NOTE: We test and develop against nightlies! Please use the most recent version of PyTorch if you plan to use this code.
45

56
## Motivation
67

@@ -44,7 +45,7 @@ Import nested tensors and torch via ```from nestedtensor import torch```
4445
### Creation
4546

4647
```
47-
nt = torch.nested_tensor(
48+
nt = nestedtensor.nested_tensor(
4849
[
4950
[
5051
torch.rand(2, 3),
@@ -62,13 +63,15 @@ b = torch.tensor([[2, 2],
6263
[3, 3],
6364
[4, 4],
6465
[5, 5]])
65-
nt2 = torch.nested_tensor([[a],[b]])
66+
nt2 = nestedtensor.nested_tensor([[a],[b]])
6667
```
6768

6869
The level of nesting is inferred from the input. The constructor always copies. Whatever you pass into the constructor will share no data with what the constructor returns. This matches torch.tensor's behavior.
6970

7071
If given a NestedTensor or Tensor it will return a detached copy, which is consistent with the behavior of torch.tensor. Remember that you cannot mix Tensors and NestedTensors within a given list.
7172

73+
A side-note on naming: nestedtensor is a python packed and as such [shouldn't have underscores and is lower case](https://www.python.org/dev/peps/pep-0008/#package-and-module-names), but nested_tensor is a python function and as [such should use underscores](https://www.python.org/dev/peps/pep-0008/#function-and-variable-names) in contrast to the [CapWorded NestedTensor class](https://www.python.org/dev/peps/pep-0008/#class-names).
74+
7275
### Conversion/unbind()
7376
A user can retrieve the constituent Tensors via unbind. Unbind is currently used by torch to turn Tensors into tuples of Tensors. Unbind always returns a tuple of views.
7477

@@ -80,7 +83,7 @@ A user can retrieve the constituent Tensors via unbind. Unbind is currently used
8083
... [torch.rand(3, 2)]
8184
... ]
8285
>>>
83-
>>> b = torch.nested_tensor(a)
86+
>>> b = nestedtensor.nested_tensor(a)
8487
>>> print(b)
8588
nested_tensor([
8689
[
@@ -136,8 +139,8 @@ tensor([ 9, 11])
136139
>>> print(simple_fn(c, d))
137140
tensor([10, 10])
138141
>>>
139-
>>> n = torch.nested_tensor([a, c])
140-
>>> m = torch.nested_tensor([b, d])
142+
>>> n = nestedtensor.nested_tensor([a, c])
143+
>>> m = nestedtensor.nested_tensor([b, d])
141144
>>> print(simple_fn(n, m))
142145
nested_tensor([
143146
tensor([ 9, 11]),

examples/basic.ipynb

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"metadata": {},
1616
"outputs": [],
1717
"source": [
18-
"from nestedtensor import torch\n",
18+
"import torch\n",
19+
"import nestedtensor\n",
1920
"from IPython.display import Markdown, display\n",
2021
"\n",
2122
"def print_eval(s):\n",
@@ -54,23 +55,23 @@
5455
"text": [
5556
"nested_tensor([\n",
5657
"\t[\n",
57-
"\t\ttensor([[0.8264, 0.2200, 0.4197],\n",
58-
"\t\t [0.6789, 0.7460, 0.1694]]),\n",
59-
"\t\ttensor([[0.7467, 0.8433, 0.6429, 0.9890, 0.0170],\n",
60-
"\t\t [0.6297, 0.3899, 0.7025, 0.0812, 0.9585],\n",
61-
"\t\t [0.1113, 0.4260, 0.4245, 0.7971, 0.7910],\n",
62-
"\t\t [0.7077, 0.6765, 0.0228, 0.5461, 0.4095]])\n",
58+
"\t\ttensor([[0.1525, 0.9457, 0.8438],\n",
59+
"\t\t [0.6784, 0.9376, 0.5344]]),\n",
60+
"\t\ttensor([[0.5654, 0.6054, 0.2726, 0.8868, 0.3417],\n",
61+
"\t\t [0.1225, 0.4104, 0.9022, 0.6978, 0.2081],\n",
62+
"\t\t [0.5641, 0.2983, 0.7589, 0.5495, 0.1304],\n",
63+
"\t\t [0.1999, 0.3803, 0.0336, 0.4855, 0.9838]])\n",
6364
"\t],\n",
6465
"\t[\n",
65-
"\t\ttensor([[0.0660, 0.9756]])\n",
66+
"\t\ttensor([[0.8105, 0.6778]])\n",
6667
"\t]\n",
6768
"])\n",
6869
"\n"
6970
]
7071
}
7172
],
7273
"source": [
73-
"nt = torch.nested_tensor(\n",
74+
"nt = nestedtensor.nested_tensor(\n",
7475
" [\n",
7576
" [\n",
7677
" torch.rand(2, 3),\n",
@@ -227,7 +228,7 @@
227228
" [3, 3],\n",
228229
" [4, 4],\n",
229230
" [5, 5]])\n",
230-
"nt2 = torch.nested_tensor([[a],[b]])\n",
231+
"nt2 = nestedtensor.nested_tensor([[a],[b]])\n",
231232
"print_eval(\"nt2.nested_dim()\")\n",
232233
"print_eval(\"nt2.tensor_dim()\")\n",
233234
"print_eval(\"nt2.dim()\")"
@@ -281,15 +282,15 @@
281282
"text": [
282283
"nested_tensor([\n",
283284
"\t[\n",
284-
"\t\ttensor([[0.8264, 0.2200, 0.4197],\n",
285-
"\t\t [0.6789, 0.7460, 0.1694]]),\n",
286-
"\t\ttensor([[0.7467, 0.8433, 0.6429, 0.9890, 0.0170],\n",
287-
"\t\t [0.6297, 0.3899, 0.7025, 0.0812, 0.9585],\n",
288-
"\t\t [0.1113, 0.4260, 0.4245, 0.7971, 0.7910],\n",
289-
"\t\t [0.7077, 0.6765, 0.0228, 0.5461, 0.4095]])\n",
285+
"\t\ttensor([[0.1525, 0.9457, 0.8438],\n",
286+
"\t\t [0.6784, 0.9376, 0.5344]]),\n",
287+
"\t\ttensor([[0.5654, 0.6054, 0.2726, 0.8868, 0.3417],\n",
288+
"\t\t [0.1225, 0.4104, 0.9022, 0.6978, 0.2081],\n",
289+
"\t\t [0.5641, 0.2983, 0.7589, 0.5495, 0.1304],\n",
290+
"\t\t [0.1999, 0.3803, 0.0336, 0.4855, 0.9838]])\n",
290291
"\t],\n",
291292
"\t[\n",
292-
"\t\ttensor([[0.0660, 0.9756]])\n",
293+
"\t\ttensor([[0.8105, 0.6778]])\n",
293294
"\t]\n",
294295
"])\n",
295296
"\n"
@@ -621,7 +622,7 @@
621622
{
622623
"data": {
623624
"text/markdown": [
624-
"**<span style='color:darkred'>$ torch.nested_tensor_from_tensor_mask(tensor, mask)</span>**"
625+
"**<span style='color:darkred'>$ nestedtensor.nested_tensor_from_tensor_mask(tensor, mask)</span>**"
625626
],
626627
"text/plain": [
627628
"<IPython.core.display.Markdown object>"
@@ -646,7 +647,7 @@
646647
{
647648
"data": {
648649
"text/markdown": [
649-
"**<span style='color:darkred'>$ torch.nested_tensor_from_padded_tensor(tensor, padding=0)</span>**"
650+
"**<span style='color:darkred'>$ nestedtensor.nested_tensor_from_padded_tensor(tensor, padding=0)</span>**"
650651
],
651652
"text/plain": [
652653
"<IPython.core.display.Markdown object>"
@@ -688,9 +689,9 @@
688689
" [ True, True, True, True]]])\n",
689690
"print_eval(\"tensor\")\n",
690691
"print_eval(\"mask\")\n",
691-
"nt2 = torch.nested_tensor_from_tensor_mask(tensor, mask)\n",
692-
"print_eval(\"torch.nested_tensor_from_tensor_mask(tensor, mask)\")\n",
693-
"print_eval(\"torch.nested_tensor_from_padded_tensor(tensor, padding=0)\")"
692+
"nt2 = nestedtensor.nested_tensor_from_tensor_mask(tensor, mask)\n",
693+
"print_eval(\"nestedtensor.nested_tensor_from_tensor_mask(tensor, mask)\")\n",
694+
"print_eval(\"nestedtensor.nested_tensor_from_padded_tensor(tensor, padding=0)\")"
694695
]
695696
},
696697
{
@@ -795,12 +796,12 @@
795796
"output_type": "stream",
796797
"text": [
797798
"nested_tensor([\n",
798-
"\ttensor([[0.8264, 0.2200, 0.4197],\n",
799-
"\t [0.6789, 0.7460, 0.1694]]),\n",
800-
"\ttensor([[0.7467, 0.8433, 0.6429, 0.9890, 0.0170],\n",
801-
"\t [0.6297, 0.3899, 0.7025, 0.0812, 0.9585],\n",
802-
"\t [0.1113, 0.4260, 0.4245, 0.7971, 0.7910],\n",
803-
"\t [0.7077, 0.6765, 0.0228, 0.5461, 0.4095]])\n",
799+
"\ttensor([[0.1525, 0.9457, 0.8438],\n",
800+
"\t [0.6784, 0.9376, 0.5344]]),\n",
801+
"\ttensor([[0.5654, 0.6054, 0.2726, 0.8868, 0.3417],\n",
802+
"\t [0.1225, 0.4104, 0.9022, 0.6978, 0.2081],\n",
803+
"\t [0.5641, 0.2983, 0.7589, 0.5495, 0.1304],\n",
804+
"\t [0.1999, 0.3803, 0.0336, 0.4855, 0.9838]])\n",
804805
"])\n",
805806
"\n"
806807
]
@@ -822,7 +823,7 @@
822823
"output_type": "stream",
823824
"text": [
824825
"nested_tensor([\n",
825-
"\ttensor([[0.0660, 0.9756]])\n",
826+
"\ttensor([[0.8105, 0.6778]])\n",
826827
"])\n",
827828
"\n"
828829
]
@@ -857,15 +858,15 @@
857858
"text": [
858859
"nested_tensor([\n",
859860
"\t[\n",
860-
"\t\ttensor([[0.6776, 0.9759, 0.9132],\n",
861-
"\t\t [0.7783, 0.7344, 0.9857]]),\n",
862-
"\t\ttensor([[0.7467, 0.8433, 0.6429, 0.9890, 0.0170],\n",
863-
"\t\t [0.6297, 0.3899, 0.7025, 0.0812, 0.9585],\n",
864-
"\t\t [0.1113, 0.4260, 0.4245, 0.7971, 0.7910],\n",
865-
"\t\t [0.7077, 0.6765, 0.0228, 0.5461, 0.4095]])\n",
861+
"\t\ttensor([[0.9884, 0.5852, 0.6646],\n",
862+
"\t\t [0.7786, 0.5917, 0.8606]]),\n",
863+
"\t\ttensor([[0.5654, 0.6054, 0.2726, 0.8868, 0.3417],\n",
864+
"\t\t [0.1225, 0.4104, 0.9022, 0.6978, 0.2081],\n",
865+
"\t\t [0.5641, 0.2983, 0.7589, 0.5495, 0.1304],\n",
866+
"\t\t [0.1999, 0.3803, 0.0336, 0.4855, 0.9838]])\n",
866867
"\t],\n",
867868
"\t[\n",
868-
"\t\ttensor([[0.0660, 0.9756]])\n",
869+
"\t\ttensor([[0.8105, 0.6778]])\n",
869870
"\t]\n",
870871
"])\n",
871872
"\n"

0 commit comments

Comments
 (0)