torchebm.samplers ¶
Sampling algorithms for energy-based models and generative models.
Includes: - MCMC samplers (Langevin dynamics, HMC) for energy-based models - Gradient-based optimization samplers for energy minimization - Flow/diffusion samplers for trained generative models
FlowSampler ¶
Bases: BaseSampler
Sampler for flow-based and diffusion generative models.
Supports ODE (probability flow) and SDE (diffusion) sampling with various numerical integration methods including Euler, Heun, and adaptive solvers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | Module | Trained neural network predicting velocity/score/noise. | required |
interpolant | Union[str, BaseInterpolant] | Interpolant type ('linear', 'cosine', 'vp') or instance. | 'linear' |
prediction | Literal['velocity', 'score', 'noise'] | Model prediction type ('velocity', 'score', or 'noise'). | 'velocity' |
train_eps | float | Epsilon used during training for time interval stability. | 0.0 |
sample_eps | float | Epsilon for sampling time interval. | 0.0 |
negate_velocity | bool | Negate the velocity during sampling. Set True for EqM models which learn (ε - x) direction; velocity is v = -f(x). | False |
dtype | dtype | Data type for computations. | float32 |
device | Optional[Union[str, device]] | Device for computations. | None |
use_mixed_precision | bool | Whether to use mixed precision. | False |
Example
Source code in torchebm/samplers/flow.py
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 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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | |
prior_logp(z) ¶
Compute log probability under standard Gaussian prior.
Source code in torchebm/samplers/flow.py
sample(x=None, dim=10, n_steps=50, n_samples=1, thin=1, return_trajectory=False, return_diagnostics=False, *, mode='ode', shape=None, ode_method='dopri5', atol=1e-06, rtol=0.001, reverse=False, sde_method='euler', diffusion_form='SBDM', diffusion_norm=1.0, last_step='Mean', last_step_size=0.04, **model_kwargs) ¶
Unified sampling entrypoint for flow/diffusion models.
This method exists for API compatibility with BaseSampler. For full control, prefer calling sample_ode or sample_sde directly.
Source code in torchebm/samplers/flow.py
sample_ode(z, num_steps=50, method='dopri5', atol=1e-06, rtol=0.001, reverse=False, **model_kwargs) ¶
Sample using probability flow ODE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z | Tensor | Initial noise tensor of shape (batch_size, ...). | required |
num_steps | int | Number of discretization steps (for fixed-step methods). | 50 |
method | str | ODE solver ('euler', 'heun', 'dopri5', 'dopri8'). | 'dopri5' |
atol | float | Absolute tolerance for adaptive solvers. | 1e-06 |
rtol | float | Relative tolerance for adaptive solvers. | 0.001 |
reverse | bool | If True, sample from data to noise. | False |
**model_kwargs | Additional arguments passed to the model. | {} |
Returns:
| Type | Description |
|---|---|
Tensor | Generated samples tensor. |
Source code in torchebm/samplers/flow.py
sample_sde(z, num_steps=250, method='euler', diffusion_form='SBDM', diffusion_norm=1.0, last_step='Mean', last_step_size=0.04, **model_kwargs) ¶
Sample using reverse-time SDE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
z | Tensor | Initial noise tensor of shape (batch_size, ...). | required |
num_steps | int | Number of discretization steps. | 250 |
method | str | SDE solver ('euler', 'heun'). | 'euler' |
diffusion_form | str | Form of diffusion coefficient. Choices: - 'constant': Constant diffusion - 'SBDM': Score-based diffusion matching (default) - 'sigma': Proportional to noise schedule - 'linear': Linear decay - 'decreasing': Faster decay towards t=1 - 'increasing-decreasing': Peak at midpoint | 'SBDM' |
diffusion_norm | float | Scaling factor for diffusion. | 1.0 |
last_step | Optional[str] | Type of last step ('Mean', 'Tweedie', 'Euler', or None). | 'Mean' |
last_step_size | float | Size of the last step. | 0.04 |
**model_kwargs | Additional arguments passed to the model. | {} |
Returns:
| Type | Description |
|---|---|
Tensor | Generated samples tensor. |
Source code in torchebm/samplers/flow.py
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | |
GradientDescentSampler ¶
Bases: BaseSampler
Gradient descent sampler for energy-based models.
Generates samples by iteratively minimizing the energy function:
This is a deterministic optimization-based sampler that finds low-energy configurations by following the negative gradient of the energy function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | BaseModel | Energy-based model with | required |
step_size | Union[float, BaseScheduler] | Step size \(\eta\) or scheduler. | 0.001 |
dtype | dtype | Data type for computations. | float32 |
device | Optional[Union[str, device]] | Device for computations. | None |
use_mixed_precision | bool | Whether to use mixed precision. | False |
Example
Source code in torchebm/samplers/gradient_descent.py
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 | |
sample(x=None, dim=10, n_steps=100, n_samples=1, thin=1, return_trajectory=False, return_diagnostics=False, *args, **kwargs) ¶
Generate samples via gradient descent optimization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Optional[Tensor] | Initial state. If None, samples from N(0, I). | None |
dim | int | Dimension of state space (used if x is None). | 10 |
n_steps | int | Number of gradient descent steps. | 100 |
n_samples | int | Number of parallel chains/samples. | 1 |
thin | int | Thinning factor (not currently supported). | 1 |
return_trajectory | bool | Whether to return full trajectory. | False |
return_diagnostics | bool | Whether to return diagnostics. | False |
Returns:
| Type | Description |
|---|---|
Union[Tensor, Tuple[Tensor, List[dict]]] | Final samples or (samples, diagnostics) if return_diagnostics=True. |
Source code in torchebm/samplers/gradient_descent.py
HamiltonianMonteCarlo ¶
Bases: BaseSampler
Hamiltonian Monte Carlo sampler.
Uses Hamiltonian dynamics with Metropolis-Hastings acceptance to sample from the target distribution defined by the energy model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | BaseModel | Energy-based model to sample from. | required |
step_size | Union[float, BaseScheduler] | Step size for leapfrog integration. | 0.001 |
n_leapfrog_steps | int | Number of leapfrog steps per trajectory. | 10 |
mass | Optional[Union[float, Tensor]] | Mass matrix (scalar or tensor). | None |
dtype | dtype | Data type for computations. | float32 |
device | Optional[Union[str, device]] | Device for computations. | None |
Example
Source code in torchebm/samplers/hmc.py
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 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 | |
LangevinDynamics ¶
Bases: BaseSampler
Langevin Dynamics sampler.
Update rule:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | BaseModel | Energy-based model to sample from. | required |
step_size | Union[float, BaseScheduler] | Step size for gradient descent. | 0.001 |
noise_scale | Union[float, BaseScheduler] | Scale of Gaussian noise injection. | 1.0 |
decay | float | Damping coefficient (not supported). | 0.0 |
dtype | dtype | Data type for computations. | float32 |
device | Optional[Union[str, device]] | Device for computations. | None |
Example
Source code in torchebm/samplers/langevin_dynamics.py
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 | |
sample(x=None, dim=10, n_steps=100, n_samples=1, thin=1, return_trajectory=False, return_diagnostics=False, *args, **kwargs) ¶
Generates samples using Langevin dynamics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Optional[Tensor] | The initial state to start sampling from. If | None |
dim | int | The dimension of the state space (if | 10 |
n_steps | int | The number of MCMC steps to perform. | 100 |
n_samples | int | The number of parallel chains/samples to generate. | 1 |
thin | int | The thinning factor (not currently supported). | 1 |
return_trajectory | bool | Whether to return the full sample trajectory. | False |
return_diagnostics | bool | Whether to return sampling diagnostics. | False |
Returns:
| Type | Description |
|---|---|
Union[Tensor, Tuple[Tensor, List[dict]]] | Union[torch.Tensor, Tuple[torch.Tensor, List[dict]]]: - The final samples. - If |
Source code in torchebm/samplers/langevin_dynamics.py
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 | |
NesterovSampler ¶
Bases: BaseSampler
Nesterov accelerated gradient sampler for energy-based models.
Uses Nesterov momentum to accelerate convergence to low-energy states:
where \(\mu\) is the momentum coefficient and \(\eta\) is the step size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model | BaseModel | Energy-based model with | required |
step_size | Union[float, BaseScheduler] | Step size \(\eta\) or scheduler. | 0.001 |
momentum | float | Momentum coefficient \(\mu \in [0, 1)\). | 0.9 |
dtype | dtype | Data type for computations. | float32 |
device | Optional[Union[str, device]] | Device for computations. | None |
use_mixed_precision | bool | Whether to use mixed precision. | False |
Example
Source code in torchebm/samplers/gradient_descent.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 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 | |
sample(x=None, dim=10, n_steps=100, n_samples=1, thin=1, return_trajectory=False, return_diagnostics=False, *args, **kwargs) ¶
Generate samples via Nesterov accelerated gradient descent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x | Optional[Tensor] | Initial state. If None, samples from N(0, I). | None |
dim | int | Dimension of state space (used if x is None). | 10 |
n_steps | int | Number of optimization steps. | 100 |
n_samples | int | Number of parallel chains/samples. | 1 |
thin | int | Thinning factor (not currently supported). | 1 |
return_trajectory | bool | Whether to return full trajectory. | False |
return_diagnostics | bool | Whether to return diagnostics. | False |
Returns:
| Type | Description |
|---|---|
Union[Tensor, Tuple[Tensor, List[dict]]] | Final samples or (samples, diagnostics) if return_diagnostics=True. |