Skip to content

[C++ API] Use torch::Tensor instead of at::Tensor/Variable mix#8680

Merged
goldsborough merged 2 commits intopytorch:masterfrom
goldsborough:torch-namespace
Jun 25, 2018
Merged

[C++ API] Use torch::Tensor instead of at::Tensor/Variable mix#8680
goldsborough merged 2 commits intopytorch:masterfrom
goldsborough:torch-namespace

Conversation

@goldsborough
Copy link
Contributor

This PR creates a namespace torch { using Tensor = autograd::Variable; } typedef and uses this type exclusively instead of the current mix of at::Tensor and autograd::Variable. This is so that users only shall see torch::Tensor and not be aware (largely) of at::Tensor.

Mostly this PR then codemods the C++ API. Complications arose in:

  1. Optimizers had to be updated to store Variables (torch::Tensor) instead of at::Tensors,
  2. Serialization code had to be updated to use Variables instead of at::Tensor,
  3. Had to create TensorRange, which copies a range of torch::Tensors into a vector of at::Tensor, and then converts implicitly to an ArrayRef<at::Tensor>. This is to support calling functions like at::stack() with a vector of torch::Tensor, which is currently not possible since vector<Variable> is unrelated to vector<Tensor> in C++.

One small caveat: ambiguity arises around Tensor when you do using namespace torch, but if anything that's a reason not to use such blanket using directives.

@jgehring could you look at serialization stuff (Or raise any other general concerns)?

@ebetica @ezyang @apaszke

Copy link
Contributor

@ezyang ezyang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure why not. Downstream is aware and OK with this change?

@goldsborough
Copy link
Contributor Author

I'll wait for @ebetica or @jgehring to stop by

@apaszke
Copy link
Contributor

apaszke commented Jun 20, 2018

That's great! Just to clarify, the using namespace torch; problems are only when you already have using namespace at; in your script, right?

@jgehring
Copy link
Contributor

Nice! This looks good to me, I'm only wondering about TensorRange. Is this expected to be a temporary solution? I think the name could be clearer, since it's reminiscent of a range or slice over a single tensor.

@goldsborough
Copy link
Contributor Author

@jgehring Ideally we'd have overloads for these methods that take an ArrayRef<Variable> instead of ArrayRef<Tensor>, but for now this is the easier solution. I'll call it TensorListView to make it clearer :)

@goldsborough goldsborough merged commit 521f511 into pytorch:master Jun 25, 2018
@goldsborough goldsborough deleted the torch-namespace branch June 25, 2018 02:03
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

Successfully merging this pull request may close these issues.

4 participants