# base_strategy

Python API: `luxonis_train.strategies.base_strategy`

The base class every training strategy inherits.

## Classes

### BaseTrainingStrategy

Base class for the training strategies.

A strategy adds parameter-group rules to the partition of the model parameters, and can change its groups on every step. A
subclass registers in the `STRATEGIES` registry under its class name, so `trainer.training_strategy.name` can name it.

[resolve_training_plan](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/training_plan.md)
evaluates the strategy rules after the `finetuning` entries of every node and before the default rule. A node entry therefore wins
over a strategy rule, and every parameter that the strategy does not claim still gets an optimizer.

> **Example**
> The `trainer` section of a config that uses a strategy:

```yaml
trainer:
  training_strategy:
    name: TripleLRSGDStrategy
    params:
      lr: 0.02
      warmup_epochs: 3
```

#### Methods

##### init

```python
def __init__(pl_module: lxt.LuxonisLightningModule, **kwargs):
```

Create the strategy for a Lightning module.

[luxonis_train.lightning.utils.build_training_strategy](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/utils.md)
calls the constructor with the module and with the entries of `trainer.training_strategy.params` as keyword arguments.

Parameters

 * `pl_module` (`lxt.LuxonisLightningModule`): The module to train.
 * `**kwargs`: The parameters of the strategy from the config.

##### attach

```python
def attach(runtime: TrainingPlanRuntime, handles: Mapping[str, tuple[GroupHandle, ...]]):
```

Store the runtime and the group handles of the strategy rules.

[LuxonisLightningModule.configure_optimizers](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/luxonis_lightning.md)
calls the method after it builds the optimizers. The method sets the `runtime` and `group_handles` attributes. An override must
keep them, or call this method. A handle holds indices, so it stays valid after a checkpoint loads.

Parameters

 * `runtime` (`TrainingPlanRuntime`): The optimizers and schedulers of the plan. `runtime.group(handle)` returns a group.
 * `handles` (`Mapping[str, tuple[GroupHandle, ...]]`): The handles of the groups of each rule, keyed by the rule tag. A rule that
   claims no parameter has no entry.

##### get_base_configs

```python
def get_base_configs(self) -> tuple[OptimizerConfig, SchedulerConfig]:
```

Return the base optimizer and scheduler of the strategy.

While the strategy is active, the base configs replace `trainer.optimizer` and `trainer.scheduler`. The `finetuning` entries of
the nodes merge their overrides into them, and the default rule uses them. An implementation can raise `NotImplementedError`.
[resolve_training_plan](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/training_plan.md)
then uses `trainer.optimizer` and `trainer.scheduler`.

Returns

 * `tuple[OptimizerConfig, SchedulerConfig]`: The base optimizer config and the base scheduler config.

##### opaque_inners

```python
def opaque_inners(self) -> list[tuple[Optimizer, Any]]:
```

Return the optimizers that the strategy builds itself.

[build_training_plan](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/training_plan.md)
adds them after the inner optimizers of the plan. The base implementation returns an empty list. Only
[LegacyStrategyAdapter](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/legacy.md)
overrides the method.

Returns

 * `list[tuple[Optimizer, Any]]`: Pairs of an optimizer and its scheduler. The scheduler is a scheduler, a Lightning scheduler
   config dictionary, or `None`.

##### opaque_parameter_ids

```python
def opaque_parameter_ids(self) -> set[int]:
```

Return the parameters that the strategy claims outside the rules.

[resolve_training_plan](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/lightning/training_plan.md)
leaves these parameters out of the plan. The base implementation returns an empty set. Only
[LegacyStrategyAdapter](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/legacy.md)
overrides the method.

Returns

 * `set[int]`: The `id()` of each claimed parameter.

##### rules

```python
def rules(self) -> list[StrategyRule]:
```

Return the parameter-group rules of the strategy, in order.

The first rule whose selector accepts a free parameter claims it. Rules with the same optimizer name and the same scheduler share
one inner optimizer.

Returns

 * `list[StrategyRule]`: The rules. A rule without a scheduler uses the scheduler of
   [get_base_configs](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/strategies/base_strategy.md).

##### update_parameters

```python
def update_parameters(self):
```

Change the groups of the strategy after a backward pass.

[TrainingManager](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/callbacks/training_manager.md)
calls the method after each backward pass, before the optimizers step. The base implementation does nothing.

#### Attributes

##### group_handles

##### runtime
