Skip to content

Commit f90d892

Browse files
committed
start fobbiden ops checks
Signed-off-by: Ryan Nett <[email protected]>
1 parent 4f51bba commit f90d892

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/EagerOperationBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
final class EagerOperationBuilder implements OperationBuilder {
5656

5757
EagerOperationBuilder(EagerSession session, String type, String name) {
58+
if(!session.isOpEnabled(type))
59+
throw new IllegalArgumentException("Op " + type + " is not valid in eager mode.");
60+
5861
this.session = session;
5962
this.type = type;
6063
this.name = name;

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/EagerSession.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import org.tensorflow.internal.c_api.TFE_Context;
2828
import org.tensorflow.internal.c_api.TFE_ContextOptions;
2929
import org.tensorflow.internal.c_api.TF_Status;
30+
import org.tensorflow.op.core.Assign;
31+
import org.tensorflow.op.core.Placeholder;
32+
import org.tensorflow.op.core.Variable;
3033
import org.tensorflow.proto.framework.ConfigProto;
3134

3235
/**
@@ -279,6 +282,18 @@ public Types environmentType() {
279282
return Types.EAGER;
280283
}
281284

285+
@Override
286+
public boolean isOpEnabled(String opType) {
287+
switch (opType) {
288+
case Variable.OP_NAME:
289+
case Placeholder.OP_NAME:
290+
case Assign.OP_NAME:
291+
return false;
292+
default:
293+
return true;
294+
}
295+
}
296+
282297
TFE_Context nativeHandle() {
283298
checkSession();
284299
return nativeHandle;

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/ExecutionEnvironment.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ enum Types {
3434
*/
3535
OperationBuilder opBuilder(String type, String name);
3636

37+
/**
38+
* Returns true if the given operation is valid in this execution environment.
39+
* @param opType The op to check.
40+
* @return Whether the given operation is valid in this execution environment.
41+
*/
42+
default boolean isOpEnabled(String opType){
43+
return true;
44+
}
45+
3746
/**
3847
* Get the type of this environment (from the `Environments` enumeration.
3948
*

tensorflow-core/tensorflow-core-api/src/main/java/org/tensorflow/GraphOperationBuilder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
public final class GraphOperationBuilder implements OperationBuilder {
5858

5959
GraphOperationBuilder(Graph graph, String type, String name) {
60+
if(!graph.isOpEnabled(type))
61+
throw new IllegalArgumentException("Op " + type + " is not valid in graph mode.");
62+
6063
this.graph = graph;
6164
Graph.Reference r = graph.ref();
6265
try {

0 commit comments

Comments
 (0)