Skip to content

softmax

Classes

SoftmaxOperator

SoftmaxOperator(ident: int, op: OperatorT, model: ModelT, subgraph: SubGraphT, prefix: str = 'aot', attributes: dict[str, str] = {})

SOFTMAX operator.

This operator computes the softmax activation function on the input tensor.

Parameters:

  • ident

    (int) –

    Operator identifier.

  • op

    (OperatorT) –

    Operator object from LiteRT model.

  • model

    (ModelT) –

    LiteRT model object.

  • subgraph

    (SubGraphT) –

    Subgraph object from LiteRT model.

  • prefix

    (str, default: 'aot' ) –

    Prefix for generated files. Defaults to "aot".

  • attributes

    (dict[str, str], default: {} ) –

    Attributes for template values. Defaults to {}.

Attributes

name property
name: str

Return the operator name.

input_indices property
input_indices: list[int]

Return the input tensor indices.

output_indices property
output_indices: list[int]

Return the output tensor indices.

input_tensors property
input_tensors: list[TensorT]

Return the input tensors.

output_tensors property
output_tensors: list[TensorT]

Return the output tensors.

Functions

compute_values
compute_values() -> dict[str, str]

Compute the values for the operator.

Returns:

  • dict[str, str]

    dict[str, str]: Computed values for the operator.

generate_source_code
generate_source_code(save_path: Path)

Generate the source code for the operator.

Parameters:

  • save_path
    (Path) –

    Path to save the generated code.

Functions

preprocess_softmax_scaling

preprocess_softmax_scaling(beta: float, input_scale: float, scaled_diff_integer_bits: int = 5) -> tuple[int, int]

Mimics litert::PreprocessSoftmaxScaling.

Parameters:

  • beta

    (float) –

    The softmax beta parameter (often 1.0).

  • input_scale

    (float) –

    The quantization scale for the input tensor.

  • scaled_diff_integer_bits

    (int, default: 5 ) –

    Number of integer bits to reserve in the scaled difference (typically 5).

Returns:

  • input_multiplier ( int ) –

    The fixed-point multiplier (in Q31 format).

  • input_left_shift ( int ) –

    The left shift to apply; computed as (shift from quantization + scaled_diff_integer_bits).

calculate_input_radius

calculate_input_radius(input_integer_bits, input_left_shift, total_signed_bits=31) -> int

Mimics litert::CalculateInputRadius (non-emulated version).

This computes the maximum representable difference in the scaled domain.

Parameters:

  • input_integer_bits

    (int) –

    Number of integer bits reserved (e.g., kScaledDiffIntegerBits, typically 5).

  • input_left_shift

    (int) –

    The left shift computed from PreprocessSoftmaxScaling.

  • total_signed_bits

    (int, default: 31 ) –

    Total bits available in the representation (typically 31 for Q31 arithmetic).

Returns:

  • int ( int ) –

    The computed input radius.