util
ls_mlkit.util
BaseGenerativeModel
Bases: BaseLoss
abstract method: compute_loss, step, sampling, inpainting
Source code in src/ls_mlkit/util/base_class/base_gm_class.py
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 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
step(x_t, t, padding_mask=None, *args, **kwargs)
abstractmethod
summary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_t
|
``Tensor``
|
description |
required |
t
|
``Tensor``
|
description |
required |
padding_mask
|
``Tensor``, *optional*
|
description. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/ls_mlkit/util/base_class/base_gm_class.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
forward(**batch)
Forward function, input batch of data and return the dictionary containing the loss
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
``dict[str, Any]``
|
the batch of data |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/ls_mlkit/util/base_class/base_gm_class.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
register_post_compute_loss_hook(name, fn, priority=0, enabled=True)
Register a hook to be called after loss computation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
``str``
|
the name of the hook |
required |
fn
|
``Callable[..., Any]``
|
the function to be called |
required |
priority
|
``int``
|
the priority of the hook. Defaults to 0. |
0
|
enabled
|
``bool``
|
whether the hook is enabled. Defaults to True. |
True
|
Source code in src/ls_mlkit/util/base_class/base_gm_class.py
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 | |
BaseLoss
Bases: Module, ABC
abstract method: compute_loss
Source code in src/ls_mlkit/util/base_class/base_loss_class.py
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 | |
compute_loss(**batch)
abstractmethod
Compute loss
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
``dict[str, Any]``
|
the batch of data |
{}
|
Returns:
| Type | Description |
|---|---|
dict
|
|
Source code in src/ls_mlkit/util/base_class/base_loss_class.py
40 41 42 43 44 45 46 47 48 49 | |
BaseTimeScheduler
Bases: ABC
Base class for time schedulers in diffusion models.
Notation Convention
Let the total diffusion time be :math:T, discretized into :math:N diffusion steps,
corresponding to :math:N+1 continuous time points:
.. math:: 0 = t_0 < t_1 < \cdots < t_N = T
where :math:\{t_i\}_{i=0}^N represents continuous time. For uniform discretization:
.. math:: t_i = \frac{i}{N} \cdot T
The corresponding discrete time steps are defined as:
.. math:: i \in {0, 1, \ldots, N}
In diffusion models, :math:t_0 corresponds to the clean data distribution :math:q(x_0),
so training and sampling typically only consider:
.. math:: i \in {1, \ldots, N}
For engineering convenience (0-based array indexing), we use:
.. math:: \text{idx} = i - 1
Therefore:
idx = 0corresponds to discrete step :math:i=1, i.e., continuous time :math:t_1idx = N-1corresponds to discrete step :math:i=N, i.e., continuous time :math:t_N = T
In this implementation:
num_train_timesteps= :math:N(number of diffusion steps)timestep_index(oridx) :math:\in \{\text{idx\_start}, \ldots, \text{idx\_start} + N - 1\}continuous_time:math:\in [t_1, t_N] = [\frac{T}{N}, T]for training/sampling
The idx_start parameter controls the starting value of timestep indices:
- When
idx_start = 0(default): :math:\text{idx} = i - 1, so :math:\text{idx} \in \{0, \ldots, N-1\} - When
idx_start = 1: :math:\text{idx} = i, so :math:\text{idx} \in \{1, \ldots, N\}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuous_time_start
|
``float``, *optional*
|
The start of continuous time range (typically 0). Defaults to 0.0. |
0.0
|
continuous_time_end
|
``float``, *optional*
|
The end of continuous time range (i.e., :math: |
1.0
|
num_train_timesteps
|
``int``, *optional*
|
Number of diffusion steps :math: |
1000
|
num_inference_steps
|
``int``, *optional*
|
Number of inference steps. If None, uses |
None
|
idx_start
|
``int``, *optional*
|
The starting value for timestep indices. Set to 1 if you prefer 1-based indexing where idx directly equals the discrete step i. Defaults to 0. |
0
|
Source code in src/ls_mlkit/util/base_class/base_time_class.py
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
initialize_timesteps_schedule()
abstractmethod
Initialize timesteps schedule for sampling/inference.
Source code in src/ls_mlkit/util/base_class/base_time_class.py
101 102 103 | |
continuous_time_to_timestep_index(continuous_time)
Convert continuous time to timestep index.
Given continuous time :math:t, compute the timestep index:
.. math:: \text{idx} = \text{round}\left(\frac{t - t_0}{T} \cdot N\right) - 1 + \text{idx_start}
where :math:t_0 is continuous_time_start, :math:T is the total time span,
:math:N is num_train_timesteps, and :math:\text{idx\_start} is the starting index.
The result is clamped to :math:[\text{idx\_start}, \text{idx\_start} + N - 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuous_time
|
``Tensor``
|
Continuous time values :math: |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in src/ls_mlkit/util/base_class/base_time_class.py
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 | |
timestep_index_to_continuous_time(timestep_index)
Convert timestep index to continuous time.
Given timestep index :math:\text{idx}, compute the continuous time:
.. math:: t = t_0 + \frac{\text{idx} + 1 - \text{idx_start}}{N} \cdot T
where :math:t_0 is continuous_time_start, :math:T is the total time span,
:math:N is num_train_timesteps, and :math:\text{idx\_start} is the starting index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestep_index
|
``Tensor``
|
Timestep indices :math: |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in src/ls_mlkit/util/base_class/base_time_class.py
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | |
get_timestep_indices_schedule()
Get the timestep indices schedule for sampling/inference.
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in src/ls_mlkit/util/base_class/base_time_class.py
156 157 158 159 160 161 162 163 164 165 | |
get_continuous_timesteps_schedule()
Get the continuous timesteps schedule for sampling/inference.
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in src/ls_mlkit/util/base_class/base_time_class.py
167 168 169 170 171 172 173 174 175 176 | |
set_timestep_indices_schedule(timestep_indices)
Set the timestep indices schedule for sampling/inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestep_indices
|
``Tensor``
|
1D tensor of timestep indices :math: |
required |
Source code in src/ls_mlkit/util/base_class/base_time_class.py
178 179 180 181 182 183 184 | |
set_continuous_timesteps_schedule(continuous_timesteps)
Set the continuous timesteps schedule for sampling/inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
continuous_timesteps
|
``Tensor``
|
1D tensor of continuous time values. |
required |
Source code in src/ls_mlkit/util/base_class/base_time_class.py
186 187 188 189 190 191 192 | |
sample_timestep_index_uniformly(macro_shape, same_for_all_samples=False)
Sample timestep indices uniformly from :math:\{\text{idx\_start}, \ldots, \text{idx\_start} + N - 1\}.
This corresponds to sampling discrete steps :math:i uniformly from :math:\{1, \ldots, N\}
and converting to index via :math:\text{idx} = i - 1 + \text{idx\_start}.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
macro_shape
|
``Tuple[int, ...]``
|
Shape of the output tensor. |
required |
same_for_all_samples
|
``bool``, *optional*
|
If True, use the same timestep index for all samples. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in src/ls_mlkit/util/base_class/base_time_class.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
sample_continuous_time_uniformly(macro_shape, same_for_all_samples=False)
Sample continuous time uniformly from :math:[t_1, t_N].
Note: This samples from :math:[t_0 + \frac{T}{N}, t_0 + T] to exclude :math:t_0
(the clean data point).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
macro_shape
|
``Tuple[int, ...]``
|
Shape of the output tensor. |
required |
same_for_all_samples
|
``bool``, *optional*
|
If True, use the same time for all samples. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in src/ls_mlkit/util/base_class/base_time_class.py
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
DeviceConfig
Bases: object
Source code in src/ls_mlkit/util/base_class/base_config_class.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
to(device, inplace=True)
Move the config to the given device
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
device | str | Tensor
|
the device to move the config to |
required |
inplace
|
bool
|
whether to move the config in place. Defaults to True. |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
BaseConfig |
DeviceConfig
|
the config moved to the given device |
Source code in src/ls_mlkit/util/base_class/base_config_class.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
HookManager
Bases: Generic[HookStageType]
Source code in src/ls_mlkit/util/hook/base_hook.py
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 | |
run_hooks(stage, tgt_key_name=None, **kwargs)
Executes all enabled hooks for a given stage, optionally updating or collecting results in kwargs, and returns either the final modified kwargs or a specific key's value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stage
|
``HookStageType``
|
description |
required |
tgt_key_name
|
``str``, *optional*
|
target key name. Defaults to None. |
None
|
Source code in src/ls_mlkit/util/hook/base_hook.py
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
SO3
Bases: LieGroup
SO(3): Special Orthogonal Group
Source code in src/ls_mlkit/util/manifold/so3.py
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 | |
exp(p=None, v=None)
Exponential map $$\exp_p(v)$$ map a point in tangent space $$T_p M$$ to a point on the manifold $$M$$ $$\exp_p(v) = p \cdot \exp(p^{-1} v)$$ if p is None, it will be set to the identity matrix
Source code in src/ls_mlkit/util/manifold/so3.py
27 28 29 30 31 32 33 34 35 36 37 38 39 | |
log(p=None, q=None)
Logarithm map $$\log_p(q)$$ map a point on the manifold $$M$$ to a point in tangent space $$T_p M$$ $$\log_p(q)=p\log(p^{-1} q)$$ if p is None, it will be set to the identity matrix
Source code in src/ls_mlkit/util/manifold/so3.py
41 42 43 44 45 46 47 48 49 50 51 52 53 | |
random_tangent(p, random_type='gaussian', std=1.0)
Sample noise from $$T_p M$$
Source code in src/ls_mlkit/util/manifold/so3.py
55 56 57 58 59 60 61 62 63 64 | |
metric(p, v, w)
Inner product
$$
Source code in src/ls_mlkit/util/manifold/so3.py
66 67 68 69 70 71 72 73 74 | |
grad(f, p)
Riemannian gradient of f at point p on SO(3)
$$ p \cdot skew(p^{-1} \nabla_p f(p)) $$
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
f
|
Callable
|
Callable[[Tensor], Tensor], scalar function of p |
required |
p
|
Tensor
|
(..., 3, 3) point on SO(3) |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
(..., 3, 3) gradient in the tangent space T_p SO(3) |
Source code in src/ls_mlkit/util/manifold/so3.py
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 | |
multiply(p, q)
Multiply in Group
Source code in src/ls_mlkit/util/manifold/so3.py
105 106 107 108 109 | |
inverse(p)
Inverse in Group
Source code in src/ls_mlkit/util/manifold/so3.py
111 112 113 114 | |
identity(macro_shape=tuple())
Identity in Group
Source code in src/ls_mlkit/util/manifold/so3.py
116 117 118 119 | |
left_translation(g, h)
$$L_g(h) = g \cdot h$$
Source code in src/ls_mlkit/util/manifold/so3.py
121 122 123 124 125 126 | |
LieGroup
Bases: RiemannianManifold
Lie Group
Source code in src/ls_mlkit/util/manifold/lie_group.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
multiply(p, q)
Multiply in Group
Source code in src/ls_mlkit/util/manifold/lie_group.py
14 15 16 | |
inverse(p)
Inverse in Group
Source code in src/ls_mlkit/util/manifold/lie_group.py
18 19 20 | |
identity()
Identity in Group
Source code in src/ls_mlkit/util/manifold/lie_group.py
22 23 24 | |
left_translation(g, h)
$$L_g(h) = g \cdot h$$
Source code in src/ls_mlkit/util/manifold/lie_group.py
26 27 28 29 | |
RiemannianManifold
Bases: ABC
Riemannian Manifold
Source code in src/ls_mlkit/util/manifold/riemannian_manifold.py
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 | |
exp(p, v)
abstractmethod
Exponential map $$exp_p(v)$$ map a point in tangent space to a point on the manifold $$ T_p M \to M $$
Source code in src/ls_mlkit/util/manifold/riemannian_manifold.py
11 12 13 14 15 16 17 18 19 | |
log(p, q)
abstractmethod
Logarithm map $$log_p(q)$$ map a point on the manifold to a point in tangent space $$ M \to T_p M $$
Source code in src/ls_mlkit/util/manifold/riemannian_manifold.py
21 22 23 24 25 26 27 28 29 | |
random_tangent(p, random_type='gaussian', std=1.0)
abstractmethod
Sample noise in the tangent space at point p $$T_p M$$
Source code in src/ls_mlkit/util/manifold/riemannian_manifold.py
31 32 33 34 35 36 | |
metric(p, v, w)
abstractmethod
Inner product
$$
Source code in src/ls_mlkit/util/manifold/riemannian_manifold.py
38 39 40 41 42 43 | |
grad(f, p)
abstractmethod
Gradient $$\nabla_p f$$ is the gradient of $$f$$ at point $$p$$
Source code in src/ls_mlkit/util/manifold/riemannian_manifold.py
45 46 47 48 49 50 | |
Masker
Bases: MaskerInterface
Source code in src/ls_mlkit/util/mask/masker.py
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 | |
count_bright_area(mask)
Bright area can be seen Dark area cannot be seen
Source code in src/ls_mlkit/util/mask/masker.py
27 28 29 30 31 32 | |
apply_inpainting_mask(x_0, x_t, inpainting_mask)
1 represents the region that can be seen
Source code in src/ls_mlkit/util/mask/masker.py
43 44 45 46 47 48 49 50 51 52 | |
MaskerInterface
Bases: ABC
Source code in src/ls_mlkit/util/mask/masker_interface.py
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 | |
check_mask_shape(x, mask)
abstractmethod
check whether the shape of mask is as expected
Source code in src/ls_mlkit/util/mask/masker_interface.py
16 17 18 19 20 | |
count_bright_area(mask)
abstractmethod
Bright area can be seen Dark area cannot be seen
Source code in src/ls_mlkit/util/mask/masker_interface.py
22 23 24 25 26 27 | |
get_full_bright_mask(x)
abstractmethod
Return a mask that is all bright
Source code in src/ls_mlkit/util/mask/masker_interface.py
29 30 31 32 33 | |
apply_inpainting_mask(x_0, x_t, inpainting_mask)
abstractmethod
1 represents the region that can be seen
Source code in src/ls_mlkit/util/mask/masker_interface.py
35 36 37 38 39 | |
Observer
Bases: object
Source code in src/ls_mlkit/util/observer.py
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
__init__(model=None, optimizer=None, scheduler=None, dataset=None, target_modules=None, no_split_classes=None)
Initialize the Observer
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
the model to observe. Defaults to None. |
None
|
optimizer
|
Optimizer
|
the optimizer to observe. Defaults to None. |
None
|
scheduler
|
LambdaLR
|
the scheduler to observe. Defaults to None. |
None
|
dataset
|
Dataset | Dataset
|
the dataset to observe. Defaults to None. |
None
|
target_modules
|
List[str]
|
the modules to observe. Defaults to None. if target_modules is not None, then no_split_classes and strategy is ignored. |
None
|
no_split_classes
|
List[str]
|
the classes to not split. Defaults to None. |
None
|
Source code in src/ls_mlkit/util/observer.py
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 | |
ForwardBackwardOffloadHookContext
Bases: ForwardHookForDevice
Source code in src/ls_mlkit/util/offload/forward_backward_offload.py
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 | |
__init__(model, device='cuda', no_split_module_classes=None, enable=True, num_block=2, strategy='block')
Offload model weights to CPU between forward/backward sub-blocks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
The model to which hooks will be applied. |
required | |
device
|
The compute device (e.g. "cuda"). |
'cuda'
|
|
no_split_module_classes
|
Module class names that should not be split further. |
None
|
|
enable
|
If False, this context is a no-op. |
True
|
|
num_block
|
int
|
Number of blocks for the "block" strategy. |
2
|
strategy
|
Only |
'block'
|
Source code in src/ls_mlkit/util/offload/forward_backward_offload.py
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 | |
GradientOffloadHookContext
Source code in src/ls_mlkit/util/offload/gradient_offload.py
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 | |
__init__(model, enable, record_dict, *args, **kwargs)
Offload gradients to CPU after each accumulation step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
The model whose gradients will be offloaded. |
required |
enable
|
bool
|
If False, this context is a no-op. |
required |
record_dict
|
dict
|
Dictionary that accumulates offloaded named gradients. |
required |
Source code in src/ls_mlkit/util/offload/gradient_offload.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
ModelOffloadHookContext
Source code in src/ls_mlkit/util/offload/model_offload.py
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 | |
__init__(model, no_split_module_classes=None, num_block=2, enable=True, device='cuda', strategy='block')
Combine forward/backward offloading with saved-tensor offloading.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
The model to which hooks will be applied. |
required | |
no_split_module_classes
|
Module class names that should not be split further. |
None
|
|
num_block
|
int
|
Number of blocks for the "block" strategy. |
2
|
enable
|
If False, this context is a no-op. |
True
|
|
device
|
The compute device (e.g. "cuda"). |
'cuda'
|
|
strategy
|
Only |
'block'
|
Source code in src/ls_mlkit/util/offload/model_offload.py
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 | |
Scheduler
Source code in src/ls_mlkit/util/scheduler.py
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 | |
step()
Step the scheduler
Source code in src/ls_mlkit/util/scheduler.py
70 71 72 73 74 | |
get(key=None)
Get the current value of the scheduler
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
str
|
The key of the scheduler to get. If None, return the entire scheduler info. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
|
dict[str, Any] or Any: The entire scheduler info or the value of the scheduler for the given key |
Source code in src/ls_mlkit/util/scheduler.py
76 77 78 79 80 81 82 83 84 85 86 87 88 | |
SDE
Bases: ABC
SDE abstract class. Functions are designed for a mini-batch of inputs.
Source code in src/ls_mlkit/util/sde/base_sde.py
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 | |
T
abstractmethod
property
End time of the SDE.
__init__(ndim_micro_shape=2, n_discretization_steps=1000)
Initialize the SDE
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ndim_micro_shape
|
``int``, *optional*
|
number of dimensions of a sample. |
2
|
n_discretization_steps
|
``int``, *optional*
|
number of discretization steps. |
1000
|
Source code in src/ls_mlkit/util/sde/base_sde.py
19 20 21 22 23 24 25 26 27 28 29 30 | |
prior_sampling(shape)
abstractmethod
Sample from the prior distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
``Tuple``
|
the shape of the sample. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Source code in src/ls_mlkit/util/sde/base_sde.py
37 38 39 40 41 42 43 44 45 46 | |
get_drift_and_diffusion(x, t, mask=None)
abstractmethod
Get the drift and diffusion of the SDE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
``Tensor``
|
the sample. |
required |
t
|
``Tensor``
|
the time step. |
required |
mask
|
``Tensor``, *optional*
|
the mask of the sample. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
|
Source code in src/ls_mlkit/util/sde/base_sde.py
48 49 50 51 52 53 54 55 56 57 58 59 | |
get_reverse_sde(score=None, score_fn=None, use_probability_flow=False)
Create the reverse-time SDE/ODE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
score_fn
|
Optional[Callable[..., Tensor]]
|
A time-dependent score-based model that takes (x ,t, mask) and returns the score. |
None
|
use_probability_flow
|
bool
|
If |
False
|
Source code in src/ls_mlkit/util/sde/base_sde.py
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 | |
VESDE
Bases: SDE
Source code in src/ls_mlkit/util/sde/sde_lib.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | |
__init__(sigma_min=0.01, sigma_max=50, n_discretization_steps=1000, ndim_micro_shape=2, drop_first_step=False)
Construct a Variance Exploding SDE.
Args:
sigma_min: smallest sigma.
sigma_max: largest sigma.
n_discretization_steps: number of discretization steps
ndim_micro_shape: number of dimensions of a sample
Source code in src/ls_mlkit/util/sde/sde_lib.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
get_drift_and_diffusion(x, t, mask=None)
.. math::
dx = 0 dt + \sigma_{min} \left(\frac{\sigma_{max}}{\sigma_{min}}\right)^t \sqrt{2 \log(\frac{\sigma_{max}}{\sigma_{min}})} dw
\sigma_t = \sigma_{min} \left(\frac{\sigma_{max}}{\sigma_{min}}\right)^t
diffusion = \sigma_t * \sqrt{2 \log(\frac{\sigma_{max}}{\sigma_{min}})}
Source code in src/ls_mlkit/util/sde/sde_lib.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
get_discretized_drift_and_diffusion(x, t, mask=None)
SMLD(NCSN) discretization. .. math::
x_t &= x_0 + g \epsilon
x_t &\sim \mathcal{N}(x_0, \sigma_t^2)
\sigma_t^2 &= \sigma_{t-1}^2 + g^2
g &= \sqrt{\sigma_t^2 - \sigma_{t-1}^2}
Source code in src/ls_mlkit/util/sde/sde_lib.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
VPSDE
Bases: SDE
Source code in src/ls_mlkit/util/sde/sde_lib.py
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 | |
__init__(beta_min=0.1, beta_max=20, ndim_micro_shape=2)
Construct a Variance Preserving SDE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
beta_min
|
float
|
value of beta(0) |
0.1
|
beta_max
|
float
|
value of beta(1) |
20
|
ndim_micro_shape
|
int
|
number of dimensions of a sample |
2
|
Source code in src/ls_mlkit/util/sde/sde_lib.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
get_drift_and_diffusion(x, t, mask=None)
continuous DDPM SDE
.. math::
dx &= -\frac{1}{2}\beta_t x dt + \sqrt{\beta_t} dw
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
t
|
Tensor
|
(macro_shape) |
required |
mask
|
|
None
|
Returns:
| Name | Type | Description |
|---|---|---|
drift |
Tensor
|
shape = x.shape |
diffusion |
Tensor
|
shape=x.macro_shape |
Source code in src/ls_mlkit/util/sde/sde_lib.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | |
get_score(x_t, mean, std)
.. math::
p_{0t} (x_t|x_0) = \nabla_{x_t} \ln p_{0t} (x_t|x_0)
Source code in src/ls_mlkit/util/sde/sde_lib.py
54 55 56 57 58 59 60 61 62 63 | |
get_a_b(t)
x_t = a * x_0 + b * epsilon, epsilon ~ N(0, 1)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
t
|
``Tensor``
|
continuous time |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
|
Source code in src/ls_mlkit/util/sde/sde_lib.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
forward_process(x_0, t, mask=None)
.. math::
p_{0t} (x_t|x_0)
.. math::
\gamma = -\frac{1}{4}t^2 (\beta_1 - \beta_0) - \frac{1}{2} t \beta_0
mean = e^{\gamma} * x
std = \sqrt{1 - e^{2 \gamma }}
Source code in src/ls_mlkit/util/sde/sde_lib.py
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 | |
prior_sampling(shape)
.. math:: \epsilon \sim \mathbfcal{N}(0,1)
Source code in src/ls_mlkit/util/sde/sde_lib.py
117 118 119 120 121 122 123 | |
prior_logp(z)
.. math::
(2\pi)^{-k/2} \det(\Sigma)^{-1/2} \exp\left( -\frac{1}{2} (\mathbf{x} - \boldsymbol{\mu})^\mathrm{T} \Sigma^{-1} (\mathbf{x} - \boldsymbol{\mu}) \right)
where :math:\Sigma = I and :math:\mathbf{\mu} = 0
Source code in src/ls_mlkit/util/sde/sde_lib.py
125 126 127 128 129 130 131 132 133 134 135 136 137 | |
Corrector
Bases: ABC
The abstract class for a corrector algorithm.
Source code in src/ls_mlkit/util/sde/corrector.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | |
update_fn(x, t, mask=None)
abstractmethod
One update of the corrector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
A PyTorch tensor representing the current state |
required |
t
|
Tensor
|
A PyTorch tensor representing the current time step. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x |
A PyTorch tensor of the next state. |
|
x_mean |
A PyTorch tensor. The next state without random noise. Useful for denoising. |
Source code in src/ls_mlkit/util/sde/corrector.py
28 29 30 31 32 33 34 35 36 37 38 39 | |
NoneCorrector
Bases: Corrector
An empty corrector that does nothing.
Source code in src/ls_mlkit/util/sde/corrector.py
42 43 44 45 46 47 48 49 | |
Predictor
Bases: ABC
Source code in src/ls_mlkit/util/sde/predictor.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
update_fn(x, t, mask=None)
abstractmethod
One update of the predictor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
A PyTorch tensor representing the current state |
required |
t
|
Tensor
|
A Pytorch tensor representing the current time step. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
x |
Tensor
|
A PyTorch tensor of the next state. |
x_mean |
Tensor
|
A PyTorch tensor. The next state without random noise. Useful for denoising. |
Source code in src/ls_mlkit/util/sde/predictor.py
25 26 27 28 29 30 31 32 33 34 35 36 | |
ReverseDiffusionPredictor
Bases: Predictor
Source code in src/ls_mlkit/util/sde/predictor.py
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 | |
update_fn(x, t, mask=None)
.. math::
x_{t+\Delta t} &= x_t + f(x_t, t)(\Delta t) + g(x_t, t) \epsilon, \epsilon \sim \mathcal{N}(0,\sqrt{\Delta t}))
f &= f(x_t, t)|\Delta t|
g &= g(x_t, t)\sqrt{|\Delta t|}
Source code in src/ls_mlkit/util/sde/predictor.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
SubVPSDE
Bases: SDE
Source code in src/ls_mlkit/util/sde/sde_lib.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | |
__init__(beta_min=0.1, beta_max=20, ndim_micro_shape=2)
Construct the sub-VP SDE that excels at likelihoods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
beta_min
|
float
|
value of beta(0) |
0.1
|
beta_max
|
float
|
value of beta(1) |
20
|
n_discretization_steps
|
number of discretization steps |
required | |
ndim_micro_shape
|
int
|
number of dimensions of a sample |
2
|
Source code in src/ls_mlkit/util/sde/sde_lib.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
Sniffer
Source code in src/ls_mlkit/util/sniffer.py
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 | |
sniff_file(directory_path, pattern, max_deep=-1)
Get all files in the directory_path that match the pattern
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory_path
|
``str``
|
the path of the directory to sniff |
required |
pattern
|
``str``
|
the pattern to match |
required |
max_deep
|
``int``, *optional*
|
the maximum depth to sniff. Defaults to -1. |
-1
|
Returns:
| Type | Description |
|---|---|
list[str]
|
|
Source code in src/ls_mlkit/util/sniffer.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
sniff_file_by_path_pattern(directory_path, pattern, max_deep=-1)
Get all files in the directory_path that match the pattern
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory_path
|
``str``
|
the path of the directory to sniff |
required |
pattern
|
``str``
|
the pattern to match |
required |
max_deep
|
``int``, *optional*
|
the maximum depth to sniff. Defaults to -1. |
-1
|
Returns:
| Type | Description |
|---|---|
list[str]
|
|
Source code in src/ls_mlkit/util/sniffer.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | |
check_cuda()
Check the CUDA environment, test whether torch can use cuda
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src/ls_mlkit/util/cuda.py
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 | |
cache_to_disk(root_datadir='cached_dataset', exclude_first_arg=False)
Cache the result of a function to disk
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root_datadir
|
str
|
the root directory to save the cached data. Defaults to "cached_dataset". |
'cached_dataset'
|
exclude_first_arg
|
bool
|
whether to exclude the first argument of the function when generating the cache filename. Defaults to False. |
False
|
Source code in src/ls_mlkit/util/decorators.py
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 | |
inherit_docstring_from_parent(method_name=None)
Method decorator that inherits docstring from a specific parent class method.
This decorator allows you to explicitly inherit a docstring from a parent class method, even if the method names are different.
Usage:
.. code-block:: python
class ChildClass(ParentClass):
@inherit_docstring_from_parent('parent_method_name')
def child_method(self):
pass
@inherit_docstring_from_parent() # Uses same method name
def some_method(self):
pass
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method_name
|
str | None
|
Name of the parent method to inherit docstring from. If None, uses the decorated method's name. |
None
|
Returns:
| Type | Description |
|---|---|
|
The decorated method with inherited docstring |
Source code in src/ls_mlkit/util/decorators.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
inherit_docstrings(cls)
Class decorator that automatically inherits docstrings from parent class methods.
This decorator will: 1. Find methods in the class that don't have docstrings 2. Look for the same method in parent classes 3. Copy the docstring from the first parent class that has one 4. Handle methods marked with @inherit_docstring_from_parent
Usage:
.. code-block:: python
@inherit_docstrings
class ChildClass(ParentClass):
def some_method(self):
# This method will inherit docstring from ParentClass.some_method
pass
@inherit_docstring_from_parent('parent_method')
def child_method(self):
# This method will inherit docstring from ParentClass.parent_method
pass
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
The class to apply docstring inheritance to |
required |
Returns:
| Type | Description |
|---|---|
|
The modified class with inherited docstrings |
Source code in src/ls_mlkit/util/decorators.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
register_class_to_dict(cls=None, *, key_name=None, global_dict=None)
Register a class to a global dictionary
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
class
|
the class to register. Defaults to None. |
None
|
key_name
|
str
|
the name of the key to register the class. Defaults to None. |
None
|
global_dict
|
dict
|
the global dictionary to register the class. Defaults to None. |
None
|
Source code in src/ls_mlkit/util/decorators.py
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 | |
require_keys(*required_keys)
Decorator to ensure returned dictionary contains required keys
Source code in src/ls_mlkit/util/decorators.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
timer(format='ms')
Timer the execution time of a function
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
format
|
str
|
the format of the execution time. Defaults to "ms". |
'ms'
|
Source code in src/ls_mlkit/util/decorators.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
inf_iterator(iterable)
An infinite iterator
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterable
|
iterable
|
the iterable to iterate over |
required |
Yields:
| Name | Type | Description |
|---|---|---|
any |
the next element in the iterable |
Source code in src/ls_mlkit/util/iterator.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
get_and_create_new_log_dir(root='./logs', prefix='', suffix='')
Get and create a new log directory
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root
|
str
|
the root directory to save the logs. Defaults to "./logs". |
'./logs'
|
prefix
|
str
|
the prefix of the log directory. Defaults to "". |
''
|
suffix
|
str
|
the suffix of the log directory. Defaults to "". |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
the new log directory |
Source code in src/ls_mlkit/util/log.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | |
get_logger(name='unnamed', log_dir=None)
Get a logger
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
the name of the logger. Defaults to "unnamed". |
'unnamed'
|
log_dir
|
str
|
the directory to save the logs. Defaults to None. |
None
|
Returns:
| Type | Description |
|---|---|
Logger
|
logging.Logger: the logger |
Source code in src/ls_mlkit/util/log.py
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 | |
find_linear_modules(model)
Find the linear modules in a model
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
the model to find the linear modules |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
List[str]: the names of the linear modules |
Source code in src/ls_mlkit/util/lora.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
get_lora_model(model, lora_config)
Get a LoRA model
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
Module
|
the model to get the LoRA model |
required |
lora_config
|
LoraConfig
|
the LoRA configuration |
required |
Returns:
| Type | Description |
|---|---|
|
torch.nn.Module: the LoRA model |
Source code in src/ls_mlkit/util/lora.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
get_nma_displacement_from_node_coordinates(node_coordinates, cutoff_distance=10.0, indexes=[6], node_mask=None)
node_coordinates: shape = (..., n, 3) node_mask: shape = (..., n)
Source code in src/ls_mlkit/util/nma/nma.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
gradient_norm_fn(module)
Compute the gradient norm of a module
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
the module to compute the gradient norm |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
the gradient norm of the module |
Source code in src/ls_mlkit/util/observer.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
gradients_fn(module)
Get the gradients of a module
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
the module to get the gradients |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list[Tensor]
|
the gradients of the module |
Source code in src/ls_mlkit/util/observer.py
63 64 65 66 67 68 69 70 71 72 | |
weight_norm_fn(module)
Compute the weight norm of a module
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
the module to compute the weight norm |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
the weight norm of the module |
Source code in src/ls_mlkit/util/observer.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
weights_fn(module)
Get the weights of a module
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
the module to get the weights |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
list[Tensor]
|
the weights of the module |
Source code in src/ls_mlkit/util/observer.py
51 52 53 54 55 56 57 58 59 60 | |
print_cpu_memory()
Print the CPU memory usage
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src/ls_mlkit/util/resource_monitor.py
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
print_gpu_memory()
Print the GPU memory usage
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src/ls_mlkit/util/resource_monitor.py
27 28 29 30 31 32 33 34 35 36 | |
show_gpu_and_cpu_memory()
Show the GPU and CPU memory usage
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src/ls_mlkit/util/resource_monitor.py
39 40 41 42 43 44 45 46 | |
get_model_fn(model, train=False)
Create a function to give the output of the score-based model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
The score model. |
required | |
train
|
|
False
|
Returns:
| Type | Description |
|---|---|
|
A model function. |
Source code in src/ls_mlkit/util/sde/score_fn_utils.py
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 | |
get_pc_sampler(sde, shape, predictor_class, corrector_class, inverse_scaler, snr, n_correct_steps=1, use_probability_flow=False, denoise_at_final=True, eps=0.001, device='cuda')
Create a Predictor-Corrector (PC) sampler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sde
|
SDE
|
An |
required |
shape
|
Tuple[int, ...]
|
A sequence of integers. The expected shape of a single sample. First dimension is batch size. |
required |
predictor_class
|
Predictor
|
A subclass of |
required |
corrector_class
|
Corrector
|
A subclass of |
required |
inverse_scaler
|
Callable
|
The inverse data normalizer. |
required |
snr
|
float
|
A |
required |
n_correct_steps
|
int
|
An integer. The number of corrector steps per predictor update. |
1
|
use_probability_flow
|
bool
|
If |
False
|
denoise_at_final
|
bool
|
If |
True
|
eps
|
float
|
A |
0.001
|
device
|
str
|
PyTorch device. |
'cuda'
|
Returns:
| Type | Description |
|---|---|
|
A sampling function that returns samples and the number of function evaluations during sampling. |
Source code in src/ls_mlkit/util/sde/sampler.py
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 | |
get_score_fn(sde, model, train=False, continuous=False)
Wraps score_fn so that the model output corresponds to a real time-dependent score function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sde
|
An |
required | |
model
|
A score model. |
required | |
train
|
|
False
|
|
continuous
|
If |
False
|
Returns:
| Type | Description |
|---|---|
|
A score function. |
Source code in src/ls_mlkit/util/sde/score_fn_utils.py
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 | |
seed_everything(seed)
fix the seed for all the random number generators
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
``int``
|
the seed to use for the random number generators |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Source code in src/ls_mlkit/util/seed.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |