mixle.models._neural_serial module

Serialization + input-validation helpers shared by the neural-leaf families (mixle.models).

The neural leaves wrap a live torch.nn.Module, which the generic JSON encoder in mixle.utils.serialization cannot walk (a module is not a registered mixle class). These helpers give every neural leaf a working to_dict/from_dict and the recursive __pysp_getstate__/__pysp_setstate__ hooks (so a leaf inside a MixtureDistribution serializes too) by persisting the module as portable bytes.

The module round-trips through torch.save/torch.load of a pickle byte buffer – which requires the wrapped nn.Module class to be reachable at module level (that is why the build_* helpers were hoisted). The bytes are base64-encoded so the whole payload is plain JSON.

module_to_bytes(module)[source]

Serialize a torch nn.Module (architecture + weights) to portable bytes via torch.save.

Parameters:

module (Any)

Return type:

bytes

module_from_bytes(data)[source]

Reconstruct a torch nn.Module previously encoded by module_to_bytes().

Parameters:

data (bytes)

Return type:

Any

encode_module(module)[source]

A JSON-safe tagged dict for a torch module (base64 of module_to_bytes()).

Parameters:

module (Any)

Return type:

dict[str, str]

decode_module(payload)[source]

Inverse of encode_module().

Parameters:

payload (Any)

Return type:

Any

check_finite(x, where)[source]

Raise a clear error if x has any non-finite entry, so a NaN cannot silently poison a mixture E-step.

A neural leaf that returned NaN log-density would corrupt every responsibility in the E-step without a diagnosable failure; validating at the density boundary turns that into an immediate, named error instead.

Parameters:
Return type:

ndarray