링킹
링크 작업은 하나 이상의 Qualcomm® AI Engine Direct DLC 모델을 단일 Qualcomm® AI Engine Direct 컨텍스트 바이너리로 변환합니다. 링크 작업은 여러 모델을 하나의 배포 가능한 자산으로 결합할 수 있으며, 이 자산은 가중치를 공유하는 여러 그래프를 포함합니다. 가중치는 이름과 내용에 따라 자동으로 공유됩니다. 일반적인 사용 사례는 동일한 네트워크의 다양한 인스턴스를 지원하는 것으로, 예를 들어 입력 형태가 다른 경우입니다. 부분적으로 또는 전혀 가중치를 공유하지 않는 것도 가능합니다.
참고
링크 작업은 Qualcomm® AI Engine Direct DLC 모델에만 적용됩니다.
가변 입력 크기 예제
이 예제에서는 세그먼테이션 네트워크를 모바일 폰에서 가로 모드와 세로 모드 모두에서 실행할 수 있도록 하고자 합니다. 모델이 이러한 입력 크기를 지원하도록 훈련되었다고 가정합니다.
이 모델을 만들기 위해 두 개의 입력에 대해 별도의 DLC 모델을 컴파일하는 것부터 시작합니다. 그런 다음 이 두 자산을 하나의 컨텍스트 바이너리로 연결합니다.
이 예제는 Torchvision 패키지가 필요합니다. 먼저 단일 그래프 DLC 모델을 컴파일합니다:
import torch
import torchvision
import qai_hub as hub
# Using pre-trained FCN ResNet-50 semantic segmentation model
# We wrap the model in a module to convert the output dictionary containing
# both the primary output and auxiliary output, to return only the primary.
class FCN(torch.nn.Module):
def __init__(self):
super().__init__()
self.model = torchvision.models.segmentation.fcn_resnet50(pretrained=True)
def forward(self, x):
return self.model(x)["out"]
torch_model = FCN()
torch_model.eval()
input_shape_landscape: tuple[int, ...] = (1, 3, 256, 384)
input_shape_portrait: tuple[int, ...] = (1, 3, 384, 256)
# Trace model
# The traced model will be the same for both input shapes, so the asset can be
# re-used for both compile jobs. For models where different input shapes
# trigger different code paths, link jobs will still work, but both inputs will
# need to be traced separately.
example_input = torch.rand(input_shape_landscape)
pt_model = torch.jit.trace(torch_model, example_input)
src_model = hub.upload_model(pt_model)
device = hub.Device("Samsung Galaxy S24 (Family)")
# Compile both models
compile_job_landscape = hub.submit_compile_job(
src_model,
name="FCN Landscape",
device=device,
options="--target_runtime qnn_dlc --qnn_options context_enable_graphs=landscape",
input_specs=dict(image=input_shape_landscape),
)
assert isinstance(compile_job_landscape, hub.CompileJob)
compile_job_portrait = hub.submit_compile_job(
src_model,
name="FCN Portrait",
device=device,
options="--target_runtime qnn_dlc --qnn_options context_enable_graphs=portrait",
input_specs=dict(image=input_shape_portrait),
)
assert isinstance(compile_job_portrait, hub.CompileJob)
model_landscape = compile_job_landscape.get_target_model()
model_portrait = compile_job_portrait.get_target_model()
assert isinstance(model_landscape, hub.Model)
assert isinstance(model_portrait, hub.Model)
이제 모델을 연결하기 위해 submit_link_job()
을 호출할 준비가 되었습니다:
# Link the models
link_job = hub.submit_link_job(
[model_landscape, model_portrait],
device=hub.Device("Samsung Galaxy S24 (Family)"),
name="FCN Landscape+Portrait",
)
assert isinstance(link_job, hub.LinkJob)
linked_model = link_job.get_target_model()
assert isinstance(linked_model, hub.Model)
결과 모델은 여러 그래프를 포함하는 Qualcomm® AI Engine Direct 컨텍스트 바이너리입니다. 이러한 모델에 대해 프로파일링하거나 추론을 수행하려면 --qnn_options context_enable_graphs=<graph_name>
옵션을 사용하여 사용할 그래프를 지정해야 합니다. 자세한 내용은 Qualcomm® AI Engine Direct Options 를 참조하세요.
Qualcomm® AI Engine Direct 컨텍스트 바이너리에서 사용 가능한 그래프 이름은 모델의 Qualcomm® AI Hub 페이지의 메타데이터 섹션에서 확인할 수 있습니다. 위 예제에서는 --qnn_options context_enable_graphs
옵션을 사용하여 컴파일 중에 그래프 이름을 명시적으로 설정했습니다. 위 모델을 프로파일링하려면:
# Profile the portrait model
profile_job = hub.submit_profile_job(
linked_model,
name="FCN Portrait",
device=device,
options="--qnn_options context_enable_graphs=portrait",
)