CoreML Custom layer issue when targeting iOS bellow 11.2

I have a model with a custom layer with a Metal Shader, have appropriated "class @available(iOS 11.2, *)" and "do not run it in devices with OS bellow 11.2".


Model


Input - Image (Color ...)

Output - MultiArray (Double ...)


Facts


1 - If i put the app targeting some version bellow 11.2, model returns NaN in all entries of the output. On the other end, if i target 11.2, and run on a 12.1 it works as expected.


2 - Targeting <11.2 has an even weider behaviour. If the app is uninstalled and installed, sometimes it returns the correct set of values only one time and after that all predictions will be NaN. You can kill the app and open it and model will always return NaN in all output entries.


3 - If remove "encode" method from MLCustomLayer or set it to run CPU, it runs just fine.


This looks like a bug.


Manuel

Replies

"If i put the app targeting some version bellow 11.2, model returns NaN in all entries of the output."

But I assume you are running the model on a >= 11.2 device, right?


Did you provide a Metal implementation of your custom layer, or just the CPU version?

Always running on 12.1.1 device. Key point to create the issue is target version of the executable.


Yes, i do have a metal implementation and it works just fine if target is >11.2 and encode method is called. CPU version works always, though.

This indeed sounds like a bug. Maybe we can narrow it down:


I assume the target version only influences the app's compilation, not its runtime. Maybe you can compare the build logs of < 11.2 and >= 11.2. Especially the step where your custom layer code gets compiled by the Metal compiler could be interesting.


Also running a GPU frame capture or a Metal System Trace in Instruments when executing your model could yield insights on what's going on.