@@ -1476,6 +1476,171 @@ def test_deploy_custom_model_with_all_config_success(self, deploy_mock):
1476
1476
)
1477
1477
)
1478
1478
1479
+ def test_list_deploy_options_with_recommendations (self ):
1480
+ """Tests list_deploy_options when recommend_spec returns recommendations."""
1481
+ aiplatform .init (
1482
+ project = _TEST_PROJECT ,
1483
+ location = _TEST_LOCATION ,
1484
+ )
1485
+ mock_model_service_client = mock .Mock ()
1486
+ with mock .patch .object (
1487
+ aiplatform .initializer .global_config ,
1488
+ "create_client" ,
1489
+ return_value = mock_model_service_client ,
1490
+ ):
1491
+ quota_state = types .RecommendSpecResponse .Recommendation .QuotaState
1492
+ mock_response = types .RecommendSpecResponse (
1493
+ recommendations = [
1494
+ types .RecommendSpecResponse .Recommendation (
1495
+ spec = types .RecommendSpecResponse .MachineAndModelContainerSpec (
1496
+ machine_spec = types .MachineSpec (
1497
+ machine_type = "n1-standard-4" ,
1498
+ accelerator_type = types .AcceleratorType .NVIDIA_TESLA_T4 ,
1499
+ accelerator_count = 1 ,
1500
+ )
1501
+ ),
1502
+ region = "us-central1" ,
1503
+ user_quota_state = quota_state .QUOTA_STATE_USER_HAS_QUOTA ,
1504
+ ),
1505
+ types .RecommendSpecResponse .Recommendation (
1506
+ spec = types .RecommendSpecResponse .MachineAndModelContainerSpec (
1507
+ machine_spec = types .MachineSpec (
1508
+ machine_type = "n1-standard-8" ,
1509
+ accelerator_type = types .AcceleratorType .NVIDIA_TESLA_V100 ,
1510
+ accelerator_count = 2 ,
1511
+ )
1512
+ ),
1513
+ region = "us-east1" ,
1514
+ user_quota_state = quota_state .QUOTA_STATE_NO_USER_QUOTA ,
1515
+ ),
1516
+ types .RecommendSpecResponse .Recommendation (
1517
+ spec = types .RecommendSpecResponse .MachineAndModelContainerSpec (
1518
+ machine_spec = types .MachineSpec (
1519
+ machine_type = "g2-standard-24" ,
1520
+ accelerator_type = types .AcceleratorType .NVIDIA_L4 ,
1521
+ accelerator_count = 2 ,
1522
+ )
1523
+ ),
1524
+ region = "us-central1" ,
1525
+ user_quota_state = quota_state .QUOTA_STATE_UNSPECIFIED ,
1526
+ ),
1527
+ ]
1528
+ )
1529
+ mock_model_service_client .recommend_spec .return_value = mock_response
1530
+
1531
+ custom_model = model_garden_preview .CustomModel (gcs_uri = _TEST_GCS_URI )
1532
+ result = custom_model .list_deploy_options ()
1533
+
1534
+ expected_output = textwrap .dedent (
1535
+ """\
1536
+ [Option 1]
1537
+ machine_type="n1-standard-4",
1538
+ accelerator_type="NVIDIA_TESLA_T4",
1539
+ accelerator_count=1,
1540
+ region="us-central1",
1541
+ user_quota_state="QUOTA_STATE_USER_HAS_QUOTA"
1542
+
1543
+ [Option 2]
1544
+ machine_type="n1-standard-8",
1545
+ accelerator_type="NVIDIA_TESLA_V100",
1546
+ accelerator_count=2,
1547
+ region="us-east1",
1548
+ user_quota_state="QUOTA_STATE_NO_USER_QUOTA"
1549
+
1550
+ [Option 3]
1551
+ machine_type="g2-standard-24",
1552
+ accelerator_type="NVIDIA_L4",
1553
+ accelerator_count=2,
1554
+ region="us-central1\" """
1555
+ )
1556
+ assert result == expected_output
1557
+ mock_model_service_client .recommend_spec .assert_called_once_with (
1558
+ types .RecommendSpecRequest (
1559
+ gcs_uri = _TEST_GCS_URI ,
1560
+ parent = f"projects/{ _TEST_PROJECT } /locations/{ _TEST_LOCATION } " ,
1561
+ check_machine_availability = True ,
1562
+ ),
1563
+ timeout = 60 ,
1564
+ )
1565
+
1566
+ def test_list_deploy_options_with_specs (self ):
1567
+ """Tests list_deploy_options when recommend_spec returns specs."""
1568
+ aiplatform .init (
1569
+ project = _TEST_PROJECT ,
1570
+ location = _TEST_LOCATION ,
1571
+ )
1572
+ mock_model_service_client = mock .Mock ()
1573
+ with mock .patch .object (
1574
+ aiplatform .initializer .global_config ,
1575
+ "create_client" ,
1576
+ return_value = mock_model_service_client ,
1577
+ ):
1578
+ mock_response = types .RecommendSpecResponse (
1579
+ specs = [
1580
+ types .RecommendSpecResponse .MachineAndModelContainerSpec (
1581
+ machine_spec = types .MachineSpec (
1582
+ machine_type = "n1-standard-4" ,
1583
+ accelerator_type = types .AcceleratorType .NVIDIA_TESLA_T4 ,
1584
+ accelerator_count = 1 ,
1585
+ )
1586
+ ),
1587
+ types .RecommendSpecResponse .MachineAndModelContainerSpec (
1588
+ machine_spec = types .MachineSpec (
1589
+ machine_type = "n1-standard-8" ,
1590
+ accelerator_type = types .AcceleratorType .NVIDIA_TESLA_V100 ,
1591
+ accelerator_count = 2 ,
1592
+ )
1593
+ ),
1594
+ ]
1595
+ )
1596
+ mock_model_service_client .recommend_spec .return_value = mock_response
1597
+
1598
+ custom_model = model_garden_preview .CustomModel (gcs_uri = _TEST_GCS_URI )
1599
+ result = custom_model .list_deploy_options (available_machines = False )
1600
+
1601
+ expected_output = textwrap .dedent (
1602
+ """\
1603
+ [Option 1]
1604
+ machine_type="n1-standard-4",
1605
+ accelerator_type="NVIDIA_TESLA_T4",
1606
+ accelerator_count=1
1607
+
1608
+ [Option 2]
1609
+ machine_type="n1-standard-8",
1610
+ accelerator_type="NVIDIA_TESLA_V100",
1611
+ accelerator_count=2"""
1612
+ )
1613
+ assert result == expected_output
1614
+ mock_model_service_client .recommend_spec .assert_called_once_with (
1615
+ types .RecommendSpecRequest (
1616
+ gcs_uri = _TEST_GCS_URI ,
1617
+ parent = f"projects/{ _TEST_PROJECT } /locations/{ _TEST_LOCATION } " ,
1618
+ check_machine_availability = False ,
1619
+ ),
1620
+ timeout = 60 ,
1621
+ )
1622
+
1623
+ def test_list_deploy_options_exception (self ):
1624
+ """Tests list_deploy_options when recommend_spec raises an exception."""
1625
+ aiplatform .init (
1626
+ project = _TEST_PROJECT ,
1627
+ location = _TEST_LOCATION ,
1628
+ )
1629
+ mock_model_service_client = mock .Mock ()
1630
+ with mock .patch .object (
1631
+ aiplatform .initializer .global_config ,
1632
+ "create_client" ,
1633
+ return_value = mock_model_service_client ,
1634
+ ):
1635
+ mock_model_service_client .recommend_spec .side_effect = ValueError (
1636
+ "Test Error"
1637
+ )
1638
+ custom_model = model_garden_preview .CustomModel (gcs_uri = _TEST_GCS_URI )
1639
+ with pytest .raises (ValueError ) as exception :
1640
+ custom_model .list_deploy_options ()
1641
+ assert str (exception .value ) == "Test Error"
1642
+ mock_model_service_client .recommend_spec .assert_called_once ()
1643
+
1479
1644
1480
1645
class TestModelGardenModel :
1481
1646
"""Test cases for Model Garden Model class."""
0 commit comments