リンク

リンクジョブは、1つまたは複数の Qualcomm® AI Engine Direct DLC モデルを単一の Qualcomm® AI Engine Direct コンテキストバイナリに変換します。リンクジョブでは、複数のモデルを1つのデプロイ可能なアセットに結合し、複数のグラフが重みを共有することができます。重みは名前と内容に基づいて自動的に共有されます。一般的なユースケースとしては、異なる入力形状など、同じネットワークの異なるインスタンスをサポートすることが挙げられます。重みの一部共有や、まったく共有しないことも可能です。

注釈

リンクジョブは Qualcomm® AI Engine Direct DLC モデル専用です。

入力サイズの変数の例

この例では、モバイルフォンで横向きまたは縦向きの両方で実行できるセグメンテーションネットワークを持っています。このモデルがこれらの入力サイズをサポートするように学習済みと仮定します。

このモデルを作成するには、まず2つの入力に対して個別の DLC モデルをコンパイルします。その後、これら2つのアセットを1つのコンテキストバイナリにリンクします。

この例では、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",
)