サポートされている TensorFlow 演算子の選択

TensorFlow コア オペレーター

以下は、Select TensorFlow Ops 機能を備えた TensorFlow Lite ランタイムによってサポートされる TensorFlow コア操作の完全なリストです。

TensorFlow Text および SentencePiece 演算子

次のTensorFlow Text演算子とSentencePiece演算子は、Python API を使用して変換し、これらのライブラリをインポートする場合にサポートされます。

TF.Text 演算子:

  • CaseFoldUTF8
  • ConstrainedSequence
  • MaxSpanningTree
  • NormalizeUTF8
  • NormalizeUTF8WithOffsetsMap
  • RegexSplitWithOffsets
  • RougeL
  • SentenceFragments
  • SentencepieceOp
  • SentencepieceTokenizeOp
  • SentencepieceTokenizeWithOffsetsOp
  • SentencepieceDetokenizeOp
  • SentencepieceVocabSizeOp
  • SplitMergeTokenizeWithOffsets
  • UnicodeScriptTokenizeWithOffsets
  • WhitespaceTokenizeWithOffsets
  • WordpieceTokenizeWithOffsets

SentencePiece 演算子:

  • SentencepieceGetPieceSize
  • SentencepiecePieceToId
  • SentencepieceIdToPiece
  • SentencepieceEncodeDense
  • SentencepieceEncodeSparse
  • SentencepieceDecode

次のスニペットは、上記の演算子を使用してモデルを変換する方法を示しています。

import tensorflow as tf
# These imports are required to load operators' definition.
import tensorflow_text as tf_text
import sentencepiece as spm

converter = tf.lite.TFLiteConverter.from_keras_model(your_model)
converter.target_spec.supported_ops = [
  tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS
]
model_data = converter.convert()

ランタイム側では、TensorFlow Text または SentencePiece ライブラリを最終的なアプリまたはバイナリにリンクすることも必要です。

ユーザー定義の演算子

独自の TensorFlow 演算子を作成した場合は、次のようにexperimental_select_user_tf_opsに必要な演算子をリストすることで、それらを含むモデルを TensorFlow Lite に変換することもできます。

import tensorflow as tf

ops_module = tf.load_op_library('./your_ops_library.so')

converter = tf.lite.TFLiteConverter.from_saved_model(your_model)
converter.target_spec.supported_ops = [
  tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS
]
converter.target_spec.experimental_select_user_tf_ops = [
    'your_op_name1',
    'your_op_name2'
]
model_data = converter.convert()

ランタイム側では、オペレーター ライブラリを最終的なアプリまたはバイナリにリンクすることも必要です。

TensorFlow コア オペレーターを許可リストに追加します。

TensorFlow コア オペレーターが上記の許可リストに含まれていない場合は、許可リストにリストされていない TensorFlow コア オペレーターの名前を使用して、ここで機能リクエストを報告できます。

ソース コードから独自のプル リクエストを作成することもできます。たとえば、許可リストにraw_ops.StringToNumber演算を追加する場合、このcommitのように更新する場所が 3 つあります。

(1) オペレーターカーネルのソースコードを、 portable_extended_ops_group2 BUILD ルールに追加します。

filegroup(
    name = "portable_extended_ops_group2",
    srcs = [
        ...
+       "string_to_number_op.cc",

        ...
    ],
)

tensorflow/core/kernelsディレクトリで関連するオペレーター カーネル ソース ファイルを見つけるには、オペレーター名を含む次のカーネル宣言が含まれるソース コードの場所を検索できます。

REGISTER_KERNEL_BUILDER(Name("StringToNumber")                 \
                            .Device(DEVICE_CPU)                \
                            .TypeConstraint<type>("out_type"), \
                        StringToNumberOp<type>)

オペレーター カーネル ソース コードで必要なヘッダー ファイルがtensorflow/core/kernelsディレクトリの下にある場合は、次のようにそのヘッダー ファイルをportable_extended_ops_headers BUILD ルールに追加する必要があります。

filegroup(
    name = "portable_extended_ops_headers",
    srcs = [
        ...
+       "string_util.h",

        ...
    ],
)

(2) オペレータ名を許可リストに追加します。

許可リストはtensorflow/lite/delegates/flex/allowlisted_flex_ops.ccで定義されています。 [TF の選択] オプションを使用できるようにするには、TensorFlow コア オペレーター名をリストする必要があります。

static const std::set<std::string>* allowlisted_flex_ops =
    new std::set<std::string>({
        ...
+       "StringToNumber",

        ...
    });

上記のリストはアルファベット順にソートされているため、名前が正しい場所に配置されることが保証されます。

(3) このガイドページに事業者名を追加します。

他の開発者に演算子の組み込みを示すには、このガイド ページも更新する必要があります。このページはtensorflow/lite/g3doc/guide/op_select_allowlist.mdにあります。

## TensorFlow core operators

The following is an exhaustive list of TensorFlow core operations that are
supported by TensorFlow Lite runtime with the Select TensorFlow Ops feature.

...
+*   `raw_ops.StringToNumber`
...

上記のリストはアルファベット順にソートされているため、名前が正しい場所に配置されることが保証されます。