How to use _get_config method in Molotov

Best Python code snippet using molotov_python

layout_optimizer_test.py

Source:layout_optimizer_test.py Github

copy

Full Screen

...119 elems = (x1, x2, x3, x4)120 outputs = functional_ops.map_fn(121 _model_with_vec_and_4d, elems, dtype=dtypes.float32)122 return outputs123def _get_config(layout_optimizer=True):124 if layout_optimizer:125 rewrite_options = rewriter_config_pb2.RewriterConfig(126 layout_optimizer=rewriter_config_pb2.RewriterConfig.ON,127 # do not remove duplicated nodes128 arithmetic_optimization=rewriter_config_pb2.RewriterConfig.OFF)129 else:130 rewrite_options = rewriter_config_pb2.RewriterConfig(131 layout_optimizer=rewriter_config_pb2.RewriterConfig.OFF,132 # do not remove duplicated nodes133 arithmetic_optimization=rewriter_config_pb2.RewriterConfig.OFF)134 rewrite_options.min_graph_nodes = -1135 graph_options = config_pb2.GraphOptions(136 rewrite_options=rewrite_options, build_cost_model=1)137 config = config_pb2.ConfigProto(graph_options=graph_options)138 config.graph_options.optimizer_options.opt_level = -1139 return config140def _simple_metagraph(depthwise=False):141 random_seed.set_random_seed(0)142 x = variables.Variable(random_ops.truncated_normal([1, 200, 200, 3], seed=0))143 conv = conv_layers.separable_conv2d if depthwise else conv_layers.conv2d144 y = conv(x, 32, [3, 3])145 z = conv(y, 32, [3, 3])146 optimizer = gradient_descent.GradientDescentOptimizer(1e-4)147 loss = math_ops.reduce_mean(z)148 train_op = optimizer.minimize(loss)149 graph = ops.get_default_graph()150 graph.add_to_collection('train_op', train_op)151 meta_graph = saver_lib.export_meta_graph(graph_def=graph.as_graph_def())152 return meta_graph153def _get_cluster():154 named_device = device_properties_pb2.NamedDevice()155 named_device.name = '/GPU:0'156 named_device.properties.type = 'GPU'157 named_device.properties.num_cores = 24158 named_device.properties.frequency = 1000159 named_device.properties.environment['architecture'] = '4'160 cluster = gcluster.Cluster(devices=[named_device])161 return cluster162def _is_transpose(node):163 return node.endswith('TransposeNHWCToNCHW-LayoutOptimizer') or node.endswith(164 'TransposeNCHWToNHWC-LayoutOptimizer')165def _is_permute(node):166 return node.endswith('VecPermuteNHWCToNCHW-LayoutOptimizer') or node.endswith(167 'VecPermuteNCHWToNHWC-LayoutOptimizer')168class LayoutOptimizerTest(test.TestCase):169 """Tests the Grappler layout optimizer."""170 def _assert_trans_nchw_to_nhwc(self, name, nodes):171 self.assertIn(name + '-TransposeNCHWToNHWC-LayoutOptimizer', nodes)172 def _assert_trans_nhwc_to_nchw(self, name, nodes):173 self.assertIn(name + '-TransposeNHWCToNCHW-LayoutOptimizer', nodes)174 def _assert_map_nhwc_to_nchw(self, name, nodes):175 self.assertIn(name + '-DimMapNHWCToNCHW-LayoutOptimizer', nodes)176 def _assert_vec_nchw_to_nhwc(self, name, nodes):177 self.assertIn(name + '-VecPermuteNCHWToNHWC-LayoutOptimizer', nodes)178 def _assert_vec_nhwc_to_nchw(self, name, nodes):179 self.assertIn(name + '-VecPermuteNHWCToNCHW-LayoutOptimizer', nodes)180 def _train(self, checkpoint_path, layout_optimizer=False, restore=False):181 ops.reset_default_graph()182 graph = ops.get_default_graph()183 with session.Session(184 config=_get_config(layout_optimizer), graph=graph) as sess:185 batch = 2186 height = 6187 width = 7188 input_channels = 3189 shape = [batch, height, width, input_channels]190 image = array_ops.placeholder(dtype='float32', shape=shape)191 conv1 = conv_layers.conv2d(image, 32, [3, 3])192 conv2 = conv_layers.conv2d(conv1, 32, [3, 3])193 optimizer = gradient_descent.GradientDescentOptimizer(0.01)194 loss = math_ops.reduce_mean(conv2)195 train_op = optimizer.minimize(loss)196 saver = saver_lib.Saver(write_version=saver_pb2.SaverDef.V2)197 if restore:198 saver.restore(sess, checkpoint_path)199 else:200 sess.run(variables.global_variables_initializer())201 np.random.seed(0)202 for _ in range(2):203 image_val = np.random.rand(*shape).astype(np.float32)204 sess.run([loss, train_op], feed_dict={image: image_val})205 if restore:206 all_vars = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)207 all_vars_values = [var.eval(session=sess) for var in all_vars]208 return all_vars_values209 else:210 saver.save(sess, checkpoint_path)211 def testTwoConvLayers(self):212 if test.is_gpu_available(cuda_only=True):213 random_seed.set_random_seed(0)214 x = random_ops.truncated_normal([1, 784], seed=0)215 output = _two_layer_model(x)216 with session.Session(config=_get_config(False)) as sess:217 output_val_ref = sess.run(output)218 with session.Session(config=_get_config()) as sess:219 metadata = config_pb2.RunMetadata()220 output_val = sess.run(output, run_metadata=metadata)221 nodes = []222 num_transposes = 0223 for node in metadata.cost_graph.node:224 if _is_transpose(node.name):225 num_transposes += 1226 nodes.append(node.name)227 # Four transposes were initially added in the Expand phase of228 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.229 expected_num_transposes = 2230 self.assertEqual(expected_num_transposes, num_transposes)231 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)232 self._assert_trans_nchw_to_nhwc('Relu_1-0-0', nodes)233 self.assertAllClose(output_val_ref, output_val, atol=1e-3)234 def testSplitWithNonConstAxis(self):235 if test.is_gpu_available(cuda_only=True):236 random_seed.set_random_seed(0)237 x = random_ops.truncated_normal([1, 784], seed=0)238 conv = _two_layer_model(x)239 dim = array_ops.placeholder(dtype='int32')240 split = array_ops.split(conv, 2, axis=dim)241 scale = constant_op.constant(0.1, shape=[32])242 offset = constant_op.constant(0.3, shape=[32])243 bn0 = nn.fused_batch_norm(split[0], scale, offset)244 bn1 = nn.fused_batch_norm(split[1], scale, offset)245 add = bn0[0] + bn1[0]246 output = array_ops.identity(add)247 with session.Session(config=_get_config(False)) as sess:248 output_val_ref = sess.run(output, feed_dict={dim: 3})249 with session.Session(config=_get_config()) as sess:250 metadata = config_pb2.RunMetadata()251 output_val = sess.run(output, run_metadata=metadata, feed_dict={dim: 3})252 nodes = []253 num_transposes = 0254 for node in metadata.cost_graph.node:255 if _is_transpose(node.name):256 num_transposes += 1257 nodes.append(node.name)258 expected_num_transposes = 2259 self.assertEqual(expected_num_transposes, num_transposes)260 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)261 self._assert_trans_nchw_to_nhwc('add_2-0-0', nodes)262 self._assert_map_nhwc_to_nchw('split-0', nodes)263 self.assertAllClose(output_val_ref, output_val, atol=1e-3)264 def testSplitVWithNonConstAxis(self):265 if test.is_gpu_available(cuda_only=True):266 random_seed.set_random_seed(0)267 x = random_ops.truncated_normal([1, 784], seed=0)268 conv = _two_layer_model(x)269 dim = array_ops.placeholder(dtype='int32')270 sizes = constant_op.constant([50, 10, 4], shape=[3])271 split = gen_array_ops.split_v(272 value=conv, size_splits=sizes, axis=dim, num_split=3)273 output = math_ops.reduce_sum(split[0])274 with session.Session(config=_get_config(False)) as sess:275 output_val_ref = sess.run(output, feed_dict={dim: 3})276 with session.Session(config=_get_config()) as sess:277 metadata = config_pb2.RunMetadata()278 output_val = sess.run(output, run_metadata=metadata, feed_dict={dim: 3})279 nodes = []280 num_transposes = 0281 for node in metadata.cost_graph.node:282 if _is_transpose(node.name):283 num_transposes += 1284 nodes.append(node.name)285 # Four transposes were initially added in the Expand phase of286 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.287 expected_num_transposes = 2288 self.assertEqual(expected_num_transposes, num_transposes)289 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)290 self._assert_trans_nchw_to_nhwc('SplitV-0-0', nodes)291 self._assert_map_nhwc_to_nchw('SplitV-2', nodes)292 self.assertAllClose(output_val_ref, output_val, atol=1e-3)293 def testPadWithConstPaddings(self):294 if test.is_gpu_available(cuda_only=True):295 random_seed.set_random_seed(0)296 x = random_ops.truncated_normal([1, 784], seed=0)297 conv = _two_layer_model(x)298 paddings_val = [[1, 2], [3, 4], [5, 6], [7, 8]]299 paddings = constant_op.constant(300 paddings_val, dtype='int32', name='PaddingsConst')301 pad = array_ops.pad(conv, paddings)302 output = array_ops.identity(pad)303 with session.Session(config=_get_config(False)) as sess:304 output_val_ref = sess.run(output)305 with session.Session(config=_get_config()) as sess:306 metadata = config_pb2.RunMetadata()307 output_val = sess.run(output, run_metadata=metadata)308 nodes = []309 num_transposes = 0310 for node in metadata.cost_graph.node:311 if _is_transpose(node.name):312 num_transposes += 1313 nodes.append(node.name)314 # Four transposes were initially added in the Expand phase of315 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.316 expected_num_transposes = 2317 self.assertEqual(expected_num_transposes, num_transposes)318 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)319 self._assert_trans_nchw_to_nhwc('Pad-0-0', nodes)320 self.assertIn('Pad-1-LayoutOptimizer', nodes)321 self.assertAllClose(output_val_ref, output_val, atol=1e-3)322 def testReduceSum(self):323 if test.is_gpu_available(cuda_only=True):324 random_seed.set_random_seed(0)325 x = random_ops.truncated_normal([1, 784], seed=0)326 conv = _two_layer_model(x)327 reduce_sum = math_ops.reduce_sum(conv)328 output = array_ops.identity(reduce_sum)329 with session.Session(config=_get_config(False)) as sess:330 output_val_ref = sess.run(output)331 with session.Session(config=_get_config()) as sess:332 metadata = config_pb2.RunMetadata()333 output_val = sess.run(output, run_metadata=metadata)334 nodes = []335 num_transposes = 0336 for node in metadata.cost_graph.node:337 if _is_transpose(node.name):338 num_transposes += 1339 nodes.append(node.name)340 # Three transposes were initially added in the Expand phase of341 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.342 expected_num_transposes = 1343 self.assertEqual(expected_num_transposes, num_transposes)344 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)345 self.assertAllClose(output_val_ref, output_val, atol=1e-3)346 def testCast(self):347 if test.is_gpu_available(cuda_only=True):348 random_seed.set_random_seed(0)349 x = random_ops.truncated_normal([1, 784], seed=0)350 conv = _two_layer_model(x)351 cast = math_ops.cast(conv, dtype='bool')352 output = array_ops.identity(cast)353 with session.Session(config=_get_config(False)) as sess:354 output_val_ref = sess.run(output)355 with session.Session(config=_get_config()) as sess:356 metadata = config_pb2.RunMetadata()357 output_val = sess.run(output, run_metadata=metadata)358 nodes = []359 num_transposes = 0360 for node in metadata.cost_graph.node:361 if _is_transpose(node.name):362 num_transposes += 1363 nodes.append(node.name)364 # Four transposes were initially added in the Expand phase of365 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.366 expected_num_transposes = 2367 self.assertEqual(expected_num_transposes, num_transposes)368 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)369 self._assert_trans_nchw_to_nhwc('Cast-0-0', nodes)370 self.assertAllClose(output_val_ref, output_val, atol=1e-3)371 def testSqueeze(self):372 if test.is_gpu_available(cuda_only=True):373 random_seed.set_random_seed(0)374 x = random_ops.truncated_normal([1, 784], seed=0)375 conv = _two_layer_model(x)376 reduce_sum = math_ops.reduce_sum(conv, axis=[1, 2])377 squeeze = array_ops.squeeze(reduce_sum)378 output = array_ops.identity(squeeze)379 with session.Session(config=_get_config(False)) as sess:380 output_val_ref = sess.run(output)381 with session.Session(config=_get_config()) as sess:382 metadata = config_pb2.RunMetadata()383 output_val = sess.run(output, run_metadata=metadata)384 nodes = []385 num_transposes = 0386 for node in metadata.cost_graph.node:387 if _is_transpose(node.name):388 num_transposes += 1389 nodes.append(node.name)390 # Three transposes were initially added in the Expand phase of391 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.392 expected_num_transposes = 1393 self.assertEqual(expected_num_transposes, num_transposes)394 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)395 self.assertAllClose(output_val_ref, output_val, atol=1e-3)396 def testSqueezeAlongHW(self):397 if test.is_gpu_available(cuda_only=True):398 random_seed.set_random_seed(0)399 x = random_ops.truncated_normal([1, 784], seed=0)400 conv = _two_layer_model(x)401 reduce_sum = math_ops.reduce_sum(conv, axis=[1, 2], keepdims=True)402 squeeze = array_ops.squeeze(reduce_sum, axis=[1, 2])403 output = array_ops.identity(squeeze)404 with session.Session(config=_get_config(False)) as sess:405 output_val_ref = sess.run(output)406 with session.Session(config=_get_config()) as sess:407 metadata = config_pb2.RunMetadata()408 output_val = sess.run(output, run_metadata=metadata)409 nodes = []410 num_transposes = 0411 for node in metadata.cost_graph.node:412 if _is_transpose(node.name):413 num_transposes += 1414 nodes.append(node.name)415 # Three transposes were initially added in the Expand phase of416 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.417 expected_num_transposes = 1418 self.assertEqual(expected_num_transposes, num_transposes)419 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)420 self.assertAllClose(output_val_ref, output_val, atol=1e-3)421 def testSqueezeAlongNHW(self):422 if test.is_gpu_available(cuda_only=True):423 random_seed.set_random_seed(0)424 x = random_ops.truncated_normal([1, 784], seed=0)425 conv = _two_layer_model(x)426 reduce_sum = math_ops.reduce_sum(conv, axis=[0, 1, 2], keepdims=True)427 squeeze = array_ops.squeeze(reduce_sum, axis=[0, 1, 2])428 output = array_ops.identity(squeeze)429 with session.Session(config=_get_config(False)) as sess:430 output_val_ref = sess.run(output)431 with session.Session(config=_get_config()) as sess:432 metadata = config_pb2.RunMetadata()433 output_val = sess.run(output, run_metadata=metadata)434 nodes = []435 num_transposes = 0436 for node in metadata.cost_graph.node:437 if _is_transpose(node.name):438 num_transposes += 1439 nodes.append(node.name)440 # Three transposes were initially added in the Expand phase of441 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.442 expected_num_transposes = 1443 self.assertEqual(expected_num_transposes, num_transposes)444 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)445 self.assertAllClose(output_val_ref, output_val, atol=1e-3)446 def testReduceSumAlongHWC(self):447 if test.is_gpu_available(cuda_only=True):448 random_seed.set_random_seed(0)449 x = random_ops.truncated_normal([1, 784], seed=0)450 conv = _two_layer_model(x)451 reduce_sum = math_ops.reduce_sum(conv, axis=[1, 2, 3])452 output = array_ops.identity(reduce_sum)453 with session.Session(config=_get_config(False)) as sess:454 output_val_ref = sess.run(output)455 with session.Session(config=_get_config()) as sess:456 metadata = config_pb2.RunMetadata()457 output_val = sess.run(output, run_metadata=metadata)458 nodes = []459 num_transposes = 0460 for node in metadata.cost_graph.node:461 if _is_transpose(node.name):462 num_transposes += 1463 nodes.append(node.name)464 # Three transposes were initially added in the Expand phase of465 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.466 expected_num_transposes = 1467 self.assertEqual(expected_num_transposes, num_transposes)468 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)469 self.assertAllClose(output_val_ref, output_val, atol=1e-3)470 def testReduceSumAlongNHW(self):471 if test.is_gpu_available(cuda_only=True):472 random_seed.set_random_seed(0)473 x = random_ops.truncated_normal([1, 784], seed=0)474 conv = _two_layer_model(x)475 reduce_sum = math_ops.reduce_sum(conv, axis=[0, 1, 2])476 output = array_ops.identity(reduce_sum)477 with session.Session(config=_get_config(False)) as sess:478 output_val_ref = sess.run(output)479 with session.Session(config=_get_config()) as sess:480 metadata = config_pb2.RunMetadata()481 output_val = sess.run(output, run_metadata=metadata)482 nodes = []483 num_transposes = 0484 for node in metadata.cost_graph.node:485 if _is_transpose(node.name):486 num_transposes += 1487 nodes.append(node.name)488 # Three transposes were initially added in the Expand phase of489 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.490 expected_num_transposes = 1491 self.assertEqual(expected_num_transposes, num_transposes)492 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)493 self.assertAllClose(output_val_ref, output_val, atol=1e-3)494 def testReduceSumAlongC(self):495 if test.is_gpu_available(cuda_only=True):496 random_seed.set_random_seed(0)497 x = random_ops.truncated_normal([1, 784], seed=0)498 conv = _two_layer_model(x)499 reduce_sum = math_ops.reduce_sum(conv, axis=[3])500 output = array_ops.identity(reduce_sum)501 with session.Session(config=_get_config(False)) as sess:502 output_val_ref = sess.run(output)503 with session.Session(config=_get_config()) as sess:504 metadata = config_pb2.RunMetadata()505 output_val = sess.run(output, run_metadata=metadata)506 nodes = []507 num_transposes = 0508 for node in metadata.cost_graph.node:509 if _is_transpose(node.name):510 num_transposes += 1511 nodes.append(node.name)512 # Three transposes were initially added in the Expand phase of513 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.514 expected_num_transposes = 1515 self.assertEqual(expected_num_transposes, num_transposes)516 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)517 self.assertAllClose(output_val_ref, output_val, atol=1e-3)518 def testReduceSumAlongCKeepDims(self):519 if test.is_gpu_available(cuda_only=True):520 random_seed.set_random_seed(0)521 x = random_ops.truncated_normal([1, 784], seed=0)522 conv = _two_layer_model(x)523 reduce_sum = math_ops.reduce_sum(conv, axis=[3], keepdims=True)524 output = array_ops.identity(reduce_sum)525 with session.Session(config=_get_config(False)) as sess:526 output_val_ref = sess.run(output)527 with session.Session(config=_get_config()) as sess:528 metadata = config_pb2.RunMetadata()529 output_val = sess.run(output, run_metadata=metadata)530 nodes = []531 num_transposes = 0532 for node in metadata.cost_graph.node:533 if _is_transpose(node.name):534 num_transposes += 1535 nodes.append(node.name)536 # Four transposes were initially added in the Expand phase of537 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.538 expected_num_transposes = 2539 self.assertEqual(expected_num_transposes, num_transposes)540 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)541 self._assert_trans_nchw_to_nhwc('Sum-0-0', nodes)542 self.assertAllClose(output_val_ref, output_val, atol=1e-3)543 def testReduceSumAlongHKeepDims(self):544 if test.is_gpu_available(cuda_only=True):545 random_seed.set_random_seed(0)546 x = random_ops.truncated_normal([1, 784], seed=0)547 conv = _two_layer_model(x)548 reduce_sum = math_ops.reduce_sum(conv, axis=[2], keepdims=True)549 output = array_ops.identity(reduce_sum)550 with session.Session(config=_get_config(False)) as sess:551 output_val_ref = sess.run(output)552 with session.Session(config=_get_config()) as sess:553 metadata = config_pb2.RunMetadata()554 output_val = sess.run(output, run_metadata=metadata)555 nodes = []556 num_transposes = 0557 for node in metadata.cost_graph.node:558 if _is_transpose(node.name):559 num_transposes += 1560 nodes.append(node.name)561 # Four transposes were initially added in the Expand phase of562 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.563 expected_num_transposes = 2564 self.assertEqual(expected_num_transposes, num_transposes)565 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)566 self.assertAllClose(output_val_ref, output_val, atol=1e-3)567 def testReduceSumAlongWCKeepDims(self):568 if test.is_gpu_available(cuda_only=True):569 random_seed.set_random_seed(0)570 x = random_ops.truncated_normal([1, 784], seed=0)571 conv = _two_layer_model(x)572 reduce_sum = math_ops.reduce_sum(conv, axis=[2, 3], keepdims=True)573 output = array_ops.identity(reduce_sum)574 with session.Session(config=_get_config(False)) as sess:575 output_val_ref = sess.run(output)576 with session.Session(config=_get_config()) as sess:577 metadata = config_pb2.RunMetadata()578 output_val = sess.run(output, run_metadata=metadata)579 nodes = []580 num_transposes = 0581 for node in metadata.cost_graph.node:582 if _is_transpose(node.name):583 num_transposes += 1584 nodes.append(node.name)585 # Four transposes were initially added in the Expand phase of586 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.587 expected_num_transposes = 2588 self.assertEqual(expected_num_transposes, num_transposes)589 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)590 self.assertAllClose(output_val_ref, output_val, atol=1e-3)591 def testConcatWithControlDependency(self):592 if test.is_gpu_available(cuda_only=True):593 random_seed.set_random_seed(0)594 x = random_ops.truncated_normal([1, 784], seed=0)595 conv = _two_layer_model(x)596 axis = constant_op.constant(3)597 var = variables.Variable(3)598 assign = state_ops.assign(var, 6)599 with ops.control_dependencies([assign]):600 concat = array_ops.concat([conv, conv], axis)601 output = array_ops.identity(concat)602 with session.Session(config=_get_config(False)) as sess:603 output_val_ref = sess.run(output)604 with session.Session(config=_get_config()) as sess:605 metadata = config_pb2.RunMetadata()606 output_val = sess.run(output, run_metadata=metadata)607 nodes = []608 num_transposes = 0609 for node in metadata.cost_graph.node:610 if _is_transpose(node.name):611 num_transposes += 1612 nodes.append(node.name)613 # Four transposes were initially added in the Expand phase of614 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.615 expected_num_transposes = 2616 self.assertEqual(expected_num_transposes, num_transposes)617 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)618 self._assert_trans_nchw_to_nhwc('concat-0-0', nodes)619 self.assertIn('concat-2-LayoutOptimizer', nodes)620 self.assertAllClose(output_val_ref, output_val, atol=1e-3)621 def testFill(self):622 if test.is_gpu_available(cuda_only=True):623 random_seed.set_random_seed(0)624 x = array_ops.placeholder(dtype='float32')625 conv = _two_layer_model(x)626 shape = array_ops.shape(conv)627 scalar = array_ops.constant(5.7)628 fill = array_ops.fill(shape, scalar)629 output = array_ops.identity(fill)630 x_val = [3.4] * 784631 with session.Session(config=_get_config(False)) as sess:632 output_val_ref = sess.run(output, feed_dict={x: x_val})633 with session.Session(config=_get_config()) as sess:634 metadata = config_pb2.RunMetadata()635 output_val = sess.run(636 output, run_metadata=metadata, feed_dict={637 x: x_val638 })639 nodes = []640 num_transposes = 0641 num_vec_permute = 0642 for node in metadata.cost_graph.node:643 if _is_transpose(node.name):644 num_transposes += 1645 if _is_permute(node.name):646 num_vec_permute += 1647 nodes.append(node.name)648 # Four transposes were initially added in the Expand phase of649 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.650 expected_num_transposes = 2651 self.assertEqual(expected_num_transposes, num_transposes)652 # Two vector permute nodes were initially added in the Expand phase of653 # LayoutOptimizer; they cancelled out each other in the Collapse phase.654 expected_vec_permute = 0655 self.assertEqual(expected_vec_permute, num_vec_permute)656 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)657 self._assert_trans_nchw_to_nhwc('Fill-0-0', nodes)658 self.assertAllClose(output_val_ref, output_val, atol=1e-3)659 def testTile(self):660 if test.is_gpu_available(cuda_only=True):661 random_seed.set_random_seed(0)662 x = random_ops.truncated_normal([1, 784], seed=0)663 conv = _two_layer_model(x)664 multiple = array_ops.placeholder(dtype='int32')665 tile = array_ops.tile(conv, multiple)666 output = array_ops.identity(tile)667 multiple_val = [2, 3, 4, 1]668 with session.Session(config=_get_config(False)) as sess:669 output_val_ref = sess.run(output, feed_dict={multiple: multiple_val})670 with session.Session(config=_get_config()) as sess:671 metadata = config_pb2.RunMetadata()672 output_val = sess.run(673 output, run_metadata=metadata, feed_dict={674 multiple: multiple_val675 })676 nodes = []677 num_transposes = 0678 for node in metadata.cost_graph.node:679 if _is_transpose(node.name):680 num_transposes += 1681 nodes.append(node.name)682 # Four transposes were initially added in the Expand phase of683 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.684 expected_num_transposes = 2685 self.assertEqual(expected_num_transposes, num_transposes)686 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)687 self._assert_trans_nchw_to_nhwc('Tile-0-0', nodes)688 self._assert_vec_nhwc_to_nchw('Tile-1', nodes)689 self.assertAllClose(output_val_ref, output_val, atol=1e-3)690 def testReverseWithConstDims(self):691 if test.is_gpu_available(cuda_only=True):692 random_seed.set_random_seed(0)693 x = random_ops.truncated_normal([1, 784], seed=0)694 conv = _two_layer_model(x)695 dims = constant_op.constant([3, 1], name='DimsConst')696 reverse = array_ops.reverse(conv, dims)697 output = array_ops.identity(reverse)698 with session.Session(config=_get_config(False)) as sess:699 output_val_ref = sess.run(output)700 with session.Session(config=_get_config()) as sess:701 metadata = config_pb2.RunMetadata()702 output_val = sess.run(output, run_metadata=metadata)703 nodes = []704 num_transposes = 0705 for node in metadata.cost_graph.node:706 if _is_transpose(node.name):707 num_transposes += 1708 nodes.append(node.name)709 # Four transposes were initially added in the Expand phase of710 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.711 expected_num_transposes = 2712 self.assertEqual(expected_num_transposes, num_transposes)713 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)714 self._assert_trans_nchw_to_nhwc('ReverseV2-0-0', nodes)715 self.assertIn('ReverseV2-1-LayoutOptimizer', nodes)716 self.assertAllClose(output_val_ref, output_val, atol=1e-3)717 def testReverseWithNonConstDims(self):718 if test.is_gpu_available(cuda_only=True):719 random_seed.set_random_seed(0)720 x = random_ops.truncated_normal([1, 784], seed=0)721 conv = _two_layer_model(x)722 dims = array_ops.placeholder(dtype='int32')723 reverse = array_ops.reverse(conv, dims)724 output = array_ops.identity(reverse)725 dims_val = [2, 3]726 with session.Session(config=_get_config(False)) as sess:727 output_val_ref = sess.run(output, feed_dict={dims: dims_val})728 with session.Session(config=_get_config()) as sess:729 metadata = config_pb2.RunMetadata()730 output_val = sess.run(731 output, run_metadata=metadata, feed_dict={732 dims: dims_val733 })734 nodes = []735 num_transposes = 0736 for node in metadata.cost_graph.node:737 if _is_transpose(node.name):738 num_transposes += 1739 nodes.append(node.name)740 # Four transposes were initially added in the Expand phase of741 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.742 expected_num_transposes = 2743 self.assertEqual(expected_num_transposes, num_transposes)744 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)745 self._assert_trans_nchw_to_nhwc('ReverseV2-0-0', nodes)746 self._assert_map_nhwc_to_nchw('ReverseV2-1', nodes)747 self.assertAllClose(output_val_ref, output_val, atol=1e-3)748 def testSelectOp(self):749 if test.is_gpu_available(cuda_only=True):750 random_seed.set_random_seed(0)751 x = random_ops.truncated_normal([1, 784], seed=0)752 conv = _two_layer_model(x)753 add = math_ops.add(conv, conv)754 mean = math_ops.reduce_mean(conv)755 condition = math_ops.less(conv, mean)756 select = gen_math_ops.select(condition, conv, add)757 output = array_ops.identity(select)758 with session.Session(config=_get_config(False)) as sess:759 output_val_ref = sess.run(output)760 with session.Session(config=_get_config()) as sess:761 metadata = config_pb2.RunMetadata()762 output_val = sess.run(output, run_metadata=metadata)763 nodes = []764 num_transposes = 0765 for node in metadata.cost_graph.node:766 if _is_transpose(node.name):767 num_transposes += 1768 nodes.append(node.name)769 expected_num_transposes = 2770 self.assertEqual(expected_num_transposes, num_transposes)771 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)772 self._assert_trans_nchw_to_nhwc('Select-0-0', nodes)773 self.assertAllClose(output_val_ref, output_val, atol=1e-3)774 def testSelectOpConditionUnknownShape(self):775 if test.is_gpu_available(cuda_only=True):776 random_seed.set_random_seed(0)777 x = random_ops.truncated_normal([1, 784], seed=0)778 conv = _two_layer_model(x)779 add = math_ops.add(conv, conv)780 condition = array_ops.placeholder(dtype='bool')781 select = gen_math_ops.select(condition, conv, add)782 output = array_ops.identity(select)783 condition_val = np.zeros((1, 7, 7, 64))784 with session.Session(config=_get_config(False)) as sess:785 output_val_ref = sess.run(output, feed_dict={condition: condition_val})786 with session.Session(config=_get_config()) as sess:787 metadata = config_pb2.RunMetadata()788 output_val = sess.run(789 output, run_metadata=metadata, feed_dict={condition: condition_val})790 nodes = []791 num_transposes = 0792 for node in metadata.cost_graph.node:793 if _is_transpose(node.name):794 num_transposes += 1795 nodes.append(node.name)796 expected_num_transposes = 3797 self.assertEqual(expected_num_transposes, num_transposes)798 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)799 self.assertAllClose(output_val_ref, output_val, atol=1e-3)800 def testSelectOpScalarCondition(self):801 if test.is_gpu_available(cuda_only=True):802 random_seed.set_random_seed(0)803 x = random_ops.truncated_normal([1, 784], seed=0)804 conv = _two_layer_model(x)805 add = math_ops.add(conv, conv)806 condition = constant_op.constant(True)807 select = gen_math_ops.select(condition, conv, add)808 output = array_ops.identity(select)809 with session.Session(config=_get_config(False)) as sess:810 output_val_ref = sess.run(output)811 with session.Session(config=_get_config()) as sess:812 metadata = config_pb2.RunMetadata()813 output_val = sess.run(output, run_metadata=metadata)814 nodes = []815 num_transposes = 0816 for node in metadata.cost_graph.node:817 if _is_transpose(node.name):818 num_transposes += 1819 nodes.append(node.name)820 expected_num_transposes = 2821 self.assertEqual(expected_num_transposes, num_transposes)822 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)823 self._assert_trans_nchw_to_nhwc('Select-0-0', nodes)824 self.assertAllClose(output_val_ref, output_val, atol=1e-3)825 def testPadWithNonConstPaddings(self):826 if test.is_gpu_available(cuda_only=True):827 random_seed.set_random_seed(0)828 x = random_ops.truncated_normal([1, 784], seed=0)829 conv = _two_layer_model(x)830 paddings = array_ops.placeholder(dtype='int32')831 pad = array_ops.pad(conv, paddings)832 output = array_ops.identity(pad)833 paddings_val = [[1, 2], [3, 4], [5, 6], [7, 8]]834 with session.Session(config=_get_config(False)) as sess:835 output_val_ref = sess.run(output, feed_dict={paddings: paddings_val})836 with session.Session(config=_get_config()) as sess:837 metadata = config_pb2.RunMetadata()838 output_val = sess.run(839 output, run_metadata=metadata, feed_dict={840 paddings: paddings_val841 })842 nodes = []843 num_transposes = 0844 for node in metadata.cost_graph.node:845 if _is_transpose(node.name):846 num_transposes += 1847 nodes.append(node.name)848 # Four transposes were initially added in the Expand phase of849 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.850 expected_num_transposes = 2851 self.assertEqual(expected_num_transposes, num_transposes)852 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)853 self._assert_trans_nchw_to_nhwc('Pad-0-0', nodes)854 self._assert_vec_nhwc_to_nchw('Pad-1', nodes)855 self.assertAllClose(output_val_ref, output_val, atol=1e-3)856 def testMaxPoolV2(self):857 if test.is_gpu_available(cuda_only=True):858 random_seed.set_random_seed(0)859 x = random_ops.truncated_normal([1, 784], seed=0)860 conv = _two_layer_model(x)861 ksize = constant_op.constant([1, 2, 3, 1], shape=[4])862 strides = array_ops.placeholder(dtype='int32', shape=[4])863 max_pool = gen_nn_ops.max_pool_v2(conv, ksize, strides, 'VALID')864 output = array_ops.identity(max_pool)865 strides_val = [1, 3, 2, 1]866 with session.Session(config=_get_config(False)) as sess:867 output_val_ref = sess.run(output, feed_dict={strides: strides_val})868 with session.Session(config=_get_config()) as sess:869 metadata = config_pb2.RunMetadata()870 output_val = sess.run(871 output, run_metadata=metadata, feed_dict={872 strides: strides_val873 })874 nodes = []875 num_transposes = 0876 for node in metadata.cost_graph.node:877 if _is_transpose(node.name):878 num_transposes += 1879 nodes.append(node.name)880 expected_num_transposes = 2881 self.assertEqual(expected_num_transposes, num_transposes)882 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)883 self._assert_trans_nchw_to_nhwc('MaxPoolV2-0-0', nodes)884 self._assert_vec_nhwc_to_nchw('MaxPoolV2-2', nodes)885 self.assertIn('MaxPoolV2-1-LayoutOptimizer', nodes)886 self.assertAllClose(output_val_ref, output_val, atol=1e-3)887 def testMaxPoolGradV2(self):888 if test.is_gpu_available(cuda_only=True):889 random_seed.set_random_seed(0)890 x = random_ops.truncated_normal([1, 784], seed=0)891 conv = _two_layer_model(x)892 ksize = constant_op.constant([1, 2, 3, 1], shape=[4])893 strides = array_ops.placeholder(dtype='int32', shape=[4])894 max_pool_grad = gen_nn_ops.max_pool_grad_v2(conv, conv, conv, ksize,895 strides, 'VALID')896 output = array_ops.identity(max_pool_grad)897 strides_val = [1, 3, 2, 1]898 with session.Session(config=_get_config(False)) as sess:899 output_val_ref = sess.run(output, feed_dict={strides: strides_val})900 with session.Session(config=_get_config()) as sess:901 metadata = config_pb2.RunMetadata()902 output_val = sess.run(903 output, run_metadata=metadata, feed_dict={904 strides: strides_val905 })906 nodes = []907 num_transposes = 0908 for node in metadata.cost_graph.node:909 if _is_transpose(node.name):910 num_transposes += 1911 nodes.append(node.name)912 expected_num_transposes = 2913 self.assertEqual(expected_num_transposes, num_transposes)914 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)915 self._assert_trans_nchw_to_nhwc('MaxPoolGradV2-0-0', nodes)916 self._assert_vec_nhwc_to_nchw('MaxPoolGradV2-4', nodes)917 self.assertIn('MaxPoolGradV2-3-LayoutOptimizer', nodes)918 self.assertAllClose(output_val_ref, output_val, atol=1e-3)919 def testSliceWithNonConstAxis(self):920 if test.is_gpu_available(cuda_only=True):921 random_seed.set_random_seed(0)922 x = random_ops.truncated_normal([1, 784], seed=0)923 conv = _two_layer_model(x)924 size = array_ops.placeholder(dtype='int32')925 s = array_ops.slice(conv, [0, 0, 0, 0], size)926 output = array_ops.identity(s)927 size_val = [1, 2, 3, 4]928 with session.Session(config=_get_config(False)) as sess:929 output_val_ref = sess.run(output, feed_dict={size: size_val})930 with session.Session(config=_get_config()) as sess:931 metadata = config_pb2.RunMetadata()932 output_val = sess.run(933 output, run_metadata=metadata, feed_dict={934 size: size_val935 })936 nodes = []937 num_transposes = 0938 for node in metadata.cost_graph.node:939 if _is_transpose(node.name):940 num_transposes += 1941 nodes.append(node.name)942 # Four transposes were initially added in the Expand phase of943 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.944 expected_num_transposes = 2945 self.assertEqual(expected_num_transposes, num_transposes)946 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)947 self._assert_trans_nchw_to_nhwc('Slice-0-0', nodes)948 self._assert_vec_nhwc_to_nchw('Slice-2', nodes)949 self.assertAllClose(output_val_ref, output_val, atol=1e-3)950 def testStridedSliceWithNonConstAxis(self):951 if test.is_gpu_available(cuda_only=True):952 random_seed.set_random_seed(0)953 x = random_ops.truncated_normal([1, 784], seed=0)954 conv = _two_layer_model(x)955 end = array_ops.placeholder(dtype='int32')956 s = array_ops.strided_slice(conv, [0, 0, 0, 0], end, strides=[1, 2, 3, 1])957 output = array_ops.identity(s)958 end_val = [1, 2, 3, 4]959 with session.Session(config=_get_config(False)) as sess:960 output_val_ref = sess.run(output, feed_dict={end: end_val})961 with session.Session(config=_get_config()) as sess:962 metadata = config_pb2.RunMetadata()963 output_val = sess.run(964 output, run_metadata=metadata, feed_dict={965 end: end_val966 })967 nodes = []968 num_transposes = 0969 for node in metadata.cost_graph.node:970 if _is_transpose(node.name):971 num_transposes += 1972 nodes.append(node.name)973 # Four transposes were initially added in the Expand phase of974 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.975 expected_num_transposes = 2976 self.assertEqual(expected_num_transposes, num_transposes)977 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)978 self._assert_trans_nchw_to_nhwc('StridedSlice-0-0', nodes)979 self._assert_vec_nhwc_to_nchw('StridedSlice-2', nodes)980 self.assertIn('StridedSlice-1-LayoutOptimizer', nodes)981 self.assertIn('StridedSlice-3-LayoutOptimizer', nodes)982 self.assertAllClose(output_val_ref, output_val, atol=1e-3)983 def testStridedSliceWithMask1011(self):984 if test.is_gpu_available(cuda_only=True):985 random_seed.set_random_seed(0)986 x = random_ops.truncated_normal([1, 784], seed=0)987 conv = _two_layer_model(x)988 # This will generate a StridedSlice op with begin mask and989 # end mask 11(1011).990 s = conv[:, :, 1:-1, :]991 output = array_ops.identity(s)992 with session.Session(config=_get_config(False)) as sess:993 output_val_ref = sess.run(output)994 with session.Session(config=_get_config()) as sess:995 metadata = config_pb2.RunMetadata()996 output_val = sess.run(output, run_metadata=metadata)997 nodes = []998 num_transposes = 0999 for node in metadata.cost_graph.node:1000 if _is_transpose(node.name):1001 num_transposes += 11002 nodes.append(node.name)1003 # Four transposes were initially added in the Expand phase of1004 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.1005 expected_num_transposes = 21006 self.assertEqual(expected_num_transposes, num_transposes)1007 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)1008 self._assert_trans_nchw_to_nhwc('strided_slice-0-0', nodes)1009 self.assertIn('strided_slice-1-LayoutOptimizer', nodes)1010 self.assertIn('strided_slice-2-LayoutOptimizer', nodes)1011 self.assertIn('strided_slice-3-LayoutOptimizer', nodes)1012 self.assertAllClose(output_val_ref, output_val, atol=1e-3)1013 def testStridedSliceWithMask0111(self):1014 if test.is_gpu_available(cuda_only=True):1015 random_seed.set_random_seed(0)1016 x = random_ops.truncated_normal([1, 784], seed=0)1017 conv = _two_layer_model(x)1018 # This will generate a StridedSlice op with begin mask and1019 # end mask 7(0111).1020 s = conv[:, :, :, 1:-1]1021 output = array_ops.identity(s)1022 with session.Session(config=_get_config(False)) as sess:1023 output_val_ref = sess.run(output)1024 with session.Session(config=_get_config()) as sess:1025 metadata = config_pb2.RunMetadata()1026 output_val = sess.run(output, run_metadata=metadata)1027 nodes = []1028 num_transposes = 01029 for node in metadata.cost_graph.node:1030 if _is_transpose(node.name):1031 num_transposes += 11032 nodes.append(node.name)1033 # Four transposes were initially added in the Expand phase of1034 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.1035 expected_num_transposes = 21036 self.assertEqual(expected_num_transposes, num_transposes)1037 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)1038 self._assert_trans_nchw_to_nhwc('strided_slice-0-0', nodes)1039 self.assertIn('strided_slice-1-LayoutOptimizer', nodes)1040 self.assertIn('strided_slice-2-LayoutOptimizer', nodes)1041 self.assertIn('strided_slice-3-LayoutOptimizer', nodes)1042 self.assertAllClose(output_val_ref, output_val, atol=1e-3)1043 def testStridedSliceGradWithNonConstAxis(self):1044 if test.is_gpu_available(cuda_only=True):1045 random_seed.set_random_seed(0)1046 x = random_ops.truncated_normal([1, 784], seed=0)1047 conv = _two_layer_model(x)1048 end = array_ops.placeholder(dtype='int32')1049 shape = array_ops.shape(conv)1050 end_val = [1, 2, 3, 4]1051 s = array_ops.strided_slice(1052 conv, [0, 0, 0, 0], end_val, strides=[1, 2, 3, 1])1053 s_grad = array_ops.strided_slice_grad(shape, [0, 0, 0, 0], end,1054 [1, 2, 3, 1], s)1055 output = array_ops.identity(s_grad)1056 with session.Session(config=_get_config(False)) as sess:1057 output_val_ref = sess.run(output, feed_dict={end: end_val})1058 with session.Session(config=_get_config()) as sess:1059 metadata = config_pb2.RunMetadata()1060 output_val = sess.run(1061 output, run_metadata=metadata, feed_dict={1062 end: end_val1063 })1064 nodes = []1065 num_transposes = 01066 for node in metadata.cost_graph.node:1067 if _is_transpose(node.name):1068 num_transposes += 11069 nodes.append(node.name)1070 # Four transposes were initially added in the Expand phase of1071 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.1072 expected_num_transposes = 21073 self.assertEqual(expected_num_transposes, num_transposes)1074 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)1075 self._assert_trans_nchw_to_nhwc('StridedSliceGrad-0-0', nodes)1076 self._assert_vec_nhwc_to_nchw('StridedSliceGrad-2', nodes)1077 self.assertIn('StridedSlice-1-LayoutOptimizer', nodes)1078 self.assertIn('StridedSlice-2-LayoutOptimizer', nodes)1079 self.assertAllClose(output_val_ref, output_val, atol=1e-3)1080 def testShapeN(self):1081 if test.is_gpu_available(cuda_only=True):1082 x = array_ops.placeholder(dtype='float32')1083 conv = _two_layer_model(x)1084 shapen = array_ops.shape_n([conv, conv])1085 output = math_ops.add(shapen[0], shapen[1])1086 x_val = [1.7] * 7841087 with session.Session(config=_get_config(False)) as sess:1088 output_val_ref = sess.run(output, feed_dict={x: x_val})1089 with session.Session(config=_get_config()) as sess:1090 metadata = config_pb2.RunMetadata()1091 output_val = sess.run(1092 output, run_metadata=metadata, feed_dict={1093 x: x_val1094 })1095 nodes = []1096 num_transposes = 01097 for node in metadata.cost_graph.node:1098 if _is_transpose(node.name):1099 num_transposes += 11100 nodes.append(node.name)1101 expected_num_transposes = 11102 self.assertEqual(expected_num_transposes, num_transposes)1103 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)1104 self._assert_vec_nchw_to_nhwc('ShapeN-0-0', nodes)1105 self.assertAllEqual(output_val_ref, output_val)1106 def testShapeNFollowedByNotConvertibleNodeReshape(self):1107 if test.is_gpu_available(cuda_only=True):1108 x = array_ops.placeholder(dtype='float32')1109 conv = _two_layer_model(x)1110 conv_reshape = array_ops.reshape(conv, [1, 1, 1, -1])1111 shapen = array_ops.shape_n([conv, conv_reshape])1112 shape = array_ops.identity(shapen[1])1113 ones = array_ops.ones(shape)1114 output = math_ops.add_n([conv_reshape, ones])1115 x_val = [1.7] * 7841116 with session.Session(config=_get_config(False)) as sess:1117 output_val_ref = sess.run(output, feed_dict={x: x_val})1118 with session.Session(config=_get_config()) as sess:1119 metadata = config_pb2.RunMetadata()1120 output_val = sess.run(1121 output, run_metadata=metadata, feed_dict={x: x_val})1122 nodes = []1123 num_transposes = 01124 for node in metadata.cost_graph.node:1125 if _is_transpose(node.name):1126 num_transposes += 11127 nodes.append(node.name)1128 expected_num_transposes = 21129 self.assertEqual(expected_num_transposes, num_transposes)1130 self._assert_trans_nhwc_to_nchw('Conv2D-0', nodes)1131 self.assertAllClose(output_val_ref, output_val, atol=1e-3)1132 def testLoop(self):1133 if test.is_gpu_available(cuda_only=True):1134 output = _loop()1135 with session.Session(config=_get_config(False)) as sess:1136 output_val_ref = sess.run(output)1137 with session.Session(config=_get_config()) as sess:1138 metadata = config_pb2.RunMetadata()1139 output_val = sess.run(output, run_metadata=metadata)1140 nodes = []1141 num_transposes = 01142 for node in metadata.cost_graph.node:1143 if _is_transpose(node.name):1144 num_transposes += 11145 nodes.append(node.name)1146 # Four transposes were initially added in the Expand phase of1147 # LayoutOptimizer; two of them are cancelled out in the Collapse phase.1148 expected_num_transposes = 21149 self.assertEqual(expected_num_transposes, num_transposes)1150 self.assertEqual(expected_num_transposes, num_transposes)1151 self._assert_trans_nhwc_to_nchw('map/while/Conv2D-0', nodes)1152 self._assert_trans_nchw_to_nhwc('map/while/MaxPool_1-0-2', nodes)1153 self.assertAllClose(output_val_ref, output_val, atol=1e-3)1154 def testLoopWithBranch(self):1155 if test.is_gpu_available(cuda_only=True):1156 output = _loop_with_branch()1157 with session.Session(config=_get_config(False)) as sess:1158 output_val_ref = sess.run(output)1159 with session.Session(config=_get_config()) as sess:1160 metadata = config_pb2.RunMetadata()1161 output_val = sess.run(output, run_metadata=metadata)1162 nodes = []1163 num_transposes = 01164 for node in metadata.cost_graph.node:1165 if _is_transpose(node.name):1166 num_transposes += 11167 nodes.append(node.name)1168 expected_num_transposes = 31169 self.assertEqual(expected_num_transposes, num_transposes)1170 self._assert_trans_nhwc_to_nchw('map/while/Conv2D-0', nodes)1171 self._assert_trans_nchw_to_nhwc('map/while/Add_1-0-2', nodes)1172 self.assertAllClose(output_val_ref, output_val, atol=1e-3)1173 def testLoopWithVecAnd4D(self):1174 if test.is_gpu_available(cuda_only=True):1175 output = _loop_with_vec_and_4d()1176 with session.Session(config=_get_config(False)) as sess:1177 output_val_ref = sess.run(output)1178 with session.Session(config=_get_config()) as sess:1179 metadata = config_pb2.RunMetadata()1180 output_val = sess.run(output, run_metadata=metadata)1181 nodes = []1182 num_transposes = 01183 for node in metadata.cost_graph.node:1184 if _is_transpose(node.name):1185 num_transposes += 11186 nodes.append(node.name)1187 expected_num_transposes = 21188 self.assertEqual(expected_num_transposes, num_transposes)1189 self._assert_trans_nhwc_to_nchw('map/while/Conv2D-0', nodes)1190 self._assert_trans_nchw_to_nhwc('map/while/Add_1-0-2', nodes)1191 self.assertAllClose(output_val_ref, output_val, atol=1e-3)1192 def testBinaryOpSecondPort(self):1193 if test.is_gpu_available(cuda_only=True):1194 output = _model_with_second_port()1195 with session.Session(config=_get_config(False)) as sess:1196 output_val_ref = sess.run(output)1197 with session.Session(config=_get_config()) as sess:1198 metadata = config_pb2.RunMetadata()1199 output_val = sess.run(output, run_metadata=metadata)1200 nodes = []1201 num_transposes = 01202 for node in metadata.cost_graph.node:1203 if _is_transpose(node.name):1204 num_transposes += 11205 nodes.append(node.name)1206 expected_num_transposes = 21207 self.assertEqual(expected_num_transposes, num_transposes)1208 self._assert_trans_nhwc_to_nchw('FusedBatchNorm-0', nodes)1209 self._assert_trans_nchw_to_nhwc('Add-0-0', nodes)1210 self.assertAllClose(output_val_ref, output_val, atol=1e-3)1211 def testGradient(self):...

Full Screen

Full Screen

config.py

Source:config.py Github

copy

Full Screen

...43 config = MigrationConfig()44 else:45 raise KeyError(f"Unknown configuration '{config_name}'")46 return config47def _get_config(config_key: str, **kwargs):48 """Get the config from environment, and throw error if there are no default values and if the value is None."""49 if 'default' in kwargs:50 value = os.getenv(config_key, kwargs.get('default'))51 else:52 value = os.getenv(config_key)53 # assert value TODO Un-comment once we find a solution to run pre-hook without initializing app54 return value55class _Config(): # pylint: disable=too-few-public-methods56 """Base class configuration that should set reasonable defaults for all the other configurations."""57 PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))58 SECRET_KEY = 'a secret'59 SQLALCHEMY_TRACK_MODIFICATIONS = False60 ALEMBIC_INI = 'migrations/alembic.ini'61 # POSTGRESQL62 DB_USER = _get_config('DATABASE_USERNAME')63 DB_PASSWORD = _get_config('DATABASE_PASSWORD')64 DB_NAME = _get_config('DATABASE_NAME')65 DB_HOST = _get_config('DATABASE_HOST')66 DB_PORT = _get_config('DATABASE_PORT', default='5432')67 SQLALCHEMY_DATABASE_URI = f'postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{int(DB_PORT)}/{DB_NAME}'68 SQLALCHEMY_ECHO = _get_config('SQLALCHEMY_ECHO', default='False').lower() == 'true'69 # JWT_OIDC Settings70 JWT_OIDC_WELL_KNOWN_CONFIG = _get_config('JWT_OIDC_WELL_KNOWN_CONFIG')71 JWT_OIDC_ALGORITHMS = _get_config('JWT_OIDC_ALGORITHMS')72 JWT_OIDC_JWKS_URI = _get_config('JWT_OIDC_JWKS_URI', default=None)73 JWT_OIDC_ISSUER = _get_config('JWT_OIDC_ISSUER')74 JWT_OIDC_AUDIENCE = _get_config('JWT_OIDC_AUDIENCE')75 JWT_OIDC_CLIENT_SECRET = _get_config('JWT_OIDC_CLIENT_SECRET')76 JWT_OIDC_CACHING_ENABLED = _get_config('JWT_OIDC_CACHING_ENABLED', default=False)77 JWT_OIDC_JWKS_CACHE_TIMEOUT = int(_get_config('JWT_OIDC_JWKS_CACHE_TIMEOUT', default=300))78 # CFS API Settings79 CFS_BASE_URL = _get_config('CFS_BASE_URL')80 CFS_CLIENT_ID = _get_config('CFS_CLIENT_ID')81 CFS_CLIENT_SECRET = _get_config('CFS_CLIENT_SECRET')82 PAYBC_PORTAL_URL = _get_config('PAYBC_PORTAL_URL')83 CONNECT_TIMEOUT = int(_get_config('CONNECT_TIMEOUT', default=10))84 GENERATE_RANDOM_INVOICE_NUMBER = _get_config('CFS_GENERATE_RANDOM_INVOICE_NUMBER', default='False')85 CFS_ACCOUNT_DESCRIPTION = _get_config('CFS_ACCOUNT_DESCRIPTION', default='BCR')86 CFS_INVOICE_PREFIX = os.getenv('CFS_INVOICE_PREFIX', 'REG')87 CFS_RECEIPT_PREFIX = os.getenv('CFS_RECEIPT_PREFIX', 'RCPT')88 CFS_PARTY_PREFIX = os.getenv('CFS_PARTY_PREFIX', 'BCR-')89 # PAYBC Direct Pay Settings90 PAYBC_DIRECT_PAY_REF_NUMBER = _get_config('PAYBC_DIRECT_PAY_REF_NUMBER')91 PAYBC_DIRECT_PAY_API_KEY = _get_config('PAYBC_DIRECT_PAY_API_KEY')92 PAYBC_DIRECT_PAY_PORTAL_URL = _get_config('PAYBC_DIRECT_PAY_PORTAL_URL')93 PAYBC_DIRECT_PAY_BASE_URL = _get_config('PAYBC_DIRECT_PAY_BASE_URL')94 PAYBC_DIRECT_PAY_CLIENT_ID = _get_config('PAYBC_DIRECT_PAY_CLIENT_ID')95 PAYBC_DIRECT_PAY_CLIENT_SECRET = _get_config('PAYBC_DIRECT_PAY_CLIENT_SECRET')96 PAYBC_DIRECT_PAY_CC_REFUND_BASE_URL = _get_config('PAYBC_DIRECT_PAY_CC_REFUND_BASE_URL')97 # NATS Config98 NATS_SERVERS = _get_config('NATS_SERVERS', default='nats://127.0.0.1:4222').split(',')99 NATS_CLUSTER_ID = _get_config('NATS_CLUSTER_ID', default='test-cluster')100 NATS_PAYMENT_CLIENT_NAME = _get_config('NATS_PAYMENT_CLIENT_NAME', default='entity.filing.worker')101 NATS_PAYMENT_SUBJECT = _get_config('NATS_PAYMENT_SUBJECT', default='entity.{product}.payment')102 NATS_MAILER_CLIENT_NAME = _get_config('NATS_MAILER_CLIENT_NAME', default='account.mailer.worker')103 NATS_MAILER_SUBJECT = _get_config('NATS_MAILER_SUBJECT', default='account.mailer')104 NATS_ACCOUNT_CLIENT_NAME = os.getenv('NATS_ACCOUNT_CLIENT_NAME', 'account.events.worker')105 NATS_ACCOUNT_SUBJECT = os.getenv('NATS_ACCOUNT_SUBJECT', 'account.events')106 # Auth API Endpoint107 AUTH_API_ENDPOINT = f'{_get_config("AUTH_API_URL")}/'108 # REPORT API Settings109 REPORT_API_BASE_URL = f'{_get_config("REPORT_API_URL")}/reports'110 # BCOL Service111 BCOL_API_ENDPOINT = _get_config('BCOL_API_URL')112 # Sentry Config113 SENTRY_ENABLE = _get_config('SENTRY_ENABLE', default=False)114 SENTRY_DSN = _get_config('SENTRY_DSN', default=None)115 # Valid Payment redirect URLs116 VALID_REDIRECT_URLS = [(val.strip() if val != '' else None)117 for val in _get_config('VALID_REDIRECT_URLS', default='').split(',')]118 # Service account details119 KEYCLOAK_SERVICE_ACCOUNT_ID = _get_config('SBC_AUTH_ADMIN_CLIENT_ID')120 KEYCLOAK_SERVICE_ACCOUNT_SECRET = _get_config('SBC_AUTH_ADMIN_CLIENT_SECRET')121 # Default number of transactions to be returned for transaction reporting122 TRANSACTION_REPORT_DEFAULT_TOTAL = int(_get_config('TRANSACTION_REPORT_DEFAULT_TOTAL', default=50))123 # Default number of routing slips to be returned for routing slip search124 ROUTING_SLIP_DEFAULT_TOTAL = int(_get_config('ROUTING_SLIP_DEFAULT_TOTAL', default=50))125 PAD_CONFIRMATION_PERIOD_IN_DAYS = int(_get_config('PAD_CONFIRMATION_PERIOD_IN_DAYS', default=3))126 # legislative timezone for future effective dating127 LEGISLATIVE_TIMEZONE = os.getenv('LEGISLATIVE_TIMEZONE', 'America/Vancouver')128 # BCOL user name for Service account payments129 BCOL_USERNAME_FOR_SERVICE_ACCOUNT_PAYMENTS = os.getenv('BCOL_USERNAME_FOR_SERVICE_ACCOUNT_PAYMENTS',130 'BCROS SERVICE ACCOUNT')131 # The number of characters which can be exposed to admins for a bank account number132 MASK_LEN = int(_get_config('MASK_LEN', default=3))133 # Config value to disable activity logs134 DISABLE_ACTIVITY_LOGS = os.getenv('DISABLE_ACTIVITY_LOGS', 'False').lower() == 'true'135 # Secret key for encrypting bank account136 ACCOUNT_SECRET_KEY = os.getenv('ACCOUNT_SECRET_KEY')137 HOLIDAYS_LIST = os.getenv('HOLIDAYS_LIST')138 OUTSTANDING_TRANSACTION_DAYS = int(os.getenv('OUTSTANDING_TRANSACTION_DAYS', '10'))139 ALLOW_LEGACY_ROUTING_SLIPS = os.getenv('ALLOW_LEGACY_ROUTING_SLIPS', 'True').lower() == 'true'140 ENABLE_PAYBC_AUTOMATED_REFUNDS = os.getenv('ENABLE_PAYBC_AUTOMATED_REFUNDS', 'false').lower() == 'true'141 TESTING = False142 DEBUG = True143class DevConfig(_Config): # pylint: disable=too-few-public-methods144 """Dev config."""145 TESTING = False146 DEBUG = True147class TestConfig(_Config): # pylint: disable=too-few-public-methods148 """In support of testing only used by the py.test suite."""149 DEBUG = True150 TESTING = True151 USE_TEST_KEYCLOAK_DOCKER = _get_config('USE_TEST_KEYCLOAK_DOCKER', default=None)152 USE_DOCKER_MOCK = _get_config('USE_DOCKER_MOCK', default=None)153 # POSTGRESQL154 DB_USER = _get_config('DATABASE_TEST_USERNAME', default='postgres')155 DB_PASSWORD = _get_config('DATABASE_TEST_PASSWORD', default='postgres')156 DB_NAME = _get_config('DATABASE_TEST_NAME', default='paytestdb')157 DB_HOST = _get_config('DATABASE_TEST_HOST', default='localhost')158 DB_PORT = _get_config('DATABASE_TEST_PORT', default='5432')159 SQLALCHEMY_DATABASE_URI = _get_config(160 'DATABASE_TEST_URL',161 default=f'postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{int(DB_PORT)}/{DB_NAME}'162 )163 JWT_OIDC_TEST_MODE = True164 # JWT_OIDC_ISSUER = _get_config('JWT_OIDC_TEST_ISSUER')165 JWT_OIDC_TEST_AUDIENCE = _get_config('JWT_OIDC_TEST_AUDIENCE')166 JWT_OIDC_TEST_CLIENT_SECRET = _get_config('JWT_OIDC_TEST_CLIENT_SECRET')167 JWT_OIDC_TEST_ISSUER = _get_config('JWT_OIDC_TEST_ISSUER')168 JWT_OIDC_WELL_KNOWN_CONFIG = _get_config('JWT_OIDC_WELL_KNOWN_CONFIG')169 JWT_OIDC_TEST_ALGORITHMS = _get_config('JWT_OIDC_TEST_ALGORITHMS')170 JWT_OIDC_TEST_JWKS_URI = _get_config('JWT_OIDC_TEST_JWKS_URI', default=None)171 JWT_OIDC_TEST_KEYS = {172 'keys': [173 {174 'kid': 'sbc-auth-web',175 'kty': 'RSA',176 'alg': 'RS256',177 'use': 'sig',178 'n': 'AN-fWcpCyE5KPzHDjigLaSUVZI0uYrcGcc40InVtl-rQRDmAh-C2W8H4_Hxhr5VLc6crsJ2LiJTV_E72S03pzpOOaaYV6-'179 'TzAjCou2GYJIXev7f6Hh512PuG5wyxda_TlBSsI-gvphRTPsKCnPutrbiukCYrnPuWxX5_cES9eStR',180 'e': 'AQAB'181 }182 ]183 }184 JWT_OIDC_TEST_PRIVATE_KEY_JWKS = {185 'keys': [186 {187 'kid': 'sbc-auth-web',188 'kty': 'RSA',189 'alg': 'RS256',190 'use': 'sig',191 'n': 'AN-fWcpCyE5KPzHDjigLaSUVZI0uYrcGcc40InVtl-rQRDmAh-C2W8H4_Hxhr5VLc6crsJ2LiJTV_E72S03pzpOOaaYV6-'192 'TzAjCou2GYJIXev7f6Hh512PuG5wyxda_TlBSsI-gvphRTPsKCnPutrbiukCYrnPuWxX5_cES9eStR',193 'e': 'AQAB',194 'd': 'C0G3QGI6OQ6tvbCNYGCqq043YI_8MiBl7C5dqbGZmx1ewdJBhMNJPStuckhskURaDwk4-'195 '8VBW9SlvcfSJJrnZhgFMjOYSSsBtPGBIMIdM5eSKbenCCjO8Tg0BUh_'196 'xa3CHST1W4RQ5rFXadZ9AeNtaGcWj2acmXNO3DVETXAX3x0',197 'p': 'APXcusFMQNHjh6KVD_hOUIw87lvK13WkDEeeuqAydai9Ig9JKEAAfV94W6Aftka7tGgE7ulg1vo3eJoLWJ1zvKM',198 'q': 'AOjX3OnPJnk0ZFUQBwhduCweRi37I6DAdLTnhDvcPTrrNWuKPg9uGwHjzFCJgKd8KBaDQ0X1rZTZLTqi3peT43s',199 'dp': 'AN9kBoA5o6_Rl9zeqdsIdWFmv4DB5lEqlEnC7HlAP-3oo3jWFO9KQqArQL1V8w2D4aCd0uJULiC9pCP7aTHvBhc',200 'dq': 'ANtbSY6njfpPploQsF9sU26U0s7MsuLljM1E8uml8bVJE1mNsiu9MgpUvg39jEu9BtM2tDD7Y51AAIEmIQex1nM',201 'qi': 'XLE5O360x-MhsdFXx8Vwz4304-MJg-oGSJXCK_ZWYOB_FGXFRTfebxCsSYi0YwJo-oNu96bvZCuMplzRI1liZw'202 }203 ]204 }205 JWT_OIDC_TEST_PRIVATE_KEY_PEM = """-----BEGIN RSA PRIVATE KEY-----206MIICXQIBAAKBgQDfn1nKQshOSj8xw44oC2klFWSNLmK3BnHONCJ1bZfq0EQ5gIfg207tlvB+Px8Ya+VS3OnK7Cdi4iU1fxO9ktN6c6TjmmmFevk8wIwqLthmCSF3r+3+h4e208ddj7hucMsXWv05QUrCPoL6YUUz7Cgpz7ra24rpAmK5z7lsV+f3BEvXkrUQIDAQAB209AoGAC0G3QGI6OQ6tvbCNYGCqq043YI/8MiBl7C5dqbGZmx1ewdJBhMNJPStuckhs210kURaDwk4+8VBW9SlvcfSJJrnZhgFMjOYSSsBtPGBIMIdM5eSKbenCCjO8Tg0BUh/211xa3CHST1W4RQ5rFXadZ9AeNtaGcWj2acmXNO3DVETXAX3x0CQQD13LrBTEDR44ei212lQ/4TlCMPO5bytd1pAxHnrqgMnWovSIPSShAAH1feFugH7ZGu7RoBO7pYNb6N3ia213C1idc7yjAkEA6Nfc6c8meTRkVRAHCF24LB5GLfsjoMB0tOeEO9w9Ous1a4o+D24b214AePMUImAp3woFoNDRfWtlNktOqLel5PjewJBAN9kBoA5o6/Rl9zeqdsIdWFmv4DB2155lEqlEnC7HlAP+3oo3jWFO9KQqArQL1V8w2D4aCd0uJULiC9pCP7aTHvBhcCQQDb216W0mOp436T6ZaELBfbFNulNLOzLLi5YzNRPLppfG1SRNZjbIrvTIKVL4N/YxLvQbT217NrQw+2OdQACBJiEHsdZzAkBcsTk7frTH4yGx0VfHxXDPjfTj4wmD6gZIlcIr9lZg2184H8UZcVFN95vEKxJiLRjAmj6g273pu9kK4ymXNEjWWJn219-----END RSA PRIVATE KEY-----"""220 CFS_BASE_URL = 'http://localhost:8080/paybc-api'221 CFS_CLIENT_ID = 'TEST'222 CFS_CLIENT_SECRET = 'TEST'223 PAYBC_PORTAL_URL = 'https://paydev.gov.bc.ca/public/directpay'224 SERVER_NAME = 'auth-web.dev.com'225 REPORT_API_BASE_URL = 'http://localhost:8080/reports-api/api/v1/reports'226 AUTH_API_ENDPOINT = 'http://localhost:8080/auth-api/'227 NATS_SUBJECT = 'entity.filing.test'228 BCOL_API_ENDPOINT = 'http://localhost:8080/bcol-api'229 VALID_REDIRECT_URLS = ['http://localhost:8080/*']230 TRANSACTION_REPORT_DEFAULT_TOTAL = 10231 PAYBC_DIRECT_PAY_API_KEY = 'TESTKEYSECRET'232 PAYBC_DIRECT_PAY_REF_NUMBER = 'REF1234'233 PAYBC_DIRECT_PAY_PORTAL_URL = 'https://paydev.gov.bc.ca/public/directsale'234 PAYBC_DIRECT_PAY_BASE_URL = 'http://localhost:8080/paybc-api'235 PAYBC_DIRECT_PAY_CC_REFUND_BASE_URL = PAYBC_DIRECT_PAY_BASE_URL236 PAYBC_DIRECT_PAY_CLIENT_ID = 'TEST'237 PAYBC_DIRECT_PAY_CLIENT_SECRET = 'TEST'238 PAD_CONFIRMATION_PERIOD_IN_DAYS = 3239 # Secret key for encrypting bank account240 ACCOUNT_SECRET_KEY = 'mysecretkeyforbank'241 HOLIDAYS_LIST = os.getenv('HOLIDAYS_LIST', default='2021-Jan-01,2021-Feb-15,2021-Apr-02,2021-May-24,2021-Jul-1, '242 '2021-Jul-1, 2021-Aug-2, 2021-Sep-6,2021-Oct-11, 2021-Nov-11, '243 '2021-Dec-25')244class ProdConfig(_Config): # pylint: disable=too-few-public-methods245 """Production environment configuration."""246 SECRET_KEY = _get_config('SECRET_KEY', default=None)247 if not SECRET_KEY:248 SECRET_KEY = os.urandom(24)249 print('WARNING: SECRET_KEY being set as a one-shot', file=sys.stderr)250 TESTING = False251 DEBUG = False252class MigrationConfig(): # pylint: disable=too-few-public-methods253 """Config for db migration."""254 TESTING = False255 DEBUG = True256 # POSTGRESQL257 DB_USER = _get_config('DATABASE_USERNAME')258 DB_PASSWORD = _get_config('DATABASE_PASSWORD')259 DB_NAME = _get_config('DATABASE_NAME')260 DB_HOST = _get_config('DATABASE_HOST')261 DB_PORT = _get_config('DATABASE_PORT', default='5432')262 SQLALCHEMY_DATABASE_URI = f'postgresql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{int(DB_PORT)}/{DB_NAME}'...

Full Screen

Full Screen

charge_api.py

Source:charge_api.py Github

copy

Full Screen

...25 :return:26 """27 # 格式化处理请求的数据28 # URI29 req_uri = self._get_config(self.__config, "CREATE.URI")30 req_method = self._get_config(self.__config, "CREATE.METHOD")31 req_data = {32 self._get_config(self.__config, "CREATE.PARAM.APP"): data_dict.get("app"),33 self._get_config(self.__config, "CREATE.PARAM.ORDER_NO"): data_dict.get("order_no"),34 self._get_config(self.__config, "CREATE.PARAM.CHANNEL"): data_dict.get("channel"),35 self._get_config(self.__config, "CREATE.PARAM.AMOUNT"): data_dict.get("amount"),36 self._get_config(self.__config, "CREATE.PARAM.CLIENT_IP"): data_dict.get("client_ip"),37 self._get_config(self.__config, "CREATE.PARAM.CURRENCY"): data_dict.get("currency"),38 self._get_config(self.__config, "CREATE.PARAM.SUBJECT"): data_dict.get("subject"),39 self._get_config(self.__config, "CREATE.PARAM.BODY"): data_dict.get("body"),40 self._get_config(self.__config, "CREATE.PARAM.DESCRIPTION"): data_dict.get("description"),41 self._get_config(self.__config, "CREATE.PARAM.EXTRA"): data_dict.get("extra")42 }43 req_data = self._remove_none_param(req_data)44 # 认证45 req_auth = self.auth46 req_cookies = {}47 req_headers = self._get_headers_for_signature(uri=req_uri, request_body=req_data)48 # 真正的发请求49 self._send(uri=req_uri,50 method=req_method,51 data_dict=req_data,52 auth=req_auth,53 cookies=req_cookies,54 headers=req_headers)55 # 返回响应的结果56 resp_body_key_list = self._get_config(self.__config, "CREATE.RESP.DATA_KEY")57 return self._parse(body_key_list=resp_body_key_list)58 def view(self, charge_id: str):59 """60 真实的调用 GET /v1/charges/{charge_id} 接口61 :param charge_id:62 :return:63 """64 # 格式化处理请求的数据65 req_uri = self._get_config(self.__config, "VIEW.URI") % charge_id66 req_method = self._get_config(self.__config, "VIEW.METHOD")67 req_auth = self.auth68 req_cookies = {}69 req_headers = self._get_headers_for_signature(uri=req_uri)70 # 真正的发请求71 self._send(uri=req_uri,72 method=req_method,73 auth=req_auth,74 cookies=req_cookies,75 headers=req_headers)76 resp_body_key_list = self._get_config(self.__config, "VIEW.RESP.DATA_KEY")77 return self._parse(body_key_list=resp_body_key_list)78 def query(self, data_dict: dict):79 """80 真实的调用 GET /v1/charges/xxxx=xxxx 接口81 :param data_dict:82 :return:83 """84 # 格式化处理请求的数据85 req_uri = self._get_config(self.__config, "VIEW.URI")86 req_method = self._get_config(self.__config, "VIEW.METHOD")87 req_data = {88 self._get_config(self.__config, "CREATE.PARAM.APP.get(id)"): data_dict.get("app"),89 self._get_config(self.__config, "CREATE.PARAM.LIMIT"): data_dict.get("limit"),90 self._get_config(self.__config, "CREATE.PARAM.CHANNEL"): data_dict.get("channel"),91 self._get_config(self.__config, "CREATE.PARAM.REFUNDED"): data_dict.get("refunded"),92 self._get_config(self.__config, "CREATE.PARAM.REVERSED"): data_dict.get("reversed"),93 self._get_config(self.__config, "CREATE.PARAM.PAID"): data_dict.get("paid"),94 self._get_config(self.__config, "CREATE.PARAM.CREATED_GT"): data_dict.get("created_gt"),95 self._get_config(self.__config, "CREATE.PARAM.CREATED_LT"): data_dict.get("created_lt"),96 self._get_config(self.__config, "CREATE.PARAM.CREATED_GTE"): data_dict.get("created_gte"),97 self._get_config(self.__config, "CREATE.PARAM.CREATED_LTE"): data_dict.get("created_lte")98 }99 req_data = self._remove_none_param(req_data)100 req_uri = encode_url(url=req_uri, params=req_data)101 req_auth = self.auth102 req_cookies = {}103 req_headers = self._get_headers_for_signature(uri=req_uri)104 # 真正的发请求105 self._send(uri=req_uri,106 method=req_method,107 auth=req_auth,108 cookies=req_cookies,109 headers=req_headers)110 resp_body_key_list = self._get_config(self.__config, "VIEW.RESP.DATA_KEY")111 return self._parse(body_key_list=resp_body_key_list)112 def reverse(self, charge_id):113 """114 真实的调用 POST .v1.charges.{charge_id}.reverse 接口115 :param charge_id:116 :return:117 """118 # 格式化处理请求的数据119 # URI120 # 格式化处理请求的数据121 req_uri = self._get_config(self.__config, "REVERSE.URI") % charge_id122 req_method = self._get_config(self.__config, "REVERSE.METHOD")123 req_auth = self.auth124 req_cookies = {}125 req_headers = self._get_headers_for_signature(uri=req_uri)126 # 真正的发请求127 self._send(uri=req_uri,128 method=req_method,129 auth=req_auth,130 cookies=req_cookies,131 headers=req_headers)132 resp_body_key_list = self._get_config(self.__config, "REVERSE.RESP.DATA_KEY")...

Full Screen

Full Screen

configuration.py

Source:configuration.py Github

copy

Full Screen

...22 except:23 print("error configparser ")24 return p25 return None26def _get_config(p, section, key, env_var, default):27 ''' helper function for get_config '''28 if env_var is not None:29 value = os.environ.get(env_var, None)30 if value is not None:31 return value32 if p is not None:33 try:34 return p.get(section, key, raw=True)35 except:36 return default37 return default38p = load_config_file()39# SAAS_HOST = _get_config(p, "cloudenv", 'saas_host', "SAAS_HOST", "172.16.2.125") #测试环境40SAAS_HOST = _get_config(p, "cloudenv", 'saas_host', "SAAS_HOST", "docker-mgt.haima.me") #开发环境41#SAAS_HOST = _get_config(p, "cloudenv", 'saas_host', "SAAS_HOST", "172.16.2.100") #demo42#SAAS_HOST = _get_config(p, "cloudenv", 'saas_host', "SAAS_HOST", "172.16.2.169") #支付43#SAAS_HOST = _get_config(p, "cloudenv", 'saas_host', "SAAS_HOST", "saasauth-migu# .haimawan.com") #teng saas44#SAAS_HOST = "saasAuth-pre.haimawan.com"45# SAAS_PORT = _get_config(p, "cloudenv", 'saas_port', "SAAS_PORT", "8010")46SAAS_PORT = _get_config(p, "cloudenv", 'saas_port', "SAAS_PORT", "8070")47DB_HOST = _get_config(p, "cloudenv", 'db_host', "DB_HOST", "docker-mgt.haima.me") #开发环境48DB_PORT = _get_config(p, "cloudenv", 'db_port', "DB_PORT", 3306)49DB_USER = _get_config(p, "cloudenv", 'db_user', "DB_USER", "admin")50DB_PASSWD = _get_config(p, "cloudenv", 'db_passwd', "DB_PASSWD", "123qwe")51TENANT_DB = _get_config(p, "cloudenv", 'tenant_db', "TENANT_DB", 'db_tenant_mgt')52CORE_DB = _get_config(p, "cloudenv", 'tenant_db', "CORE_DB", 'db_service_core')53MC_DB = _get_config(p, "cloudenv", 'msg_center_db', "MC_DB", 'db_msg_center')54#55# AMQP_USER = _get_config(p, 'cloudenv', 'amqp_user', "AMQP_USER", "admin")56# AMQP_PASSWD = _get_config(p, 'cloudenv', 'amqp_passwd', "AMQP_PASSWD", "admin")57# AMQP_HOST = _get_config(p, 'cloudenv', 'amqp_host', "AMQP_HOST", "docker-mgt.haima.me")58# AMQP_PORT = _get_config(p, 'cloudenv', 'amqp_port', "AMQP_PORT", 5672)59# VIRTUAL_HOST = _get_config(p, 'cloudenv', 'virtual_host', "VIRTUAL_HOST", "/")60AMQP_USER = _get_config(p, 'cloudenv', 'amqp_user', "AMQP_USER", "admin")61AMQP_PASSWD = _get_config(p, 'cloudenv', 'amqp_passwd', "AMQP_PASSWD", "HaimaRabbBit81")62AMQP_HOST = _get_config(p, 'cloudenv', 'amqp_host', "AMQP_HOST", "service-core.stable.haima001.com")63AMQP_PORT = _get_config(p, 'cloudenv', 'amqp_port', "AMQP_PORT", 5672)64VIRTUAL_HOST = _get_config(p, 'cloudenv', 'virtual_host', "VIRTUAL_HOST", "fc")65REDIS_HOST = _get_config(p, 'cloudenv', 'redis_host', "REDIS_HOST", "123.206.46.217")66# REDIS_HOST = _get_config(p, 'cloudenv', 'redis_host', "REDIS_HOST", "docker-mgt.haima.me")67REDIS_PORT = _get_config(p, 'cloudenv', 'redis_port', "REDIS_PORT", 6379)68REDIS_DB = _get_config(p, 'cloudenv', 'redis_db', "REDIS_DB", 0)69# REDIS_DB = _get_config(p, 'cloudenv', 'redis_db', "REDIS_DB", 0)70REDIS_PASSWD = _get_config(p, 'cloudenv', 'redis_passwd', "REDIS_PASSWD", "redispass")71# REDIS_PASSWD = _get_config(p, 'cloudenv', 'redis_passwd', "REDIS_PASSWD", "123qwe")72MONGO_HOST = _get_config(p, 'cloudenv', 'redis_host', "REDIS_HOST", "172.16.2.16")73MONGO_PORT = _get_config(p, 'cloudenv', 'redis_port', "REDIS_PORT", 27017)74CONFIG_DB = _get_config(p, 'cloudenv', 'config_db', "CONFIG_DB", 'db_tenant_test')75MONGO_USER = _get_config(p, 'cloudenv', 'mongo_user', "MONGO_USER", "admin")76MONGO_PASSWORD = _get_config(p, 'cloudenv', 'mongo_password', "MONGO_PASSWORD", "123qwe")77USE_CTOKEN_BACKDOOR = _get_config(p, 'cloudenv', 'use_ctoken_backdoor', "USE_CTOKEN_BACKDOOR", 1)78PROTOCOL_VERSION = _get_config(p, 'cloudenv', 'protocol_version', "PROTOCOL_VERSION", "1.1")79MOCK_SERVER = _get_config(p, "cloudenv", 'mock_server', "MOCK_SERVER", "127.0.0.1")80MOCK_PORT = _get_config(p, "cloudenv", 'mock_port', "MOCK_PORT", "8080")81SECRET_KEY = _get_config(p, 'cloudenv', 'secret_key', 'SECRET_KEY', 'and0123456789012')...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Molotov automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful