|
13 | 13 | # limitations under the License. |
14 | 14 | # ============================================================================== |
15 | 15 |
|
16 | | -import jax |
17 | | -import distrax as dx |
18 | | -import jax.numpy as jnp |
19 | | -import jax.random as jr |
20 | | -import tensorflow_probability.substrates.jax.bijectors as tfb |
21 | | -from ml_collections import ConfigDict |
22 | 16 |
|
23 | | -__config = None |
| 17 | +import deprecation |
24 | 18 |
|
25 | | -Identity = dx.Lambda(forward=lambda x: x, inverse=lambda x: x) |
26 | | -Softplus = dx.Lambda( |
27 | | - forward=lambda x: jnp.log(1 + jnp.exp(x)), |
28 | | - inverse=lambda x: jnp.log(jnp.exp(x) - 1.0), |
| 19 | +depreciate = deprecation.deprecated( |
| 20 | + deprecated_in="0.5.6", |
| 21 | + removed_in="0.6.0", |
| 22 | + details="Use method from jaxutils.config instead.", |
29 | 23 | ) |
30 | 24 |
|
31 | | - |
32 | | -def reset_global_config() -> None: |
33 | | - global __config |
34 | | - __config = get_default_config() |
35 | | - |
36 | | - |
37 | | -def get_global_config() -> ConfigDict: |
38 | | - """Get the global config file used within GPJax. |
39 | | -
|
40 | | - Returns: |
41 | | - ConfigDict: A `ConfigDict` describing parameter transforms and default values. |
42 | | - """ |
43 | | - global __config |
44 | | - |
45 | | - if __config is None: |
46 | | - __config = get_default_config() |
47 | | - return __config |
48 | | - |
49 | | - # If the global config is available, check if the x64 state has changed |
50 | | - x64_state = jax.config.x64_enabled |
51 | | - |
52 | | - # If the x64 state has not changed, return the existing global config |
53 | | - if x64_state is __config.x64_state: |
54 | | - return __config |
55 | | - |
56 | | - # If the x64 state has changed, return the updated global config |
57 | | - update_x64_sensitive_settings() |
58 | | - return __config |
59 | | - |
60 | | - |
61 | | -def update_x64_sensitive_settings() -> None: |
62 | | - """Update the global config if x64 state changes.""" |
63 | | - global __config |
64 | | - |
65 | | - # Update the x64 state |
66 | | - x64_state = jax.config.x64_enabled |
67 | | - __config.x64_state = x64_state |
68 | | - |
69 | | - # Update the x64 sensitive bijectors |
70 | | - FillScaleTriL = dx.Chain( |
71 | | - [ |
72 | | - tfb.FillScaleTriL(diag_shift=jnp.array(__config.jitter)), |
73 | | - ] |
74 | | - ) |
75 | | - |
76 | | - transformations = __config.transformations |
77 | | - transformations.triangular_transform = FillScaleTriL |
78 | | - |
79 | | - |
80 | | -def get_default_config() -> ConfigDict: |
81 | | - """Construct and return the default config file. |
82 | | -
|
83 | | - Returns: |
84 | | - ConfigDict: A `ConfigDict` describing parameter transforms and default values. |
85 | | - """ |
86 | | - |
87 | | - config = ConfigDict(type_safe=False) |
88 | | - config.key = jr.PRNGKey(123) |
89 | | - |
90 | | - # Set the x64 state |
91 | | - config.x64_state = jax.config.x64_enabled |
92 | | - |
93 | | - # Covariance matrix stabilising jitter |
94 | | - config.jitter = 1e-6 |
95 | | - |
96 | | - FillScaleTriL = dx.Chain( |
97 | | - [ |
98 | | - tfb.FillScaleTriL(diag_shift=jnp.array(config.jitter)), |
99 | | - ] |
100 | | - ) |
101 | | - |
102 | | - # Default bijections |
103 | | - config.transformations = transformations = ConfigDict() |
104 | | - transformations.positive_transform = Softplus |
105 | | - transformations.identity_transform = Identity |
106 | | - transformations.triangular_transform = FillScaleTriL |
107 | | - |
108 | | - # Default parameter transforms |
109 | | - transformations.alpha = "positive_transform" |
110 | | - transformations.lengthscale = "positive_transform" |
111 | | - transformations.variance = "positive_transform" |
112 | | - transformations.smoothness = "positive_transform" |
113 | | - transformations.shift = "positive_transform" |
114 | | - transformations.obs_noise = "positive_transform" |
115 | | - transformations.latent = "identity_transform" |
116 | | - transformations.basis_fns = "identity_transform" |
117 | | - transformations.offset = "identity_transform" |
118 | | - transformations.inducing_inputs = "identity_transform" |
119 | | - transformations.variational_mean = "identity_transform" |
120 | | - transformations.variational_root_covariance = "triangular_transform" |
121 | | - transformations.natural_vector = "identity_transform" |
122 | | - transformations.natural_matrix = "identity_transform" |
123 | | - transformations.expectation_vector = "identity_transform" |
124 | | - transformations.expectation_matrix = "identity_transform" |
125 | | - |
126 | | - return config |
127 | | - |
128 | | - |
129 | | -# This function is created for testing purposes only |
130 | | -def get_global_config_if_exists() -> ConfigDict: |
131 | | - """Get the global config file used within GPJax if it is available. |
132 | | -
|
133 | | - Returns: |
134 | | - ConfigDict: A `ConfigDict` describing parameter transforms and default values. |
135 | | - """ |
136 | | - global __config |
137 | | - return __config |
138 | | - |
139 | | - |
140 | | -def add_parameter(param_name: str, bijection: dx.Bijector) -> None: |
141 | | - """Add a parameter and its corresponding transform to GPJax's config file. |
142 | | -
|
143 | | - Args: |
144 | | - param_name (str): The name of the parameter that is to be added. |
145 | | - bijection (dx.Bijector): The bijection that should be used to unconstrain the parameter's value. |
146 | | - """ |
147 | | - lookup_name = f"{param_name}_transform" |
148 | | - get_global_config() |
149 | | - __config.transformations[lookup_name] = bijection |
150 | | - __config.transformations[param_name] = lookup_name |
| 25 | +from jaxutils import config |
| 26 | + |
| 27 | +Identity = config.Identity |
| 28 | +Softplus = config.Softplus |
| 29 | +reset_global_config = depreciate(config.reset_global_config) |
| 30 | +get_global_config = depreciate(config.get_global_config) |
| 31 | +get_default_config = depreciate(config.get_default_config) |
| 32 | +update_x64_sensitive_settings = depreciate(config.update_x64_sensitive_settings) |
| 33 | +get_global_config_if_exists = depreciate(config.get_global_config_if_exists) |
| 34 | +add_parameter = depreciate(config.add_parameter) |
| 35 | + |
| 36 | +__all__ = [ |
| 37 | + "Identity", |
| 38 | + "Softplus", |
| 39 | + "reset_global_config", |
| 40 | + "get_global_config", |
| 41 | + "get_default_config", |
| 42 | + "update_x64_sensitive_settings", |
| 43 | + "get_global_config_if_exists", |
| 44 | + "set_global_config", |
| 45 | +] |
0 commit comments