KEMBAR78
Updates torch.tensor, torch.as_tensor, and sparse ctors to use the device of inputs tensors they're given, by default by mruberry · Pull Request #41984 · pytorch/pytorch · GitHub
Skip to content

Conversation

mruberry
Copy link
Collaborator

@mruberry mruberry commented Jul 24, 2020

BC-Breaking Note

This PR changes the behavior of the torch.tensor, torch.as_tensor, and sparse constructors. When given a tensor as input and a device is not explicitly specified, these constructors now always infer their device from the tensor. Historically, if the optional dtype kwarg was provided then these constructors would not infer their device from tensor inputs. Additionally, for the sparse ctor a runtime error is now thrown if the indices and values tensors are on different devices and the device kwarg is not specified.

PR Summary
This PR's functional change is a single line:

auto device = device_opt.has_value() ? *device_opt : (type_inference ? var.device() : at::Device(computeDeviceType(dispatch_key)));

=>

auto device = device_opt.has_value() ? *device_opt : var.device();

in internal_new_from_data. This line entangled whether the function was performing type inference with whether it inferred its device from an input tensor, and in practice meant that

t = torch.tensor((1, 2, 3), device='cuda')
torch.tensor(t, dtype=torch.float64)

would return a tensor on the CPU, not the default CUDA device, while

t = torch.tensor((1, 2, 3), device='cuda')
torch.tensor(t)

would return a tensor on the device of t!

This behavior is niche and odd, but came up while @aocsa was fixing #40648.

An additional side affect of this change is that the indices and values tensors given to a sparse constructor must be on the same device, or the sparse ctor must specify the dtype kwarg. The tests in test_sparse.py have been updated to reflect this behavior.

@mruberry mruberry changed the title [WIP] Stops setting dtype from no longer using tensor's device in constructors [WIP] Stops setting dtype from no longer using tensor's device in tensor factories Jul 24, 2020
@mruberry mruberry changed the title [WIP] Stops setting dtype from no longer using tensor's device in tensor factories [WIP] Stops setting dtype from no longer using tensor's device in tensor constructors Jul 24, 2020
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@mruberry has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@mruberry mruberry added the module: bc-breaking Related to a BC-breaking change label Jul 24, 2020
@dr-ci
Copy link

dr-ci bot commented Jul 24, 2020

💊 CI failures summary and remediations

As of commit 8419daf (more details on the Dr. CI page):


  • 1/1 failures possibly* introduced in this PR
    • 1/1 non-CircleCI failure(s)

Extra GitHub checks: 1 failed


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker or post in the (internal) Dr. CI Users group.

See how this bot performed.

This comment has been revised 18 times.

@mruberry mruberry changed the title [WIP] Stops setting dtype from no longer using tensor's device in tensor constructors Updates torch.tensor, torch.as_tensor, and sparse ctors to use the device of inputs tensors they're given, by default Jul 24, 2020
@mruberry mruberry requested review from aocsa and ngimel July 24, 2020 19:27
Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@mruberry has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@mruberry has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@mruberry merged this pull request in 12cd083.

@mruberry mruberry deleted the dtype_device_inference branch August 21, 2020 19:47
facebook-github-bot pushed a commit that referenced this pull request Nov 3, 2020
Summary:
This behavior was changed by side effect by #41984
Update the doc to reflect the actual behavior of the function.

Pull Request resolved: #46937

Reviewed By: mruberry

Differential Revision: D24682750

Pulled By: albanD

fbshipit-source-id: 89b94b61f54dbcfc6a6988d7e7d361bd24ee4964
pytorchmergebot pushed a commit that referenced this pull request Jan 24, 2025
Fixes #144957
Closes #73838 cc @albanD @ezyang

Currently, `tensor_a.new_tensor()` will return a on-cpu tensor no matter where is `tensor_a`. This differs from the document and is a side-effect of #41984.

See #144957 how current logic breaks dynamo.

This PR restore the documented behavior and add tests for `new_tensor`.

Pull Request resolved: #144958
Approved by: https://github.com/ezyang
nWEIdia pushed a commit to nWEIdia/pytorch that referenced this pull request Jan 27, 2025
…ch#144958)

Fixes pytorch#144957
Closes pytorch#73838 cc @albanD @ezyang

Currently, `tensor_a.new_tensor()` will return a on-cpu tensor no matter where is `tensor_a`. This differs from the document and is a side-effect of pytorch#41984.

See pytorch#144957 how current logic breaks dynamo.

This PR restore the documented behavior and add tests for `new_tensor`.

Pull Request resolved: pytorch#144958
Approved by: https://github.com/ezyang
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Merged module: bc-breaking Related to a BC-breaking change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants