View Source TFLiteElixir.TFLiteTensor (tflite_elixir v1.0.0)

A typed multi-dimensional array used in Tensorflow Lite.

A %TFLiteTensor{} is a snapshot of the tensor's metadata plus a live handle to it. type/1, dims/1, shape/1 and quantization_params/1 answer from the snapshot; to_binary/2, to_nx/2 and set_data/2 go through the handle. The two can disagree after TFLiteElixir.Interpreter.resize_input_tensor/3, which moves the interpreter's tensors and retires every handle taken before it: the snapshot still reports the old shape while the handle reports that it has been retired. Fetch the tensor again with TFLiteElixir.Interpreter.tensor/2 after a resize, or pass tensor.reference to ask the interpreter directly.

Summary

Functions

Get the dimensions (C++) API

Get the quantization params

Set tensor data

Get the tensor shape

Get binary data

Get the data type

Types

@type nif_error() :: {:error, String.t()}
@type nif_resource_ok() :: {:ok, reference()}
@type tensor_type() ::
  :no_type
  | {:f, 32}
  | {:s, 32}
  | {:u, 8}
  | {:s, 64}
  | :string
  | :bool
  | {:s, 16}
  | {:c, 64}
  | {:s, 8}
  | {:f, 16}
  | {:f, 64}
  | {:c, 128}
  | {:u, 64}
  | :resource
  | :variant
  | {:u, 32}
  | {:u, 16}
  | {:bf, 16}
  | {:f, 8}
  | {:f8_e4m3fn, 8}
  | :unknown

Functions

@spec dims(
  %TFLiteElixir.TFLiteTensor{
    index: term(),
    name: term(),
    quantization_params: term(),
    reference: term(),
    shape: term(),
    shape_signature: term(),
    sparsity_params: term(),
    type: term()
  }
  | {:error, String.t()}
) :: [integer()] | {:error, String.t()}
@spec dims(reference()) :: [integer()] | nif_error()

Get the dimensions (C++) API

Given a struct this answers the snapshot taken when the struct was built, so it does not follow a later TFLiteElixir.Interpreter.resize_input_tensor/3. Sizing a buffer from a stale answer is the way this bites: the dimensions still multiply out to the old byte count while set_data/2 on the same struct answers {:error, _}. Pass tensor.reference to ask the interpreter.

Link to this function

quantization_params(self)

View Source
@spec quantization_params(
  %TFLiteElixir.TFLiteTensor{
    index: term(),
    name: term(),
    quantization_params: term(),
    reference: term(),
    shape: term(),
    shape_signature: term(),
    sparsity_params: term(),
    type: term()
  }
  | reference()
  | {:error, String.t()}
) ::
  %TFLiteElixir.TFLiteQuantizationParams{
    quantized_dimension: term(),
    scale: term(),
    zero_point: term()
  }
  | nif_error()

Get the quantization params

Given a struct this answers the snapshot; pass tensor.reference to ask the interpreter, which reports a retired handle rather than stale params.

@spec set_data(
  %TFLiteElixir.TFLiteTensor{
    index: term(),
    name: term(),
    quantization_params: term(),
    reference: term(),
    shape: term(),
    shape_signature: term(),
    sparsity_params: term(),
    type: term()
  }
  | reference()
  | {:error, String.t()},
  binary()
  | %Nx.Tensor{
      data: term(),
      names: term(),
      shape: term(),
      type: term(),
      vectorized_axes: term()
    }
) :: :ok | nif_error()

Set tensor data

@spec shape(
  %TFLiteElixir.TFLiteTensor{
    index: term(),
    name: term(),
    quantization_params: term(),
    reference: term(),
    shape: term(),
    shape_signature: term(),
    sparsity_params: term(),
    type: term()
  }
  | {:error, String.t()}
) :: tuple() | {:error, String.t()}
@spec shape(reference()) :: tuple() | nif_error()

Get the tensor shape

Given a struct this answers the snapshot; pass tensor.reference to ask the interpreter, which reports a retired handle rather than a stale shape.

Link to this function

to_binary(self, limit \\ 0)

View Source
@spec to_binary(
  %TFLiteElixir.TFLiteTensor{
    index: term(),
    name: term(),
    quantization_params: term(),
    reference: term(),
    shape: term(),
    shape_signature: term(),
    sparsity_params: term(),
    type: term()
  }
  | reference()
  | {:error, String.t()},
  non_neg_integer()
) :: binary() | {:error, String.t()}

Get binary data

Link to this function

to_nx(self_struct, opts \\ [])

View Source
@spec to_nx(
  reference()
  | %TFLiteElixir.TFLiteTensor{
      index: term(),
      name: term(),
      quantization_params: term(),
      reference: term(),
      shape: term(),
      shape_signature: term(),
      sparsity_params: term(),
      type: term()
    }
  | {:error, String.t()},
  Keyword.t()
) ::
  %Nx.Tensor{
    data: term(),
    names: term(),
    shape: term(),
    type: term(),
    vectorized_axes: term()
  }
  | {:error, String.t()}

Convert TFLiteElixir.TFLiteTensor to Nx.Tensor

@spec type(
  %TFLiteElixir.TFLiteTensor{
    index: term(),
    name: term(),
    quantization_params: term(),
    reference: term(),
    shape: term(),
    shape_signature: term(),
    sparsity_params: term(),
    type: term()
  }
  | {:error, String.t()}
) :: tensor_type() | {:error, String.t()}
@spec type(reference()) :: tensor_type() | nif_error()

Get the data type

Given a struct this answers the snapshot; pass tensor.reference to ask the interpreter, which reports a retired handle rather than a stale type.