Skip to content

Config

clear_session(free_memory=True)

Resets all state generated by synalinks.

synalinks manages a global state, which it uses to implement the Functional model-building API and to uniquify autogenerated modules names.

If you are creating many models in a loop, this global state will consume an increasing amount of memory over time, and you may want to clear it. Calling clear_session() releases the global state: this helps avoid clutter from old programs and modules, especially when memory is limited.

Parameters:

Name Type Description Default
free_memory bool

Whether to call Python garbage collection. It's usually a good practice to call it to make sure memory used by deleted objects is immediately freed. However, it may take a few seconds to execute, so when using clear_session() in a short loop, you may want to skip it.

True
Source code in synalinks/src/backend/common/global_state.py
@synalinks_export(
    [
        "synalinks.utils.clear_session",
        "synalinks.backend.clear_session",
        "synalinks.clear_session",
    ]
)
def clear_session(free_memory=True):
    """Resets all state generated by synalinks.

    synalinks manages a global state, which it uses to implement the Functional
    model-building API and to uniquify autogenerated modules names.

    If you are creating many models in a loop, this global state will consume
    an increasing amount of memory over time, and you may want to clear it.
    Calling `clear_session()` releases the global state: this helps avoid
    clutter from old programs and modules, especially when memory is limited.

    Args:
        free_memory (bool): Whether to call Python garbage collection.
            It's usually a good practice to call it to make sure
            memory used by deleted objects is immediately freed.
            However, it may take a few seconds to execute, so
            when using `clear_session()` in a short loop,
            you may want to skip it.
    """
    global GLOBAL_STATE_TRACKER
    global GLOBAL_SETTINGS_TRACKER

    GLOBAL_STATE_TRACKER = threading.local()
    GLOBAL_SETTINGS_TRACKER = threading.local()

    if free_memory:
        # Manually trigger garbage collection.
        gc.collect()

api_key()

Synalinks API key.

Returns:

Type Description
str

Synalinks API key.

>>> synalinks.config.api_key()
'my-secret-api-key'
Source code in synalinks/src/backend/config.py
@synalinks_export(
    [
        "synalinks.config.api_key",
        "synalinks.backend.api_key",
    ]
)
def api_key():
    """Synalinks API key.

    Returns:
        (str): Synalinks API key.

    ```python
    >>> synalinks.config.api_key()
    'my-secret-api-key'
    ```

    """
    return _SYNALINKS_API_KEY

backend()

Publicly accessible method for determining the current backend.

Returns:

Type Description
str

The name of the backend synalinks is currently using. like "pydantic".

Example:

>>> synalinks.config.backend()
'pydantic'
Source code in synalinks/src/backend/config.py
@synalinks_export(["synalinks.config.backend", "synalinks.backend.backend"])
def backend():
    """Publicly accessible method for determining the current backend.

    Returns:
        (str): The name of the backend synalinks is currently using. like
            `"pydantic"`.

    Example:

    ```python
    >>> synalinks.config.backend()
    'pydantic'
    ```
    """
    return _BACKEND

epsilon()

Return the value of the fuzz factor used in numeric expressions.

Returns:

Type Description
float

The epsilon value.

Example:

>>> synalinks.config.epsilon()
1e-07
Source code in synalinks/src/backend/config.py
@synalinks_export(["synalinks.config.epsilon", "synalinks.backend.epsilon"])
def epsilon():
    """Return the value of the fuzz factor used in numeric expressions.

    Returns:
        (float): The epsilon value.

    Example:

    ```python
    >>> synalinks.config.epsilon()
    1e-07
    ```

    """
    return _EPSILON

floatx()

Return the default float type, as a string.

E.g. 'bfloat16', 'float16', 'float32', 'float64'.

Returns:

Type Description
str

The current default float type.

Example:

>>> synalinks.config.floatx()
'float32'
Source code in synalinks/src/backend/config.py
@synalinks_export(["synalinks.config.floatx", "synalinks.backend.floatx"])
def floatx():
    """Return the default float type, as a string.

    E.g. `'bfloat16'`, `'float16'`, `'float32'`, `'float64'`.

    Returns:
        (str): The current default float type.

    Example:

    ```python
    >>> synalinks.config.floatx()
    'float32'
    ```

    """
    return _FLOATX

set_api_key(key)

Set Synalinks API key.

Parameters:

Name Type Description Default
key str

The API key value.

required

The API key is retrieved from the env variables at start.

>>> os.environ["SYNALINKS_API_KEY"] = 'my-secret-api-key'

Or you can setup it using the config

>>> synalinks.config.set_api_key('my-secret-api-key')
>>> synalinks.config.api_key()
'my-secret-api-key'

Parameters:

Name Type Description Default
key str

Synalinks API key.

required
Source code in synalinks/src/backend/config.py
@synalinks_export(
    [
        "synalinks.config.set_api_key",
        "synalinks.backend.set_api_key",
    ]
)
def set_api_key(key):
    """Set Synalinks API key.

    Args:
        key (str): The API key value.

    The API key is retrieved from the env variables at start.

    ```python
    >>> os.environ["SYNALINKS_API_KEY"] = 'my-secret-api-key'
    ```

    Or you can setup it using the config

    ```python
    >>> synalinks.config.set_api_key('my-secret-api-key')
    >>> synalinks.config.api_key()
    'my-secret-api-key'
    ```

    Args:
        key (str): Synalinks API key.
    """
    global _SYNALINKS_API_KEY
    _SYNALINKS_API_KEY = key

set_epsilon(value)

Set the value of the fuzz factor used in numeric expressions.

Parameters:

Name Type Description Default
value float

The new value of epsilon.

required

Examples:

>>> synalinks.config.epsilon()
1e-07
>>> synalinks.config.set_epsilon(1e-5)
>>> synalinks.config.epsilon()
1e-05
>>> # Set it back to the default value.
>>> synalinks.config.set_epsilon(1e-7)
Source code in synalinks/src/backend/config.py
@synalinks_export(["synalinks.config.set_epsilon", "synalinks.backend.set_epsilon"])
def set_epsilon(value):
    """Set the value of the fuzz factor used in numeric expressions.

    Args:
        value (float): The new value of epsilon.

    Examples:

    ```python
    >>> synalinks.config.epsilon()
    1e-07
    ```

    ```python
    >>> synalinks.config.set_epsilon(1e-5)
    >>> synalinks.config.epsilon()
    1e-05
    ```

    ```python
    >>> # Set it back to the default value.
    >>> synalinks.config.set_epsilon(1e-7)
    ```

    """
    global _EPSILON
    _EPSILON = value

set_floatx(value)

Set the default float dtype.

Note: It is not recommended to set this to "float16", as this will likely cause numeric stability issues. Instead, use float64 or float32.

Parameters:

Name Type Description Default
value str

The float type between 'bfloat16', 'float16', 'float32', or 'float64'.

required

Examples:

>>> synalinks.config.floatx()
'float32'
>>> synalinks.config.set_floatx('float64')
>>> synalinks.config.floatx()
'float64'
>>> # Set it back to float32
>>> synalinks.config.set_floatx('float32')

Raises:

Type Description
ValueError

In case of invalid value.

Source code in synalinks/src/backend/config.py
@synalinks_export(["synalinks.config.set_floatx", "synalinks.backend.set_floatx"])
def set_floatx(value):
    """Set the default float dtype.

    Note: It is not recommended to set this to `"float16"`,
    as this will likely cause numeric stability issues.
    Instead, use `float64` or `float32`.

    Args:
        value (str): The float type between `'bfloat16'`, `'float16'`, `'float32'`,
            or `'float64'`.

    Examples:

    ```python
    >>> synalinks.config.floatx()
    'float32'
    ```

    ```python
    >>> synalinks.config.set_floatx('float64')
    >>> synalinks.config.floatx()
    'float64'
    ```

    ```python
    >>> # Set it back to float32
    >>> synalinks.config.set_floatx('float32')
    ```

    Raises:
        ValueError: In case of invalid value.
    """
    global _FLOATX
    accepted_dtypes = {"bfloat16", "float16", "float32", "float64"}
    if value not in accepted_dtypes:
        raise ValueError(
            f"Unknown `floatx` value: {value}. Expected one of {accepted_dtypes}"
        )
    _FLOATX = str(value)