File size: 5,592 Bytes
5ccb4fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright 2022 DeepMind Technologies Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""K-FAC related utility classes and functions."""

from kfac_jax._src.utils import accumulators
from kfac_jax._src.utils import math
from kfac_jax._src.utils import misc
from kfac_jax._src.utils import parallel
from kfac_jax._src.utils import staging
from kfac_jax._src.utils import types

# types
Array = types.Array
PRNGKey = types.PRNGKey
Scalar = types.Scalar
Numeric = types.Numeric
Shape = types.Shape
DType = types.DType
PyTree = types.PyTree
ArrayTree = types.ArrayTree
TArrayTree = types.TArrayTree
Params = types.Params
Batch = types.Batch
FuncState = types.FuncState
FuncAux = types.FuncAux
PyTreeDef = types.PyTreeDef
FuncArgs = types.FuncArgs
FuncOuts = types.FuncOuts
Func = types.Func
ValueFunc = types.ValueFunc
ValueAndGradFunc = types.ValueAndGradFunc
AssumedFuncOutput = types.AssumedFuncOutput
ScheduleType = types.ScheduleType
tree_is_empty = types.tree_is_empty
abstract_objects_equal = types.abstract_objects_equal
get_float_dtype_and_check_consistency = (
    types.get_float_dtype_and_check_consistency)
del types

# misc
deserialize_state_tree = misc.deserialize_state_tree
serialize_state_tree = misc.serialize_state_tree
to_tuple_or_repeat = misc.to_tuple_or_repeat
filter_sequence = misc.filter_sequence
first_dim_is_size = misc.first_dim_is_size
fake_element_from_iterator = misc.fake_element_from_iterator
default_batch_size_extractor = misc.default_batch_size_extractor
auto_scope_function = misc.auto_scope_function
auto_scope_method = misc.auto_scope_method
register_state_class = misc.register_state_class
replace_char = misc.replace_char
call_func_with_conditional_kwargs = misc.call_func_with_conditional_kwargs
Finalizable = misc.Finalizable
State = misc.State
rearrange = misc.rearrange
del misc

# parallel
in_pmap = parallel.in_pmap
wrap_if_pmap = parallel.wrap_if_pmap
pmean_if_pmap = parallel.pmean_if_pmap
psum_if_pmap = parallel.psum_if_pmap
pmap_mean = parallel.pmap_mean
pmap_sum = parallel.pmap_sum
using_legacy_pmap = parallel.using_legacy_pmap
get_device_n_contents = parallel.get_device_n_contents
get_first = parallel.get_first
get_mean = parallel.get_mean
get_sum = parallel.get_sum
broadcast_all_local_devices = parallel.broadcast_all_local_devices
pmap_zeros_like = parallel.pmap_zeros_like
jit_zeros_like = parallel.jit_zeros_like
replicate_all_local_devices = parallel.replicate_all_local_devices
make_different_rng_key_on_all_devices = (
    parallel.make_different_rng_key_on_all_devices)
p_split = parallel.p_split
p_split_num = parallel.p_split_num
host_sync = parallel.host_sync
host_all_gather = parallel.host_all_gather
host_mean = parallel.host_mean
pmap_sync_and_divide_value = parallel.pmap_sync_and_divide_value
jit_sync_and_divide_value = parallel.jit_sync_and_divide_value
copy_array = parallel.copy_array
copy_obj = parallel.copy_obj
pmap_copy_obj = parallel.pmap_copy_obj
distribute_thunks = parallel.distribute_thunks
del parallel

# math
set_special_case_zero_inv = math.set_special_case_zero_inv
get_special_case_zero_inv = math.get_special_case_zero_inv
set_use_cholesky_inversion = math.set_use_cholesky_inversion
get_use_cholesky_inversion = math.get_use_cholesky_inversion
product = math.product
outer_product = math.outer_product
scalar_mul = math.scalar_mul
scalar_div = math.scalar_div
weighted_sum_of_objects = math.weighted_sum_of_objects
sum_of_objects = math.sum_objects
pytree_size = math.pytree_size
inner_product = math.inner_product
symmetric_matrix_inner_products = math.symmetric_matrix_inner_products
asymmetric_matrix_inner_products = math.asymmetric_matrix_inner_products
matrix_of_inner_products = math.matrix_of_inner_products
vector_of_inner_products = math.vector_of_inner_products
block_permuted = math.block_permuted
norm = math.norm
squared_norm = math.squared_norm
per_parameter_norm = math.per_parameter_norm
psd_inv = math.psd_inv
psd_solve = math.psd_solve
psd_solve_maybe_zero_last_idx = math.psd_solve_maybe_zero_last_idx
pi_adjusted_kronecker_factors = math.pi_adjusted_kronecker_factors
pi_adjusted_kronecker_inverse = math.pi_adjusted_kronecker_inverse
kronecker_product_axis_mul_v = math.kronecker_product_axis_mul_v
kronecker_eigen_basis_axis_mul_v = math.kronecker_eigen_basis_axis_mul_v
kronecker_product_mul_v = math.kronecker_product_mul_v
kronecker_eigen_basis_mul_v = math.kronecker_eigen_basis_mul_v
safe_psd_eigh = math.safe_psd_eigh
tnt_scale = math.tnt_scale
loop_and_parallelize_average = math.loop_and_parallelize_average
psd_matrix_norm = math.psd_matrix_norm
invert_psd_matrices = math.invert_psd_matrices
inverse_sqrt_psd_matrices = math.inverse_sqrt_psd_matrices
stable_sqrt = math.stable_sqrt
cosine_similarity = math.cosine_similarity

del math

# accumulators
default_add_function = accumulators.default_add_function
WeightedMovingAverage = accumulators.WeightedMovingAverage
MultiChunkAccumulator = accumulators.MultiChunkAccumulator
del accumulators

# staged
staged = staging.staged
WithStagedMethods = staging.WithStagedMethods
del staging