Index
CsvWriter
Bases: BaseWriter
Writer for saving predictions in a CSV format. It accumulates predictions in a temporary file and saves them to the final destination at the end of the prediction epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to save the final CSV file. |
required |
keys
|
list[str]
|
A list of keys to be included in the CSV file.
These keys must be present in the |
required |
Example
Source code in src/lighter/callbacks/csv_writer.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 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 | |
_close_file()
Close the CSV file if it's open and reset related state.
on_exception(trainer, pl_module, exception)
on_predict_epoch_end(trainer, pl_module)
At the end of the prediction epoch, it saves the temporary file to the final destination.
Source code in src/lighter/callbacks/csv_writer.py
teardown(trainer, pl_module, stage)
FileWriter
Bases: BaseWriter
Persist a prediction value per sample to disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
directory
|
str | Path
|
Directory to save prediction files. |
required |
value_key
|
str
|
Key in the prediction outputs dict containing values to save. |
required |
writer_fn
|
str | Callable[[Path, Any], None]
|
Writer function name (e.g., "tensor", "image_2d", "text") or callable. |
required |
name_key
|
str | None
|
Optional key for custom file names. If None, uses sequential numbering. |
None
|
Example
Source code in src/lighter/callbacks/file_writer.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 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 | |
Freezer
Bases: Callback
Callback to freeze model parameters during training. Parameters can be frozen by exact name or prefix. Freezing can be applied indefinitely or until a specified step/epoch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
str | list[str] | None
|
Full names of parameters to freeze. |
None
|
name_starts_with
|
str | list[str] | None
|
Prefixes of parameter names to freeze. |
None
|
except_names
|
str | list[str] | None
|
Names of parameters to exclude from freezing. |
None
|
except_name_starts_with
|
str | list[str] | None
|
Prefixes of parameter names to exclude from freezing. |
None
|
until_step
|
int | None
|
Maximum step to freeze parameters until. |
None
|
until_epoch
|
int | None
|
Maximum epoch to freeze parameters until. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither |
ValueError
|
If both |
Source code in src/lighter/callbacks/freezer.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |
_set_model_requires_grad(model, requires_grad)
Sets the requires_grad attribute for model parameters.
When freezing (requires_grad=False): - Freeze specified parameters - Keep all others trainable (requires_grad=True) - Respect exception rules
When unfreezing (requires_grad=True): - Unfreeze specified parameters - Keep all others trainable
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
LightningModule
|
The model whose parameters to modify. |
required |
requires_grad
|
bool
|
Whether to allow gradients (unfreeze) or not (freeze). |
required |
Source code in src/lighter/callbacks/freezer.py
on_train_batch_start(trainer, pl_module, batch, batch_idx)
Called at the start of each training batch to freeze or unfreeze model parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer
|
Trainer
|
The trainer instance. |
required |
pl_module
|
LightningModule
|
The LightningModule instance. |
required |
batch
|
Any
|
The current batch. |
required |
batch_idx
|
int
|
The index of the batch. |
required |