Search in sources :

Example 1 with DedicatedResources

use of com.google.cloud.aiplatform.v1.DedicatedResources in project java-aiplatform by googleapis.

the class DeployModelCustomTrainedModelSample method deployModelCustomTrainedModelSample.

static void deployModelCustomTrainedModelSample(String project, String endpointId, String model, String deployedModelDisplayName) throws IOException, ExecutionException, InterruptedException {
    EndpointServiceSettings settings = EndpointServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    String location = "us-central1";
    // the "close" method on the client to safely clean up any remaining background resources.
    try (EndpointServiceClient client = EndpointServiceClient.create(settings)) {
        MachineSpec machineSpec = MachineSpec.newBuilder().setMachineType("n1-standard-2").build();
        DedicatedResources dedicatedResources = DedicatedResources.newBuilder().setMinReplicaCount(1).setMachineSpec(machineSpec).build();
        String modelName = ModelName.of(project, location, model).toString();
        DeployedModel deployedModel = DeployedModel.newBuilder().setModel(modelName).setDisplayName(deployedModelDisplayName).setDedicatedResources(dedicatedResources).build();
        // key '0' assigns traffic for the newly deployed model
        // Traffic percentage values must add up to 100
        // Leave dictionary empty if endpoint should not accept any traffic
        Map<String, Integer> trafficSplit = new HashMap<>();
        trafficSplit.put("0", 100);
        EndpointName endpoint = EndpointName.of(project, location, endpointId);
        OperationFuture<DeployModelResponse, DeployModelOperationMetadata> response = client.deployModelAsync(endpoint, deployedModel, trafficSplit);
        // You can use OperationFuture.getInitialFuture to get a future representing the initial
        // response to the request, which contains information while the operation is in progress.
        System.out.format("Operation name: %s\n", response.getInitialFuture().get().getName());
        // OperationFuture.get() will block until the operation is finished.
        DeployModelResponse deployModelResponse = response.get();
        System.out.format("deployModelResponse: %s\n", deployModelResponse);
    }
}
Also used : HashMap(java.util.HashMap) DedicatedResources(com.google.cloud.aiplatform.v1.DedicatedResources) MachineSpec(com.google.cloud.aiplatform.v1.MachineSpec) DeployedModel(com.google.cloud.aiplatform.v1.DeployedModel) DeployModelResponse(com.google.cloud.aiplatform.v1.DeployModelResponse) DeployModelOperationMetadata(com.google.cloud.aiplatform.v1.DeployModelOperationMetadata) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) EndpointServiceClient(com.google.cloud.aiplatform.v1.EndpointServiceClient) EndpointServiceSettings(com.google.cloud.aiplatform.v1.EndpointServiceSettings)

Example 2 with DedicatedResources

use of com.google.cloud.aiplatform.v1.DedicatedResources in project java-aiplatform by googleapis.

the class DeployModelSample method deployModelSample.

static void deployModelSample(String project, String deployedModelDisplayName, String endpointId, String modelId) throws IOException, InterruptedException, ExecutionException, TimeoutException {
    EndpointServiceSettings endpointServiceSettings = EndpointServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    // the "close" method on the client to safely clean up any remaining background resources.
    try (EndpointServiceClient endpointServiceClient = EndpointServiceClient.create(endpointServiceSettings)) {
        String location = "us-central1";
        EndpointName endpointName = EndpointName.of(project, location, endpointId);
        // key '0' assigns traffic for the newly deployed model
        // Traffic percentage values must add up to 100
        // Leave dictionary empty if endpoint should not accept any traffic
        Map<String, Integer> trafficSplit = new HashMap<>();
        trafficSplit.put("0", 100);
        ModelName modelName = ModelName.of(project, location, modelId);
        AutomaticResources automaticResourcesInput = AutomaticResources.newBuilder().setMinReplicaCount(1).setMaxReplicaCount(1).build();
        DeployedModel deployedModelInput = DeployedModel.newBuilder().setModel(modelName.toString()).setDisplayName(deployedModelDisplayName).setAutomaticResources(automaticResourcesInput).build();
        OperationFuture<DeployModelResponse, DeployModelOperationMetadata> deployModelResponseFuture = endpointServiceClient.deployModelAsync(endpointName, deployedModelInput, trafficSplit);
        System.out.format("Operation name: %s\n", deployModelResponseFuture.getInitialFuture().get().getName());
        System.out.println("Waiting for operation to finish...");
        DeployModelResponse deployModelResponse = deployModelResponseFuture.get(20, TimeUnit.MINUTES);
        System.out.println("Deploy Model Response");
        DeployedModel deployedModel = deployModelResponse.getDeployedModel();
        System.out.println("\tDeployed Model");
        System.out.format("\t\tid: %s\n", deployedModel.getId());
        System.out.format("\t\tmodel: %s\n", deployedModel.getModel());
        System.out.format("\t\tDisplay Name: %s\n", deployedModel.getDisplayName());
        System.out.format("\t\tCreate Time: %s\n", deployedModel.getCreateTime());
        DedicatedResources dedicatedResources = deployedModel.getDedicatedResources();
        System.out.println("\t\tDedicated Resources");
        System.out.format("\t\t\tMin Replica Count: %s\n", dedicatedResources.getMinReplicaCount());
        MachineSpec machineSpec = dedicatedResources.getMachineSpec();
        System.out.println("\t\t\tMachine Spec");
        System.out.format("\t\t\t\tMachine Type: %s\n", machineSpec.getMachineType());
        System.out.format("\t\t\t\tAccelerator Type: %s\n", machineSpec.getAcceleratorType());
        System.out.format("\t\t\t\tAccelerator Count: %s\n", machineSpec.getAcceleratorCount());
        AutomaticResources automaticResources = deployedModel.getAutomaticResources();
        System.out.println("\t\tAutomatic Resources");
        System.out.format("\t\t\tMin Replica Count: %s\n", automaticResources.getMinReplicaCount());
        System.out.format("\t\t\tMax Replica Count: %s\n", automaticResources.getMaxReplicaCount());
    }
}
Also used : ModelName(com.google.cloud.aiplatform.v1.ModelName) HashMap(java.util.HashMap) DedicatedResources(com.google.cloud.aiplatform.v1.DedicatedResources) MachineSpec(com.google.cloud.aiplatform.v1.MachineSpec) DeployedModel(com.google.cloud.aiplatform.v1.DeployedModel) DeployModelResponse(com.google.cloud.aiplatform.v1.DeployModelResponse) DeployModelOperationMetadata(com.google.cloud.aiplatform.v1.DeployModelOperationMetadata) EndpointName(com.google.cloud.aiplatform.v1.EndpointName) AutomaticResources(com.google.cloud.aiplatform.v1.AutomaticResources) EndpointServiceClient(com.google.cloud.aiplatform.v1.EndpointServiceClient) EndpointServiceSettings(com.google.cloud.aiplatform.v1.EndpointServiceSettings)

Example 3 with DedicatedResources

use of com.google.cloud.aiplatform.v1.DedicatedResources in project java-aiplatform by googleapis.

the class CreateBatchPredictionJobSample method createBatchPredictionJobSample.

static void createBatchPredictionJobSample(String project, String displayName, String model, String instancesFormat, String gcsSourceUri, String predictionsFormat, String gcsDestinationOutputUriPrefix) throws IOException {
    JobServiceSettings settings = JobServiceSettings.newBuilder().setEndpoint("us-central1-aiplatform.googleapis.com:443").build();
    String location = "us-central1";
    // the "close" method on the client to safely clean up any remaining background resources.
    try (JobServiceClient client = JobServiceClient.create(settings)) {
        // Passing in an empty Value object for model parameters
        Value modelParameters = ValueConverter.EMPTY_VALUE;
        GcsSource gcsSource = GcsSource.newBuilder().addUris(gcsSourceUri).build();
        BatchPredictionJob.InputConfig inputConfig = BatchPredictionJob.InputConfig.newBuilder().setInstancesFormat(instancesFormat).setGcsSource(gcsSource).build();
        GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(gcsDestinationOutputUriPrefix).build();
        BatchPredictionJob.OutputConfig outputConfig = BatchPredictionJob.OutputConfig.newBuilder().setPredictionsFormat(predictionsFormat).setGcsDestination(gcsDestination).build();
        MachineSpec machineSpec = MachineSpec.newBuilder().setMachineType("n1-standard-2").setAcceleratorType(AcceleratorType.NVIDIA_TESLA_K80).setAcceleratorCount(1).build();
        BatchDedicatedResources dedicatedResources = BatchDedicatedResources.newBuilder().setMachineSpec(machineSpec).setStartingReplicaCount(1).setMaxReplicaCount(1).build();
        String modelName = ModelName.of(project, location, model).toString();
        BatchPredictionJob batchPredictionJob = BatchPredictionJob.newBuilder().setDisplayName(displayName).setModel(modelName).setModelParameters(modelParameters).setInputConfig(inputConfig).setOutputConfig(outputConfig).setDedicatedResources(dedicatedResources).build();
        LocationName parent = LocationName.of(project, location);
        BatchPredictionJob response = client.createBatchPredictionJob(parent, batchPredictionJob);
        System.out.format("response: %s\n", response);
        System.out.format("\tName: %s\n", response.getName());
    }
}
Also used : JobServiceSettings(com.google.cloud.aiplatform.v1.JobServiceSettings) BatchDedicatedResources(com.google.cloud.aiplatform.v1.BatchDedicatedResources) GcsSource(com.google.cloud.aiplatform.v1.GcsSource) BatchPredictionJob(com.google.cloud.aiplatform.v1.BatchPredictionJob) Value(com.google.protobuf.Value) JobServiceClient(com.google.cloud.aiplatform.v1.JobServiceClient) MachineSpec(com.google.cloud.aiplatform.v1.MachineSpec) GcsDestination(com.google.cloud.aiplatform.v1.GcsDestination) LocationName(com.google.cloud.aiplatform.v1.LocationName)

Aggregations

MachineSpec (com.google.cloud.aiplatform.v1.MachineSpec)3 DedicatedResources (com.google.cloud.aiplatform.v1.DedicatedResources)2 DeployModelOperationMetadata (com.google.cloud.aiplatform.v1.DeployModelOperationMetadata)2 DeployModelResponse (com.google.cloud.aiplatform.v1.DeployModelResponse)2 DeployedModel (com.google.cloud.aiplatform.v1.DeployedModel)2 EndpointName (com.google.cloud.aiplatform.v1.EndpointName)2 EndpointServiceClient (com.google.cloud.aiplatform.v1.EndpointServiceClient)2 EndpointServiceSettings (com.google.cloud.aiplatform.v1.EndpointServiceSettings)2 HashMap (java.util.HashMap)2 AutomaticResources (com.google.cloud.aiplatform.v1.AutomaticResources)1 BatchDedicatedResources (com.google.cloud.aiplatform.v1.BatchDedicatedResources)1 BatchPredictionJob (com.google.cloud.aiplatform.v1.BatchPredictionJob)1 GcsDestination (com.google.cloud.aiplatform.v1.GcsDestination)1 GcsSource (com.google.cloud.aiplatform.v1.GcsSource)1 JobServiceClient (com.google.cloud.aiplatform.v1.JobServiceClient)1 JobServiceSettings (com.google.cloud.aiplatform.v1.JobServiceSettings)1 LocationName (com.google.cloud.aiplatform.v1.LocationName)1 ModelName (com.google.cloud.aiplatform.v1.ModelName)1 Value (com.google.protobuf.Value)1