Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Anyscale working

Isaac Miller62b1f8d3729c
diff --git a/dspy/adapters/base.py b/dspy/adapters/base.py
index 3467ca31f..ddb0ee3e8 100644
--- a/dspy/adapters/base.py
+++ b/dspy/adapters/base.py
@@ -10,5 +10,5 @@ class Adapter:
value = self.parse(signature, output, _parse_values=_parse_values)
assert set(value.keys()) == set(signature.output_fields.keys()), f"Expected {signature.output_fields.keys()} but got {value.keys()}"
values.append(value)
-
+
return values
diff --git a/dspy/clients/anyscale.py b/dspy/clients/anyscale.py
index 5844abe55..a10a0453b 100644
--- a/dspy/clients/anyscale.py
+++ b/dspy/clients/anyscale.py
@@ -67,8 +67,9 @@ class FinetuneJobAnyScale(FinetuneJob):
def __init__(self, *args, **kwargs):
self.job_id = None
+ self.model_names = None
super().__init__(*args, **kwargs)
-
+
def cancel(self):
"""Cancel the finetune job."""
raise NotImplementedError("Method `cancel` is not implemented.")
@@ -84,72 +85,75 @@ class FinetuneJobAnyScale(FinetuneJob):
def finetune_anyscale(
job: FinetuneJobAnyScale,
model: str,
+ method: TrainingMethod,
train_data: List[Dict[str, Any]],
+ eval_data: List[Dict[str, Any]] = None,
train_kwargs: Optional[Dict[str, Any]]=None,
- ) -> Union[str, Exception]:
+ ) -> List[str]:
"""Start the finetune job."""
- try:
- train_kwargs = train_kwargs or {}
- assert "model" not in train_kwargs, "Model should not be in the train_kwargs"
- train_kwargs["model"] = model
- train_method = TrainingMethod.SFT # Note: This could be an argument
-
- logger.info("[Finetune] Starting training process...")
- if train_method not in TRAINING_METHODS_ANYSCALE:
- raise NotImplementedError(f"AnyScale can only support {TRAINING_METHODS_ANYSCALE} for the time being")
-
- logger.info("[Finetune] Retrieving the `serve_config_path` if provided, otherwise defaulting to `serve_1B.yaml`...")
- serve_config_path = train_kwargs.pop("serve_config_path", "serve_1B.yaml")
-
- logger.info("[Finetune] Validating the dataset format...")
- if not verify_dataset(train_data):
- # TODO: Does AnyScale support text completion models?
- err = "[Finetune] Error: Unable to verify that the dataset is in the correct format."
- logger.error(err)
- raise RuntimeError(err)
-
- logger.info("[Finetune] Converting data to JSONL format...")
- data_path = save_data(train_data, provider_name=PROVIDER_ANYSCALE)
-
- logger.info("[Finetune] Submitting data to remote storage...")
- remote_train_path, _ = submit_data(train_path=data_path, eval_path=None)
- logger.info(f"[Finetune] Data submitted. Remote train path: {remote_train_path}")
-
- logger.info("[Finetune] Generating configuration files...")
- _, compute_config = generate_config_files(train_path=remote_train_path, eval_path=None, **train_kwargs)
-
- logger.info("[Finetune] Starting remote training...")
- job_id = start_remote_training(compute_config=compute_config, **train_kwargs)
- job.job_id = job_id
- logger.info(f"[Finetune] Remote training started. Job ID: {job.job_id}")
-
- logger.info("[Finetune] Waiting for training to complete...")
- wait_for_training(job.job_id)
- logger.info("[Finetune] Training completed.")
+ train_kwargs = train_kwargs or {}
+ assert "model" not in train_kwargs, "Model should not be in the train_kwargs"
+ train_kwargs_copy = train_kwargs.copy()
+ train_kwargs_copy["model"] = model
+
+ logger.info("[Finetune] Starting training process...")
+ if method not in TRAINING_METHODS_ANYSCALE:
+ raise NotImplementedError(f"AnyScale can only support {TRAINING_METHODS_ANYSCALE} for the time being")
+
+ logger.info("[Finetune] Validating the dataset format...")
+ if not verify_dataset(train_data):
+ # TODO: Does AnyScale support text completion models?
+ err = "[Finetune] Error: Unable to verify that the dataset is in the correct format."
+ logger.error(err)
+ raise RuntimeError(err)
+
+ if eval_data and not verify_dataset(eval_data):
+ err = "[Finetune] Error: Unable to verify that the eval dataset is in the correct format."
+ logger.error(err)
+ raise RuntimeError(err)
+
+ logger.info("[Finetune] Converting data to JSONL format...")
+ train_data_path = save_data(train_data, provider_name=PROVIDER_ANYSCALE)
+ eval_data_path = save_data(eval_data, provider_name=PROVIDER_ANYSCALE) if eval_data else None
+
+ logger.info("[Finetune] Submitting data to remote storage...")
+ remote_train_path, _ = submit_data(train_path=train_data_path, eval_path=eval_data_path)
+ logger.info(f"[Finetune] Data submitted. Remote train path: {remote_train_path}")
+
+ logger.info("[Finetune] Generating configuration files...")
+ _, compute_config = generate_config_files(train_path=remote_train_path, eval_path=None, **train_kwargs_copy)
+
+ logger.info("[Finetune] Starting remote training...")
+ job_id = start_remote_training(compute_config=compute_config, **train_kwargs_copy)
+ job.job_id = job_id
+ logger.info(f"[Finetune] Remote training started. Job ID: {job_id}")
- logger.info("[Finetune] Retrieving model information...")
- model_info = get_model_info(job.job_id)
- logger.info(f"[Finetune] Model info retrieved: {model_info}")
+ logger.info("[Finetune] Waiting for training to complete...")
+ wait_for_training(job.job_id)
+ logger.info("[Finetune] Training completed.")
- storage_uri = model_info["storage_uri"]
- logger.info(f"[Finetune] Copying LoRA weights from {storage_uri}...")
- model_names, lora_dynamic_path = copy_lora_weights(storage_uri, model_info, job.job_id)
- logger.info(f"[Finetune] LoRA weights copied. Model names: {model_names}")
+ logger.info("[Finetune] Retrieving model information...")
+ model_info = get_model_info(job.job_id)
+ logger.info(f"[Finetune] Model info retrieved: {model_info}")
- logger.info("[Finetune] Setting result in future object...")
- last_model_checkpoint = model_names[-1] # TODO: Is this correct?
- logger.info("[Finetune] Training process completed successfully.")
+ storage_uri = model_info["storage_uri"]
+ logger.info(f"[Finetune] Copying LoRA weights from {storage_uri}...")
+ model_names, lora_dynamic_path = copy_lora_weights(storage_uri, model_info, job.job_id)
+ logger.info(f"[Finetune] LoRA weights copied. Model names: {model_names}")
- # TODO: Is this the right place to call update_model_config?
- logger.info("[Finetune] Updating model config with the proper dynamic path")
- update_model_config(lora_dynamic_path, serve_config_path)
- return last_model_checkpoint
+ logger.info("[Finetune] Setting result in future object...")
+ model_step_pairs = sorted([(model_name, int(model_name.split("-")[-1])) for model_name in model_names], key=lambda x: x[1])
+ last_model_checkpoint = model_step_pairs[-1][0]
+ logger.info("[Finetune] Training process completed successfully.")
- except Exception as e:
- logger.error(f"[Finetune] Error occurred during training: {str(e)}")
- raise e
+ # TODO: Is this the right place to call update_model_config?
+ logger.info("[Finetune] Updating model config with the proper dynamic path")
+ serve_config_path = train_kwargs.pop("serve_config_path", "serve_1B.yaml")
+ update_model_config(lora_dynamic_path, serve_config_path, job_id)
+ job.model_names = model_names
+ return last_model_checkpoint
#-------------------------------------------------------------------------------
# Custom functions to support the finetune_* function
@@ -160,7 +164,7 @@ def wait_for_training(job_id):
anyscale.job.wait(id=job_id, timeout_s=18000)
-def update_model_config(lora_dynamic_path: str, serve_config_path: str):
+def update_model_config(lora_dynamic_path: str, serve_config_path: str, job_id: str):
"""Update the model config storage location with the job_id."""
with open(serve_config_path, "r") as f:
serve_config = yaml.safe_load(f)
@@ -170,7 +174,8 @@ def update_model_config(lora_dynamic_path: str, serve_config_path: str):
with open(model_config_location, "r") as f:
model_config = yaml.safe_load(f)
- model_config["lora_config"]["dynamic_lora_loading_path"] = lora_dynamic_path
+ dynamic_path_until_job_id = lora_dynamic_path.split(job_id)[0] + job_id
+ model_config["lora_config"]["dynamic_lora_loading_path"] = dynamic_path_until_job_id
with open(model_config_location, "w") as f:
yaml.safe_dump(model_config, f)
@@ -314,9 +319,9 @@ def start_remote_training(compute_config, **kwargs) -> str:
return job_id
-def wait_for_training(job):
+def wait_for_training(job_id):
logger.info("Waiting for training to complete")
- anyscale.job.wait(id=job.job_id, timeout_s=18000)
+ anyscale.job.wait(id=job_id, timeout_s=18000)
def get_model_info(job_id):
@@ -374,11 +379,4 @@ def copy_lora_weights(storage_uri, model_info, job_id):
def read_jsonl(filename):
with open(filename, "r") as f:
- return [json.loads(line) for line in f]
-
-
-# TODO: Is it true that we don't need this function anymore?
-def write_jsonl(filename, data):
- with open(filename, "w") as f:
- for item in data:
- f.write(json.dumps(item) + "\n")
+ return [json.loads(line) for line in f]
\ No newline at end of file
diff --git a/dspy/clients/finetune.py b/dspy/clients/finetune.py
index c4464fbf3..8cedf5c75 100644
--- a/dspy/clients/finetune.py
+++ b/dspy/clients/finetune.py
@@ -51,17 +51,20 @@ TRAINING_METHOD_TO_DATA_KEYS = {
TrainingMethod.Preference: ["prompt", "chosen", "rejected"],
}
-
class FinetuneJob(Future):
def __init__(self,
model: str,
train_data: List[Dict[str, Any]],
train_kwargs: Optional[Dict[str, Any]]=None,
+ provider: str = "openai",
+ method: TrainingMethod = TrainingMethod.SFT,
):
self.model = model
self.train_data = train_data
self.train_kwargs: Dict[str, Any] = train_kwargs or {}
+ self.provider = provider
+ self.method = method
super().__init__()
def get_kwargs(self):
@@ -69,10 +72,12 @@ class FinetuneJob(Future):
model=self.model,
train_data=self.train_data,
train_kwargs=self.train_kwargs,
+ provider=self.provider,
+ method=self.method,
)
def __str__(self):
- return f"FinetuningJob({self.to_dict()})"
+ return f"FinetuningJob({self.model})"
def __repr__(self):
return str(self)
diff --git a/dspy/clients/lm.py b/dspy/clients/lm.py
index e6cfcff3d..76326bc08 100644
--- a/dspy/clients/lm.py
+++ b/dspy/clients/lm.py
@@ -7,9 +7,8 @@ from typing import Any, Dict, List, Optional, Type, Union
import ujson
import uuid
-
from dspy.utils.logging import logger
-from dspy.clients.finetune import FinetuneJob
+from dspy.clients.finetune import FinetuneJob, TrainingMethod
from dspy.clients.self_hosted import (
is_self_hosted_model,
self_hosted_model_launch,
@@ -124,9 +123,12 @@ class LM:
logger.info(msg)
def finetune(self,
+ train_method: TrainingMethod,
train_data: List[Dict[str, Any]],
+ eval_data: List[Dict[str, Any]] = None,
train_kwargs: Optional[Dict[str, Any]]=None,
cache_finetune: bool = True,
+ provider: str = "openai",
) -> FinetuneJob:
"""Start model fine-tuning, if supported."""
# Fine-tuning is experimental and requires the experimental flag
@@ -135,16 +137,11 @@ class LM:
err += " Set `dspy.settings.experimental` to `True` to use it."
assert settings.experimental, err
- # Get the fine-tuning provider, if it is supported
- try:
- provider = _get_supported_finetune_provider(self.model)
- except Exception as err:
- raise err
-
# Initialize the finetune job
FinetuneJobClass = get_provider_finetune_job_class(provider=provider)
finetune_job = FinetuneJobClass(
model=self.model,
+ provider=provider,
train_data=train_data,
train_kwargs=train_kwargs,
)
@@ -282,7 +279,7 @@ def _get_supported_finetune_provider(model: str) -> Union[str, ValueError]:
if is_anyscale_model(model):
return _PROVIDER_ANYSCALE
- return ValueError(f"DSPy does not have fine-tuning support for {model}")
+ return ValueError(f"Unable {model}")
def get_provider_finetune_job_class(provider: str) -> Type[FinetuneJob]:
@@ -334,31 +331,36 @@ def execute_finetune_job(
# TODO: Perhaps we shouldn't directly cache the data
# TODO: Add DiskCache, ignore job
+
def cached_finetune(
job,
model: str,
train_data: List[Dict[str, Any]],
train_kwargs: Optional[Dict[str, Any]]=None,
+ provider: str = "openai",
+ method: TrainingMethod = TrainingMethod.SFT,
) -> Union[str, ValueError]:
return finetune(
job=job,
model=model,
+ provider=provider,
train_data=train_data,
train_kwargs=train_kwargs,
+ method=method,
)
def finetune(
job,
model: str,
+ method: TrainingMethod,
+ provider: str,
train_data: List[Dict[str, Any]],
train_kwargs: Optional[Dict[str, Any]]=None,
) -> Union[str, Exception]:
"""Fine-tune a new model based on the given model."""
# Get the fine-tuning provider
try:
- provider = _get_supported_finetune_provider(model)
-
# Get the finetune function
provider_finetune_function = get_provider_finetune_function(provider)
@@ -366,6 +368,7 @@ def finetune(
model = provider_finetune_function(
job=job,
model=model,
+ method=method,
train_data=train_data,
train_kwargs=train_kwargs,
)
diff --git a/dspy/predict/chain_of_thought.py b/dspy/predict/chain_of_thought.py
index 74656697c..b6b9cb575 100644
--- a/dspy/predict/chain_of_thought.py
+++ b/dspy/predict/chain_of_thought.py
@@ -29,7 +29,7 @@ class ChainOfThought(Module):
rationale_type = rationale_type or dspy.OutputField(prefix=prefix, desc=desc)
# Add "rationale" field to the output signature.
- if isinstance(dspy.settings.lm, dspy.LM):
+ if isinstance(dspy.settings.lm, dspy.LM) or dspy.settings.experimental:
extended_signature = signature.prepend("reasoning", rationale_type, type_=str)
else:
extended_signature = signature.prepend("rationale", rationale_type, type_=str)