dspy icon indicating copy to clipboard operation
dspy copied to clipboard

Error loading program compiled using the Ensemble optimizer

Open drawal1 opened this issue 10 months ago • 4 comments

The app crashes in below code with a keyerror: File "/home/drawal/miniconda3/envs/xray/lib/python3.11/site-packages/dspy/primitives/module.py", line 75, in load_state param.load_state(state[name])

This is because the load function expects the top level keys in the saved json file to look like this: "predict_func": { But the key entry in the json file looks like this instead: "programs[1].predict_func": {

Deserializing a compiled ensemble program is not implemented?

drawal1 avatar Apr 04 '24 23:04 drawal1

Hi @drawal1 , could you share your program and stack trace? I suspect this is because dspy.Ensemble returns a list of DSPy programs and that's how they got saved in the serialization, which can be resolved by looping over that list and saving each one but we can also add a Ensemble-specific function to handle that if it makes sense.

arnavsinghvi11 avatar Apr 09 '24 22:04 arnavsinghvi11

@arnavsinghvi11 - I see the problem now. Its probably same time to fix as creating a program (my program is too complex).

Revised thinking:

  1. Make reduce_fn, size, and deterministic - members of the EnsembledProgram class
  2. Override save and load to handle loading an array of programs as well as the other member variables

Old thinking: Thinking I can implement load/save as static methods in the EnsembledProgram class with some minor refactoring.

  1. Make the reduce_fn a property of the EnsembledProgram class instead of the Ensemble class
  2. save() would take a filename as input. It would save all the properties of the EnsembledProgram class
  3. load() would take a filename and deserialize and return an instance of the EnsembledProgram class

Thoughts?

drawal1 avatar Apr 12 '24 18:04 drawal1

@arnavsinghvi11 - I did what you suggested and used loops to load/save the candidate programs. But I noticed that the json for candidate program at index 0 actually had ALL the other programs in it. So really, it should be possible to reconstruct ALL the candidate programs from the information in just that 1 file. What is your Discord id? Would like to discuss 1-1

drawal1 avatar Apr 16 '24 14:04 drawal1

@arnavsinghvi11 - I have submitted PR #843 for this issue. Ready for review

I have punted on the "json for candidate program at index 0 actually had ALL the other programs in it" for now. We can revisit that as an optimization at some later date

drawal1 avatar Apr 16 '24 23:04 drawal1