分析模型
在設備上部署神經網絡模型時,會出現許多重要問題:
目標硬件上的推理延遲是多少?
模型是否符合某個內存預算?
我的模型能否利用神經處理單元?
分析工作通過在雲端的實體設備上運行您的模型並分析其性能。
分析任務支持使用 --qairt_version 來選擇特定的 Qualcomm® AI Runtime 版本。如果未指定,則會根據 版本選擇 選擇一個版本。
分析先前編譯的模型
Qualcomm® AI Hub Workbench supports profiling a previously compiled model.
In this example, we optimize and profile a model that is previously compiled
using a submit_compile_job(). Note how we were able to use the
compiled model from compile_job using
get_target_model().
import qai_hub as hub
client = hub.Client()
# Profile the previously compiled model
profile_job = client.submit_profile_job(
model=compile_job.get_target_model(),
device=hub.Device("Samsung Galaxy S24 (Family)"),
)
assert isinstance(profile_job, hub.ProfileJob)
返回值是 ProfileJob 的實例。要查看所有工作的列表,請轉到 /jobs/。
分析 PyTorch 模型
此示例需要 PyTorch,可以按如下方式安裝。
pip3 install "qai-hub[torch]"
在此範例中,我們使用 Qualcomm® AI Hub Workbench 來最佳化並分析一個 PyTorch 模型。
import torch
import qai_hub as hub
client = hub.Client()
class SimpleNet(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear = torch.nn.Linear(5, 2)
def forward(self, x):
return self.linear(x)
input_shapes: list[tuple[int, ...]] = [(3, 5)]
torch_model = SimpleNet()
# Trace the model using random inputs
torch_inputs = tuple(torch.randn(shape) for shape in input_shapes)
pt_model = torch.jit.trace(torch_model, torch_inputs)
# Submit compile job
compile_job = client.submit_compile_job(
model=pt_model,
device=hub.Device("Samsung Galaxy S23 (Family)"),
input_specs=dict(x=input_shapes[0]),
)
assert isinstance(compile_job, hub.CompileJob)
# Submit profile job using results form compile job
profile_job = client.submit_profile_job(
model=compile_job.get_target_model(),
device=hub.Device("Samsung Galaxy S23 (Family)"),
)
assert isinstance(profile_job, hub.ProfileJob)
For more information on options when uploading, compiling, and submitting a job, see
upload_model(), submit_compile_job(), and
submit_profile_job().
分析 TorchScript 模型
如果您已經有保存的跟踪或腳本化的 torch 模型(使用 torch.jit.save 保存),您可以直接提交。我們將使用 mobilenet_v2.pt 作為示例。與前一個示例類似,您只能在將 TorchScript 模型編譯為合適的目標後配置它。
import qai_hub as hub
client = hub.Client()
# Compile previously saved torchscript model
compile_job = client.submit_compile_job(
model="mobilenet_v2.pt",
device=hub.Device("Samsung Galaxy S23 (Family)"),
input_specs=dict(image=(1, 3, 224, 224)),
)
assert isinstance(compile_job, hub.CompileJob)
profile_job = client.submit_profile_job(
model=compile_job.get_target_model(),
device=hub.Device("Samsung Galaxy S23 (Family)"),
)
assert isinstance(profile_job, hub.ProfileJob)
分析 ONNX 模型
Qualcomm® AI Hub Workbench 也支援 ONNX 模型。ONNX 模型可以透過編譯成目標(例如 TensorFlow Lite)來進行分析,或直接使用 ONNX Runtime 進行分析。我們將使用 mobilenet_v2.onnx 作為兩種方法的範例。本範例會編譯成 TensorFlow Lite 目標模型。
import qai_hub as hub
client = hub.Client()
compile_job = client.submit_compile_job(
model="mobilenet_v2.onnx",
device=hub.Device("Samsung Galaxy S23 (Family)"),
)
assert isinstance(compile_job, hub.CompileJob)
profile_job = client.submit_profile_job(
model=compile_job.get_target_model(),
device=hub.Device("Samsung Galaxy S23 (Family)"),
)
assert isinstance(profile_job, hub.ProfileJob)
此示例直接使用 ONNX Runtime 配置 ONNX 模型。
import qai_hub as hub
client = hub.Client()
profile_job = client.submit_profile_job(
model="mobilenet_v2.onnx",
device=hub.Device("Samsung Galaxy S23 (Family)"),
)
assert isinstance(profile_job, hub.ProfileJob)
預編譯的 QNN ONNX 模型與 QNN 上下文二進制文件也可以直接分析。在此示例中,我們繼續 編譯為預編譯的 QNN ONNX 中的編譯示例並分析模型:
import qai_hub as hub
# Profile the previously compiled model
profile_job = hub.submit_profile_job(
model=compile_job.get_target_model(),
device=hub.Device("Snapdragon 8 Elite QRD"),
)
assert isinstance(profile_job, hub.ProfileJob)
在不支援浮點運算的裝置上分析 ONNX 模型
某些裝置的 HTP 不支援浮點(FP32 或 FP16)模型。當在這些裝置上使用 ONNX Runtime 執行時,Qualcomm® AI Hub Workbench 不會自動啟用 QNN Execution Provider。此行為允許所有模型透過 CPU 執行而成功。如果您有完全量化的模型,啟用 QNN Execution Provider 並使用選項 --onnx_execution_providers=qnn,可能在 NPU 上獲得更佳效能。在此範例中,我們先使用預設選項在 CPU 上執行完全量化的模型,然後啟用 QNN Execution Provider 在 NPU 上執行該模型。我們將使用 resnet50_w8a8.onnx 作為兩種方法的範例。
import qai_hub as hub
client = hub.Client()
# Profile a quantized model with default options - the model will run on the CPU
profile_job = client.submit_profile_job(
"resnet50_w8a8.onnx",
device=hub.Device("Dragonwing RB3 Gen 2 Vision Kit"),
)
assert isinstance(profile_job, hub.ProfileJob)
# Profile a quantized model with the QNN Execution Provider
profile_job = client.submit_profile_job(
"resnet50_w8a8.onnx",
device=hub.Device("Dragonwing RB3 Gen 2 Vision Kit"),
options="--onnx_execution_providers=qnn",
)
assert isinstance(profile_job, hub.ProfileJob)
QNN DLC 的分析
Qualcomm® AI Hub Workbench 支援 QNN DLC 格式進行分析。在此範例中,我們延續 將 PyTorch 模型編譯為 QNN DLC 的範例並分析該模型:
import qai_hub as hub
client = hub.Client()
# Profile the previously compiled model
profile_job = client.submit_profile_job(
model=compile_job.get_target_model(),
device=hub.Device("Samsung Galaxy S24 (Family)"),
)
assert isinstance(profile_job, hub.ProfileJob)
分析QNN 上下文二進制文件
Qualcomm® AI Hub Workbench 支援 QNN 內容二進位格式進行分析。在此範例中,我們延續 編譯 PyTorch 模型到 QNN 上下文二進位檔 的範例並分析該模型:
import qai_hub as hub
client = hub.Client()
# Profile the previously compiled model
profile_job = client.submit_profile_job(
model=compile_job.get_target_model(),
device=hub.Device("Samsung Galaxy S24 (Family)"),
)
assert isinstance(profile_job, hub.ProfileJob)
分析 TensorFlow Lite 模型
Qualcomm® AI Hub Workbench 也支援分析 .tflite 格式的模型。我們將使用 SqueezeNet10 模型。
import qai_hub as hub
client = hub.Client()
# Profile TensorFlow Lite model (from file)
profile_job = client.submit_profile_job(
model="SqueezeNet10.tflite",
device=hub.Device("Samsung Galaxy S23 (Family)"),
)
在多個設備上分析模型
通常在多個設備上建模性能很重要。在此示例中,我們在最近的 Snapdragon® 8 Gen 1 和 Snapdragon® 8 Gen 2 設備上進行分析以獲得良好的測試覆蓋。我們重用 TensorFlow Lite 示例中的 SqueezeNet model,但這次我們在兩個設備上進行分析。
import qai_hub as hub
client = hub.Client()
devices = [
hub.Device("Samsung Galaxy S23 (Family)"), # Snapdragon 8 Gen 2
hub.Device("Samsung Galaxy S24 (Family)"), # Snapdragon 8 Gen 3
]
jobs = client.submit_profile_job(model="SqueezeNet10.tflite", device=devices)
為每個設備創建單獨的分析工作。
上傳模型以進行分析
可以上傳模型(例如 SqueezeNet10.tflite)而不提交分析工作。
import qai_hub as hub
client = hub.Client()
hub_model = client.upload_model("SqueezeNet10.tflite")
print(hub_model)
您現在可以使用上傳模型的 model_id 運行分析工作。
import qai_hub as hub
client = hub.Client()
# Retrieve model using ID
hub_model = client.get_model("mabc123")
# Submit job
profile_job = client.submit_profile_job(
model=hub_model,
device=hub.Device("Samsung Galaxy S23 (Family)"),
)
分析先前上傳的模型
我們可以重用先前工作的模型來啟動新的分析工作(例如,在不同的設備上)。這避免了多次上傳相同的模型。
import qai_hub as hub
client = hub.Client()
# Get the model from the profile job
profile_job = client.get_job("jabc123")
hub_model = profile_job.model
# Run the model from the job
new_profile_job = client.submit_profile_job(
model=hub_model,
device=hub.Device("Samsung Galaxy S22 Ultra 5G"),
)