nif_network (faber_neuroevolution v1.2.4)

View Source

NIF-accelerated network operations for faber-neuroevolution.

This module provides high-performance network evaluation using Rust NIFs from faber_tweann. When the NIF is available, operations use the native path. Speedup over pure Erlang is not measured; see ROADMAP.md.

Features

- Network compilation for fast repeated evaluation - Batch evaluation (multiple inputs, same network) - NIF-accelerated compatibility distance for speciation - LTC/CfC neuron support for temporal processing - Automatic fallback to pure Erlang when NIF unavailable

Usage

%% Compile a network for fast evaluation {ok, CompiledNet} = nif_network:compile(Network),

%% Evaluate via the native path Outputs = nif_network:evaluate(CompiledNet, Inputs),

%% Batch evaluate (even more efficient) OutputsList = nif_network:evaluate_batch(CompiledNet, InputsList).

Summary

Functions

Calculate NEAT compatibility distance using NIF.

Calculate compatibility distance with explicit coefficients.

Compile a network for fast NIF evaluation.

Compile a feedforward network directly from topology.

Evaluate a compiled network with given inputs.

Evaluate a compiled network with multiple input sets.

Evaluate CfC (Closed-form Continuous-time) neuron.

Batch CfC evaluation for time series.

Check if NIF acceleration is available.

Types

compiled_network/0

-type compiled_network() ::
          #compiled_network{ref :: reference() | undefined,
                            fallback :: term(),
                            input_count :: pos_integer(),
                            output_count :: pos_integer(),
                            use_nif :: boolean()}.

Functions

compatibility_distance(Genome1, Genome2, Config)

-spec compatibility_distance(Genome1 :: [tuple()], Genome2 :: [tuple()], Config :: tuple()) -> float().

Calculate NEAT compatibility distance using NIF.

Uses the NIF-accelerated distance calculation when available. Falls back to pure Erlang genome_crossover when not.

compatibility_distance(Genome1, Genome2, C1, C2, C3)

-spec compatibility_distance(Genome1 :: [tuple()],
                             Genome2 :: [tuple()],
                             C1 :: float(),
                             C2 :: float(),
                             C3 :: float()) ->
                                float().

Calculate compatibility distance with explicit coefficients.

compile(Network)

-spec compile(Network :: term()) -> {ok, compiled_network()} | {error, term()}.

Compile a network for fast NIF evaluation.

Takes a network from network_evaluator and compiles it to the NIF format for fast repeated evaluation. If NIF is unavailable, returns a wrapper that uses pure Erlang evaluation.

compile_feedforward(InputSize, HiddenLayers, OutputSize)

-spec compile_feedforward(InputSize :: pos_integer(),
                          HiddenLayers :: [pos_integer()],
                          OutputSize :: pos_integer()) ->
                             {ok, compiled_network()} | {error, term()}.

Compile a feedforward network directly from topology.

Creates and compiles a feedforward network in one step. More efficient than create_feedforward + compile separately.

evaluate(CompiledNetwork, Inputs)

-spec evaluate(CompiledNetwork :: compiled_network(), Inputs :: [float()]) -> [float()].

Evaluate a compiled network with given inputs.

Uses NIF evaluation when configured (speedup unmeasured), otherwise falls back to pure Erlang network_evaluator.

evaluate_batch(CompiledNetwork, InputsList)

-spec evaluate_batch(CompiledNetwork :: compiled_network(), InputsList :: [[float()]]) -> [[float()]].

Evaluate a compiled network with multiple input sets.

More efficient than calling evaluate/2 multiple times when evaluating the same network with different inputs.

evaluate_cfc(Input, State, Tau, Bound)

-spec evaluate_cfc(Input :: float(), State :: float(), Tau :: float(), Bound :: float()) ->
                      {float(), float()}.

Evaluate CfC (Closed-form Continuous-time) neuron.

Fast closed-form approximation of LTC dynamics. Suitable for temporal/sequential processing tasks.

evaluate_cfc_batch(Inputs, InitialState, Tau, Bound)

-spec evaluate_cfc_batch(Inputs :: [float()], InitialState :: float(), Tau :: float(), Bound :: float()) ->
                            [{float(), float()}].

Batch CfC evaluation for time series.

Evaluates a sequence of inputs, maintaining state between steps.

is_nif_available()

-spec is_nif_available() -> boolean().

Check if NIF acceleration is available.

Returns true if the Rust NIF library is loaded and functional. When false, all operations use pure Erlang fallbacks.