Skip to content

Commit

Permalink
Merge pull request kruize#1324 from bharathappali/gpu-support-pr-8
Browse files Browse the repository at this point in the history
Add adapter for DeviceDetails
  • Loading branch information
dinogun authored Oct 8, 2024
2 parents 8a8753e + 3e894fd commit 14e4e4c
Show file tree
Hide file tree
Showing 20 changed files with 227 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.autotune.analyzer.adapters;

import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.common.data.system.info.device.DeviceDetails;
import com.autotune.common.data.system.info.device.accelerator.AcceleratorDeviceData;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;


/**
* This adapter actually specifies the GSON to identify the type of implementation of DeviceDetails
* to serialize or deserialize
*/
public class DeviceDetailsAdapter extends TypeAdapter<DeviceDetails> {

@Override
public void write(JsonWriter out, DeviceDetails value) throws IOException {
out.beginObject();
out.name("type").value(value.getType().name());

if (value instanceof AcceleratorDeviceData accelerator) {
out.name("manufacturer").value(accelerator.getManufacturer());
out.name("modelName").value(accelerator.getModelName());
out.name("hostName").value(accelerator.getHostName());
out.name("UUID").value(accelerator.getUUID());
out.name("deviceName").value(accelerator.getDeviceName());
out.name("isMIG").value(accelerator.isMIG());
}
// Add for other devices when added

out.endObject();
}

@Override
public DeviceDetails read(JsonReader in) throws IOException {
String type = null;
String manufacturer = null;
String modelName = null;
String hostName = null;
String UUID = null;
String deviceName = null;
boolean isMIG = false;

in.beginObject();
while (in.hasNext()) {
switch (in.nextName()) {
case "type":
type = in.nextString();
break;
case "manufacturer":
manufacturer = in.nextString();
break;
case "modelName":
modelName = in.nextString();
break;
case "hostName":
hostName = in.nextString();
break;
case "UUID":
UUID = in.nextString();
break;
case "deviceName":
deviceName = in.nextString();
break;
case "isMIG":
isMIG = in.nextBoolean();
break;
default:
in.skipValue();
}
}
in.endObject();

if (type != null && type.equals(AnalyzerConstants.DeviceType.ACCELERATOR.name())) {
return (DeviceDetails) new AcceleratorDeviceData(modelName, hostName, UUID, deviceName, isMIG);
}
// Add for other device types if implemented in future

return null;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

package com.autotune.analyzer.adapters;

import com.autotune.analyzer.utils.AnalyzerConstants;
import com.google.gson.*;

import java.lang.reflect.Type;

/**
* Earlier the RecommendationItem enum has only two entries cpu and memory.
* At the time if serialization (store in DB or return as JSON via API)
* java has handled the toString conversion and have converted them to "cpu" and "memory" strings.
* They are also keys in the recommendation (requests & limits)
*
* But in case of NVIDIA the resources have / and . in their string representation of the MIG name.
* So we cannot add them as enums as is, So we had to create an entry which accepts a string
* and then the toString returns the string value of it.
*
* At the time of deserailization the string entries are converted to enum entries and vice versa in serialization.
* For example if the entry is NVIDIA_GPU_PARTITION_1_CORE_5GB("nvidia.com/mig-1g.5gb") then tostring of it
* will be nvidia.com/mig-1g.5gb which will not match the enum entry NVIDIA_GPU_PARTITION_1_CORE_5GB
*
* Also to maintain consistency we changed the cpu to CPU so without the adapter
* the JSON will be generated with CPU as the key.
*/
public class RecommendationItemAdapter implements JsonSerializer<AnalyzerConstants.RecommendationItem>, JsonDeserializer<AnalyzerConstants.RecommendationItem> {
@Override
public JsonElement serialize(AnalyzerConstants.RecommendationItem recommendationItem, Type type, JsonSerializationContext jsonSerializationContext) {
return jsonSerializationContext.serialize(recommendationItem.toString());
}


@Override
public AnalyzerConstants.RecommendationItem deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
String value = jsonElement.getAsString();
for (AnalyzerConstants.RecommendationItem item : AnalyzerConstants.RecommendationItem.values()) {
if (item.toString().equals(value)) {
return item;
}
}
throw new JsonParseException("Unknown element " + value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*******************************************************************************/
package com.autotune.analyzer.exceptions;

import com.autotune.analyzer.adapters.RecommendationItemAdapter;
import com.autotune.analyzer.serviceObjects.FailedUpdateResultsAPIObject;
import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.analyzer.utils.GsonUTCDateAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -56,6 +58,7 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques
.disableHtmlEscaping()
.enableComplexMapKeySerialization()
.registerTypeAdapter(Date.class, new GsonUTCDateAdapter())
.registerTypeAdapter(AnalyzerConstants.RecommendationItem.class, new RecommendationItemAdapter())
.create();
String gsonStr = gsonObj.toJson(new KruizeResponse(origMessage, errorCode, "", "ERROR", myList));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,6 @@ public static class PercentileConstants {
public static final Integer FIFTY_PERCENTILE = 50;
public static final Integer COST_ACCELERATOR_PERCENTILE = 60;
public static final Integer PERFORMANCE_ACCELERATOR_PERCENTILE = 98;

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,9 @@ public Map<AnalyzerConstants.RecommendationItem, RecommendationConfigItem> getAc
if (null == acceleratorModel
&& null != gpuMetricResult.getAcceleratorDeviceData().getModelName()
&& !gpuMetricResult.getAcceleratorDeviceData().getModelName().isEmpty()
&& CommonUtils.checkIfModelIsKruizeSupportedMIG(gpuMetricResult.getAcceleratorDeviceData().getModelName())
&& RecommendationUtils.checkIfModelIsKruizeSupportedMIG(gpuMetricResult.getAcceleratorDeviceData().getModelName())
) {
String obtainedAcceleratorName = CommonUtils.getSupportedModelBasedOnModelName(gpuMetricResult.getAcceleratorDeviceData().getModelName());
String obtainedAcceleratorName = RecommendationUtils.getSupportedModelBasedOnModelName(gpuMetricResult.getAcceleratorDeviceData().getModelName());
if (null != obtainedAcceleratorName)
acceleratorModel = obtainedAcceleratorName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,9 @@ public Map<AnalyzerConstants.RecommendationItem, RecommendationConfigItem> getAc
if (null == acceleratorModel
&& null != gpuMetricResult.getAcceleratorDeviceData().getModelName()
&& !gpuMetricResult.getAcceleratorDeviceData().getModelName().isEmpty()
&& CommonUtils.checkIfModelIsKruizeSupportedMIG(gpuMetricResult.getAcceleratorDeviceData().getModelName())
&& RecommendationUtils.checkIfModelIsKruizeSupportedMIG(gpuMetricResult.getAcceleratorDeviceData().getModelName())
) {
String obtainedAcceleratorName = CommonUtils.getSupportedModelBasedOnModelName(gpuMetricResult.getAcceleratorDeviceData().getModelName());
String obtainedAcceleratorName = RecommendationUtils.getSupportedModelBasedOnModelName(gpuMetricResult.getAcceleratorDeviceData().getModelName());
if (null != obtainedAcceleratorName)
acceleratorModel = obtainedAcceleratorName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,5 +352,23 @@ public static HashMap<AnalyzerConstants.RecommendationItem, RecommendationConfig
}
return returnMap;
}

public static String getSupportedModelBasedOnModelName(String modelName) {
if (null == modelName || modelName.isEmpty())
return null;

modelName = modelName.toUpperCase();

if (modelName.contains("A100") && modelName.contains("40GB"))
return AnalyzerConstants.AcceleratorConstants.SupportedAccelerators.A100_40_GB;

if (modelName.contains("A100") && modelName.contains("80GB"))
return AnalyzerConstants.AcceleratorConstants.SupportedAccelerators.A100_80_GB;

if (modelName.contains("H100") && modelName.contains("80GB"))
return AnalyzerConstants.AcceleratorConstants.SupportedAccelerators.H100_80_GB;

return null;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

package com.autotune.analyzer.services;

import com.autotune.analyzer.adapters.DeviceDetailsAdapter;
import com.autotune.analyzer.adapters.RecommendationItemAdapter;
import com.autotune.analyzer.exceptions.KruizeResponse;
import com.autotune.analyzer.serviceObjects.DSMetadataAPIObject;
import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.analyzer.utils.AnalyzerErrorConstants;
import com.autotune.analyzer.utils.GsonUTCDateAdapter;
import com.autotune.common.data.ValidationOutputData;
import com.autotune.common.data.dataSourceMetadata.DataSourceMetadataInfo;
import com.autotune.common.data.system.info.device.DeviceDetails;
import com.autotune.common.datasource.DataSourceInfo;
import com.autotune.common.datasource.DataSourceManager;
import com.autotune.common.datasource.DataSourceMetadataValidation;
Expand Down Expand Up @@ -240,6 +243,7 @@ private void sendSuccessResponse(HttpServletResponse response, DataSourceMetadat
.setPrettyPrinting()
.enableComplexMapKeySerialization()
.registerTypeAdapter(Date.class, new GsonUTCDateAdapter())
.registerTypeAdapter(AnalyzerConstants.RecommendationItem.class, new RecommendationItemAdapter())
.create();
gsonStr = gsonObj.toJson(dataSourceMetadata);
}
Expand Down Expand Up @@ -416,6 +420,8 @@ private Gson createGsonObject() {
.setPrettyPrinting()
.enableComplexMapKeySerialization()
.registerTypeAdapter(Date.class, new GsonUTCDateAdapter())
.registerTypeAdapter(AnalyzerConstants.RecommendationItem.class, new RecommendationItemAdapter())
.registerTypeAdapter(DeviceDetails.class, new DeviceDetailsAdapter())
.create();
}
private boolean isValidBooleanValue(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*******************************************************************************/
package com.autotune.analyzer.services;

import com.autotune.analyzer.adapters.DeviceDetailsAdapter;
import com.autotune.analyzer.adapters.RecommendationItemAdapter;
import com.autotune.analyzer.exceptions.FetchMetricsError;
import com.autotune.analyzer.kruizeObject.KruizeObject;
import com.autotune.analyzer.recommendations.engine.RecommendationEngine;
Expand All @@ -29,6 +31,7 @@
import com.autotune.common.data.metrics.MetricResults;
import com.autotune.common.data.result.ContainerData;
import com.autotune.common.data.result.IntervalResults;
import com.autotune.common.data.system.info.device.DeviceDetails;
import com.autotune.common.datasource.DataSourceInfo;
import com.autotune.common.k8sObjects.K8sObject;
import com.autotune.utils.GenericRestApiClient;
Expand Down Expand Up @@ -171,6 +174,8 @@ public boolean shouldSkipClass(Class<?> clazz) {
.setPrettyPrinting()
.enableComplexMapKeySerialization()
.registerTypeAdapter(Date.class, new GsonUTCDateAdapter())
.registerTypeAdapter(AnalyzerConstants.RecommendationItem.class, new RecommendationItemAdapter())
.registerTypeAdapter(DeviceDetails.class, new DeviceDetailsAdapter())
.setExclusionStrategies(strategy)
.create();
gsonStr = gsonObj.toJson(recommendationList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@

package com.autotune.analyzer.services;

import com.autotune.analyzer.adapters.DeviceDetailsAdapter;
import com.autotune.analyzer.adapters.RecommendationItemAdapter;
import com.autotune.analyzer.serviceObjects.ListDatasourcesAPIObject;
import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.analyzer.utils.AnalyzerErrorConstants;
import com.autotune.analyzer.utils.GsonUTCDateAdapter;
import com.autotune.common.data.system.info.device.DeviceDetails;
import com.autotune.common.datasource.DataSourceInfo;
import com.autotune.database.service.ExperimentDBService;
import com.autotune.utils.MetricsConfig;
Expand Down Expand Up @@ -148,6 +151,8 @@ private Gson createGsonObject() {
.setPrettyPrinting()
.enableComplexMapKeySerialization()
.registerTypeAdapter(Date.class, new GsonUTCDateAdapter())
.registerTypeAdapter(AnalyzerConstants.RecommendationItem.class, new RecommendationItemAdapter())
.registerTypeAdapter(DeviceDetails.class, new DeviceDetailsAdapter())
.create();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.autotune.analyzer.services;

import com.autotune.analyzer.adapters.DeviceDetailsAdapter;
import com.autotune.analyzer.adapters.RecommendationItemAdapter;
import com.autotune.analyzer.experiment.KruizeExperiment;
import com.autotune.analyzer.kruizeObject.KruizeObject;
import com.autotune.analyzer.serviceObjects.ContainerAPIObject;
Expand All @@ -29,6 +31,7 @@
import com.autotune.common.data.metrics.MetricResults;
import com.autotune.common.data.result.ContainerData;
import com.autotune.common.data.result.IntervalResults;
import com.autotune.common.data.system.info.device.DeviceDetails;
import com.autotune.common.k8sObjects.K8sObject;
import com.autotune.common.target.kubernetes.service.KubernetesServices;
import com.autotune.common.trials.ExperimentTrial;
Expand Down Expand Up @@ -281,6 +284,8 @@ private Gson createGsonObject() {
.setPrettyPrinting()
.enableComplexMapKeySerialization()
.registerTypeAdapter(Date.class, new GsonUTCDateAdapter())
.registerTypeAdapter(AnalyzerConstants.RecommendationItem.class, new RecommendationItemAdapter())
.registerTypeAdapter(DeviceDetails.class, new DeviceDetailsAdapter())
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.autotune.analyzer.services;

import com.autotune.analyzer.adapters.DeviceDetailsAdapter;
import com.autotune.analyzer.adapters.RecommendationItemAdapter;
import com.autotune.analyzer.exceptions.KruizeResponse;
import com.autotune.analyzer.kruizeObject.KruizeObject;
import com.autotune.analyzer.serviceObjects.ContainerAPIObject;
Expand All @@ -26,6 +28,7 @@
import com.autotune.analyzer.utils.GsonUTCDateAdapter;
import com.autotune.analyzer.utils.ServiceHelpers;
import com.autotune.common.data.result.ContainerData;
import com.autotune.common.data.system.info.device.DeviceDetails;
import com.autotune.database.service.ExperimentDBService;
import com.autotune.utils.KruizeConstants;
import com.autotune.utils.MetricsConfig;
Expand Down Expand Up @@ -224,6 +227,8 @@ public boolean shouldSkipClass(Class<?> clazz) {
.setPrettyPrinting()
.enableComplexMapKeySerialization()
.registerTypeAdapter(Date.class, new GsonUTCDateAdapter())
.registerTypeAdapter(AnalyzerConstants.RecommendationItem.class, new RecommendationItemAdapter())
.registerTypeAdapter(DeviceDetails.class, new DeviceDetailsAdapter())
.setExclusionStrategies(strategy)
.create();
gsonStr = gsonObj.toJson(recommendationList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
*******************************************************************************/
package com.autotune.analyzer.services;

import com.autotune.analyzer.adapters.DeviceDetailsAdapter;
import com.autotune.analyzer.adapters.RecommendationItemAdapter;
import com.autotune.analyzer.serviceObjects.ListSupportedK8sObjectsSO;
import com.autotune.analyzer.utils.GsonUTCDateAdapter;
import com.autotune.analyzer.utils.AnalyzerConstants;
import com.autotune.common.data.system.info.device.DeviceDetails;
import com.autotune.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -57,6 +60,8 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
.setPrettyPrinting()
.enableComplexMapKeySerialization()
.registerTypeAdapter(Date.class, new GsonUTCDateAdapter())
.registerTypeAdapter(AnalyzerConstants.RecommendationItem.class, new RecommendationItemAdapter())
.registerTypeAdapter(DeviceDetails.class, new DeviceDetailsAdapter())
.create();
// Convert the Service object to JSON
responseGSONString = gsonObj.toJson(listSupportedK8sObjectsSO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.autotune.analyzer.services;

import com.autotune.analyzer.adapters.DeviceDetailsAdapter;
import com.autotune.analyzer.adapters.RecommendationItemAdapter;
import com.autotune.analyzer.exceptions.InvalidValueException;
import com.autotune.analyzer.exceptions.PerformanceProfileResponse;
import com.autotune.analyzer.performanceProfiles.MetricProfileCollection;
Expand All @@ -28,6 +30,7 @@
import com.autotune.common.data.ValidationOutputData;
import com.autotune.common.data.metrics.Metric;
import com.autotune.common.data.result.ContainerData;
import com.autotune.common.data.system.info.device.DeviceDetails;
import com.autotune.database.dao.ExperimentDAOImpl;
import com.autotune.database.service.ExperimentDBService;
import com.autotune.utils.KruizeConstants;
Expand Down Expand Up @@ -378,6 +381,8 @@ private Gson createGsonObject() {
.setPrettyPrinting()
.enableComplexMapKeySerialization()
.registerTypeAdapter(Date.class, new GsonUTCDateAdapter())
.registerTypeAdapter(AnalyzerConstants.RecommendationItem.class, new RecommendationItemAdapter())
.registerTypeAdapter(DeviceDetails.class, new DeviceDetailsAdapter())
// a custom serializer for serializing metadata of JsonNode type.
.registerTypeAdapter(JsonNode.class, new JsonSerializer<JsonNode>() {
@Override
Expand Down
Loading

0 comments on commit 14e4e4c

Please sign in to comment.