ขอขอบคุณที่เข้าร่วม Google I/O ดูเซสชั่นทั้งหมดตามความต้องการ ดูตามความต้องการ

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

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

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

ตัวดำเนินการ TensorFlow Text และ SentencePiece

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

TF ตัวดำเนินการข้อความ:

  • 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 ของคุณเอง คุณยังสามารถแปลงโมเดลที่มีพวกมันเป็น 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 ดังนี้:

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

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