Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code readability for generic solver module #255

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/lava/lib/optimization/solvers/generic/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ def constructor(
name: ty.Optional[str] = None,
log_config: ty.Optional[LogConfig] = None,
) -> None:
# TODO try method extraction to simplify some methods might be
# useful for both proc and model constructors
super(type(self), self).__init__(
backend=backend,
hyperparameters=hyperparameters,
Expand Down Expand Up @@ -229,6 +231,8 @@ def _create_model_constructor(self, target_cost: int):
"""

def constructor(self, proc):
# TODO try method extraction to simplify some methods might be
# useful for both proc and model constructors
discrete_var_shape = None
if hasattr(proc, "discrete_variables"):
discrete_var_shape = proc.discrete_variables.shape
Expand Down
2 changes: 2 additions & 0 deletions src/lava/lib/optimization/solvers/generic/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from lava.proc.dense.process import Dense


# TODO convert all these into subproc models, move to
# hierarchical_processes.py file and delete dataclasses.py
@dataclass
class CostMinimizer:
"""Processes implementing an optimization problem's cost function."""
Expand Down
5 changes: 4 additions & 1 deletion src/lava/lib/optimization/solvers/generic/nebm/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def __init__(
# Initial state determined in DiscreteVariables
self.state = Var(shape=shape, init=init_state.astype(int))

# TODO: is this property indented wrongly?
@property
def shape(self) -> ty.Tuple[int, ...]:
return self.proc_params["shape"]
Expand Down Expand Up @@ -124,7 +125,7 @@ def __init__(
)

self.temperature = Var(shape=shape, init=int(max_temperature))

# TODO we can avoid line breaking by adding an intermediary variable
self.refract_counter = Var(
shape=shape,
init=(refract or 0)
Expand All @@ -141,6 +142,8 @@ def __init__(
else np.zeros(shape=shape, dtype=int),
)


# TODO here too, is this property idented wrongly?
@property
def shape(self) -> ty.Tuple[int, ...]:
return self.proc_params["shape"]
40 changes: 0 additions & 40 deletions src/lava/lib/optimization/solvers/generic/processes.py

This file was deleted.

7 changes: 6 additions & 1 deletion src/lava/lib/optimization/solvers/generic/qp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ def __init__(self, proc):
shape=shape_constraint_matrix_T,
weights=constraint_matrix_T,
)

# TODO: perform method extraction, this is doing more than one thing
# by definition. Maybe the inner conditional is unnecessary if the
# interface is common and the leaf proc deals with the input
if sparse:
if model == "SigDel":
print("[INFO]: Using Sigma Delta Solution Neurons")
Expand Down Expand Up @@ -305,6 +307,7 @@ def __init__(self, proc_params: dict) -> None:
self.connectivity_spike = 0

def run_spk(self):
# TODO all this conditionals should be done in an extracted method.
self.decay_counter += 1
a_in = self.a_in.recv()
if self.decay_counter % 2 == 1:
Expand Down Expand Up @@ -373,6 +376,7 @@ def run_spk(self):
self.growth_counter += 1
a_in = self.a_in.recv()

# TODO all this conditionals should be done in an extracted method.
if self.growth_counter % 2 == 1:
if self.lr_growth_type == "schedule":
if self.growth_counter == self.beta_growth_schedule:
Expand Down Expand Up @@ -436,6 +440,7 @@ def run_spk(self):
delta_state = s_in - self.x_internal
self.x_internal = s_in
self.decay_counter += 1
# TODO all this conditionals should be done in an extracted method.
if self.theta_decay_type == "schedule":
if self.decay_counter == self.theta_decay_schedule:
# TODO: guard against shift overflows in fixed-point
Expand Down
105 changes: 0 additions & 105 deletions src/lava/lib/optimization/solvers/generic/qp/solver.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def __init__(self, proc):
problem = proc.proc_params.get("problem")
backend = proc.proc_params.get("backend")

# TODO perform method extraction and simplify, the following is too
# large and might include some code duplication.
# Subprocesses
self.variables = VariablesImplementation()
if discrete_var_shape is not None:
Expand Down
Loading