forked from kruize/autotune
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request kruize#1324 from bharathappali/gpu-support-pr-8
Add adapter for DeviceDetails
- Loading branch information
Showing
20 changed files
with
227 additions
and
24 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
src/main/java/com/autotune/analyzer/adapters/DeviceDetailsAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
43 changes: 43 additions & 0 deletions
43
src/main/java/com/autotune/analyzer/adapters/RecommendationItemAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.