Skip to content

Add numpy.array-like type inference to torch.tensor.#5997

Merged
gchanan merged 13 commits intopytorch:masterfrom
gchanan:type_inference_ctor2
Mar 27, 2018
Merged

Add numpy.array-like type inference to torch.tensor.#5997
gchanan merged 13 commits intopytorch:masterfrom
gchanan:type_inference_ctor2

Conversation

@gchanan
Copy link
Copy Markdown
Contributor

@gchanan gchanan commented Mar 26, 2018

i.e. torch.tensor(0) will create a tensor with scalar type int64, torch.tensor(0.) will create a tensor with scalar type float64.

@gchanan
Copy link
Copy Markdown
Contributor Author

gchanan commented Mar 26, 2018

open questions:

  1. Should we treat python floats as doubles (as numpy does), or as the default scalar dtype (which will usually be float)?
  2. Should we do device-type inference from the default dtype? That is, if the default dtype is a cuda type, should torch.tensor(0), torch.tensor(0.) create cuda tensors? (This is moot if we separate out device type from dtype, which we may do)
  3. @fritzo @neerajprad I had to make a number of changes to distributions to get it to pass; basically, tensors are treated as is (so many needed to be passed floats instead of ints in the tests), but numbers are transformed into default dtype tensors by broadcast_all; this seems convenient, but leads to Distribution(0) behaving differently than Distribution(torch.tensor(0)) (you would need Distributions(torch.tensor(0.)) to get the same effect. Let me know if you think this is reasonable or prefer something else (like treating numbers as torch.tensor does, so we'd have to change the tests to be something like Distribution(0.).

@gchanan
Copy link
Copy Markdown
Contributor Author

gchanan commented Mar 26, 2018

@pytorchbot retest this please.

@gchanan
Copy link
Copy Markdown
Contributor Author

gchanan commented Mar 26, 2018

Latest commit treats python floats as the default (scalar) dtype instead of doubles.

@gchanan
Copy link
Copy Markdown
Contributor Author

gchanan commented Mar 26, 2018

For 2. above, we now have tests that exercise cuda type inference.

Also, I changed the promoteTypes logic to favor float32 over float64 with the combination (float32, int32) and (float32, int64); I believe @fmassa and @apaszke discussed this before. The particular reason in this case is so that:

torch.tensor(5., 0) uses the default tensor type (to match (torch.tensor(5.)); otherwise, the addition of the long, causes the promote types to return double.

@fmassa
Copy link
Copy Markdown
Member

fmassa commented Mar 26, 2018

@gchanan I think it's better like this.

Plus, current tensor/number operations in TH use the scalar type anyway, so the Python double type is already casted to float32 when we are using float32 anyway.

@gchanan gchanan mentioned this pull request Mar 26, 2018
6 tasks
Comment thread torch/csrc/utils/tensor_new.cpp Outdated
new_with_type_conversion(type, var, device);
}
const auto& type_to_use = type_inference ? var.type() : type;
return copy_variables ? new_with_tensor_copy(type_to_use, var, device) :

This comment was marked as off-topic.

/* i8 */ { i8, i8, i8, i8, i8, f8, f4, f8, ud },
/* f2 */ { f2, f2, f4, f8, f8, f2, f4, f8, ud },
/* f4 */ { f4, f4, f4, f8, f8, f4, f4, f8, ud },
/* f4 */ { f4, f4, f4, f4, f4, f4, f4, f8, ud },

This comment was marked as off-topic.

This comment was marked as off-topic.

Comment thread torch/csrc/utils/tensor_new.cpp Outdated
}
#endif
if (PySequence_Check(obj)) {
ScalarType scalarType = ScalarType::NumOptions;

This comment was marked as off-topic.

// match NumPy semantics, except use default tensor type instead of double.
if (length == 0) return torch::tensor::get_default_tensor_type().scalarType();
for (int i = 0; i < length; ++i) {
THPObjectPtr handle(PySequence_GetItem(obj, i));

This comment was marked as off-topic.

@fritzo
Copy link
Copy Markdown
Collaborator

fritzo commented Mar 27, 2018

IIUC, I think you'll also want to change a couple of the distributions.constraints.* to floats. In https://github.com/pytorch/pytorch/blob/master/torch/distributions/constraints.py#L240:

- positive = _GreaterThan(0)
+ positive = _GreaterThan(0.)
  ...
- unit_interval = _Interval(0, 1)
+ unit_interval = _Interval(0., 1.)

Note that we have separate constraints for integers.

@gchanan gchanan merged commit db53389 into pytorch:master Mar 27, 2018
laurentdupin pushed a commit to laurentdupin/pytorch that referenced this pull request Apr 24, 2026
* Add numpy.array-like type inference to torch.tensor.

* Temporary fix for int/double types.

* Treat python floats as the default (scalar) dtype.

* Also make 0-length sequences the default scalar type and add more tests.

* Add type inference to sparse_coo_tensor.

* Fix sparse test.

* Remove allow_variables.

* Check numpy platform bits.

* Address review comments.

* Make suggested changes to constraints.

* More checking windows builds.

* Fix test for windows.
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