Skip to content

nonzero() output results in incorrect type conversion to float #94980

@pbr142

Description

@pbr142

🐛 Describe the bug

After applying nonzero() to a boolean tensor on the MPS decide, the resulting tensor behaves differently when converting from int to float for different slicing. Minimal example:

import torch

d = torch.tensor([[False,False, True, False],[False,False, False, True]]).to('mps')
e = d.nonzero()
e

This gives (as expected)

tensor([[0, 2],
        [1, 3]], device='mps:0')

Now, converting different slices of the tensor e to float results in wrong behavior for some slices:

e[0].float()

gives correctly the first row as floats

tensor([0., 2.], device='mps:0')

Converting the entire tensor e to float and then selecting the column gives the correct result:

e.float()[:,1]

returns

tensor([2., 3.], device='mps:0')

as expected. However, converting a column to float converts the numbers to 0. and 1.:

e[:,1].float()

returns

tensor([0., 1.], device='mps:0')

Versions

Collecting environment information...
PyTorch version: 1.13.1
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 13.1 (arm64)
GCC version: Could not collect
Clang version: 14.0.0 (clang-1400.0.29.202)
CMake version: Could not collect
Libc version: N/A

Python version: 3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:26:40) [Clang 14.0.6 ] (64-bit runtime)
Python platform: macOS-13.1-arm64-arm-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Apple M1 Max

Versions of relevant libraries:
[pip3] mypy==1.0.0
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.24.2
[pip3] torch==1.13.1
[conda] numpy 1.24.2 py311h60f8152_0 conda-forge
[conda] pytorch 1.13.1 cpu_py311hb4bb8ad_1 conda-forge

cc @kulinseth @albanD @malfet @DenisVieriu97 @razarmehr @abhudev

Metadata

Metadata

Assignees

Labels

module: mpsRelated to Apple Metal Performance Shaders frameworktriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions