container#
Container component for composing multiple components, such as Sequential and ComponentList.
This design draws inspiration from PyTorch’s modular container patterns, including nn.Sequential and nn.ModuleList. The Container component allows for grouping several components into one, enabling flexible and reusable model architectures.
Design Motivation:#
This implementation follows the same principles as PyTorch’s component-based design, encouraging modularity, reusability, and extensibility. The Container component provides an easy way to manage multiple layers or other components, while ensuring that their parameters are properly registered and updated during training.
Credits:#
The design of this component takes inspiration from the PyTorch project (https://pytorch.org). PyTorch is an open-source deep learning framework, licensed under a BSD-style license. Although this code is not part of the official PyTorch library, it mirrors the same design principles.
For more details on PyTorch’s licensing, refer to: pytorch/pytorch
Usage Example:#
- class MyModule(nn.Module):
- def __init__(self):
super().__init__()
- self.model = nn.Sequential(
nn.Conv2d(1,20,5), nn.ReLU(), nn.Conv2d(20,64,5), nn.ReLU()
)
self.linears = nn.ModuleList([nn.Linear(10, 10) for i in range(10)])
- def forward(self, x):
# ModuleList can act as an iterable, or be indexed using ints for i, l in enumerate(self.linears):
x = self.linears[i // 2](x) + l(x)
return x
Classes
|
Holds subcomponents in a list. |
A sequential container. |
- class Sequential(*args: Component)[source]#
- class Sequential(arg: OrderedDict[str, Component])
Bases:
Component
A sequential container.
Adapted from PyTorch’s
nn.Sequential
.Components will be added to it in the order they are passed to the constructor. Alternatively, an
OrderedDict
of components can be passed in. It “chains” outputs of the previous component to the input of the next component sequentially. Output of the previous component is input to the next component as positional argument.Benefits of using Sequential: 1. Convenient for data pipeline that often consists of multiple components. This allow users to encapsulate the pipeline in a single component. Examples:
Without Sequential:
class AddAB(Component): def call(self, a: int, b: int) -> int: return a + b class MultiplyByTwo(Component): def call(self, input: int) -> int: return input * 2 class DivideByThree(Component): def call(self, input: int) -> int: return input / 3 # Manually chaining the components add_a_b = AddAB() multiply_by_two = MultiplyByTwo() divide_by_three = DivideByThree() result = divide_by_three(multiply_by_two(add_a_b(2, 3)))
With Sequential:
seq = Sequential(AddAB(), MultiplyByTwo(), DivideByThree()) result = seq(2, 3)
Note
Only the first component can receive arbitrary positional and keyword arguments. The rest of the components should have a single positional argument as input and have it to be exactly the same type as the output of the previous component.
2. Apply a transformation or operation (like training, evaluation, or serialization) to the Sequential object, it automatically applies that operation to each component it contains. This can be useful for In-context learning training.
Examples:
- Use positional arguments:
>>> seq = Sequential(component1, component2)
- Add components:
>>> seq.append(component4)
- Get a component:
>>> seq[0]
- Delete a component:
>>> del seq[0]
- Iterate over components:
>>> for component in seq: >>> print(component)
- Add two Sequentials:
>>> seq1 = Sequential(component1, component2) >>> seq2 = Sequential(component3, component4) >>> seq3 = seq1 + seq2
- Use OrderedDict:
>>> seq = Sequential(OrderedDict({"component1": component1, "component2": component2}))
- Index OrderDict:
>>> seq = Sequential(OrderedDict({"component1": component1, "component2": component2})) >>> seq["component1"] # or >>> seq[0]
- Call with a single argument as input:
>>> seq = Sequential(component1, component2) >>> result = seq.call(2)
Call with multiple arguments as input: >>> seq = Sequential(component1, component2) >>> result = seq.call(2, 3)
- async acall(input: Any) object [source]#
- async acall(*args: Any, **kwargs: Any) object
When you for loop or multiple await calls inside each component, use acall method can potentially speed up the execution.
- append(component: Component) Sequential [source]#
Appends a component to the end of the Sequential.
- insert(idx: int, component: Component) None [source]#
Inserts a component at a given index in the Sequential.
- extend(components: Iterable[Component]) Sequential [source]#
Extends the Sequential with components from an iterable.
- class ComponentList(components: Iterable[Component] | None = None)[source]#
Bases:
Component
Holds subcomponents in a list.
adalflow.core.ComponentList
can be indexed like a regular Python list, but the components it holds are properly registered, and will be visible by alladalflow.core.Component
methods.- Parameters:
components (iterable, optional) – an iterable of components to add
Examples:
# Example of how to use ComponentList class MyComponents(Component): def __init__(self): super().__init__() self.llms = ComponentList([adal.Generator() for i in range(10)]) def forward(self, x): for layer in self.layers: x = layer(x) return x
- append(component: Component) ComponentList [source]#
Append a component to the list.
- extend(components: Iterable[Component]) ComponentList [source]#
Extend the list by appending multiple components.