รองรับตัวดำเนินการเลือก TensorFlow

ตัวดำเนินการหลักของ TensorFlow

ต่อไปนี้คือรายการการดำเนินการหลัก TensorFlow โดยละเอียดซึ่งได้รับการสนับสนุนโดยรันไทม์ TensorFlow Lite พร้อมฟีเจอร์ Select TensorFlow Ops

ตัวดำเนินการข้อความและ Sentence Piece ของ TensorFlow

ตัวดำเนินการ TensorFlow Text และ Sentence Piece ต่อไปนี้ได้รับการรองรับหากคุณใช้ Python API สำหรับการแปลงและนำเข้าไลบรารีเหล่านั้น

ตัวดำเนินการ TF.Text:

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

ตัวดำเนินการ Sentence Piece:

  • 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 หรือ Sentence Piece เข้ากับแอปสุดท้ายหรือไบนารีด้วย

ตัวดำเนินการที่กำหนดโดยผู้ใช้

หากคุณ สร้างตัวดำเนินการ TensorFlow ของคุณเอง คุณยังสามารถแปลงโมเดลที่มีตัวดำเนินการเหล่านี้เป็น TensorFlow Lite ได้โดยแสดงรายการตัวดำเนินการที่จำเป็นใน experimental_select_user_tf_ops ดังต่อไปนี้:

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 op ในรายการที่อนุญาต มีสามที่ที่ต้องอัปเดตในลักษณะนี้ commit

(1) เพิ่มซอร์สโค้ดเคอร์เนลตัวดำเนินการลงในกฎ BUILD portable_extended_ops_group2

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 ซึ่งจำเป็นในซอร์สโค้ดเคอร์เนลของโอเปอเรเตอร์ คุณจะต้องเพิ่มไฟล์ส่วนหัวลงในกฎ BUILD ของ portable_extended_ops_headers BUILD ดังต่อไปนี้:

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

        ...
    ],
)

(2) เพิ่มชื่อผู้ดำเนินการลงในรายการที่อนุญาต

รายการที่อนุญาตถูกกำหนดไว้ใน tensorflow/lite/delegates/flex/allowlisted_flex_ops.cc ต้องระบุชื่อโอเปอเรเตอร์หลักของ TensorFlow จึงจะได้รับอนุญาตผ่านตัวเลือก Select TF

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`
...

เนื่องจากรายการด้านบนจัดเรียงตามลำดับตัวอักษร จึงช่วยให้แน่ใจว่าได้วางชื่อในตำแหน่งที่ถูกต้อง