ดูบน TensorFlow.org | ทำงานใน Google Colab | ดูแหล่งที่มาบน GitHub | ดาวน์โหลดโน๊ตบุ๊ค |
TensorFlow Lite เขียน API มีวิธีการรักษาของคุณ tf.function
รุ่น compatibile กับ TensorFlow Lite
ติดตั้ง
import tensorflow as tf
ปัญหาความเข้ากันได้ของ TensorFlow ถึง TensorFlow Lite
หากคุณต้องการใช้รุ่น TF บนอุปกรณ์ คุณต้องแปลงเป็นรุ่น TFLite เพื่อใช้งานจากตัวแปล TFLite ระหว่างการแปลง คุณอาจพบข้อผิดพลาดด้านความเข้ากันได้เนื่องจาก TensorFlow ops ที่ไม่สนับสนุนโดยชุด op ในตัว TFLite
นี่เป็นปัญหาที่น่ารำคาญ คุณจะตรวจพบมันก่อนหน้านี้ได้อย่างไรเหมือนเวลาสร้างแบบจำลอง?
โปรดทราบว่ารหัสต่อไปนี้จะล้มเหลวใน converter.convert()
โทร
@tf.function(input_signature=[
tf.TensorSpec(shape=[None], dtype=tf.float32)
])
def f(x):
return tf.cosh(x)
# Evaluate the tf.function
result = f(tf.constant([0.0]))
print (f"result = {result}")
result = [1.]
# Convert the tf.function
converter = tf.lite.TFLiteConverter.from_concrete_functions(
[f.get_concrete_function()], f)
try:
fb_model = converter.convert()
except Exception as e:
print(f"Got an exception: {e}")
Got an exception: /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:746:0: error: 'tf.Cosh' op is neither a custom op nor a flex op /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py:2538:0: note: called from /tmp/ipykernel_11095/885400331.py:5:0: note: called from /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py:1125:0: note: called from /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py:672:0: note: called from /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py:1143:0: note: called from /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py:3402:0: note: called from /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py:3557:0: note: called from /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py:3157:0: note: called from /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py:781:0: note: called from /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:746:0: note: Error code: ERROR_NEEDS_FLEX_OPS <unknown>:0: error: failed while converting: 'main': Some ops are not supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select. See instructions: https://www.tensorflow.org/lite/guide/ops_select TF Select ops: Cosh Details: tf.Cosh(tensor<?xf32>) -> (tensor<?xf32>) : {device = ""} 2021-11-15 12:14:34.777684: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format. 2021-11-15 12:14:34.777724: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency. loc(callsite("Cosh"("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py":746:0) at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py":2538:0 at callsite("/tmp/ipykernel_11095/885400331.py":5:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py":1125:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py":672:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py":1143:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3402:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3557:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3157:0 at "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py":781:0)))))))))): error: 'tf.Cosh' op is neither a custom op nor a flex op error: failed while converting: 'main': Some ops are not supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select. See instructions: https://www.tensorflow.org/lite/guide/ops_select TF Select ops: Cosh Details: tf.Cosh(tensor<?xf32>) -> (tensor<?xf32>) : {device = ""}
การใช้งานการเขียน Target Aware อย่างง่าย
เราแนะนำ API การเขียนเพื่อตรวจหาปัญหาความเข้ากันได้ของ TensorFlow Lite ในช่วงเวลาที่สร้างแบบจำลอง
คุณเพียงแค่ต้องเพิ่ม @tf.lite.experimental.authoring.compatible
มัณฑนากรในการห่อของคุณ tf.function
รุ่นเพื่อตรวจสอบความเข้ากันได้ TFLite
หลังจากนี้ จะตรวจสอบความเข้ากันได้โดยอัตโนมัติเมื่อคุณประเมินแบบจำลองของคุณ
@tf.lite.experimental.authoring.compatible
@tf.function(input_signature=[
tf.TensorSpec(shape=[None], dtype=tf.float32)
])
def f(x):
return tf.cosh(x)
# Evaluate the tf.function
result = f(tf.constant([0.0]))
print (f"result = {result}")
COMPATIBILITY WARNING: op 'tf.Cosh' require(s) "Select TF Ops" for model conversion for TensorFlow Lite. https://www.tensorflow.org/lite/guide/ops_select Op: tf.Cosh - /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:746 - /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py:2538 - /tmp/ipykernel_11095/885400331.py:5 result = [1.] 2021-11-15 12:14:34.816991: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format. 2021-11-15 12:14:34.817023: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency. loc(callsite("Cosh"("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py":746:0) at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py":2538:0 at callsite("/tmp/ipykernel_11095/885400331.py":5:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py":1125:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py":672:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py":1143:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3402:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3557:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3157:0 at "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py":781:0)))))))))): error: 'tf.Cosh' op is neither a custom op nor a flex op error: failed while converting: 'main': Some ops are not supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select. See instructions: https://www.tensorflow.org/lite/guide/ops_select TF Select ops: Cosh Details: tf.Cosh(tensor<?xf32>) -> (tensor<?xf32>) : {device = ""}
ถ้ามีปัญหาความเข้ากัน TensorFlow Lite พบก็จะแสดง COMPATIBILITY WARNING
หรือ COMPATIBILITY ERROR
กับตำแหน่งที่แน่นอนของสหกรณ์ที่มีปัญหา ในตัวอย่างนี้ก็แสดงให้เห็นสถานที่ตั้งของ tf.Cosh
สหกรณ์ในรูปแบบ tf.function ของคุณ
นอกจากนี้คุณยังสามารถตรวจสอบบันทึกการเข้ากันได้กับ <function_name>.get_compatibility_log()
วิธีการ
compatibility_log = '\n'.join(f.get_compatibility_log())
print (f"compatibility_log = {compatibility_log}")
compatibility_log = COMPATIBILITY WARNING: op 'tf.Cosh' require(s) "Select TF Ops" for model conversion for TensorFlow Lite. https://www.tensorflow.org/lite/guide/ops_select Op: tf.Cosh - /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:746 - /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py:2538 - /tmp/ipykernel_11095/885400331.py:5
ยกข้อยกเว้นสำหรับความไม่ลงรอยกัน
คุณสามารถให้ตัวเลือกในการที่ @tf.lite.experimental.authoring.compatible
มัณฑนากร raise_exception
ตัวเลือกที่ช่วยให้คุณมีข้อยกเว้นเมื่อคุณกำลังพยายามที่จะประเมินผลการศึกษารูปแบบการตกแต่ง
@tf.lite.experimental.authoring.compatible(raise_exception=True)
@tf.function(input_signature=[
tf.TensorSpec(shape=[None], dtype=tf.float32)
])
def f(x):
return tf.cosh(x)
# Evaluate the tf.function
try:
result = f(tf.constant([0.0]))
print (f"result = {result}")
except Exception as e:
print(f"Got an exception: {e}")
COMPATIBILITY WARNING: op 'tf.Cosh' require(s) "Select TF Ops" for model conversion for TensorFlow Lite. https://www.tensorflow.org/lite/guide/ops_select Op: tf.Cosh - /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:746 - /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py:2538 - /tmp/ipykernel_11095/885400331.py:5 Got an exception: CompatibilityException at <tensorflow.python.eager.def_function.Function object at 0x7ff2e016fc50> 2021-11-15 12:14:34.863629: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format. 2021-11-15 12:14:34.863657: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency. loc(callsite("Cosh"("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py":746:0) at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py":2538:0 at callsite("/tmp/ipykernel_11095/885400331.py":5:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py":1125:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py":672:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py":1143:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3402:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3557:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3157:0 at "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py":781:0)))))))))): error: 'tf.Cosh' op is neither a custom op nor a flex op error: failed while converting: 'main': Some ops are not supported by the native TFLite runtime, you can enable TF kernels fallback using TF Select. See instructions: https://www.tensorflow.org/lite/guide/ops_select TF Select ops: Cosh Details: tf.Cosh(tensor<?xf32>) -> (tensor<?xf32>) : {device = ""}
การระบุการใช้งาน "Select TF ops"
หากคุณทราบแล้ว Ops เลือก TF การใช้งานที่คุณสามารถบอกนี้ไปเขียน API โดยการตั้งค่า converter_target_spec
ก็เหมือนกัน tf.lite.TargetSpec วัตถุที่คุณจะใช้มันสำหรับ tf.lite.TFLiteConverter API
target_spec = tf.lite.TargetSpec()
target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS,
tf.lite.OpsSet.SELECT_TF_OPS,
]
@tf.lite.experimental.authoring.compatible(converter_target_spec=target_spec, raise_exception=True)
@tf.function(input_signature=[
tf.TensorSpec(shape=[None], dtype=tf.float32)
])
def f(x):
return tf.cosh(x)
# Evaluate the tf.function
result = f(tf.constant([0.0]))
print (f"result = {result}")
2021-11-15 12:14:34.921628: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format. WARNING:absl:Buffer deduplication procedure will be skipped when flatbuffer library is not properly loaded result = [1.] 2021-11-15 12:14:34.921661: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency. 2021-11-15 12:14:34.934915: W tensorflow/compiler/mlir/lite/flatbuffer_export.cc:1891] TFLite interpreter needs to link Flex delegate in order to run the model since it contains the following Select TFop(s): Flex ops: FlexCosh Details: tf.Cosh(tensor<?xf32>) -> (tensor<?xf32>) : {device = ""} See instructions: https://www.tensorflow.org/lite/guide/ops_select
กำลังตรวจสอบความเข้ากันได้ของ GPU
ถ้าคุณต้องการเพื่อให้แน่ใจว่ารูปแบบของคุณเป็น compatibile กับ ผู้ร่วมประชุม GPU ของ TensorFlow Lite คุณสามารถตั้งค่า experimental_supported_backends
ของ tf.lite.TargetSpec
ตัวอย่างต่อไปนี้แสดงวิธีการตรวจสอบความเข้ากันได้ของผู้ร่วมประชุม GPU ของรุ่นของคุณ โปรดทราบว่าโมเดลนี้มีปัญหาด้านความเข้ากันได้เนื่องจากใช้เมตริกซ์ 2 มิติกับโอเปอเรเตอร์ tf.slice และโอเปอเรเตอร์ tf.cosh ที่ไม่รองรับ คุณจะได้เห็นสอง COMPATIBILITY WARNING
ที่มีข้อมูลสถานที่
target_spec = tf.lite.TargetSpec()
target_spec.supported_ops = [
tf.lite.OpsSet.TFLITE_BUILTINS,
tf.lite.OpsSet.SELECT_TF_OPS,
]
target_spec.experimental_supported_backends = ["GPU"]
@tf.lite.experimental.authoring.compatible(converter_target_spec=target_spec)
@tf.function(input_signature=[
tf.TensorSpec(shape=[4, 4], dtype=tf.float32)
])
def func(x):
y = tf.cosh(x)
return y + tf.slice(x, [1, 1], [1, 1])
result = func(tf.ones(shape=(4,4), dtype=tf.float32))
'tfl.slice' op is not GPU compatible: SLICE supports for 3 or 4 dimensional tensors only, but node has 2 dimensional tensors. - /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py:1096 - /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/util/traceback_utils.py:150 - /tmp/ipykernel_11095/3833138856.py:13 'tf.Cosh' op is not GPU compatible: Not supported custom op FlexCosh - /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:746 - /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py:2538 - /tmp/ipykernel_11095/3833138856.py:12 COMPATIBILITY WARNING: op 'tf.Cosh, tfl.slice' aren't compatible with TensorFlow Lite GPU delegate. https://www.tensorflow.org/lite/performance/gpu 2021-11-15 12:14:34.998044: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format. 2021-11-15 12:14:34.998077: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency. 2021-11-15 12:14:35.012457: W tensorflow/compiler/mlir/lite/flatbuffer_export.cc:1891] TFLite interpreter needs to link Flex delegate in order to run the model since it contains the following Select TFop(s): Flex ops: FlexCosh Details: tf.Cosh(tensor<4x4xf32>) -> (tensor<4x4xf32>) : {device = ""} See instructions: https://www.tensorflow.org/lite/guide/ops_select loc(callsite("Cosh"("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py":746:0) at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py":2538:0 at callsite("/tmp/ipykernel_11095/3833138856.py":12:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py":1125:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py":672:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py":1143:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3402:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3557:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3157:0 at "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py":781:0)))))))))): error: 'tf.Cosh' op is not GPU compatible: Not supported custom op FlexCosh loc(callsite("Slice"("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py":1096:0) at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/util/traceback_utils.py":150:0 at callsite("/tmp/ipykernel_11095/3833138856.py":13:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py":1125:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py":672:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py":1143:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3402:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3557:0 at callsite("/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/function.py":3157:0 at "/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py":781:0)))))))))): error: 'tfl.slice' op is not GPU compatible: SLICE supports for 3 or 4 dimensional tensors only, but node has 2 dimensional tensors.
อ่านเพิ่มเติม
สำหรับข้อมูลเพิ่มเติม โปรดดูที่: