15
15
"""Classes for working with language models."""
16
16
17
17
import dataclasses
18
- from typing import Any , AsyncIterator , Dict , Iterator , List , Optional , Sequence , Union
18
+ from typing import Any , AsyncIterator , Dict , Iterator , List , Literal , Optional , Sequence , Union
19
19
import warnings
20
20
21
21
from google .cloud import aiplatform
42
42
# Endpoint label/metadata key to preserve the base model ID information
43
43
_TUNING_BASE_MODEL_ID_LABEL_KEY = "google-vertex-llm-tuning-base-model-id"
44
44
45
+ _ACCELERATOR_TYPES = ["TPU" , "GPU" ]
46
+ _ACCELERATOR_TYPE_TYPE = Literal ["TPU" , "GPU" ]
47
+
45
48
46
49
def _get_model_id_from_tuning_model_id (tuning_model_id : str ) -> str :
47
50
"""Gets the base model ID for the model ID labels used the tuned models.
@@ -166,6 +169,7 @@ def tune_model(
166
169
model_display_name : Optional [str ] = None ,
167
170
tuning_evaluation_spec : Optional ["TuningEvaluationSpec" ] = None ,
168
171
default_context : Optional [str ] = None ,
172
+ accelerator_type : Optional [_ACCELERATOR_TYPE_TYPE ] = None ,
169
173
) -> "_LanguageModelTuningJob" :
170
174
"""Tunes a model based on training data.
171
175
@@ -191,6 +195,7 @@ def tune_model(
191
195
model_display_name: Custom display name for the tuned model.
192
196
tuning_evaluation_spec: Specification for the model evaluation during tuning.
193
197
default_context: The context to use for all training samples by default.
198
+ accelerator_type: Type of accelerator to use. Can be "TPU" or "GPU".
194
199
195
200
Returns:
196
201
A `LanguageModelTuningJob` object that represents the tuning job.
@@ -252,6 +257,14 @@ def tune_model(
252
257
if default_context :
253
258
tuning_parameters ["default_context" ] = default_context
254
259
260
+ if accelerator_type :
261
+ if accelerator_type not in _ACCELERATOR_TYPES :
262
+ raise ValueError (
263
+ f"Unsupported accelerator type: { accelerator_type } ."
264
+ f" Supported types: { _ACCELERATOR_TYPES } "
265
+ )
266
+ tuning_parameters ["accelerator_type" ] = accelerator_type
267
+
255
268
return self ._tune_model (
256
269
training_data = training_data ,
257
270
tuning_parameters = tuning_parameters ,
@@ -336,6 +349,7 @@ def tune_model(
336
349
tuned_model_location : Optional [str ] = None ,
337
350
model_display_name : Optional [str ] = None ,
338
351
tuning_evaluation_spec : Optional ["TuningEvaluationSpec" ] = None ,
352
+ accelerator_type : Optional [_ACCELERATOR_TYPE_TYPE ] = None ,
339
353
) -> "_LanguageModelTuningJob" :
340
354
"""Tunes a model based on training data.
341
355
@@ -357,6 +371,7 @@ def tune_model(
357
371
tuned_model_location: GCP location where the tuned model should be deployed. Only "us-central1" is supported for now.
358
372
model_display_name: Custom display name for the tuned model.
359
373
tuning_evaluation_spec: Specification for the model evaluation during tuning.
374
+ accelerator_type: Type of accelerator to use. Can be "TPU" or "GPU".
360
375
361
376
Returns:
362
377
A `LanguageModelTuningJob` object that represents the tuning job.
@@ -376,6 +391,7 @@ def tune_model(
376
391
tuned_model_location = tuned_model_location ,
377
392
model_display_name = model_display_name ,
378
393
tuning_evaluation_spec = tuning_evaluation_spec ,
394
+ accelerator_type = accelerator_type ,
379
395
)
380
396
381
397
@@ -393,6 +409,7 @@ def tune_model(
393
409
tuned_model_location : Optional [str ] = None ,
394
410
model_display_name : Optional [str ] = None ,
395
411
tuning_evaluation_spec : Optional ["TuningEvaluationSpec" ] = None ,
412
+ accelerator_type : Optional [_ACCELERATOR_TYPE_TYPE ] = None ,
396
413
) -> "_LanguageModelTuningJob" :
397
414
"""Tunes a model based on training data.
398
415
@@ -421,6 +438,7 @@ def tune_model(
421
438
tuned_model_location: GCP location where the tuned model should be deployed. Only "us-central1" is supported for now.
422
439
model_display_name: Custom display name for the tuned model.
423
440
tuning_evaluation_spec: Specification for the model evaluation during tuning.
441
+ accelerator_type: Type of accelerator to use. Can be "TPU" or "GPU".
424
442
425
443
Returns:
426
444
A `LanguageModelTuningJob` object that represents the tuning job.
@@ -441,6 +459,7 @@ def tune_model(
441
459
tuned_model_location = tuned_model_location ,
442
460
model_display_name = model_display_name ,
443
461
tuning_evaluation_spec = tuning_evaluation_spec ,
462
+ accelerator_type = accelerator_type ,
444
463
)
445
464
tuned_model = job .get_tuned_model ()
446
465
self ._endpoint = tuned_model ._endpoint
@@ -461,6 +480,7 @@ def tune_model(
461
480
tuned_model_location : Optional [str ] = None ,
462
481
model_display_name : Optional [str ] = None ,
463
482
default_context : Optional [str ] = None ,
483
+ accelerator_type : Optional [_ACCELERATOR_TYPE_TYPE ] = None ,
464
484
) -> "_LanguageModelTuningJob" :
465
485
"""Tunes a model based on training data.
466
486
@@ -485,6 +505,7 @@ def tune_model(
485
505
tuned_model_location: GCP location where the tuned model should be deployed. Only "us-central1" is supported for now.
486
506
model_display_name: Custom display name for the tuned model.
487
507
default_context: The context to use for all training samples by default.
508
+ accelerator_type: Type of accelerator to use. Can be "TPU" or "GPU".
488
509
489
510
Returns:
490
511
A `LanguageModelTuningJob` object that represents the tuning job.
@@ -504,6 +525,7 @@ def tune_model(
504
525
tuned_model_location = tuned_model_location ,
505
526
model_display_name = model_display_name ,
506
527
default_context = default_context ,
528
+ accelerator_type = accelerator_type ,
507
529
)
508
530
509
531
@@ -521,6 +543,7 @@ def tune_model(
521
543
tuned_model_location : Optional [str ] = None ,
522
544
model_display_name : Optional [str ] = None ,
523
545
default_context : Optional [str ] = None ,
546
+ accelerator_type : Optional [_ACCELERATOR_TYPE_TYPE ] = None ,
524
547
) -> "_LanguageModelTuningJob" :
525
548
"""Tunes a model based on training data.
526
549
@@ -549,6 +572,7 @@ def tune_model(
549
572
tuned_model_location: GCP location where the tuned model should be deployed. Only "us-central1" is supported for now.
550
573
model_display_name: Custom display name for the tuned model.
551
574
default_context: The context to use for all training samples by default.
575
+ accelerator_type: Type of accelerator to use. Can be "TPU" or "GPU".
552
576
553
577
Returns:
554
578
A `LanguageModelTuningJob` object that represents the tuning job.
@@ -569,6 +593,7 @@ def tune_model(
569
593
tuned_model_location = tuned_model_location ,
570
594
model_display_name = model_display_name ,
571
595
default_context = default_context ,
596
+ accelerator_type = accelerator_type ,
572
597
)
573
598
tuned_model = job .get_tuned_model ()
574
599
self ._endpoint = tuned_model ._endpoint
0 commit comments