loadDecisionHeadBackend method

Future<BackendDecisionHeadInfo> loadDecisionHeadBackend(
  1. String headPath, {
  2. String? configPath,
})

Loads the decision head at headPath for the loaded model.

This is the low-level integration hook used by DecisionEngine. configPath names a JSON config for head files without laya.config metadata. The returned BackendDecisionHeadInfo.handle is an engine handle that this engine never reuses, not the backend's own handle; pass it to runDecisionBackend and freeDecisionHeadBackend. The head stays usable until it is freed or the model is unloaded; on Web, a bridge that restarts its runtime frees it too.

Implementation

Future<BackendDecisionHeadInfo> loadDecisionHeadBackend(
  String headPath, {
  String? configPath,
}) async {
  final decisionBackend = _decisionBackend();
  _ensureReady(requireContext: false);
  final epoch = _decisionHeadEpoch;
  final head = await decisionBackend.decisionHeadLoad(
    _modelHandle!,
    headPath,
    configPath: configPath,
  );
  if (epoch != _decisionHeadEpoch) {
    await decisionBackend
        .decisionHeadFree(head.handle)
        .catchError((Object _) {});
    throw LlamaStateException(
      'The model was unloaded while its decision head was loading. Load '
      'the model and the DecisionEngine again.',
    );
  }
  final handle = _nextDecisionHeadHandle++;
  _decisionHeadHandles[handle] = head.handle;
  return BackendDecisionHeadInfo(
    handle: handle,
    hiddenSize: head.hiddenSize,
    clsToken: head.clsToken,
    sepToken: head.sepToken,
    maskToken: head.maskToken,
    maskText: head.maskText,
    configJson: head.configJson,
    deviceName: head.deviceName,
  );
}