Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Devicelist method to Tensorflow.java #171

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@

package org.tensorflow;

import static org.tensorflow.internal.c_api.global.tensorflow.TF_DeleteBuffer;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_DeleteLibraryHandle;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_GetAllOpList;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_GetOpList;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_LoadLibrary;
import static org.tensorflow.internal.c_api.global.tensorflow.TF_Version;

import com.google.protobuf.InvalidProtocolBufferException;
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.PointerScope;
import org.tensorflow.exceptions.TensorFlowException;
import org.tensorflow.internal.c_api.TF_Buffer;
import org.tensorflow.internal.c_api.TF_Library;
import org.tensorflow.internal.c_api.TF_Status;
import org.tensorflow.internal.c_api.*;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No star imports in the main classes.

import org.tensorflow.proto.framework.OpList;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.tensorflow.internal.c_api.global.tensorflow.*;

/** Static utility methods describing the TensorFlow runtime. */
public final class TensorFlow {
/** Returns the version of the underlying TensorFlow runtime. */
Expand Down Expand Up @@ -103,6 +102,22 @@ private static OpList libraryOpList(TF_Library handle) {
}
}

public static List<DeviceSpec> listDevices(Optional<DeviceSpec.DeviceType> deviceType, TFE_Context ctx) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't expose TFE_Context at the public level, these classes are generated from the C API and we always wrapped them up in public endpoints for more flexibility and scalability. So in this case, I suggest maybe to create an EagerSession.Context static nested class that encapsulates a TFE_Context?

List<DeviceSpec> deviceList = new ArrayList();
TF_Status status = TF_Status.newStatus();
TF_DeviceList devices = TFE_ContextListDevices(ctx, status);
for(int i = 0; i<TF_DeviceListCount(devices); i++){
BytePointer devName = TF_DeviceListName(devices,i, status);
BytePointer devType = TF_DeviceListType(devices,i,status);
DeviceSpec devSpec = DeviceSpec.newBuilder().deviceIndex(i).deviceType(DeviceSpec.DeviceType.valueOf(devType.getString()))
.job(devName.getString()).build();
deviceList.add(devSpec);
}
TF_DeleteDeviceList(devices);
if(deviceType.isPresent()) return deviceList;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just general comment, be careful to format your code according to Google Java Style Guide, I saw a bunch of missing spaces in the code that will fail the lint checks to pass when enabled.

return deviceList.stream().filter(d -> d.deviceType().equals(deviceType.get())).collect(Collectors.toList());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java streams tends to be slower than simple for loops, the overhead is even worst when manipulating such a small list of objects. While I don't think this method needs to be time-critical, I would suggest to take the simple/faster route here (just my two cents).

}

private TensorFlow() {}

/** Load the TensorFlow runtime C library. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@

package org.tensorflow;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.File;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.tensorflow.internal.c_api.TFE_Context;
import org.tensorflow.proto.framework.OpList;

/** Unit tests for {@link org.tensorflow.TensorFlow}. */
Expand Down Expand Up @@ -67,4 +67,14 @@ public void loadLibrary() {
g.opBuilder("MyTest", "MyTest").build();
}
}

@Test
public void getDeviceListTest(){
TFE_Context context = new TFE_Context();
List<DeviceSpec> devices = TensorFlow.listDevices(Optional.empty(), context);
assertFalse(devices.isEmpty());
devices = TensorFlow.listDevices(Optional.of(DeviceSpec.DeviceType.CPU), context);
assertFalse(devices.isEmpty());

}
}