Git Product home page Git Product logo

Comments (6)

stevenryoung avatar stevenryoung commented on September 23, 2024 1

For any user needing a quick (probably unsafe) fix, the following worked for my use case:

def patch_abs(t):
    if (t.device == torch.device("mps:0")) and (t.dtype == torch.complex64):
        return torch.sqrt(torch.pow(torch.real(t), 2) + torch.pow(torch.imag(t), 2) + 1e-12)
    return torch.abs(t)

torch.Tensor.abs = patch_abs

with torch.device("cpu"):
    print(torch.ones((2,), dtype=torch.complex64).abs())
    
with torch.device("mps"):
    print(torch.ones((2,), dtype=torch.complex64).abs())
    print(torch.tensor([1.0 + 0.0j, 0.0 + 10.0j, 100.0 + 0.0j, 1000.0 + 0.0j]).abs())

Output:

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

from pytorch.

malfet avatar malfet commented on September 23, 2024 1

Grabbing for myself, wish there was a proper doc for https://developer.apple.com/documentation/metalperformanceshadersgraph/mpsgraph/3564540-absolutewithtensor?language=objc

from pytorch.

jhavukainen avatar jhavukainen commented on September 23, 2024 1

Hi @malfet! Wanted to add some context as I did have a brief discussion with some of the MPSGraph people regarding this issue and that particular op. My current hypothesis on what happens:

  • If MPSGraph abs op gets passed an op that is data type Complex64, it will return a tensor of the same type. However if I print the type of the output from the CPU abs() (and the MPS abs for that matter) using the above snippet provided in the error it seems to be in float32. So probably the complex parts get dropped due to this conversion not being handled explicitly/correctly in the MPS abs implementation and we end up just interpreting the complex tensor as a float32 tensor. This would make complex 1+0i look like a float2 [1,0] tensor.
  • If that's the case, we should check for complex data type in the abs op and explicitly take the real part of the complex output of the graph op absoluteWithTensor. There should be an API

-(MPSGraphTensor *) realPartOfTensor:(MPSGraphTensor *) tensor name:(NSString * _Nullable) name
that should do the trick.

Unfortunately I'm drowning in other work this week so I'm more than happy to let anyone take a shot at this if they'd like.

from pytorch.

gambiTarun avatar gambiTarun commented on September 23, 2024

Hi @bdhirsh !
First time contributor here. Could I work on this bug?

from pytorch.

malfet avatar malfet commented on September 23, 2024

@gambiTarun sorry, missed your comment. How familiar are you with ObjC/MPS framework?

from pytorch.

gambiTarun avatar gambiTarun commented on September 23, 2024

Hi @malfet, no worries! I’m not familiar with ObjC/MPS, but I’m willing to learn. If you could point me in the right direction for resolving this bug, or suggest some resources, I'd appreciate it!

from pytorch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.