連結

Link 作業會將一個或多個 Qualcomm® AI Engine Direct DLC 模型轉換為單一的 Qualcomm® AI Engine Direct context 二進位檔。Link 作業可以將多個模型合併為一個可部署的資產,該資產包含多個共享權重的圖(graph)。權重會根據名稱與內容自動共享。常見的使用情境包括支援同一個網路的不同實例,例如不同的輸入形狀。也可以選擇部分共享或完全不共享權重。

備註

Link 作業僅適用於 Qualcomm® AI Engine Direct DLC 模型。

變數輸入大小範例

在此示例中,我們有一個分割網絡,我們希望能夠在手機上的橫向或縱向模式下執行。我們假設模型已經訓練以支持這些輸入大小。

要建立這個模型,我們首先為兩個輸入分別編譯兩個獨立的 DLC 模型。接著,將這兩個資產連結成一個 context 二進位檔。

此範例需要安裝 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 context 二進位檔。若要對此類模型進行效能分析或推論,需使用 --qnn_options context_enable_graphs=<graph_name> 指定要使用的圖。更多資訊請參見 Qualcomm® AI Engine Direct Options

在 Qualcomm® AI Engine Direct context 二進位檔中可用的圖名稱,可以在模型的 Qualcomm® AI Hub 頁面中的 Metadata 區段找到。在上述範例中,我們使用 --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",
)