adjoint ¤
Transient parameter sensitivity via discrete adjoint method.
Computes ∂loss/∂params through a transient simulation trajectory using the discrete adjoint of the Backward Euler time-stepping scheme. Avoids autodiff through the OSDI XLA FFI.
Mathematical background¤
Time-stepping residual at step k (Backward Euler):
G[k](y[k], y[k-1], p) = F(y[k], p) + (Q(y[k], p) - Q(y[k-1], p)) / dt = 0
Implicit function: y[k] = Φ(y[k-1], p) defined by G[k] = 0.
Sensitivity through implicit differentiation:
∂y[k]/∂y[k-1] = J_eff[k]^{-1} · (J_q[k-1] / dt)
∂y[k]/∂p = -J_eff[k]^{-1} · (∂F[k]/∂p + ∂Q[k]/∂p / dt)
where J_eff[k] = J_f[k] + J_q[k] / dt.
Discrete adjoint recurrence (backward sweep, k = N down to 0):
ψ[N] = ∂L/∂y[N]|direct
ψ[k] = ∂L/∂y[k]|direct + (J_q[k] / dt)^T · λ[k+1]
J_eff[k]^T · λ[k] = ψ[k] (adjoint linear solve at each step)
Parameter gradient accumulation:
∂loss/∂p = -Σ_{k=1}^{N} λ[k]^T · (∂F/∂p(y[k]) + ∂Q/∂p(y[k]) / dt)
Note on the J_q coupling term¤
The term (J_q[k]/dt)^T · λ[k+1] propagates sensitivity backward through the capacitive coupling between time steps. For purely resistive circuits (J_q ≈ 0) this term is zero and the adjoint reduces to N independent DC-like adjoint solves. For RC/RLC circuits it is essential for accuracy.
Functions:
| Name | Description |
|---|---|
transient_parameter_sensitivity | Compute ∂loss/∂params via discrete adjoint over a transient trajectory. |
transient_parameter_sensitivity_dense | Dense-solver fallback for :func: |
transient_parameter_sensitivity ¤
transient_parameter_sensitivity(
component_groups: dict,
solver: CircuitLinearSolver,
y_trajectory: Array,
ts: Array,
loss_fn: Callable,
*,
osdi_group_key: str,
param_names: list[str],
model_descriptor: Any | None = None,
param_to_col: dict[str, int] | None = None,
eps: float = 1e-06,
shared_params: bool = False
) -> dict[str, Array | float]
Compute ∂loss/∂params via discrete adjoint over a transient trajectory.
Implements the correct discrete adjoint of the Backward Euler time-stepping scheme, with full inter-step coupling through the capacitance matrix (J_q).
Note
This function uses host-side loops and jax.device_get for finite-difference perturbations and cannot be JIT-compiled.
The adjoint recurrence is (k = N, N-1, ..., 1):
ψ[k] = ∂L/∂y[k]|direct + (J_q[k] / dt)^T · λ[k+1]
J_eff[k]^T · λ[k] = ψ[k] (linear solve)
∂loss/∂p -= λ[k]^T · (∂F[k]/∂p + ∂Q[k]/∂p / dt) (gradient)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
component_groups | dict | Compiled circuit groups (dict returned by :func: | required |
solver | CircuitLinearSolver | A :class: | required |
y_trajectory | Array | Saved trajectory array of shape | required |
ts | Array | Time points of the checkpoints, shape | required |
loss_fn | Callable | Callable | required |
osdi_group_key | str | Key in | required |
param_names | list[str] | List of canonical OSDI parameter names to differentiate. | required |
model_descriptor | Any | None | The :class: | None |
param_to_col | dict[str, int] | None | Explicit | None |
eps | float | Relative finite difference step size. | 1e-06 |
shared_params | bool | If True, all devices share the same parameter values (process params). Perturbs all devices at once per parameter, reducing OSDI evals from | False |
Returns:
| Type | Description |
|---|---|
dict[str, Array | float] | Dict mapping each name in |
dict[str, Array | float] | When |
dict[str, Array | float] | When |
Raises:
| Type | Description |
|---|---|
ValueError | If |
TypeError | If |
ImportError | If bosdi / osdi_jax is not available. |
Source code in circulax/solvers/adjoint.py
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 | |
transient_parameter_sensitivity_dense ¤
transient_parameter_sensitivity_dense(
component_groups: dict,
y_trajectory: Array,
ts: Array,
loss_fn: Callable,
*,
osdi_group_key: str,
param_names: list[str],
model_descriptor: Any | None = None,
param_to_col: dict[str, int] | None = None,
eps: float = 1e-06,
shared_params: bool = False
) -> dict[str, Array | float]
Dense-solver fallback for :func:transient_parameter_sensitivity.
Uses jnp.linalg.solve instead of KLU for adjoint solves, so it works with any solver backend — including :class:~circulax.solvers.linear.DenseSolver. Intended for small circuits and unit tests.
Args match :func:transient_parameter_sensitivity except solver is not required.
Returns:
| Type | Description |
|---|---|
dict[str, Array | float] | Same as :func: |
Source code in circulax/solvers/adjoint.py
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |