createInt64Tensor method

Pointer<Int64> createInt64Tensor(
  1. Pointer<Pointer<OrtValue>> inputTensorPointer, {
  2. required Pointer<OrtMemoryInfo> memoryInfo,
  3. required List<int> values,
  4. List<int>? shape,
})

You MUST call calloc.free on the returned pointer when you are done with it, i.e. once inference is complete.

Implementation

Pointer<Int64> createInt64Tensor(
  Pointer<Pointer<OrtValue>> inputTensorPointer, {
  required Pointer<OrtMemoryInfo> memoryInfo,
  required List<int> values,
  List<int>? shape,
}) {
  // Compatibility with what method assumed prior to shape being in API.
  shape = shape ?? [1, values.length];
  final sizeOfInt64 = sizeOf<Int64>();
  final inputTensorNative = calloc<Int64>(values.length * sizeOfInt64);

  for (var i = 0; i < values.length; i++) {
    inputTensorNative[i] = values[i];
  }

  // If shape is provided, use it; otherwise default to 1D tensor with shape [values.length]
  final inputShapeLengthInBytes = shape.length;
  final inputShape = calloc<Int64>(inputShapeLengthInBytes * sizeOfInt64);

  for (var i = 0; i < shape.length; i++) {
    inputShape[i] = shape[i];
  }

  final ptrVoid = inputTensorNative.cast<Void>();
  final status = createTensorWithDataAsOrtValue(
    inputTensorPointer,
    memoryInfo: memoryInfo,
    inputData: ptrVoid,
    inputDataLengthInBytes: values.length * sizeOfInt64,
    inputShape: inputShape,
    inputShapeLengthInBytes: inputShapeLengthInBytes,
    onnxTensorElementDataType:
        ONNXTensorElementDataType.ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64.value,
  );
  if (status.isError) {
    final error =
        'Code: ${getErrorCodeMessage(status)}\n'
        'Message: ${consumeErrorMessage(status)}';
    calloc.free(inputTensorNative);
    calloc.free(inputShape);
    throw Exception(error);
  }
  calloc.free(inputShape);
  return inputTensorNative;
}