感謝您提供的連結。根據 TVM 官方文件的說明,TVM 的 tvm.runtime.profiling 模組提供了詳細的 API 參考,讓我們能夠更深入地了解如何使用 TVM 的 profiling 功能。以下是一些關鍵的類別和方法,以及如何應用它們來分析已編譯的 Relax 模型(.so 檔案):


🔍 tvm.runtime.profiling.Report 類別

這個類別代表一個 profiling 報告,包含了模型執行期間每個操作的詳細資訊。主要屬性和方法包括:


🧪 實作範例:分析已編譯的 Relax 模型

以下是一個完整的範例,展示如何載入已編譯的 .so 模型,執行並生成 profiling 報告:

import tvm
from tvm.relax.vm import Executable, VirtualMachine
from tvm.runtime.profiling import Report
import numpy as np

# 載入已編譯的 Relax 模型
ex = Executable.load("./encoder_model_fp16.so")
dev = tvm.cpu()  # 或 tvm.cuda(),視您的硬體而定

# 建立虛擬機器
vm = VirtualMachine(ex, dev)

# 準備輸入資料
input_tensor = np.random.rand(1, 80, 3000).astype("float16")
tvm_input = tvm.nd.array(input_tensor, device=dev)

# 執行並生成 profiling 報告
report: Report = vm.profile("main", [tvm_input])

# 顯示報告的表格形式
df = report.table()
print(df)

# 將報告儲存為 CSV 檔案
with open("profile_report.csv", "w") as f:
    f.write(report.csv())

# 將報告儲存為 JSON 檔案
with open("profile_report.json", "w") as f:
    f.write(report.as_json())


📌 注意事項


如果您需要進一步的協助,例如分析 Decoder 模型、視覺化報告、或是整合其他工具,請隨時告訴我,我將樂意提供協助。

profile_encoder.py