Implementing Custom Modules Via Subclassing
Implementing Custom Modules Via Subclassing
This tutorial is for more advanced users, it will cover how to create custom modules/programs via subclassing.
In this tutorial, we will cover the following themes:
- The
Moduleclass - The
add_variable()method - Trainable and non-trainable variables
- The
compute_output_spec()andbuild()method - The training argument in
call() - Making sure your module/program can be serialized
One of the main abstraction of Synalinks is the Module class.
A Module encapsulate both a state (the module's variables) and
a transformation from inputs to outputs (the call() method).
For this tutorial, we are going to make a simple neuro-symbolic component
called BacktrackingOfThought. This component is an adaptation of the
famous backtracking algorithm, used a lot in symbolic planning/reasoning,
combined with chain of thought, nowadays most used technique to enhance
the LMs predicitons.
The principle is straitforward, the component will have to "think" then we will critique at runtime the thinking and aggregate it to the current chain of thinking only if it is above the given threshold. This mechanism will allow the system to discard bad thinking to resume at the previsous step. Additionally we will add a stop condition.
graph TD
A[Input] --> B[Think]
B --> C[Critique]
C --> D{Score >= Threshold?}
D -->|Yes| E[Add to Chain]
D -->|No| B
E --> F{Stop Condition?}
F -->|No| B
F -->|Yes| G[Output]
This algorithm a simplified version of the popular TreeOfThought that
instead of being a tree strucutre, is only a sequential chain of thinking.
Module Structure
A custom module inherits from synalinks.Module and implements key methods:
class MyCustomModule(synalinks.Module):
def __init__(self, language_model=None, ...):
super().__init__(name=name, description=description, trainable=trainable)
# Initialize your generators and components
self.generator = synalinks.Generator(...)
async def call(self, inputs, training=False):
# Define the forward pass logic
return await self.generator(inputs, training=training)
async def compute_output_spec(self, inputs, training=False):
# Define how to compute output specification
return await self.generator(inputs)
def get_config(self):
# Return configuration for serialization
return {"language_model": synalinks.saving.serialize_synalinks_object(...)}
@classmethod
def from_config(cls, config):
# Reconstruct the module from config
return cls(...)
The __init__() function
When implementing modules that use a Generator, you want to externalize
the generator's parameters (prompt_template, instructions, examples,
use_inputs_schema, use_outputs_schema) to give maximum flexibility to
your module when possible.
How to know when using a Variable?
As a rule of thumb, the variables should be anything that evolve over time during inference/training. These variables could be updated by the module itself, or by the optimizer if you have an optimizer designed for that.
The call() function
The call() function is the core of the Module class. It defines the
computation performed at every call of the module.
The compute_output_spec() function
The compute_output_spec() function is responsible for defining the output
data model of the module/program. Its inputs is always a SymbolicDataModel,
a placeholder that only contains a JSON schema that serve as data specification.
Serialization and Deserialization
To ensure that your module can be saved and loaded correctly, you need to
implement serialization and deserialization methods (get_config() and
from_config()).
Key Takeaways
- Module Class: Encapsulates both state (variables) and transformation
logic (
call()method). - Initialization and Variables: Externalize generator parameters for
flexibility. Use
add_variablesfor state management. - Call Function: Defines the core computation of the module.
- Output Specification:
compute_output_spec()defines the output data model. - Serialization: Implement
get_config()andfrom_config()for saving and loading.
Program Visualization
API References
BacktrackingOfThought
Bases: Module
A Backtracking of Thought algorithm.
This component combines the backtracking algorithm with chain of thought to enhance LMs predictions through iterative self-critique.
Source code in examples/9_custom_modules.py
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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |