apple / apple/coremltools

How to support Python build-in functions like len()?

Open
#1,340 3 comments 0 reactions 0 assignees View on GitHub
missing layer type PyTorch (not traced)
Dominant language
Python
Stars
5.4k
Forks
850
Avg merge
4d 5h
Merged PRs (30d)
10

Description

## ❓How to support Python build-in functions like len()?

## System Information
- pytorch: 1.9.1
- coremltools: 1.5.1

I am trying to convert my model in Core ML with Coremltools. I had some issue that I replicated in this simple example.

```python

import torch
import torch.nn as nn
import coremltools

class SimpleTest(nn.Module):
def forward(self, x):
value = len(x)
return torch.tensor(value)

simple_model = SimpleTest()

scripted_model = torch.jit.script(simple_model)
mlmodel = coremltools.converters.convert(scripted_model, inputs=[coremltools.TensorType(shape=(400,))], debug=True)

```

I have this error.

Screenshot 2021-11-13 at 08 06 00

*debug=True info*

```txt
the following model ops are IMPLEMENTED:
constant
tensor
the following model ops are MISSING:
len
```

How can I use Python **len()** funcion inside the model?

Contributor guide

Open the contributing guide

Research direction

Start with the SimpleTest.forward reproduction and the coremltools.converters.convert call, then inspect the converter's debug report showing len as missing. Determine where supported model operations are registered and how similar operations are tested. Done means the example converts successfully with len(x) and the resulting model behaves as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning, tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.