torch.distributions.lazy_property
function lazy_property<T>(target: object, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptorDecorator for lazy-loaded computed properties with automatic caching.
Converts a getter method into a lazy-loaded property that computes its value only once and caches the result for subsequent accesses. Useful for expensive computations that may not always be needed, or that should only be computed once. Useful for:
- Expensive properties: Deferring computation of derived values until needed
- Caching: Automatically caching computed values to avoid recomputation
- Distribution parameters: Computing derived properties like scale from variance
- Memory efficiency: Computing properties only when accessed, not on construction
- Memoization: Simple per-instance memoization pattern for getters
Uses Symbol-based caching to store computed values privately on the instance. Each decorated property has its own cache key, preventing conflicts between multiple lazy properties on the same object.
- One-time computation: Value is computed exactly once, on first access
- Symbol-based caching: Uses Symbols to avoid property name conflicts
- Per-instance cache: Each object instance has its own cached values
- Transparent: Looks like a normal property to the user (no function call)
- Immutable after first access: Cached value won't change even if dependencies change
- Dependency changes: Cached value not recomputed if dependencies change
- Memory: Cached values persist in memory for the object's lifetime
- Not reactive: No automatic invalidation if source data changes
- Side effects: Getter function should be pure (no side effects expected to rerun)
Parameters
targetobject- The prototype object of the class (unused, set by decorator)
propertyKeystring- The name of the property being decorated
descriptorPropertyDescriptor- The property descriptor with the getter function
Returns
PropertyDescriptor– Modified property descriptor that implements lazy cachingExamples
class Distribution {
private _variance: Tensor;
@lazy_property
get stddev(): Tensor {
console.log('Computing stddev...'); // Only prints once
return this._variance.sqrt();
}
}
const dist = new Distribution();
dist.stddev; // Prints "Computing stddev...", computes value
dist.stddev; // No print, returns cached valueclass MultivariateNormal {
_cov: Tensor;
@lazy_property
get precision(): Tensor {
// Compute inverse, expensive operation
return this._cov.inverse();
}
@lazy_property
get entropy(): number {
// Complex entropy calculation
return computeEntropy(this);
}
}
const mvn = new MultivariateNormal();
mvn.precision; // Computed on first access
mvn.entropy; // Computed on first access// Multiple lazy properties work independently
class Normal {
@lazy_property
get variance() { return Math.pow(this.scale, 2); }
@lazy_property
get entropy() { return 0.5 * Math.log(2 * Math.PI * this.variance); }
}
const n = new Normal();
// Each is computed once, independently cachedSee Also
- [PyTorch Similar pattern used in PyTorch distributions](https://pytorch.org/docs/stable/generated/Similar pattern used in PyTorch distributions.html)
- Object.defineProperty - Underlying mechanism used for property definition