Skip to content

torch-directml: different results from cpu and directml on float16 calculation #370

@lshqqytiger

Description

@lshqqytiger

First of all, thank you for developing a library like DirectML.
I've been waiting for the float16 operation to be implemented in DirectML, and I tried it because it seems to have been implemented in 0.1.13.1.dev230119.
I am a student who is not a major yet, but I submitted this issue because I thought there was a problem with the difference in some calculations between cpu and DirectML as follows.

Environment
Python 3.10.8 | packaged by conda-forge | (main, Nov 24 2022, 14:07:00)
Windows 11 Build 25247
torch==1.13.1
torch-directml==0.1.13.1.dev230119
AMD Radeon RX 5700 XT
Radeon Driver 22.11.2

Code

import torch
import torch_directml

dml = torch_directml.device()

tensor1 = torch.randn(1, dtype=torch.float16) # tensor([0.6094], dtype=torch.float16)
tensor2 = torch.randn(1, dtype=torch.float16) # tensor([-0.5024], dtype=torch.float16)

tensor1 ** tensor2 # tensor([1.2822], dtype=torch.float16)
tensor1.to(dml) ** tensor2.to(dml) # tensor([1.2822], device='privateuseone:0', dtype=torch.float16)

tensor1 = tensor1.abs() # tensor([0.6094], dtype=torch.float16)
tensor2 = tensor2.abs() # tensor([0.5024], dtype=torch.float16)

tensor1 ** tensor2 # tensor([0.7798], dtype=torch.float16)
tensor1.to(dml) ** tensor2.to(dml) # tensor([0.7793], device='privateuseone:0', dtype=torch.float16)

# I found same issue from other operators too
tensor1 = torch.randn(1, dtype=torch.float16) # tensor([0.8931], dtype=torch.float16)

tensor1 + 0.02 # tensor([0.9131], dtype=torch.float16)
tensor1.to(dml) + 0.02 # tensor([0.9126], device='privateuseone:0', dtype=torch.float16)

tensor1 / 0.02 # tensor([44.6562], dtype=torch.float16)
tensor1.to(dml) / 0.02 # tensor([44.6250], device='privateuseone:0', dtype=torch.float16)

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