모델 프로파일링
디바이스에 신경망 모델을 배포할 때 많은 중요한 질문이 발생합니다:
타깃 하드웨어 전체에서 추론 지연 시간은 얼마입니까?
모델이 특정 메모리 예산 내에 맞습니까?
모델이 신경 처리 장치(NPU)를 활용할 수 있습니까?
프로파일 작업은 클라우드에서 물리적 디바이스에서 모델을 실행하고 성능을 분석하여 이러한 질문에 대한 답을 제공합니다.
프로파일 작업도 --qairt_version 플래그를 사용하여 특정 Qualcomm® AI Runtime 버전을 선택할 수 있습니다. 지정하지 않으면 버전 선택 에 따라 버전이 선택됩니다.
이전에 컴파일된 모델 프로파일링
Qualcomm® AI Hub Workbench 는 이전에 컴파일된 모델 에 대한 프로파일링을 지원합니다. 이 예제에서는 submit_compile_job() 을 사용해 미리 컴파일된 모델을 최적화하고 프로파일링합니다. compile_job 에서 생성된 컴파일된 모델을 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)
모델 업로드, 컴파일, 작업 제출 시 사용할 수 있는 옵션에 대한 자세한 내용은 upload_model(), submit_compile_job(), 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 컨텍스트 바이너리가 있는 사전 컴파일된 QNN ONNX 모델도 직접 프로파일링할 수 있습니다. 이 예제에서는 Compiling to a Precompiled 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 모델 프로파일링
일부 디바이스에는 부동소수(FP32 또는 FP16) 모델을 지원하지 않는 HTP가 있습니다. 이러한 디바이스에서 ONNX Runtime 를 사용하여 실행할 때, Qualcomm® AI Hub Workbench 는 QNN Execution Provider를 자동으로 활성화하지 않습니다. 이 동작은 모든 모델을 CPU에서 실행하여 성공할 수 있도록 합니다. 완전 양자화된 모델이 있는 경우, --onnx_execution_providers=qnn 옵션을 사용하여 QNN Execution Provider를 활성화하면 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 모델 을 재사용하지만, 이번에는 두 디바이스에서 프로파일링합니다.
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)
각 디바이스에 대해 별도의 프로파일 작업이 생성됩니다.
프로파일링을 위해 모델 업로드
프로파일 작업을 제출하지 않고 모델(e.g. 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)"),
)
이전에 업로드된 모델 프로파일링
이전 작업에서 모델을 재사용하여 새 프로파일 작업(e.g., 다른 디바이스에서)을 시작할 수 있습니다. 이렇게 하면 동일한 모델을 여러 번 업로드할 필요가 없습니다.:
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"),
)