Implement SVD with simple PyTorch functions for Core ML ops compatibility

Hi everyone!


I want to implement torch.svd function using simpler PyTorch functions like torch.matmul and others.


The reason being wanting to do is that I have a trained neural network (having SVD op) that I want to convert to Core ML model but Core ML’s MIL Ops don’t have implementation of svd() function.

One possible solution. But it’s not correct.

One implementation I found is in NumPy but doesn’t produces same dimensional ndarray outputs as produced by PyTorch’s svd implementation. Moreover, the signs of output ndarray's scalar elements are also wrong.

Can you please help me?

I am stuck at this problem since January 2021 (like almost 9 months now). If anyone could please help me I’d be really really thankful to you!!

Rahul Bhalley

The link in your post implemented reduced SVD. You need to call
torch.linalg. svd (A , full_matrices=False)
to do the same.

Note that the returned UV is not unique. If you think it’s still not right, can you share some example?

Thanks for the reply @Linbin!

I don’t know how to compute non-reduced SVD without the help of PyTorch. And there are no examples available in Python on GitHub to implement it either. Could you please give me a clue of how to do that? I need it to rewrite my neural network in Core ML format. I did try to look into SciPy’s and NumPy’s codebase but all in vain. I couldn’t understand how it could be done.

Edit: I’m not good with understanding the underlying C++ codebase.

Any help is highly appreciated!

@xta0 any thoughts on supporting PyTorch SVD in core ml?

I can create custom operator of SVD in Core ML but I first need to know how to compute SVD withfull_matrices=True without using torch.svd() function but simpler ones.

CC @xta0.

Haven’t looked into detail, but can you open an issue in the coremltools Github? See if they can try helping you implement that op in MIL.