Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python][dspy] instrument module #134

Closed
Tracked by #109
mikeldking opened this issue Jan 24, 2024 · 1 comment · Fixed by #158
Closed
Tracked by #109

[python][dspy] instrument module #134

mikeldking opened this issue Jan 24, 2024 · 1 comment · Fixed by #158
Assignees
Labels
enhancement New feature or request language: python Related to Python integration

Comments

@mikeldking
Copy link
Contributor

mikeldking commented Jan 24, 2024

Instrument module so that the forward passes are captured

Copy link

dosubot bot commented Jan 24, 2024

🤖

To instrument a module in dspy so that the forward passes are captured, you can use the DSPyInstrumentor class from the openinference.instrumentation.dspy module. This class provides the instrument() method which you can call to instrument your dspy module.

Here is a basic example of how you can use it:

from openinference.instrumentation.dspy import DSPyInstrumentor

# ...

DSPyInstrumentor().instrument()

In the provided code, the DSPyInstrumentor().instrument() line is responsible for instrumenting the dspy module. After this line, any forward passes in the dspy module will be captured and traced.

The DSPyInstrumentor().instrument() method in the OpenInference codebase is used to instrument or add tracing to the DSPy (Data Science Python) code. This is done by wrapping certain function calls with wrappers that start and end spans, which are units of work within a trace.

The method does the following:

  1. It first checks if a tracer_provider is provided in the arguments. If not, it gets the default tracer_provider using trace_api.get_tracer_provider(). A tracer is then obtained using this tracer_provider.

  2. It instruments the basic_request method of all subclasses of the LM (Language Model) class from the dsp.modules.lm module. This is done by wrapping the basic_request method with the _LMBasicRequestWrapper which starts a span, calls the original method, and then ends the span.

  3. It also instruments the forward method of the Predict class from the dspy module. This is done by wrapping the forward method with the _PredictForwardWrapper which also starts a span, calls the original method, and then ends the span.

The _LMBasicRequestWrapper and _PredictForwardWrapper classes are defined in the same file. They both inherit from the _WithTracer class, which provides them with a tracer object. These wrappers are used to start a span before the wrapped function is called and end the span after the function call. They also handle exceptions by recording them in the span and re-raising them. They also set certain attributes in the span such as the input and output values and their MIME types.

You can find more details in the OpenInference repository.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

@github-project-automation github-project-automation bot moved this to 📘 Todo in phoenix Jan 24, 2024
@dosubot dosubot bot added enhancement New feature or request language: python Related to Python integration labels Jan 24, 2024
@mikeldking mikeldking moved this from 📘 Todo to 👨‍💻 In progress in phoenix Jan 25, 2024
@axiomofjoy axiomofjoy linked a pull request Jan 30, 2024 that will close this issue
@github-project-automation github-project-automation bot moved this from 👨‍💻 In progress to ✅ Done in phoenix Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request language: python Related to Python integration
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants