file_writer
Callback for persisting prediction artifacts to the filesystem.
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 | |
WriterRegistry
A registry for writer functions, allowing them to be registered by name and retrieved later.
Source code in src/lighter/callbacks/file_writer.py
get(name)
Get a writer from the registry by its registered name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the writer to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
The writer function associated with the given name. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no writer with the given name is registered. |
Source code in src/lighter/callbacks/file_writer.py
register(name)
Register a new writer function in this registry as a decorator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The unique name to register the writer under. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
A decorator that registers the decorated function. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a writer with the given name is already registered. |
Source code in src/lighter/callbacks/file_writer.py
write_image_2d(path, tensor, *, suffix='.png')
Write a 2D tensor as an image using PNG encoding.
Source code in src/lighter/callbacks/file_writer.py
write_image_3d(path, tensor, *, suffix='.png')
Write a 3D tensor as a 2D image by stacking slices vertically.
Source code in src/lighter/callbacks/file_writer.py
write_tensor(path, tensor, *, suffix='.pt')
Serialise a tensor to disk using :func:torch.save.
Source code in src/lighter/callbacks/file_writer.py
write_text(path, value, *, suffix='.txt', encoding='utf-8')
Write the string representation of value to disk.