Skip to content

Commit

Permalink
separate the output parameter out from the base parameter which handl…
Browse files Browse the repository at this point in the history
…es the component trace, and make the backward engine with only eval mode and without output a parameter
  • Loading branch information
liyin2015 committed Dec 21, 2024
1 parent ac9dfe6 commit fadcf35
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 109 deletions.
27 changes: 21 additions & 6 deletions adalflow/adalflow/core/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
from adalflow.core.base_data_class import DataClass


from adalflow.optim.parameter import Parameter, GradientContext, Gradient
from adalflow.optim.parameter import (
Parameter,
GradientContext,
Gradient,
OutputParameter,
)
from adalflow.optim.types import ParameterType

from adalflow.core.prompt_builder import Prompt
Expand Down Expand Up @@ -523,7 +528,7 @@ def forward(
if output and not output.error
else f"Error: {output.error}, raw_response: {output.raw_response}"
)
response: Parameter = Parameter(
response: Parameter = OutputParameter(
data=param_data,
name=self.name + "_output",
role_desc=f"Output from (llm) {self.name}",
Expand Down Expand Up @@ -740,6 +745,11 @@ def _backward_through_one_predecessor(
gradient_output: GeneratorOutput = backward_engine(
prompt_kwargs=backward_engine_prompt_kwargs
)
if not isinstance(gradient_output, GeneratorOutput):
raise ValueError(
f"Generator: Backward Engine should return a GeneratorOutput. Got {gradient_output} instead."
)

# USE this to trace each node's input and output, all nodes can be visualized
log.info(
f"Generator Backward Engine Prompt: {backward_engine.get_prompt( **backward_engine_prompt_kwargs)}"
Expand All @@ -748,9 +758,6 @@ def _backward_through_one_predecessor(
gradient_output.data
or backward_engine.failure_message_to_optimizer(gradient_output)
)
log.info(
f"Generator Gradient value: {gradient_value}, raw response: {gradient_output.raw_response}"
)
# TODO: make it a debug feature
var_gradient = Gradient(
data=gradient_value,
Expand Down Expand Up @@ -947,7 +954,11 @@ class BackwardEngine(Generator): # it is a generator with defaule template

__doc__ = """The backward engine is a Generator with a default template for the backward pass.
If you want to customize the template, you can create your own backward engine"""
If you want to customize the template, you can create your own backward engine.
Yet, we will forever keep the training mode to False for the backward engine.
This is achieved by making forward the same as call.
"""

def __init__(self, **kwargs):
if kwargs is None:
Expand All @@ -965,6 +976,10 @@ def call(self, **kwargs) -> GeneratorOutputType:
raise ValueError(f"Error in the backward engine: {output.error}")
return output

def forward(self, **kwargs):
r"""Forward pass for the backward engine."""
return self.call(**kwargs)

@staticmethod
def failure_message_to_optimizer(
gradient_response: GeneratorOutput,
Expand Down
4 changes: 2 additions & 2 deletions adalflow/adalflow/optim/grad_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def forward(self, *args, **kwargs) -> "Parameter":
3. Return the parameter object.
"""

from adalflow.optim.parameter import Parameter
from adalflow.optim.parameter import Parameter, OutputParameter

log.debug(
f"Forwarding through {self.name} with args: {args} and kwargs: {kwargs}"
Expand Down Expand Up @@ -122,7 +122,7 @@ def forward(self, *args, **kwargs) -> "Parameter":

# 4. Create a Parameter object to trace the forward pass
input_args.update(kwargs)
response = Parameter(
response = OutputParameter(
data=call_response,
name=self.name + "_output",
role_desc=self.name + " response",
Expand Down
3 changes: 3 additions & 0 deletions adalflow/adalflow/optim/loss_component.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Base class for Autograd Components that can be called and backpropagated through."""

from typing import TYPE_CHECKING
import uuid

if TYPE_CHECKING:
from adalflow.core.generator import BackwardEngine
Expand All @@ -27,10 +28,12 @@ class LossComponent(Component):
"""
backward_engine: "BackwardEngine"
_component_type = "loss"
id = None

def __init__(self, *args, **kwargs):
super().__init__()
super().__setattr__("backward_engine", None)
super().__setattr__("id", str(uuid.uuid4()))

def __call__(self, *args, **kwargs):
return self.forward(*args, **kwargs)
Expand Down
Loading

0 comments on commit fadcf35

Please sign in to comment.