Test explicit marking of operators to compile.
(self)
| 237 | self._compare(MnistForward, [w, b, x]) |
| 238 | |
| 239 | def testExplicitMarking(self): |
| 240 | """Test explicit marking of operators to compile.""" |
| 241 | batch_size = 16 |
| 242 | image_size = 28 * 28 |
| 243 | num_classes = 10 |
| 244 | |
| 245 | with ops.Graph().as_default(): |
| 246 | x = array_ops.placeholder(dtypes.float32) |
| 247 | w = array_ops.placeholder(dtypes.float32) |
| 248 | b = array_ops.placeholder(dtypes.float32) |
| 249 | with jit_scope(): |
| 250 | y1 = math_ops.matmul(x, w) |
| 251 | y2 = math_ops.add(y1, b) |
| 252 | with jit_scope(): |
| 253 | y = math_ops.square(y2) |
| 254 | |
| 255 | dw = np.random.random_sample((image_size, num_classes)).astype(np.float32) |
| 256 | db = np.random.random_sample((num_classes)).astype(np.float32) |
| 257 | dx = np.random.random_sample((batch_size, image_size)).astype(np.float32) |
| 258 | with session_lib.Session() as sess: |
| 259 | run_metadata = config_pb2.RunMetadata() |
| 260 | output = test_utils.RunWithWarmup( |
| 261 | sess, |
| 262 | y, { |
| 263 | x: dx, |
| 264 | w: dw, |
| 265 | b: db |
| 266 | }, |
| 267 | run_metadata=run_metadata, |
| 268 | options=config_pb2.RunOptions( |
| 269 | trace_level=config_pb2.RunOptions.FULL_TRACE)) |
| 270 | |
| 271 | # TODO(phawkins): really we would like to test that there were exactly |
| 272 | # two kernel launches. However, we have no reliable way to determine |
| 273 | # that. |
| 274 | self.assert_(MetadataHasXlaRunOp(run_metadata)) |
| 275 | |
| 276 | expected = np.square(np.dot(dx, dw) + db) |
| 277 | self.assertAllClose(expected, output, rtol=1e-1) |
| 278 | |
| 279 | |
| 280 | class XlaCompilationTest(test.TestCase): |
nothing calls this directly
no test coverage detected