apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.32k stars 627 forks source link

Support for torch.linalg.lstsq operation #2084

Open zack-dev-cm opened 8 months ago

zack-dev-cm commented 8 months ago

❓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)
TobyRoseman commented 8 months ago

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.

YifanShenSZ commented 8 months ago

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

TobyRoseman commented 8 months ago

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

dhruvmk commented 8 months ago

@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

TobyRoseman commented 8 months ago

@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.

dhruvmk commented 8 months ago

@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?

dneprDroid commented 3 months ago

@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.