optimization.variable.base#

Base Variable Module

This module contains the base class for a variable in an optic system. The VariableBehavior class is an abstract class that represents the behavior of a variable in an optic system. It is used as a base class for all variables in the optimization process.

Kramer Harrison, 2024

Functions

release_attribute(owner, name)

Replace owner.<name> by a copy without an autograd graph.

without_graph(value)

Return value without the autograd graph of the last step.

Classes

VariableBehavior(optic, surface_number[, scaler])

Represents the behavior of a variable in an optic system.

class VariableBehavior(optic, surface_number, scaler: Scaler = None, **kwargs)[source]#

Represents the behavior of a variable in an optic system.

Parameters:
  • optic (Optic) – The optic system to which the variable belongs.

  • surface_number (int) – The surface number of the variable.

  • scaler (Scaler) – The scaler to use for the variable. Defaults to IdentityScaler().

  • **kwargs – Additional keyword arguments.

Variables:
  • optic (Optic) – The optic system to which the variable belongs.

  • _surfaces (SurfaceGroup) – The group of surfaces in the optic system.

  • surface_number (int) – The surface number of the variable.

  • scaler (Scaler) – The scaler to use for the variable.

abstractmethod get_value()[source]#

Get the value of the variable.

Returns:

The value of the variable.

inverse_scale(scaled_value)[source]#

Inverse scale the value of the variable.

Parameters:

scaled_value – The scaled value to inverse scale

release_graph()[source]#

Drop the autograd graph that the last update left in the optic.

A Torch optimizer calls this for all variables before it writes the values of a new step (VariableManager.release_graphs). A variable that writes one entry of an array, which other variables can share (a coefficient array), must start each step from plain values. Otherwise the array still holds the graph of the last step: the next backward pass then runs into that freed graph and fails, or the graph grows with each step.

The default does nothing: a variable that replaces a whole attribute leaves no old graph behind.

scale(value)[source]#

Scale the value of the variable for improved optimization performance.

Parameters:

value – The value to scale

abstractmethod update_value(new_value)[source]#

Update the value of the variable.

Parameters:

new_value – The new value of the variable.

release_attribute(owner, name)[source]#

Replace owner.<name> by a copy without an autograd graph.

Only a value that holds a graph is replaced (see without_graph). So in one step only the first variable of a shared array copies it, and a setter of the attribute (ZernikePolynomialGeometry.coefficients builds a new Zernike object) runs only then.

Parameters:
  • owner – The object that holds the array, for example a geometry.

  • name (str) – The name of the attribute.

without_graph(value)[source]#

Return value without the autograd graph of the last step.

A tensor that requires grad gives a detached copy with its own memory, so that a later in-place write cannot change a tensor that the graph of the last step saved. Other values hold no graph and are returned as they are: NumPy arrays, numbers, and tensors that do not require grad.

Parameters:

value – A tensor, an array or a number.

Returns:

The value without an autograd graph.