Coder Social home page Coder Social logo

Comments (10)

dvd42 avatar dvd42 commented on August 15, 2024 4

I should say, I am more than happy to make a PR for this, if in fact, threaded execution is not possible with retries, since this is very important for us at the moment.

Will also be happy to work on Async support which would simplify a lot of this. Let me know if and how I can help :)

from dspy.

dvd42 avatar dvd42 commented on August 15, 2024 2

It seems that there is a threading lock in the backrack_handler wrapper . Is it possible to build our own backtrack_handler and use that? How should we go about this? Thanks in advance!

from dspy.

theta-lin avatar theta-lin commented on August 15, 2024 1

I am also looking forward to see multi-threaded assertions.

I see. But doesn't the settings singleton handle threads correctly?

thread_id = threading.get_ident()

Couldn't this be leveraged to get the relevant config for the current thread in the backtrack_handler?

I think this is a good observation as I also think the only thing making assertions not supporting threads is the sharing of global config (which is not really "config", but rather storing the module call traces and backtracking information globally). However, I also note that each thread does get its copy of the config:

@property
def config(self):
thread_id = threading.get_ident()
if thread_id not in self.stack_by_thread:
self.stack_by_thread[thread_id] = [self.main_stack[-1].copy()]
return self.stack_by_thread[thread_id][-1]
def __getattr__(self, name):
if hasattr(self.config, name):
return getattr(self.config, name)
if name in self.config:
return self.config[name]
super().__getattr__(name)

This implementation is not without its problems though, as pointed out in this part of the evaluator code also leveraging threads:

# NOTE: TODO: Won't work if threads create threads!
thread_stacks = dspy.settings.stack_by_thread
creating_new_thread = threading.get_ident() not in thread_stacks
if creating_new_thread:
thread_stacks[threading.get_ident()] = list(dspy.settings.main_stack)

You can see that as the thread-local config only copies from the main thread, it won't work if threads spawn more threads. Therefore, I can see the problem of maybe using multi-threaded module with assertions inside multi-threaded evaluator.

However, it appears that settings_v2.py is created to address this issue? It would be better for the developers to offer more insight here.

What is the most problematic at hand is that I guess even if we can create our own custom_backtrack_handler, then how are we supposed to build our own thread-safe version of dsp.settings.trace? I mean the access of dsp.settings.trace might be thread-safe, but having multiple threads might mess up the traces.

from dspy.

dvd42 avatar dvd42 commented on August 15, 2024

Any updates? I am still facing this problem, which effectively prevents making many requests in parallel with the retry mechanism. Since the library does not support async requests yet, threads are the main way to achieve this concurrency. Any help would be much appreciated.

from dspy.

arnavsinghvi11 avatar arnavsinghvi11 commented on August 15, 2024

Hi @dvd42 , would love to see a PR for this! Currently, DSPy assertions require the lock to ensure the Retries are mapped to the correct requests (and were breaking otherwise) and have not been tested with async. activate_assertions uses the default backtrack_handler, but you can configure a custom backtrack_handler logic compatible with assertions.py and pass it to the assert_transform_module:

Example:

from dspy.primitives.assertions import assert_transform_module#, backtrack_handler

# define custom_backtrack_handler

program_with_assertions = assert_transform_module(ProgramWithAssertions(), custom_backtrack_handler)

from dspy.

dvd42 avatar dvd42 commented on August 15, 2024

I see. But doesn't the settings singleton handle threads correctly?

thread_id = threading.get_ident()

Couldn't this be leveraged to get the relevant config for the current thread in the backtrack_handler?

Let me know if I am on the right track here and I'll create a PR :)

from dspy.

RahulBalan avatar RahulBalan commented on August 15, 2024

@arnavsinghvi11 Am also facing this problem.
Can you show an example of custom_backtrack_handler. What all things should be considered in a custom_backtrack_handler

from dspy.

chiragshah285 avatar chiragshah285 commented on August 15, 2024

following looking to do the same

from dspy.

enchyisle avatar enchyisle commented on August 15, 2024

Facing the same issue here. Hope that multi-thread support would be available soon so that we won't need to do work-arounds.

from dspy.

dvd42 avatar dvd42 commented on August 15, 2024

This seems like a solution.

def funcion_that_your_threads_execute()
  model = your_llm()
  dspy.configure(
      lm=model,
      trace=[],
      assert_failures=0,
      suggest_failures=0,
      backtrack_to=None,
      predictor_feedbacks={},
      backtrack_to_args={},
  )

If I replace the lock with nullcontext (dspy.settings.lock = nullcontext()). This seems to work for me, resulting in consistent outputs with multithreading.

@arnavsinghvi11 @okhat could you confirm if I am missing something?

If this is correct, I could make a PR updating/creating an example wherever you point me to with this use case.

from dspy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.