分析模型
在設備上部署神經網絡模型時,會出現許多重要問題:
目標硬件上的推理延遲是多少?
模型是否符合某個內存預算?
我的模型能否利用神經處理單元?
分析工作通過在雲端的實體設備上運行您的模型並分析其性能。
分析任務支持使用 --qairt_version
來選擇特定的 Qualcomm® AI Runtime 版本。如果未指定,則會根據 版本選擇 選擇一個版本。
分析先前編譯的模型
Qualcomm® AI Hub 支持配置 previously compiled model。在此示例中,我們優化並分析使用 submit_compile_job()
先前編譯的模型。請注意,我們如何使用 get_target_model()
從 compile_job
中使用編譯的模型。
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("Samsung Galaxy S23 (Family)"),
)
assert isinstance(profile_job, hub.ProfileJob)
返回值是 ProfileJob
的實例。要查看所有工作的列表,請轉到 /jobs/。
分析 PyTorch 模型
此示例需要 PyTorch,可以按如下方式安裝。
pip3 install "qai-hub[torch]"
在此示例中,我們使用 Qualcomm® AI Hub 優化並分析 PyTorch 模型。
import torch
import qai_hub as hub
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 = hub.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 = hub.submit_profile_job(
model=compile_job.get_target_model(),
device=hub.Device("Samsung Galaxy S23 (Family)"),
)
assert isinstance(profile_job, hub.ProfileJob)
有關上傳、編譯和提交工作時的選項的更多信息,請參閱 upload_model()
、submit_compile_job()
和 submit_profile_job()
。
分析 TorchScript 模型
如果您已經有保存的跟踪或腳本化的 torch 模型(使用 torch.jit.save
保存),您可以直接提交。我們將使用 mobilenet_v2.pt 作為示例。與前一個示例類似,您只能在將 TorchScript 模型編譯為合適的目標後配置它。
import qai_hub as hub
# Compile previously saved torchscript model
compile_job = hub.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 = hub.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 也支持 ONNX 模型。ONNX 模型可以通過將它們編譯為目標(例如 TensorFlow Lite)來分析,或者直接使用 ONNX Runtime 進行分析。我們將使用 mobilenet_v2.onnx 作為這兩種方法的示例。此示例編譯為 TensorFlow Lite 目標模型。
import qai_hub as hub
compile_job = hub.submit_compile_job(
model="mobilenet_v2.onnx",
device=hub.Device("Samsung Galaxy S23 (Family)"),
)
assert isinstance(compile_job, hub.CompileJob)
profile_job = hub.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
profile_job = hub.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)
QNN DLC 的分析
Qualcomm® AI Hub 支持 QNN DLC 格式進行分析。在此示例中,我們繼續 將 PyTorch 模型編譯為 QNN DLC 的示例並分析模型:
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("Samsung Galaxy S24 (Family)"),
)
assert isinstance(profile_job, hub.ProfileJob)
分析QNN 上下文二進制文件
Qualcomm® AI Hub 支持 QNN 上下文二進制格式的配置。在此示例中,我們繼續 編譯 PyTorch 模型到 QNN 上下文二進位檔 中的示例並分析模型:
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("Samsung Galaxy S24 (Family)"),
)
assert isinstance(profile_job, hub.ProfileJob)
分析 TensorFlow Lite 模型
Qualcomm® AI Hub 也支持配置 .tflite 格式的模型。我們將使用 SqueezeNet10 model。
import qai_hub as hub
# Profile TensorFlow Lite model (from file)
profile_job = hub.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
devices = [
hub.Device("Samsung Galaxy S23 (Family)"), # Snapdragon 8 Gen 2
hub.Device("Samsung Galaxy S24 (Family)"), # Snapdragon 8 Gen 3
]
jobs = hub.submit_profile_job(model="SqueezeNet10.tflite", device=devices)
為每個設備創建單獨的分析工作。
上傳模型以進行分析
可以上傳模型(例如 SqueezeNet10.tflite)而不提交分析工作。
import qai_hub as hub
hub_model = hub.upload_model("SqueezeNet10.tflite")
print(hub_model)
您現在可以使用上傳模型的 model_id
運行分析工作。
import qai_hub as hub
# Retrieve model using ID
hub_model = hub.get_model("mabc123")
# Submit job
profile_job = hub.submit_profile_job(
model=hub_model,
device=hub.Device("Samsung Galaxy S23 (Family)"),
)
分析先前上傳的模型
我們可以重用先前工作的模型來啟動新的分析工作(例如,在不同的設備上)。這避免了多次上傳相同的模型。
import qai_hub as hub
# Get the model from the profile job
profile_job = hub.get_job("jabc123")
hub_model = profile_job.model
# Run the model from the job
new_profile_job = hub.submit_profile_job(
model=hub_model,
device=hub.Device("Samsung Galaxy S22 Ultra 5G"),
)