Enzyme main
Loading...
Searching...
No Matches
HMCUtils.h
Go to the documentation of this file.
1//===- HMCUtils.h - Utilities for HMC/NUTS algorithms -------* C++ -*-===//
2//
3// This file declares utility functions for Hamiltonian Monte Carlo (HMC) and
4// No-U-Turn Sampler (NUTS) implementations.
5//
6// Reference:
7// https://github.com/pyro-ppl/numpyro/blob/master/numpyro/infer/hmc_util.py
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef ENZYME_MLIR_INTERFACES_HMC_UTILS_H
12#define ENZYME_MLIR_INTERFACES_HMC_UTILS_H
13
15#include "Dialect/Ops.h"
17#include "mlir/Dialect/Arith/IR/Arith.h"
18#include "mlir/Dialect/Func/IR/FuncOps.h"
19#include "mlir/Dialect/Math/IR/Math.h"
20#include "mlir/IR/Builders.h"
21#include "mlir/IR/BuiltinAttributes.h"
22#include "mlir/IR/BuiltinTypes.h"
23#include "mlir/IR/Value.h"
24
25namespace mlir {
26namespace impulse {
27
29 int64_t offset;
30 int64_t traceOffset;
31 int64_t size;
32 impulse::SupportAttr support;
33
34 SupportInfo(int64_t offset, int64_t traceOffset, int64_t size,
35 impulse::SupportAttr support)
37 }
38};
39
41 Value q;
42 Value p;
43 Value grad;
44 Value U;
45 Value rng;
46};
47
49 Value U; // U(q) = -log p(q)
50 Value grad; // dU/dq
51 Value rng; // Updated RNG state
52};
53
55 Value q0; // Flattened position vector
56 Value U0; // Initial potential energy
57 Value grad0; // Initial gradient at position q0
58 Value rng; // RNG state for SampleHMC
59};
60
61/// Result of one MCMC kernel step
63 Value q; // New position vector
64 Value grad; // Gradient at new position
65 Value U; // Potential energy at new position
66 Value accepted; // Whether proposal was accepted
67 Value divergent; // Whether step was divergent (NUTS only; currently always
68 // false for HMC)
69 Value accept_prob; // Mean acceptance probability
70 Value rng; // Updated RNG state
71};
72
79
80 SmallVector<Value> toValues() const {
83 }
84 static DualAveragingState fromValues(ArrayRef<Value> values) {
85 return {values[0], values[1], values[2], values[3], values[4]};
86 }
87 SmallVector<Type> getTypes() const {
88 return {log_step_size.getType(), log_step_size_avg.getType(),
89 gradient_avg.getType(), step_count.getType(),
90 prox_center.getType()};
91 }
92};
93
95 Value q;
96 Value p;
97 Value grad;
98};
99
101 FlatSymbolRefAttr fn;
102 ArrayRef<Value> fnInputs;
103 SmallVector<Type> fnResultTypes;
105 ArrayAttr selection;
106 ArrayAttr allAddresses;
107 Value invMass;
109 Value stepSize;
112 SmallVector<SupportInfo> supports;
113 FlatSymbolRefAttr logpdfFn;
114 DictionaryAttr autodiffAttrs;
115
116 HMCContext(FlatSymbolRefAttr fn, ArrayRef<Value> fnInputs,
117 ArrayRef<Type> fnResultTypes, Value originalTrace,
118 ArrayAttr selection, ArrayAttr allAddresses, Value invMass,
119 Value massMatrixSqrt, Value stepSize, Value trajectoryLength,
120 int64_t positionSize, ArrayRef<SupportInfo> supports,
121 DictionaryAttr autodiffAttrs = {})
122 : fn(fn), fnInputs(fnInputs),
128 supports(supports.begin(), supports.end()),
130
139
140 bool hasCustomLogpdf() const { return logpdfFn != nullptr; }
141
142 int64_t getFullTraceSize() const {
143 auto traceType = cast<RankedTensorType>(originalTrace.getType());
144 return traceType.getShape()[1];
145 }
146
147 Type getElementType() const {
148 return cast<RankedTensorType>(stepSize.getType()).getElementType();
149 }
150
151 RankedTensorType getPositionType() const {
152 return RankedTensorType::get({1, positionSize}, getElementType());
153 }
154
155 RankedTensorType getScalarType() const {
156 return RankedTensorType::get({}, getElementType());
157 }
158
160 for (const auto &info : supports) {
161 if (info.support && info.support.getKind() != impulse::SupportKind::REAL)
162 return true;
163 }
164 return false;
165 }
166
167 HMCContext withStepSize(Value newStepSize) const {
168 HMCContext copy = *this;
169 copy.stepSize = newStepSize;
170 return copy;
171 }
172};
173
174struct NUTSContext : public HMCContext {
175 Value H0;
178
179 NUTSContext(FlatSymbolRefAttr fn, ArrayRef<Value> fnInputs,
180 ArrayRef<Type> fnResultTypes, Value originalTrace,
181 ArrayAttr selection, ArrayAttr allAddresses, Value invMass,
182 Value massMatrixSqrt, Value stepSize, int64_t positionSize,
183 ArrayRef<SupportInfo> supports, Value H0, Value maxDeltaEnergy,
184 int64_t maxTreeDepth, DictionaryAttr autodiffAttrs = {})
187 /* Unused trajectoryLength */ Value(), positionSize,
190
191 NUTSContext(FlatSymbolRefAttr logpdfFn, ArrayRef<Value> fnInputs,
192 Value invMass, Value massMatrixSqrt, Value stepSize,
193 int64_t positionSize, Value H0, Value maxDeltaEnergy,
194 int64_t maxTreeDepth, DictionaryAttr autodiffAttrs = {})
196 /* Unused trajectoryLength */ Value(), positionSize,
199
200 NUTSContext withH0(Value newH0) const {
201 NUTSContext copy = *this;
202 copy.H0 = newH0;
203 return copy;
204 }
205};
206
213 Value rng;
214
215 SmallVector<Value> toValues() const;
216 static NUTSTreeState fromValues(ArrayRef<Value> values);
217 SmallVector<Type> getTypes() const;
218
221 return {q_right, p_right, grad_right};
222 }
223};
224
230
231/// Conditionally dump a value for debugging.
232/// Emits an enzyme::DumpOp if `debugDump` is true; otherwise has no effect.
233Value conditionalDump(OpBuilder &builder, Location loc, Value value,
234 StringRef label, bool debugDump);
235
236/// Computes `v = M^-1 @ p`
237/// If `invMass` is nullptr, returns `momentum` unchanged (assumes identity)
238/// If `invMass` is a diagonal matrix, computes `v = invMass * momentum`
239/// If `invMass` is a dense matrix, computes `v = invMass @ momentum`
240Value applyInverseMassMatrix(OpBuilder &builder, Location loc, Value invMass,
241 Value momentum, RankedTensorType positionType);
242
243/// Computes `K = 0.5 * p^T @ M^-1 @ p`
244Value computeKineticEnergy(OpBuilder &builder, Location loc, Value momentum,
245 Value invMass, RankedTensorType positionType);
246
247/// Computes the square root of the mass matrix from the inverse mass matrix.
248Value computeMassMatrixSqrt(OpBuilder &builder, Location loc, Value invMass,
249 RankedTensorType positionType);
250
251/// Samples momentum from `N(0, M)` where M is the mass matrix.
252/// Returns `(momentum, updated_rng_state)`.
253std::pair<Value, Value> sampleMomentum(OpBuilder &builder, Location loc,
254 Value rng, Value invMass,
255 Value massMatrixSqrt,
256 RankedTensorType positionType,
257 bool debugDump = false);
258
259/// Computes potential energy `U(q) = -log p(q)` and its gradient `dU/dq`
260GradientResult computePotentialAndGradient(OpBuilder &builder, Location loc,
261 Value position, Value rng,
262 const HMCContext &ctx);
263
264/// Computes a single leapfrog integration step.
265/// Specifically:
266/// - `p_half = p - (eps/2) * grad`
267/// - `q_new = q + eps * M^-1 * p_half`
268/// - `grad_new = dU/dq(q_new)`
269/// - `p_new = p_half - (eps/2) * grad_new`
270IntegrationResult computeIntegrationStep(OpBuilder &builder, Location loc,
271 const IntegratorState &leaf, Value rng,
272 Value direction,
273 const HMCContext &ctx);
274
275/// U-turn termination criterion.
276/// Returns `true` if U-turn detected in the trajectory.
277Value checkTurning(OpBuilder &builder, Location loc, Value pLeft, Value pRight,
278 Value pSum, const NUTSContext &ctx);
279
280/// Computes the uniform transition probability for subtree combination.
281/// Specifically: `sigmoid(new_weight - current_weight)`.
282Value computeUniformTransitionProb(OpBuilder &builder, Location loc,
283 Value currentWeight, Value newWeight);
284
285/// Computes the biased transition probability for main tree combination.
286/// Specifically: `min(1, exp(new_weight - current_weight))`, zeroed if
287/// turning or diverging.
288Value computeBiasedTransitionProb(OpBuilder &builder, Location loc,
289 Value currentWeight, Value newWeight,
290 Value turning, Value diverging);
291
292/// Combines a tree with a newly-built subtree during NUTS doubling process.
293///
294/// The `subtree` is merged into `tree` based on `direction`:
295/// - If going right (`direction` is true), then we keep `tree`'s left
296/// boundary, takes `subtree`'s right boundary
297/// - If going left (`direction` is false), then we take `subtree`'s left
298/// boundary, keeps `tree`'s right boundary
299///
300/// Proposal selection has two options:
301/// - Biased kernel (`biased` is true):
302/// `prob = min(1, exp(subtree.weight - tree.weight))`,
303/// zeroed if `turning` or `diverging`
304/// - Uniform kernel (`biased` is false):
305/// `prob = sigmoid(subtree.weight - tree.weight)`
306///
307/// Also checks the U-turn criterion on the combined momentum sum.
308NUTSTreeState combineTrees(OpBuilder &builder, Location loc,
309 const NUTSTreeState &tree,
310 const NUTSTreeState &subTree, Value direction,
311 Value rng, bool biased, const NUTSContext &ctx);
312
313/// Initializes HMC/NUTS state from a trace
314/// Specifically:
315/// - Extracts position from trace
316/// - Computes initial potential energy U0 = -weight
317/// - Samples initial momentum p0 ~ N(0, M)
318/// - Computes initial kinetic energy and Hamiltonian
319/// - Computes initial gradient via AutoDiffRegionOp
320InitialHMCState InitHMC(OpBuilder &builder, Location loc, Value rng,
321 const HMCContext &ctx, Value initialPosition = Value(),
322 bool debugDump = false);
323
324/// Single HMC iteration: momentum sampling + leapfrog + MH accept/reject
325MCMCKernelResult SampleHMC(OpBuilder &builder, Location loc, Value q,
326 Value grad, Value U, Value rng,
327 const HMCContext &ctx, bool debugDump = false);
328
329/// Single NUTS iteration: momentum sampling + tree building
330MCMCKernelResult SampleNUTS(OpBuilder &builder, Location loc, Value q,
331 Value grad, Value U, Value rng,
332 const NUTSContext &ctx, bool debugDump = false);
333
334/// Builds a base tree (leaf node) by taking one leapfrog step.
335NUTSTreeState buildBaseTree(OpBuilder &builder, Location loc,
336 const IntegratorState &leaf, Value rng,
337 Value direction, const NUTSContext &ctx);
338
339/// Extracts an appropriate leaf based on direction.
340IntegratorState getLeafFromTree(OpBuilder &builder, Location loc,
341 const NUTSTreeState &tree, Value direction,
342 const NUTSContext &ctx);
343
344/// Builds a subtree iteratively by appending leaves one at a time.
345SubtreeBuildResult buildIterativeSubtree(OpBuilder &builder, Location loc,
346 const NUTSTreeState &initialTree,
347 Value direction, Value pCkpts,
348 Value pSumCkpts,
349 const NUTSContext &ctx,
350 bool debugDump = false);
351
352/// Tree doubling by building a subtree of same depth and combining.
353SubtreeBuildResult doubleTree(OpBuilder &builder, Location loc,
354 const NUTSTreeState &tree, Value direction,
355 Value pCkpts, Value pSumCkpts,
356 const NUTSContext &ctx, bool debugDump = false);
357
358/// Main NUTS tree building loop.
359NUTSTreeState buildTree(OpBuilder &builder, Location loc,
360 const NUTSTreeState &initialTree,
361 const NUTSContext &ctx, bool debugDump = false);
362
363/// Computes checkpoint indices from leaf index for iterative turning check.
364std::pair<Value, Value> leafIdxToCheckpointIdxs(OpBuilder &builder,
365 Location loc, Value leafIdx);
366
367/// Checkpoint-based iterative turning check.
368Value checkIterativeTurning(OpBuilder &builder, Location loc, Value p,
369 Value pSum, Value pCkpts, Value pSumCkpts,
370 Value idxMin, Value idxMax, const NUTSContext &ctx,
371 bool debugDump = false);
372
373/// Update checkpoint arrays at even leaf indices.
374std::pair<Value, Value> updateCheckpoints(OpBuilder &builder, Location loc,
375 Value leafIdx, Value ckptIdxMax,
376 Value p, Value pSum, Value pCkpts,
377 Value pSumCkpts,
378 const NUTSContext &ctx,
379 bool debugDump = false);
380
381// TODO: Proper customization
383 double t0 = 10.0; // Stabilization
384 double kappa = 0.75; // Weight decay
385 double gamma = 0.05; // Convergence
386 double target_accept_prob = 0.8;
387};
388
389/// Initialize dual averaging state from initial step size.
390DualAveragingState initDualAveraging(OpBuilder &builder, Location loc,
391 Value stepSize, bool logOfProduct = false);
392
393/// Update dual averaging state with observed acceptance probability.
394DualAveragingState updateDualAveraging(OpBuilder &builder, Location loc,
395 const DualAveragingState &state,
396 Value acceptProb,
397 const DualAveragingConfig &config);
398
399/// Get step size from dual averaging state.
400/// If `final` is true, returns the averaged step size.
401/// Otherwise, returns the updated step size.
402Value getStepSizeFromDualAveraging(OpBuilder &builder, Location loc,
403 const DualAveragingState &state,
404 bool final = false);
405
406/// State for Welford covariance estimation
408 Value mean;
409 Value m2; // Sum of squared deviations
410 Value n;
411
412 SmallVector<Value> toValues() const { return {mean, m2, n}; }
413 static WelfordState fromValues(ArrayRef<Value> values) {
414 return {values[0], values[1], values[2]};
415 }
416 SmallVector<Type> getTypes() const {
417 return {mean.getType(), m2.getType(), n.getType()};
418 }
419};
420
421/// Configuration for Welford covariance estimation
423 bool diagonal = true;
424 bool regularize = true; // Optional regularization (Stan's shrinkage)
425};
426
427/// Initialize state for Welford covariance estimation.
428WelfordState initWelford(OpBuilder &builder, Location loc, int64_t positionSize,
429 bool diagonal);
430
431/// Update Welford state with a new sample.
432WelfordState updateWelford(OpBuilder &builder, Location loc,
433 const WelfordState &state, Value sample,
434 const WelfordConfig &config);
435
436/// Finalize Welford state to produce sample covariance (returned as inverse
437/// mass matrix).
438Value finalizeWelford(OpBuilder &builder, Location loc,
439 const WelfordState &state, const WelfordConfig &config);
440
442 int64_t start;
443 int64_t end;
444};
445
446/// Build warmup adaptation schedule.
447/// TODO: Make customizable
448SmallVector<AdaptWindow> buildAdaptationSchedule(int64_t numSteps);
449
450/// Transform an entire position vector from constrained to unconstrained space
451/// based on the support information.
452Value unconstrainPosition(OpBuilder &builder, Location loc, Value constrained,
453 ArrayRef<SupportInfo> supports);
454
455/// Transform an entire position vector from unconstrained to constrained space.
456/// based on the support information.
457Value constrainPosition(OpBuilder &builder, Location loc, Value unconstrained,
458 ArrayRef<SupportInfo> supports);
459
460/// Compute total Jacobian correction for the constrain transform over all
461/// position vector slices.
462Value computeTotalJacobianCorrection(OpBuilder &builder, Location loc,
463 Value unconstrained,
464 ArrayRef<SupportInfo> supports);
465} // namespace impulse
466} // namespace mlir
467
468#endif // ENZYME_MLIR_INTERFACES_HMC_UTILS_H
PointerType * traceType(LLVMContext &C)
Value computeTotalJacobianCorrection(OpBuilder &builder, Location loc, Value unconstrained, ArrayRef< SupportInfo > supports)
Compute total Jacobian correction for the constrain transform over all position vector slices.
Value finalizeWelford(OpBuilder &builder, Location loc, const WelfordState &state, const WelfordConfig &config)
Finalize Welford state to produce sample covariance (returned as inverse mass matrix).
Value checkTurning(OpBuilder &builder, Location loc, Value pLeft, Value pRight, Value pSum, const NUTSContext &ctx)
U-turn termination criterion.
Definition HMCUtils.cpp:509
GradientResult computePotentialAndGradient(OpBuilder &builder, Location loc, Value position, Value rng, const HMCContext &ctx)
Computes potential energy U(q) = -log p(q) and its gradient dU/dq
Definition HMCUtils.cpp:352
DualAveragingState initDualAveraging(OpBuilder &builder, Location loc, Value stepSize, bool logOfProduct=false)
Initialize dual averaging state from initial step size.
std::pair< Value, Value > leafIdxToCheckpointIdxs(OpBuilder &builder, Location loc, Value leafIdx)
Computes checkpoint indices from leaf index for iterative turning check.
IntegratorState getLeafFromTree(OpBuilder &builder, Location loc, const NUTSTreeState &tree, Value direction, const NUTSContext &ctx)
Extracts an appropriate leaf based on direction.
std::pair< Value, Value > updateCheckpoints(OpBuilder &builder, Location loc, Value leafIdx, Value ckptIdxMax, Value p, Value pSum, Value pCkpts, Value pSumCkpts, const NUTSContext &ctx, bool debugDump=false)
Update checkpoint arrays at even leaf indices.
SubtreeBuildResult buildIterativeSubtree(OpBuilder &builder, Location loc, const NUTSTreeState &initialTree, Value direction, Value pCkpts, Value pSumCkpts, const NUTSContext &ctx, bool debugDump=false)
Builds a subtree iteratively by appending leaves one at a time.
std::pair< Value, Value > sampleMomentum(OpBuilder &builder, Location loc, Value rng, Value invMass, Value massMatrixSqrt, RankedTensorType positionType, bool debugDump=false)
Samples momentum from N(0, M) where M is the mass matrix.
Definition HMCUtils.cpp:233
Value computeUniformTransitionProb(OpBuilder &builder, Location loc, Value currentWeight, Value newWeight)
Computes the uniform transition probability for subtree combination.
Definition HMCUtils.cpp:557
SmallVector< AdaptWindow > buildAdaptationSchedule(int64_t numSteps)
Build warmup adaptation schedule.
NUTSTreeState buildBaseTree(OpBuilder &builder, Location loc, const IntegratorState &leaf, Value rng, Value direction, const NUTSContext &ctx)
Builds a base tree (leaf node) by taking one leapfrog step.
Value computeKineticEnergy(OpBuilder &builder, Location loc, Value momentum, Value invMass, RankedTensorType positionType)
Computes K = 0.5 * p^T @ M^-1 @ p
Definition HMCUtils.cpp:166
NUTSTreeState combineTrees(OpBuilder &builder, Location loc, const NUTSTreeState &tree, const NUTSTreeState &subTree, Value direction, Value rng, bool biased, const NUTSContext &ctx)
Combines a tree with a newly-built subtree during NUTS doubling process.
Definition HMCUtils.cpp:590
Value computeBiasedTransitionProb(OpBuilder &builder, Location loc, Value currentWeight, Value newWeight, Value turning, Value diverging)
Computes the biased transition probability for main tree combination.
Definition HMCUtils.cpp:566
Value checkIterativeTurning(OpBuilder &builder, Location loc, Value p, Value pSum, Value pCkpts, Value pSumCkpts, Value idxMin, Value idxMax, const NUTSContext &ctx, bool debugDump=false)
Checkpoint-based iterative turning check.
NUTSTreeState buildTree(OpBuilder &builder, Location loc, const NUTSTreeState &initialTree, const NUTSContext &ctx, bool debugDump=false)
Main NUTS tree building loop.
Value conditionalDump(OpBuilder &builder, Location loc, Value value, StringRef label, bool debugDump)
Conditionally dump a value for debugging.
Definition HMCUtils.cpp:64
MCMCKernelResult SampleNUTS(OpBuilder &builder, Location loc, Value q, Value grad, Value U, Value rng, const NUTSContext &ctx, bool debugDump=false)
Single NUTS iteration: momentum sampling + tree building.
InitialHMCState InitHMC(OpBuilder &builder, Location loc, Value rng, const HMCContext &ctx, Value initialPosition=Value(), bool debugDump=false)
Initializes HMC/NUTS state from a trace Specifically:
Definition HMCUtils.cpp:701
SubtreeBuildResult doubleTree(OpBuilder &builder, Location loc, const NUTSTreeState &tree, Value direction, Value pCkpts, Value pSumCkpts, const NUTSContext &ctx, bool debugDump=false)
Tree doubling by building a subtree of same depth and combining.
IntegrationResult computeIntegrationStep(OpBuilder &builder, Location loc, const IntegratorState &leaf, Value rng, Value direction, const HMCContext &ctx)
Computes a single leapfrog integration step.
Definition HMCUtils.cpp:460
DualAveragingState updateDualAveraging(OpBuilder &builder, Location loc, const DualAveragingState &state, Value acceptProb, const DualAveragingConfig &config)
Update dual averaging state with observed acceptance probability.
Value unconstrainPosition(OpBuilder &builder, Location loc, Value constrained, ArrayRef< SupportInfo > supports)
Transform an entire position vector from constrained to unconstrained space based on the support info...
Value computeMassMatrixSqrt(OpBuilder &builder, Location loc, Value invMass, RankedTensorType positionType)
Computes the square root of the mass matrix from the inverse mass matrix.
Definition HMCUtils.cpp:190
WelfordState updateWelford(OpBuilder &builder, Location loc, const WelfordState &state, Value sample, const WelfordConfig &config)
Update Welford state with a new sample.
WelfordState initWelford(OpBuilder &builder, Location loc, int64_t positionSize, bool diagonal)
Initialize state for Welford covariance estimation.
Value getStepSizeFromDualAveraging(OpBuilder &builder, Location loc, const DualAveragingState &state, bool final=false)
Get step size from dual averaging state.
MCMCKernelResult SampleHMC(OpBuilder &builder, Location loc, Value q, Value grad, Value U, Value rng, const HMCContext &ctx, bool debugDump=false)
Single HMC iteration: momentum sampling + leapfrog + MH accept/reject.
Definition HMCUtils.cpp:862
Value constrainPosition(OpBuilder &builder, Location loc, Value unconstrained, ArrayRef< SupportInfo > supports)
Transform an entire position vector from unconstrained to constrained space.
Value applyInverseMassMatrix(OpBuilder &builder, Location loc, Value invMass, Value momentum, RankedTensorType positionType)
Computes v = M^-1 @ p If invMass is nullptr, returns momentum unchanged (assumes identity) If invMass...
Definition HMCUtils.cpp:139
SmallVector< Type > getTypes() const
Definition HMCUtils.h:87
static DualAveragingState fromValues(ArrayRef< Value > values)
Definition HMCUtils.h:84
SmallVector< Value > toValues() const
Definition HMCUtils.h:80
DictionaryAttr autodiffAttrs
Definition HMCUtils.h:114
bool hasConstrainedSupports() const
Definition HMCUtils.h:159
Type getElementType() const
Definition HMCUtils.h:147
FlatSymbolRefAttr fn
Definition HMCUtils.h:101
HMCContext(FlatSymbolRefAttr logpdfFn, ArrayRef< Value > fnInputs, Value invMass, Value massMatrixSqrt, Value stepSize, Value trajectoryLength, int64_t positionSize, DictionaryAttr autodiffAttrs={})
Definition HMCUtils.h:131
HMCContext withStepSize(Value newStepSize) const
Definition HMCUtils.h:167
ArrayRef< Value > fnInputs
Definition HMCUtils.h:102
HMCContext(FlatSymbolRefAttr fn, ArrayRef< Value > fnInputs, ArrayRef< Type > fnResultTypes, Value originalTrace, ArrayAttr selection, ArrayAttr allAddresses, Value invMass, Value massMatrixSqrt, Value stepSize, Value trajectoryLength, int64_t positionSize, ArrayRef< SupportInfo > supports, DictionaryAttr autodiffAttrs={})
Definition HMCUtils.h:116
RankedTensorType getPositionType() const
Definition HMCUtils.h:151
int64_t getFullTraceSize() const
Definition HMCUtils.h:142
SmallVector< Type > fnResultTypes
Definition HMCUtils.h:103
SmallVector< SupportInfo > supports
Definition HMCUtils.h:112
bool hasCustomLogpdf() const
Definition HMCUtils.h:140
RankedTensorType getScalarType() const
Definition HMCUtils.h:155
FlatSymbolRefAttr logpdfFn
Definition HMCUtils.h:113
Result of one MCMC kernel step.
Definition HMCUtils.h:62
NUTSContext withH0(Value newH0) const
Definition HMCUtils.h:200
NUTSContext(FlatSymbolRefAttr fn, ArrayRef< Value > fnInputs, ArrayRef< Type > fnResultTypes, Value originalTrace, ArrayAttr selection, ArrayAttr allAddresses, Value invMass, Value massMatrixSqrt, Value stepSize, int64_t positionSize, ArrayRef< SupportInfo > supports, Value H0, Value maxDeltaEnergy, int64_t maxTreeDepth, DictionaryAttr autodiffAttrs={})
Definition HMCUtils.h:179
NUTSContext(FlatSymbolRefAttr logpdfFn, ArrayRef< Value > fnInputs, Value invMass, Value massMatrixSqrt, Value stepSize, int64_t positionSize, Value H0, Value maxDeltaEnergy, int64_t maxTreeDepth, DictionaryAttr autodiffAttrs={})
Definition HMCUtils.h:191
IntegratorState getRightLeaf() const
Definition HMCUtils.h:220
SmallVector< Value > toValues() const
Definition HMCUtils.cpp:26
IntegratorState getLeftLeaf() const
Definition HMCUtils.h:219
SmallVector< Type > getTypes() const
Definition HMCUtils.cpp:57
static NUTSTreeState fromValues(ArrayRef< Value > values)
Definition HMCUtils.cpp:35
SupportInfo(int64_t offset, int64_t traceOffset, int64_t size, impulse::SupportAttr support)
Definition HMCUtils.h:34
impulse::SupportAttr support
Definition HMCUtils.h:32
Configuration for Welford covariance estimation.
Definition HMCUtils.h:422
State for Welford covariance estimation.
Definition HMCUtils.h:407
SmallVector< Value > toValues() const
Definition HMCUtils.h:412
SmallVector< Type > getTypes() const
Definition HMCUtils.h:416
static WelfordState fromValues(ArrayRef< Value > values)
Definition HMCUtils.h:413