run method

  1. @override
Future<Float32List> run(
  1. Int32List ids
)
override

The two logits, benign and injection, of ids: <bos>, at most 2046 tokens, and <eos>. The graph takes int64; web has no Int64List, so each backend converts.

Implementation

@override
Future<Float32List> run(Int32List ids) async {
  if (_closed) throw StateError('Wolf Defender ONNX backend is closed');
  final shape = [1, ids.length];
  final inputs = [
    _graph.int64Tensor(Int64List.fromList(ids), shape),
    _graph.int64Tensor(
      Int64List(ids.length)..fillRange(0, ids.length, 1),
      shape,
    ),
  ];
  List<Pointer<OrtValue>> outputs = const [];
  try {
    outputs = _graph.run(
      inputNames: const ['input_ids', 'attention_mask'],
      inputValues: [for (final input in inputs) input.value],
      outputNames: const ['logits'],
    );
    return _graph.copyFloatTensor(outputs.single);
  } finally {
    for (final input in inputs) {
      input.release();
    }
    for (final output in outputs) {
      _graph.releaseValue(output);
    }
  }
}