Samsung / ONE

On-device Neural Engine
Other
428 stars 152 forks source link

[luci-interpreter] Simplify specialization of Platform Abstraction Layer functionality #7710

Open binarman opened 3 years ago

binarman commented 3 years ago

What?

Need to implement mechanism to specialize PAL partially (override only some functions, instead of implementing functions for all platforms)

For example, lets take a look at following case.

We want to specialize some kernel for arm architecture, so we add new pal function in arm-specific pal. This breaks build for all other target platforms, because these platforms do not provide new function.

Current flow requires to add new function (at least some dummy implementation) in all PAL at once.

Why?

This could simplify process of target platform specialization and reduce code duplication between different platforms.

binarman commented 3 years ago

I am thinking of following solution for this problem:

Toy example:

luci-interpreter/pal/reference/PALConv2d.h

#include <tflite_kernel.h>

struct PALConv2dBase
{
  static void Conv2d(float *input, float *output)
  {
    tflite::Conv2d(input, output)
  }

  static void Conv2d(int8_t *input, int8_t *output)
  {
    tflite::Conv2d(input, output)
  }
};

luci-interpreter/pal/arm_mcu/PALConv2d.h

#include "pal/reference/PALConv2d.h"

#include <arm/cmsis-nn>

struct PALConv2d: public PALConv2dBase
{
  static void Conv2d(int8_t *input, int8_t *output)
  {
    cmsis::Conv2d(input, output)
  }
};

luci-interpreter/kernels/Conv2d.cpp

#include "PALConv2d.h"

void Conv2d::execute()
{
  if (input()->element_type() == DataType::S8)
    PALConv2d::Conv2d(input()->data<int8_t>(), output()->data<int8_t>());
  else
    PALConv2d::Conv2d(input()->data<float>(), output()->data<float>());
}
binarman commented 3 years ago

+cc @m-bronnikov

m-bronnikov commented 3 years ago

I am thinking of following solution for this problem:

  • introduce "reference" PAL class, which contains platform agnostic implementations static methods
  • inherit specialized PAL classes from this reference one and redefine needed functions

LGTM =) Let's use this approach!