-
Notifications
You must be signed in to change notification settings - Fork 21
feat: add price estimation #400
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
Changes from 10 commits
efefbbb
2ee9bbc
d35e770
5eaea00
f73533d
dea7fdf
a422b53
ca42382
3c11c5f
faffc82
dc923a4
9897bc6
aa0d8e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,8 @@ | |
| FinetuneLRScheduler, | ||
| FinetuneRequest, | ||
| FinetuneResponse, | ||
| FinetunePriceEstimationRequest, | ||
| FinetunePriceEstimationResponse, | ||
| FinetuneTrainingLimits, | ||
| FullTrainingType, | ||
| LinearLRScheduler, | ||
|
|
@@ -31,7 +33,7 @@ | |
| TrainingMethodSFT, | ||
| TrainingType, | ||
| ) | ||
| from together.types.finetune import DownloadCheckpointType | ||
| from together.types.finetune import DownloadCheckpointType, TrainingMethod | ||
| from together.utils import log_warn_once, normalize_key | ||
|
|
||
|
|
||
|
|
@@ -42,6 +44,12 @@ | |
| TrainingMethodSFT().method, | ||
| TrainingMethodDPO().method, | ||
| } | ||
| _WARNING_MESSAGE_INSUFFICIENT_FUNDS = ( | ||
| "The estimated price of the fine-tuning job is {} which is significantly " | ||
| "greater than your current credit limit and balance combined. " | ||
| "It will likely get cancelled due to insufficient funds. " | ||
| "Proceed at your own risk." | ||
| ) | ||
|
|
||
|
|
||
| def create_finetune_request( | ||
|
|
@@ -473,12 +481,39 @@ def create( | |
| hf_api_token=hf_api_token, | ||
| hf_output_repo_name=hf_output_repo_name, | ||
| ) | ||
| if from_checkpoint is None and from_hf_model is None: | ||
| price_estimation_result = self.estimate_price( | ||
| training_file=training_file, | ||
| validation_file=validation_file, | ||
| model=model_name, | ||
| n_epochs=finetune_request.n_epochs, | ||
| n_evals=finetune_request.n_evals, | ||
| training_type="lora" if lora else "full", | ||
| training_method=training_method, | ||
| ) | ||
| else: | ||
| # unsupported case | ||
| price_estimation_result = FinetunePriceEstimationResponse( | ||
| estimated_total_price=0.0, | ||
| allowed_to_proceed=True, | ||
| estimated_train_token_count=0, | ||
| estimated_eval_token_count=0, | ||
| user_limit=0.0, | ||
| ) | ||
|
|
||
| if verbose: | ||
| rprint( | ||
| "Submitting a fine-tuning job with the following parameters:", | ||
| finetune_request, | ||
| ) | ||
| if not price_estimation_result.allowed_to_proceed: | ||
| rprint( | ||
| "[red]" | ||
| + _WARNING_MESSAGE_INSUFFICIENT_FUNDS.format( | ||
| price_estimation_result.estimated_total_price | ||
| ) | ||
| + "[/red]", | ||
| ) | ||
| parameter_payload = finetune_request.model_dump(exclude_none=True) | ||
|
|
||
| response, _, _ = requestor.request( | ||
|
|
@@ -493,6 +528,75 @@ def create( | |
|
|
||
| return FinetuneResponse(**response.data) | ||
|
|
||
| def estimate_price( | ||
| self, | ||
| *, | ||
| training_file: str, | ||
| model: str, | ||
| validation_file: str | None = None, | ||
| n_epochs: int | None = 1, | ||
| n_evals: int | None = 0, | ||
| training_type: str = "lora", | ||
| training_method: str = "sft", | ||
| ) -> FinetunePriceEstimationResponse: | ||
| """ | ||
| Estimates the price of a fine-tuning job | ||
|
|
||
| Args: | ||
| request (FinetunePriceEstimationRequest): Request object containing the parameters for the price estimation. | ||
|
||
|
|
||
| Returns: | ||
| FinetunePriceEstimationResponse: Object containing the estimated price. | ||
| """ | ||
| training_type_cls: TrainingType | ||
| training_method_cls: TrainingMethod | ||
|
|
||
| if training_method == "sft": | ||
| training_method_cls = TrainingMethodSFT(method="sft") | ||
| elif training_method == "dpo": | ||
| training_method_cls = TrainingMethodDPO(method="dpo") | ||
| else: | ||
| raise ValueError(f"Unknown training method: {training_method}") | ||
|
|
||
| if training_type.lower() == "lora": | ||
| # parameters of lora are unused in price estimation | ||
| # but we need to set them to valid values | ||
| training_type_cls = LoRATrainingType( | ||
| type="Lora", | ||
| lora_r=16, | ||
newokaerinasai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| lora_alpha=16, | ||
| lora_dropout=0.0, | ||
| lora_trainable_modules="all-linear", | ||
| ) | ||
| elif training_type.lower() == "full": | ||
| training_type_cls = FullTrainingType(type="Full") | ||
| else: | ||
| raise ValueError(f"Unknown training type: {training_type}") | ||
|
|
||
| request = FinetunePriceEstimationRequest( | ||
| training_file=training_file, | ||
| validation_file=validation_file, | ||
| model=model, | ||
| n_epochs=n_epochs, | ||
| n_evals=n_evals, | ||
| training_type=training_type_cls, | ||
| training_method=training_method_cls, | ||
| ) | ||
| parameter_payload = request.model_dump(exclude_none=True) | ||
| requestor = api_requestor.APIRequestor( | ||
| client=self._client, | ||
| ) | ||
|
|
||
| response, _, _ = requestor.request( | ||
| options=TogetherRequest( | ||
| method="POST", url="fine-tunes/estimate-price", params=parameter_payload | ||
| ), | ||
| stream=False, | ||
| ) | ||
| assert isinstance(response, TogetherResponse) | ||
|
|
||
| return FinetunePriceEstimationResponse(**response.data) | ||
|
|
||
| def list(self) -> FinetuneList: | ||
| """ | ||
| Lists fine-tune job history | ||
|
|
@@ -941,11 +1045,39 @@ async def create( | |
| hf_output_repo_name=hf_output_repo_name, | ||
| ) | ||
|
|
||
| if from_checkpoint is None and from_hf_model is None: | ||
| price_estimation_result = await self.estimate_price( | ||
| training_file=training_file, | ||
| validation_file=validation_file, | ||
| model=model_name, | ||
| n_epochs=finetune_request.n_epochs, | ||
| n_evals=finetune_request.n_evals, | ||
| training_type="lora" if lora else "full", | ||
| training_method=training_method, | ||
| ) | ||
| else: | ||
| # unsupported case | ||
| price_estimation_result = FinetunePriceEstimationResponse( | ||
| estimated_total_price=0.0, | ||
| allowed_to_proceed=True, | ||
| estimated_train_token_count=0, | ||
| estimated_eval_token_count=0, | ||
| user_limit=0.0, | ||
| ) | ||
|
||
|
|
||
| if verbose: | ||
| rprint( | ||
| "Submitting a fine-tuning job with the following parameters:", | ||
| finetune_request, | ||
| ) | ||
| if not price_estimation_result.allowed_to_proceed: | ||
| rprint( | ||
| "[red]" | ||
| + _WARNING_MESSAGE_INSUFFICIENT_FUNDS.format( | ||
| price_estimation_result.estimated_total_price | ||
| ) | ||
| + "[/red]", | ||
| ) | ||
| parameter_payload = finetune_request.model_dump(exclude_none=True) | ||
|
|
||
| response, _, _ = await requestor.arequest( | ||
|
|
@@ -961,6 +1093,75 @@ async def create( | |
|
|
||
| return FinetuneResponse(**response.data) | ||
|
|
||
| async def estimate_price( | ||
| self, | ||
| *, | ||
| training_file: str, | ||
| model: str, | ||
| validation_file: str | None = None, | ||
| n_epochs: int | None = 1, | ||
| n_evals: int | None = 0, | ||
| training_type: str = "lora", | ||
| training_method: str = "sft", | ||
| ) -> FinetunePriceEstimationResponse: | ||
| """ | ||
| Estimates the price of a fine-tuning job | ||
|
|
||
| Args: | ||
| request (FinetunePriceEstimationRequest): Request object containing the parameters for the price estimation. | ||
|
||
|
|
||
| Returns: | ||
| FinetunePriceEstimationResponse: Object containing the estimated price. | ||
| """ | ||
| training_type_cls: TrainingType | ||
| training_method_cls: TrainingMethod | ||
|
|
||
| if training_method == "sft": | ||
| training_method_cls = TrainingMethodSFT(method="sft") | ||
| elif training_method == "dpo": | ||
| training_method_cls = TrainingMethodDPO(method="dpo") | ||
| else: | ||
| raise ValueError(f"Unknown training method: {training_method}") | ||
|
|
||
| if training_type.lower() == "lora": | ||
| # parameters of lora are unused in price estimation | ||
| # but we need to set them to valid values | ||
| training_type_cls = LoRATrainingType( | ||
| type="Lora", | ||
| lora_r=16, | ||
| lora_alpha=16, | ||
| lora_dropout=0.0, | ||
| lora_trainable_modules="all-linear", | ||
| ) | ||
| elif training_type.lower() == "full": | ||
| training_type_cls = FullTrainingType(type="Full") | ||
| else: | ||
| raise ValueError(f"Unknown training type: {training_type}") | ||
|
|
||
| request = FinetunePriceEstimationRequest( | ||
| training_file=training_file, | ||
| validation_file=validation_file, | ||
| model=model, | ||
| n_epochs=n_epochs, | ||
| n_evals=n_evals, | ||
| training_type=training_type_cls, | ||
| training_method=training_method_cls, | ||
| ) | ||
| parameter_payload = request.model_dump(exclude_none=True) | ||
| requestor = api_requestor.APIRequestor( | ||
| client=self._client, | ||
| ) | ||
|
|
||
| response, _, _ = await requestor.arequest( | ||
| options=TogetherRequest( | ||
| method="POST", url="fine-tunes/estimate-price", params=parameter_payload | ||
| ), | ||
| stream=False, | ||
| ) | ||
| assert isinstance(response, TogetherResponse) | ||
|
|
||
| return FinetunePriceEstimationResponse(**response.data) | ||
|
|
||
| async def list(self) -> FinetuneList: | ||
| """ | ||
| Async method to list fine-tune job history | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.