Skip to content

Python float to torch.float32  #136350

@whatdhack

Description

@whatdhack

🐛 Describe the bug

Noticing the following . As I understand, python float is 64 bit. Hence , converting to torch.float32 would be lossy. What is the recommended way to preserve as much accuracy as possible in this transition ?

python -c "import torch;import time;offset=1726274430;torch.set_printoptions(precision=10);l1=[time.time()-offset,time.time()-offset];print('list:', l1, type(l1[0]));t1=torch.tensor(l1);print('cpu:', t1, t1.dtype);t1=t1.to('cuda'); print('gpu:', t1, t1.dtype);"

list: [521468.6812365055, 521468.6812376976] <class 'float'>
cpu: tensor([521468.6875000000, 521468.6875000000]) torch.float32
gpu: tensor([521468.6875000000, 521468.6875000000], device='cuda:0') torch.float32 

Versions

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions