Skip to content

torch.sgn function giving wrong results #102525

@fnhirwa

Description

@fnhirwa

🐛 Describe the bug

Given that the function torch.sgn is an extention of torch.sign and the torch.sign function returns elementwise indication signs of the input tensor, the torch.sgn should be doing the same as well.

import torch
x = torch.tensor([3+4j, 7-24j, 0, 1+2j])
print(torch.sgn(x)) # tensor([0.6000+0.8000j, 0.2800-0.9600j, 0.0000+0.0000j, 0.4472+0.8944j])

From the torch.sign prospective the output should be `tensor([1.+0.j, 1.+0.j, 0.+0.j, 1.+0.j])

And as for numpy.sign the output for complex input is sign(x.real) + 0j if x.real != 0 else sign(x.imag) + 0j

This will need the documentation updated as well if fixed
During experimentation I think we will need to implement some behaviour like this:

import torch

def torch_sgn_exp(x):
    return torch.where(x.real != 0, torch.sign(x.real) + 0.0j, torch.sign(x.imag) + 0.0j)

x = torch.tensor([3+4j, 7-24j, 0, 1+2j])
print(torch_sgn_exp(x)) # tensor([1.+0.j, 1.+0.j, 0.+0.j, 1.+0.j])

Versions

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

OS: Debian GNU/Linux 10 (buster) (x86_64)
GCC version: (Debian 8.3.0-6) 8.3.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.28

Python version: 3.8.10 (default, Jun 4 2021, 15:09:15) [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.4.0-1106-azure-x86_64-with-glibc2.17
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:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 46 bits physical, 48 bits virtual
CPU(s): 16
On-line CPU(s) list: 0-15
Thread(s) per core: 2
Core(s) per socket: 8
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 85
Model name: Intel(R) Xeon(R) Platinum 8272CL CPU @ 2.60GHz
Stepping: 7
CPU MHz: 2593.908
BogoMIPS: 5187.81
Virtualization: VT-x
Hypervisor vendor: Microsoft
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 1024K
L3 cache: 36608K
NUMA node0 CPU(s): 0-15
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology cpuid pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti tpr_shadow vnmi ept vpid fsgsbase bmi1 hle avx2 smep bmi2 erms invpcid rtm avx512f avx512dq rdseed adx smap clflushopt avx512cd avx512bw avx512vl xsaveopt xsavec xsaves md_clear

Versions of relevant libraries:
[pip3] numpy==1.24.3
[pip3] torch==2.0.1+cpu
[pip3] torch-scatter==2.1.1
[conda] No relevant packages

cc @ezyang @anjali411 @dylanbespalko @mruberry @lezcano @nikitaved @rgommers

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: complexRelated to complex number support in PyTorchmodule: numpyRelated to numpy support, and also numpy compatibility of our operatorstriagedThis 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