Code Monkey home page Code Monkey logo

maskedtensor's Introduction

maskedtensor

Warning: This is a prototype library that is actively under development. If you have suggestions or potential use cases that you'd like addressed, please open a Github issue; we welcome any thoughts, feedback, and contributions!

MaskedTensor is a prototype library that is part of the PyTorch project and is an extension of torch.Tensor that provides the ability to mask out the value for any given element. Elements with masked out values are ignored during computation and give the user access to advanced semantics such as masked reductions, safe softmax, masked matrix multiplication, filtering NaNs, and masking out certain gradient values.

Installation

Binaries

To install the official MaskedTensor via pip, use the following command:

pip install maskedtensor

For the dev (unstable) nightly version that contains the most recent features, please replace maskedtensor with maskedtensor-nightly.

Note that MaskedTensor requires PyTorch >= 1.11, which you can get on the the main website

From Source

To install from source, you will need Python 3.7 or later, and we highly recommend that you use an Anaconda environment. Then run:

python setup.py develop

Documentation

Please find documentation on the MaskedTensor Website.

Building documentation

Please follow the instructions in the docs README.

Notebooks

For an introduction and instructions on how to use MaskedTensors and what they are useful for, there are a nubmer of tutorials on the MaskedTensor website.

License

maskedtensor is licensed under BSD 3-Clause

maskedtensor's People

Contributors

bigfootjon avatar cpuhrsch avatar facebook-github-bot avatar george-qi avatar thatch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

maskedtensor's Issues

AttributeError: 'builtin_function_or_method' object has no attribute 'overloadpacket'

๐Ÿ› Describe the bug

Thanks for building this project! I just tried the example from http://pytorch.org/maskedtensor/main/notebooks/nan_grad.html but got the following error:

Traceback (most recent call last):
  File "test.py", line 156, in <module>
    y = torch.where(mask, torch.exp(mx), my)
  File "/workspace/.conda/envs/main/lib/python3.7/site-packages/maskedtensor/core.py", line 218, in __torch_function__
    ret = func(*args, **kwargs)
  File "/workspace/.conda/envs/main/lib/python3.7/site-packages/maskedtensor/core.py", line 230, in __torch_dispatch__
    func = func.overloadpacket
AttributeError: 'builtin_function_or_method' object has no attribute 'overloadpacket'

Below is my code

import torch
from maskedtensor import masked_tensor
from maskedtensor import as_masked_tensor

x = torch.tensor([-10., -5, 0, 5, 10, 50, 60, 70,
                 80, 90, 100], requires_grad=True)
mask = x < 0
mx = masked_tensor(x, mask, requires_grad=True)
my = masked_tensor(torch.ones_like(x), ~mask, requires_grad=True)
y = torch.where(mask, torch.exp(mx), my)
s = y.sum()
s.backward()
# Gradient is only provided to selected subset.
# Effectively this changes the gradient of where to mask out elements instead
# of setting them to zero.
print("mx.grad: ", mx.grad)

My maskedtensor version: 0.11.dev2022416
My PyTorch version: 1.11.0+cu102
My Python version: 3.7.11

Any idea how to workaround this?

[Feature Request] Add more unary operators

As shown in the comments here:

UNARY_NAMES_UNSUPPORTED = [
"atan2",
"arctan2",
"bitwise_left_shift",
"bitwise_right_shift",
"copysign",
"float_power",
"fmod",
"frexp",
"gradient",
"imag",
"ldexp",
"lerp",
"logical_not",
"hypot",
"igamma",
"igammac",
"mvlgamma",
"nextafter",
"polygamma",
"real",
"remainder",
"true_divide",
"xlogy",
]

we have a number of unary operators that are not yet supported, possibly due to missing codegen or complex semantics. Opening this issue as a feature request to have these added

Support mse_loss

๐Ÿš€ The feature, motivation and pitch

Many developers, like myself, use masks in both the prediction output and ground truth to evaluate whether or not an image, for example, is valid for backpropagation.

Alternatives

In my case it is not the same to do
x[mask] -> NaN error loss in model
x * mask -> 0.0 good
Masked tensor(x, mask) - - error F.mse_loss...

Additional context

image

Create a MaskedTensor from a single sparse tensor

๐Ÿš€ The feature, motivation and pitch

Sparse tensors already encode a masking patterns. It would be convenient to be able to consider a sparse matrix as a MaskedTensor where the non-zero indices of the sparse matrix would be considered as a mask of True value.

Alternatives

Duplicating the input sparse matrix is doable but confusing and requires additional storage for the mask.

Here is code I couldn't test (see #70 ):

t_csr # some sparse CSR matrix

# Create mask from CSR
mask_csr = torch.sparse_csr_tensor(t_csr.crow_indices(), t_csr.col_indices(), torch.ones_like(t_csr.values(),dtype=torch.bool), dtype=torch.bool)

# Create MaskedTensor
t_mt = maskedtensor.masked_tensor(t_csr, mask_csr)

Additional context

This span from pytorch/pytorch#87358 (comment)

Type Promotion for masked operators

๐Ÿš€ The feature, motivation and pitch

As seen in Issue #54, type promotion has not been implemented for masked reductions. We'd like to implement this to be consistent with PyTorch.

Alternatives

No response

Additional context

No response

AssertionError on NATIVE_INPLACE_BINARY_MAP

Considering the code below:

import torch
import torch.optim as optim
from maskedtensor import masked_tensor

x = torch.Tensor([[ 0., 32., 23.],
        [18.,  0., torch.nan],
        [54., 67.,  0.]])

y = (x/2).nanmean(dim=0)
y.requires_grad = True

def loss(x: torch.Tensor, t: torch.Tensor) -> torch.Tensor:
    x = masked_tensor(x, ~x.isnan())
    return (x - t).square().mean()

opt = optim.Adam([y,], lr=1)

for _ in range(500):
    opt.zero_grad()
    e = loss(x, y)
    e.backward()
    opt.step()

Environment info:

Python==3.9.8

torch==1.11.0
maskedtensor==0.10.0

Exception will occur:

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
  File "/Users/cavivie/.pyenv/versions/env398_torch/lib/python3.9/site-packages/torch/optim/optimizer.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "/Users/cavivie/.pyenv/versions/env398_torch/lib/python3.9/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "/Users/cavivie/.pyenv/versions/env398_torch/lib/python3.9/site-packages/torch/optim/adam.py", line 141, in step
    F.adam(params_with_grad,
  File "/Users/cavivie/.pyenv/versions/env398_torch/lib/python3.9/site-packages/torch/optim/_functional.py", line 97, in adam
    exp_avg.mul_(beta1).add_(grad, alpha=1 - beta1)
  File "/Users/cavivie/.pyenv/versions/env398_torch/lib/python3.9/site-packages/maskedtensor/core.py", line 218, in __torch_function__
    ret = func(*args, **kwargs)
  File "/Users/cavivie/.pyenv/versions/env398_torch/lib/python3.9/site-packages/maskedtensor/core.py", line 252, in __torch_dispatch__
    return apply_native_binary(func, *args, **kwargs)
  File "/Users/cavivie/.pyenv/versions/env398_torch/lib/python3.9/site-packages/maskedtensor/binary.py", line 178, in apply_native_binary
    return NATIVE_INPLACE_BINARY_MAP[fn](*args, **kwargs)
  File "/Users/cavivie/.pyenv/versions/env398_torch/lib/python3.9/site-packages/maskedtensor/binary.py", line 134, in binary_fn
    assert len(kwargs) == 0
AssertionError

Exception code line:

opt.step()

TypeError when running the tutorial code for CSR

๐Ÿ› Describe the bug

The tutorial code from https://pytorch.org/maskedtensor/main/notebooks/sparse.html#sparse-csr is failling on google colab.

!pip install maskedtensor-nightly

import torch
import maskedtensor
print(f'Running PyTorch version: {torch.__version__}')
print(f'Running maskedtensor version: {maskedtensor.__version__}')

crow_indices = torch.tensor([0, 2, 4])
col_indices = torch.tensor([0, 1, 0, 1])
values = torch.tensor([1, 2, 3, 4])
mask_values = torch.tensor([True, False, False, True])

csr = torch.sparse_csr_tensor(crow_indices, col_indices, values, dtype=torch.double)
mask = torch.sparse_csr_tensor(crow_indices, col_indices, mask_values, dtype=torch.bool)

mt = maskedtensor.masked_tensor(csr, mask)

leads to

Running PyTorch version: 1.12.1+cu113
Running maskedtensor version: 0.12.dev2022714

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

[<ipython-input-5-8513d08b650e>](https://localhost:8080/#) in <module>
      7 mask = torch.sparse_csr_tensor(crow_indices, col_indices, mask_values, dtype=torch.bool)
      8 
----> 9 mt = maskedtensor.masked_tensor(csr, mask)

1 frames

[/usr/local/lib/python3.7/dist-packages/maskedtensor/core.py](https://localhost:8080/#) in __new__(cls, data, mask, requires_grad)
    225         kwargs["dispatch_sizes_strides_policy"] = "strides"
    226         kwargs["dispatch_layout"] = True
--> 227         return torch.Tensor._make_wrapper_subclass(cls, data.size(), **kwargs)  # type: ignore[attr-defined]
    228 
    229     def _preprocess_data(self, data, mask):

TypeError: _make_wrapper_subclass() got an unexpected keyword argument 'dispatch_sizes_strides_policy'

License

This is a hidden gem! You should promote it more aggressively (for some reason I can't find it on google).
I just have one question before using this: what is the license for this code?

Inconsistent sum behavior between boolean regular tensor and maskedtensor

๐Ÿ› Describe the bug

Reporting a possible inconsistent-semantic issue that might be worth looking at:

import torch
from maskedtensor import masked_tensor

t = torch.tensor([True, True, True])
print(t.sum().item())
print(masked_tensor(t, t).sum().item())

In the above code, regular tensor sums to 3 but masked tensor sums to True, but I expected them to be the same. (Casting to float solves my issue and I have no problem with that. Just thought it might be helpful.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.