kups.core.capacity
¶
Dynamic capacity management with automatic resizing for fixed-size arrays.
This module provides utilities for managing array capacities in JAX computations where the required size may change dynamically. The system automatically detects capacity violations and can resize arrays using a growth strategy (typically powers of 2) to amortize allocation costs.
Capacity sizes can be scalar integers or arbitrary pytrees of integers (e.g., tuples, dicts) for tracking multiple independent capacities that resize individually.
Key features:
- Scalar or pytree capacity sizes for independent tracking
- Automatic capacity detection and assertion generation
- Configurable growth strategies (default: exponential with base 2)
- Integration with runtime assertion system for automatic fixing
- Lens-based state modification for type-safe resizing
Capacity
¶
Bases: Protocol
Protocol defining the interface for capacity management.
Implementations track array capacity and generate runtime assertions that can automatically resize arrays when capacity is exceeded.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
Value
|
The capacity size type - either int or a pytree of ints |
required |
Source code in src/kups/core/capacity.py
size
property
¶
Current capacity as scalar int or pytree of ints.
generate_assertion(required_capacity)
¶
Generate a runtime assertion that checks and fixes capacity violations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
required_capacity
|
Array
|
The minimum capacity needed |
required |
Returns:
| Type | Description |
|---|---|
Capacity[Value]
|
Updated Capacity with potentially increased size |
Source code in src/kups/core/capacity.py
CapacityError
¶
Bases: ValueError
Exception raised when array capacity is exceeded.
This error is raised when attempting to store more elements than the current capacity allows. When used with the runtime assertion system, this error can trigger automatic resizing.
Source code in src/kups/core/capacity.py
FixedCapacity
¶
Static capacity that asserts without automatic resizing.
Unlike LensCapacity, a FixedCapacity cannot grow automatically
because it has no lens into the state. Its generate_assertion emits
a runtime assertion that raises CapacityError when the required
capacity exceeds the current size, but does not attach a fix function.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
Value
|
The capacity size type -- either int or a pytree of ints. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
size |
Value
|
Current capacity as int or pytree of ints. |
error_msg |
str
|
Optional message appended to the assertion error. |
Source code in src/kups/core/capacity.py
LensCapacity
¶
Lens-based implementation of the Capacity protocol.
Manages dynamic capacity with automatic resizing for fixed-size arrays. Uses a lens to update the capacity value within the state, enabling integration with the runtime assertion system for automatic resizing.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
The type of simulation state containing the capacity value. |
required | |
Value
|
The capacity size type -- either int or a pytree of ints. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
size |
Value
|
Current capacity as int or pytree of ints (e.g., tuple, dict). |
size_lens |
Lens[State, Value]
|
Lens focusing on the capacity value within the state. |
base |
float
|
Growth factor for exponential resizing (default: 2.0). |
Example
Scalar capacity:
capacity = LensCapacity(size=100, size_lens=lens(lambda s: s.avg_edges))
capacity = capacity.generate_assertion(required_capacity=150)
# Resizes to next power of 2: 256
Pytree capacity for independent tracking:
Source code in src/kups/core/capacity.py
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 | |
generate_assertion(required_capacity)
¶
Generate a runtime assertion that checks and fixes capacity violations.
This method creates an assertion that validates whether the current capacity is sufficient for the required size. If not, it generates a fix that resizes to the next appropriate capacity based on the growth strategy.
For pytree sizes, each element is checked and resized independently. The required_capacity array's last dimension should match the flattened pytree length. Batch dimensions are reduced via max.
Growth strategy:
- If base > 1: Grows to the smallest power of base ≥ required_capacity
- If base ≤ 1: Grows exactly to required_capacity (linear growth)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
required_capacity
|
Array
|
The minimum capacity needed. For pytree sizes,
shape should be |
required |
Returns:
| Type | Description |
|---|---|
LensCapacity[State, Value]
|
Updated Capacity object with potentially increased size (if not traced) |
Note
During JAX tracing, returns self unchanged. The actual resize happens when the assertion fix is applied to the state.
Source code in src/kups/core/capacity.py
LensCapacityFix
¶
Fix function for automatically resizing capacity in the state.
This callable fix function is used by the runtime assertion system to automatically resize arrays when capacity is exceeded. It updates the capacity field in the state using the provided lens.
For pytree sizes, each element is updated independently, taking the max of the current value and the target to ensure capacity never decreases.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
State
|
The type of simulation state to modify |
required | |
Value
|
The capacity size type - either int or a pytree of ints |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
lens |
Lens[State, Value]
|
Lens focusing on the capacity value within the state |
Example
Used internally by Capacity.generate_assertion(), but can be used directly:
Source code in src/kups/core/capacity.py
__call__(state, target_capacity)
¶
Apply the capacity fix by updating the state.
For batched targets, reduces via max over batch dimensions. Each pytree element is set to max(current, target) to ensure capacity never decreases.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
Current simulation state |
required |
target_capacity
|
Array
|
New capacity. Shape |
required |
Returns:
| Type | Description |
|---|---|
State
|
State with updated capacity |
Source code in src/kups/core/capacity.py
MultipliedCapacity
¶
A scaled view of another Capacity.
Wraps a base capacity and multiplies its effective size by a constant factor. When checking capacity, the required amount is divided by the factor before delegating to the base capacity, enabling capacity sharing across related arrays.
For pytree sizes, each element is multiplied by the factor independently.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
Value
|
The capacity size type - either int or a pytree of ints |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
base_capacity |
Capacity[Value]
|
The underlying capacity to scale |
factor |
int
|
Multiplier applied to each element of the base capacity's size |