coremltools icon indicating copy to clipboard operation
coremltools copied to clipboard

Support for torch.linalg.lstsq operation

Open zack-dev-cm opened this issue 1 year ago • 7 comments

❓Question about torch.linalg.lstsq Operation Support

Summary

I am attempting to convert a PyTorch model that uses torch.linalg.lstsq to CoreML using coremltools, but it seems that this operation is not supported as well as other torch.linalg ops

Issue Details

When I try to convert my model, I get an error stating that torch.linalg is unsupported. This is causing significant issues as there doesn't seem to be a straightforward workaround.

Version

coremltools 7.1 Latest

Code Sample

ref, residuals, rank, singular_values = torch.linalg.lstsq(
        A_regul * weights_regul, b_regul * weights_regul)

zack-dev-cm avatar Dec 15 '23 09:12 zack-dev-cm

In order to support torch.linalg.lstsq it needs to be implemented using our MIL ops. Looking at the documentation for that PyTorch op, I think it's not going to be possible to implement it using our current set of MIL ops. If anyone has any ideas here, please share.

TobyRoseman avatar Dec 15 '23 17:12 TobyRoseman

Yeah our MIL ops do not have anything to solve linear algebra (linear equation, inverse, eigen value, ...), do we?

YifanShenSZ avatar Dec 15 '23 20:12 YifanShenSZ

@YifanShenSZ - we don't have any MIL ops like that. However, let's leave this issue open since it is not resolved.

TobyRoseman avatar Dec 15 '23 20:12 TobyRoseman

@TobyRoseman I'm also using a model that needs torch.linalg.lstsq, so I think it could be worthwhile to implement since it seems like Tensorflow has similar operations too. I would be happy to work on this and see if I can create new MIL ops for some common linear algebra operations

dhruvmk avatar Dec 20 '23 18:12 dhruvmk

@dhruvmk - By all means, feel free to work on this issue. However as I said before, I don't think this is currently going to be possible, given the MIL ops we have right now, but let us know if you make progress.

TobyRoseman avatar Dec 20 '23 20:12 TobyRoseman

@TobyRoseman I understand that this can't be done using the MIL ops currently available, so I was thinking I would create custom MIL ops to support this as outlined here. Am I misunderstanding this issue?

dhruvmk avatar Dec 21 '23 03:12 dhruvmk

@zack-dev-cm I've implemented a CoreML custom layer and a MIL op for torch.linalg.lstsq. It supports multi-batch input tensors and gelsd driver:

https://github.com/dneprDroid/Lstsq-CoreML

You can try the demo app and the example converter script to generate a CoreML model with custom layers.

dneprDroid avatar Jun 05 '24 15:06 dneprDroid