File size: 3,503 Bytes
5a5e72e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from __future__ import annotations

import os
from typing import Any

from .asset_contract import need

ENVIRONMENT = {
    "TF_DETERMINISTIC_OPS": "1",
    "TF_ENABLE_ONEDNN_OPTS": "0",
    "NVIDIA_TF32_OVERRIDE": "0",
    "CUBLAS_WORKSPACE_CONFIG": ":4096:8",
    "OMP_NUM_THREADS": "1",
    "OPENBLAS_NUM_THREADS": "1",
    "MKL_NUM_THREADS": "1",
    "BLIS_NUM_THREADS": "1",
    "NUMEXPR_NUM_THREADS": "1",
    "VECLIB_MAXIMUM_THREADS": "1",
    "CUDA_VISIBLE_DEVICES": "",
    "NPY_DISABLE_CPU_FEATURES": "AVX512F AVX512CD AVX512_SKX AVX512_CLX AVX512_CNL AVX512_ICL",
}
os.environ.update(ENVIRONMENT)
_TF = None
_TORCH = None


def configure_tensorflow() -> Any:
    global _TF
    if _TF is None:
        import numpy as np
        import tensorflow as tf
        import keras

        need(
            tf.__version__ == "2.20.0"
            and keras.__version__ == "3.11.3"
            and np.__version__ == "2.2.6",
            "Use the pinned voice/Space runtime",
        )
        tf.config.threading.set_intra_op_parallelism_threads(1)
        tf.config.threading.set_inter_op_parallelism_threads(1)
        tf.config.set_visible_devices([], "GPU")
        tf.config.experimental.enable_op_determinism()
        tf.config.experimental.enable_tensor_float_32_execution(False)
        tf.keras.mixed_precision.set_global_policy("float32")
        tf.keras.utils.set_random_seed(1729)
        tf.random.set_global_generator(tf.random.Generator.from_seed(1729, alg="philox"))
        _TF = tf
    need(
        not _TF.config.experimental.tensor_float_32_execution_enabled()
        and _TF.keras.mixed_precision.global_policy().name == "float32"
        and not _TF.config.list_logical_devices("GPU"),
        "TF CPU/precision policy changed",
    )
    return _TF


def cpu_voice_call(model: Any, tensor: Any) -> Any:
    tf = configure_tensorflow()
    devices = [str(getattr(v, "handle", v).device) for v in model.variables]
    need(
        devices and all(d.upper().endswith("/DEVICE:CPU:0") for d in devices),
        "Voice variables must be on CPU:0",
    )
    with tf.device("/CPU:0"):
        result = model(tensor, training=False)
    need("CPU:0" in result.device.upper(), "Voice output must be on CPU:0")
    return result


def configure_torch() -> Any:
    global _TORCH
    if _TORCH is None:
        import torch

        need(torch.__version__ == "2.5.1+cu124", "Use the pinned decoder Torch build")
        torch.use_deterministic_algorithms(True, warn_only=False)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False
        torch.backends.cuda.matmul.allow_tf32 = False
        torch.backends.cudnn.allow_tf32 = False
        torch.set_float32_matmul_precision("highest")
        torch.set_num_threads(1)
        if torch.get_num_interop_threads() != 1:
            torch.set_num_interop_threads(1)
        _TORCH = torch
    need(
        _TORCH.get_default_dtype() == _TORCH.float32
        and _TORCH.are_deterministic_algorithms_enabled()
        and not _TORCH.is_deterministic_algorithms_warn_only_enabled()
        and not _TORCH.backends.cuda.matmul.allow_tf32
        and not _TORCH.backends.cudnn.allow_tf32
        and _TORCH.backends.cudnn.deterministic
        and not _TORCH.backends.cudnn.benchmark
        and _TORCH.get_float32_matmul_precision() == "highest"
        and _TORCH.get_num_threads() == _TORCH.get_num_interop_threads() == 1,
        "Torch precision policy changed",
    )
    return _TORCH