Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

None Default Arguments #5

Closed
rusty1s opened this issue Apr 26, 2018 · 8 comments
Closed

None Default Arguments #5

rusty1s opened this issue Apr 26, 2018 · 8 comments

Comments

@rusty1s
Copy link

rusty1s commented Apr 26, 2018

Hello and thanks for this great repository!

Can you give me a hint on how to implement default arguments? I tried the following so far but it does not work:

at::Tensor torch_func(at::Tensor tensor1, at::Tensor tensor2={}) {
  ...
}

PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
  m.def("torch_func", &torch_func, py::arg("tensor1"), py::arg("tensor2")={});
}
torch_func(tensor1, None)

I would be very grateful for help.

  • OS: macOS
  • PyTorch version: 0.4.0
  • How you installed PyTorch (conda, pip, source): source
  • Python version: 3.6
  • CUDA/cuDNN version: -
  • GPU models and configuration: -
  • GCC version (if compiling from source): 4.9
@goldsborough
Copy link
Contributor

Hi, we don't currently provide a way of creating a "void"/"None" Tensor in our C++ extension API. Would it be enough for you to use something like at::empty(torch::CPU(at::kDouble), {0})); as the default argument and then check with .numel() == 0 whether or not it is empty? This would work both in the function prototype and for the py::arg() = ... .

Let me know whether this would be sufficient for you.

@rusty1s
Copy link
Author

rusty1s commented Apr 27, 2018

Thanks! python setup.py install now works perfectly.

However, calling the function in Python with a None argument gives the following error message:

incompatible function arguments. The following argument types are supported:
1. (tensor1: at::Tensor, tensor2 = tensor([], dtype=torch.float64)) -> at::Tensor

Invoked with: tensor([2, 2]), None

@goldsborough
Copy link
Contributor

goldsborough commented Apr 27, 2018 via email

@rusty1s
Copy link
Author

rusty1s commented Apr 27, 2018

Yeah, I see. Actually, I wanted to pass None, but I guess I'm fine with the proposed solution (unless you know of an easy way to allow for None arguments). Thanks again!

@goldsborough
Copy link
Contributor

I will keep the possibility of None converting to an undefined Tensor in mind as we expand the extension API. Thanks for posting the issue! I'll mark it as resolved.

@apaszke
Copy link

apaszke commented Apr 27, 2018

@goldsborough how about we expose torch::optional and let Nones bind to that?

@goldsborough
Copy link
Contributor

Yes that's a good idea, I'll do that

@goldsborough
Copy link
Contributor

@rusty1s you can now pass None if you use at::optional<at::Tensor> in C++ (and at::nullopt or nullptr as the C++ default type)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants