• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 package org.tensorflow;
17 
18 import static org.junit.Assert.fail;
19 
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.junit.runners.JUnit4;
23 
24 /** Unit tests for {@link EagerOperationBuilder} class. */
25 @RunWith(JUnit4.class)
26 public class EagerOperationBuilderTest {
27 
28   @Test
failToCreateIfSessionIsClosed()29   public void failToCreateIfSessionIsClosed() {
30     EagerSession session = EagerSession.create();
31     session.close();
32     try {
33       new EagerOperationBuilder(session, "Add", "add");
34       fail();
35     } catch (IllegalStateException e) {
36       // expected
37     }
38   }
39 
40   @Test
failToBuildOpIfSessionIsClosed()41   public void failToBuildOpIfSessionIsClosed() {
42     EagerOperationBuilder opBuilder;
43     try (EagerSession session = EagerSession.create()) {
44       opBuilder = new EagerOperationBuilder(session, "Empty", "empty");
45     }
46     try {
47       opBuilder.setAttr("dtype", DataType.FLOAT);
48       fail();
49     } catch (IllegalStateException e) {
50       // expected
51     }
52   }
53 
54   @Test
addInputs()55   public void addInputs() {
56     try (EagerSession session = EagerSession.create()) {
57       Operation asrt =
58           opBuilder(session, "Assert", "assert")
59               .addInput(TestUtil.constant(session, "Cond", true))
60               .addInputList(new Output<?>[] {TestUtil.constant(session, "Error", -1)})
61               .build();
62       try {
63         opBuilder(session, "Const", "var").addControlInput(asrt);
64         fail();
65       } catch (UnsupportedOperationException e) {
66         // expected
67       }
68     }
69   }
70 
71   @Test
setDevice()72   public void setDevice() {
73     try (EagerSession session = EagerSession.create()) {
74       opBuilder(session, "Add", "SetDevice")
75           .setDevice("/job:localhost/replica:0/task:0/device:CPU:0")
76           .addInput(TestUtil.constant(session, "Const1", 2))
77           .addInput(TestUtil.constant(session, "Const2", 4))
78           .build();
79     }
80   }
81 
82   @Test
setAttrs()83   public void setAttrs() {
84     // The effect of setting an attribute may not easily be visible from the other parts of this
85     // package's API. Thus, for now, the test simply executes the various setAttr variants to see
86     // that there are no exceptions.
87     //
88     // This is a bit of an awkward test since it has to find operations with attributes of specific
89     // types that aren't inferred from the input arguments.
90     try (EagerSession session = EagerSession.create()) {
91       // dtype, tensor attributes.
92       try (Tensor<Integer> t = Tensors.create(1)) {
93         opBuilder(session, "Const", "DataTypeAndTensor")
94             .setAttr("dtype", DataType.INT32)
95             .setAttr("value", t)
96             .build();
97       }
98       // type, int (TF "int" attributes are 64-bit signed, so a Java long).
99       opBuilder(session, "RandomUniform", "DataTypeAndInt")
100           .addInput(TestUtil.constant(session, "RandomUniformShape", new int[] {1}))
101           .setAttr("seed", 10)
102           .setAttr("dtype", DataType.FLOAT)
103           .build();
104       // list(int), string
105       opBuilder(session, "MaxPool", "IntListAndString")
106           .addInput(TestUtil.constant(session, "MaxPoolInput", new float[2][2][2][2]))
107           .setAttr("ksize", new long[] {1, 1, 1, 1})
108           .setAttr("strides", new long[] {1, 1, 1, 1})
109           .setAttr("padding", "SAME")
110           .build();
111       // list(float), device
112       opBuilder(session, "FractionalMaxPool", "FloatList")
113           .addInput(TestUtil.constant(session, "FractionalMaxPoolInput", new float[2][2][2][2]))
114           .setAttr("pooling_ratio", new float[] {1.0f, 1.44f, 1.73f, 1.0f})
115           .build();
116       // shape
117       opBuilder(session, "EnsureShape", "ShapeAttr")
118           .addInput(TestUtil.constant(session, "Const", new int[2][2]))
119           .setAttr("shape", Shape.make(2, 2))
120           .build();
121       // list(shape)
122       opBuilder(session, "FIFOQueue", "queue")
123           .setAttr("component_types", new DataType[] {DataType.INT32, DataType.INT32})
124           .setAttr("shapes", new Shape[] {Shape.make(2, 2), Shape.make(2, 2, 2)})
125           .build();
126       // bool
127       opBuilder(session, "All", "Bool")
128           .addInput(TestUtil.constant(session, "Const", new boolean[] {true, true, false}))
129           .addInput(TestUtil.constant(session, "Axis", 0))
130           .setAttr("keep_dims", false)
131           .build();
132       // float
133       opBuilder(session, "ApproximateEqual", "Float")
134           .addInput(TestUtil.constant(session, "Const1", 10.00001f))
135           .addInput(TestUtil.constant(session, "Const2", 10.00000f))
136           .setAttr("tolerance", 0.1f)
137           .build();
138       // Missing tests: list(string), list(byte), list(bool), list(type)
139     }
140   }
141 
opBuilder(EagerSession session, String type, String name)142   private static EagerOperationBuilder opBuilder(EagerSession session, String type, String name) {
143     return new EagerOperationBuilder(session, type, name);
144   }
145 }
146