Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add torch ops for d2go models #1509

Open
wants to merge 59 commits into
base: main
Choose a base branch
from

Conversation

dncnbuck
Copy link

@dncnbuck dncnbuck commented Jun 7, 2022

added torch ops

nms
repeat_interleave
numel
roi_align
logicaland

ops with minor patches

clamp
narrow 
index - handle broadcasting indicies
max -  inputs are len == 1
split - handle case when num_splits == 1
to -  handle the case where the dtype is not set, this should be inferred from the Tensor dtype. see, https://pytorch.org/docs/stable/generated/torch.Tensor.to.html?highlight=#torch.Tensor.to
tupleunpack - handle case when len(output) == 1

these were added in order to make progress with a conversion of
pytorch Mask-RCNN model from d2go / detectron2.

Form more details see: #1505 (comment)

Copy link
Collaborator

@TobyRoseman TobyRoseman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. These changes look good. I just had a couple of questions.

Before we can merge it. We need unit tests for the bug fixes and the new ops. @dncnbuck - can you add that?

It also looks like there is a merge conflict.

coremltools/converters/mil/frontend/torch/ops.py Outdated Show resolved Hide resolved
coremltools/converters/mil/frontend/torch/ops.py Outdated Show resolved Hide resolved
context.add(out, torch_name=node.name)

@register_torch_op(torch_alias=["__and_", '__and__'])
def logicaland(context, node):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logical_and was already implemented.
We need this function?

Copy link
Author

@dncnbuck dncnbuck Jun 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fukatani Awesome! Thanks, I've removed the op and used the logical_and version and added the torch alias there.

A note on the alias __and_. Within the model the RPN requires the op

item = torch.__and__(torch.gt(widths0, 0.), torch.gt(heights0, 0.))

However convert_nodes assumes that ops ending in _ are inplace ops and removes the trailing _ before op lookup.

op_lookup = node.kind
if op_lookup.endswith("_"):
    # This is an "in place" op.
    # Look up the standard op instead by removing underscore.
    op_lookup = op_lookup[:-1]
add_op = _TORCH_OPS_REGISTRY.get(op_lookup, None)

Maybe I should just patch this issue instead and leave only the alias __and__.
I'll think it's probably best to leave it out of this PR for now and open another PR specifically to patch that issue with convert_nodes.

something like

add_op = _TORCH_OPS_REGISTRY.get(op_lookup, None)
if add_op is None and op_lookup.endswith("_"):
    # This is an "in place" op.
    # Look up the standard op instead by removing underscore.
    op_lookup = op_lookup[:-1]
    add_op = _TORCH_OPS_REGISTRY.get(op_lookup, None)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making this change as a separate PR sounds good.

Could someone provide a toy network with a __and__ layer?

@dncnbuck
Copy link
Author

dncnbuck commented Jun 8, 2022

@TobyRoseman "We need unit tests for the bug fixes and the new ops. @dncnbuck - can you add that?"

Yeah good point. I'll be able to add these but this might not happen today sadly. I'll try to get to it this week 😄

@TobyRoseman
Copy link
Collaborator

I'll be able to add these but this might not happen today sadly. I'll try to get to it this week 😄

No worries about the timing. Let me know if you have any questions when adding the unit tests.

Copy link
Contributor

@ArjunSharda ArjunSharda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should update to the latest code, as I don't think this can be merged atm without the update to the latest code.

@dncnbuck
Copy link
Author

dncnbuck commented Sep 3, 2022

@TobyRoseman sorry this dropped off of my radar these last months. I've updated the Pr to include some tests for ops and removed the repeat interleave op implementation for the time being as it was not implemented well enough so I will hold out on adding that until I can give it some time

@@ -3398,6 +3405,18 @@ def _slice(context, node):
context.add(res)


def _num_splits_and_sizes(split_sizes):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this just be an inner method of the split method?

@register_torch_op
def scatter_add(context, node):
inputs = _get_inputs(context, node)
_scatter(context, inputs, 'add', node.name)

@register_torch_op
def roi_align(context, node):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there unit tests for this method?

@TobyRoseman
Copy link
Collaborator

@dncnbuck - no worries about this falling off your radar. Thanks for getting back to it.

Please rebase this pull request on top of main. Then I will kick off a CI run.

@diegogranziol
Copy link

Has this PR been included? i.e. is it possible to convert d2go models to coreml?

@kells1986
Copy link

Has this PR been included? i.e. is it possible to convert d2go models to coreml?

+1 would love to know the status on this PR, adding these ops would be a huge benefit to me

@michaelbinary
Copy link

Has this PR been included? i.e. is it possible to convert d2go models to coreml?

+1 would love to know the status on this PR, adding these ops would be a huge benefit to me

So has anyone successfully converted a detectron2 / d2go model to coreml? I have been struggling with this as using cocoapods are not ideal for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants