def testAvgPoolValidWithBroadcast(self): with self.session() as sess: np.random.seed(0) shape = [1, 10, 10, 1] data = np.random.uniform(0, 1, shape) # The expected answer was generated using TF on the cpu expected = [[[[0.52647954], [0.44196457], [0.49284577]], [[0.44039682], [0.44067329], [0.44934618]], [[0.46444583], [0.45419583], [0.38236427]]]] with ops.device("/device:IPU:0"): pa = array_ops.placeholder(np.float32, shape, name="a") output = nn.avg_pool(pa, ksize=[1, 5, 5, 1], strides=[1, 2, 2, 1], data_format='NHWC', padding='VALID', name="avg") report = tu.ReportJSON(self, sess) sess.run(variables.global_variables_initializer()) report.reset() fd = {pa: data} result = sess.run(output, fd) self.assertAllClose(result, expected) report.parse_log(assert_len=4) ok = ['__seed*', 'avg/avg-pool*/avgPool5x5'] report.assert_all_compute_sets_and_list(ok)
def avg_pool2d(inputs, kernel_size, stride=2, padding='VALID', outputs_collections=None, scope=None): """Adds a Avg Pooling op. It is assumed by the wrapper that the pooling is only done per image and not in depth or batch. Args: inputs: a tensor of size [batch_size, height, width, depth]. kernel_size: a list of length 2: [kernel_height, kernel_width] of the pooling kernel over which the op is computed. Can be an int if both values are the same. stride: a list of length 2: [stride_height, stride_width]. Can be an int if both strides are the same. Note that presently both strides must have the same value. padding: the padding method, either 'VALID' or 'SAME'. outputs_collections: collection to add the outputs. scope: Optional scope for op_scope. Returns: a tensor representing the results of the pooling operation. """ with ops.op_scope([inputs], scope, 'AvgPool2D') as sc: kernel_h, kernel_w = utils.two_element_tuple(kernel_size) stride_h, stride_w = utils.two_element_tuple(stride) outputs = nn.avg_pool(inputs, ksize=[1, kernel_h, kernel_w, 1], strides=[1, stride_h, stride_w, 1], padding=padding) return utils.collect_named_outputs(outputs_collections, sc, outputs)
def loop_fn(i): with g: x1 = array_ops.gather(x, i) output = nn.avg_pool( x1, ksize, strides=[1, 2, 2, 1], padding="VALID", data_format="NHWC") loss = nn.l2_loss(output) return output, g.gradient(loss, x1)
def testAvgPoolValidPaddingWithStridesF32(self): with self.session() as sess: with ops.device("/device:IPU:0"): pa = array_ops.placeholder(np.float32, [1, 1, 10, 10], name="a") output = nn.avg_pool(pa, ksize=[1, 1, 5, 5], strides=[1, 1, 2, 2], data_format='NCHW', padding='VALID') fd = {pa: np.ones([1, 1, 10, 10])} result = sess.run(output, fd) self.assertAllClose(result, np.ones([1, 1, 3, 3]))
def testAvgPoolSamePaddingWithStridesF32Dim12(self): with self.session() as sess: with ops.device("/device:IPU:0"): pa = array_ops.placeholder(np.float32, [1, 10, 10, 1], name="a") output = nn.avg_pool(pa, ksize=[1, 5, 5, 1], strides=[1, 2, 2, 1], data_format='NHWC', padding='SAME', name="avg") fd = {pa: np.ones([1, 10, 10, 1])} result = sess.run(output, fd) self.assertAllClose(result, np.ones([1, 5, 5, 1]))
def template(x_shape=[2, 3, 4, 5], ksize=[1, 3, 3, 1], strides=[1, 1, 1, 1], padding="VALID", description: str = ""): from tensorflow.python.ops import nn x = tf.placeholder(np.float32, x_shape, "x") y = nn.avg_pool(x, ksize=ksize, strides=strides, padding=padding) vx = np.random.rand(*x_shape).astype(np.float32) - 0.5 with tf.Session() as sess: vy, = sess.run([y], {x: vx}) graph = TensorFlowConverter(sess, batch_size=2).convert([x], [y]) generate_kernel_test_case( description=f"[TensorFlow] AvgPool {description}", graph=graph, inputs={graph.inputs[0]: vx}, expected={graph.outputs[0]: vy} )
def testAvgPoolSameWithReshape(self): np.random.seed(0) shape = [1, 10, 10, 1] data = np.random.uniform(0, 1, shape) # The expected answer was generated using TF on the cpu expected = [[[[0.64431685], [0.51738459], [0.49705142], [0.60235918], [0.73694557]], [[0.57755166], [0.47387227], [0.40451217], [0.4876942], [0.55843753]], [[0.49037799], [0.4466258], [0.35829377], [0.40070742], [0.37205362]], [[0.47563809], [0.4075647], [0.34894851], [0.35470542], [0.3322109]], [[0.52914065], [0.45464769], [0.38156652], [0.32455513], [0.33199897]]]] with ops.device("/device:IPU:0"): pa = array_ops.placeholder(np.float32, shape, name="a") output = nn.avg_pool(pa, ksize=[1, 5, 5, 1], strides=[1, 2, 2, 1], data_format='NHWC', padding='SAME', name="avg") with ops.device('cpu'): report = gen_ipu_ops.ipu_event_trace() tu.configure_ipu_system(True, True, True) with tu.ipu_session() as sess: sess.run(variables.global_variables_initializer()) sess.run(report) fd = {pa: data} result = sess.run(output, fd) self.assertAllClose(result, expected) result = sess.run(report) self.assertEqual(len(result), 4) s = tu.extract_all_strings_from_event_trace(result) cs_list = tu.get_compute_sets_from_report(s) ok = ['__seed*', 'avg/custom-call*/avgPool5x5'] self.assertTrue(tu.check_all_compute_sets_and_list(cs_list, ok))
def testAvgPoolValidWithBroadcast(self): np.random.seed(0) shape = [1, 10, 10, 1] data = np.random.uniform(0, 1, shape) # The expected answer was generated using TF on the cpu expected = [[[[0.52647954], [0.44196457], [0.49284577]], [[0.44039682], [0.44067329], [0.44934618]], [[0.46444583], [0.45419583], [0.38236427]]]] with ops.device("/device:IPU:0"): pa = array_ops.placeholder(np.float32, shape, name="a") output = nn.avg_pool(pa, ksize=[1, 5, 5, 1], strides=[1, 2, 2, 1], data_format='NHWC', padding='VALID', name="avg") with ops.device('cpu'): report = gen_ipu_ops.ipu_event_trace() tu.configure_ipu_system(True, True, True) with tu.ipu_session() as sess: sess.run(variables.global_variables_initializer()) sess.run(report) fd = {pa: data} result = sess.run(output, fd) self.assertAllClose(result, expected) result = sess.run(report) self.assertEqual(len(result), 4) s = tu.extract_all_strings_from_event_trace(result) cs_list = tu.get_compute_sets_from_report(s) ok = ['__seed*', 'avg/custom-call*/avgPool5x5'] self.assertTrue(tu.check_all_compute_sets_and_list(cs_list, ok))
def testAvgPoolSameWithReshape(self): with self.session() as sess: np.random.seed(0) shape = [1, 10, 10, 1] data = np.random.uniform(0, 1, shape) # The expected answer was generated using TF on the cpu expected = [[[[0.64431685], [0.51738459], [0.49705142], [0.60235918], [0.73694557]], [[0.57755166], [0.47387227], [0.40451217], [0.4876942], [0.55843753]], [[0.49037799], [0.4466258], [0.35829377], [0.40070742], [0.37205362]], [[0.47563809], [0.4075647], [0.34894851], [0.35470542], [0.3322109]], [[0.52914065], [0.45464769], [0.38156652], [0.32455513], [0.33199897]]]] with ops.device("/device:IPU:0"): pa = array_ops.placeholder(np.float32, shape, name="a") output = nn.avg_pool(pa, ksize=[1, 5, 5, 1], strides=[1, 2, 2, 1], data_format='NHWC', padding='SAME', name="avg") report = tu.ReportJSON(self, sess) sess.run(variables.global_variables_initializer()) report.reset() fd = {pa: data} result = sess.run(output, fd) self.assertAllClose(result, expected) report.parse_log(assert_len=4) ok = ['__seed*', 'avg/avg-pool*/avgPool5x5'] report.assert_all_compute_sets_and_list(ok)
fixed_prec = 4 testdata_scale = 10 inputs_vals = np.random.normal(size=(batch_size, input_width, input_height, input_channels)) * testdata_scale // 1 inputs = tf.constant(inputs_vals, dtype=tf.float32) quantizer = Quantizers.NoQuantizer() output = QAP.avg_pool2d(inputs, kernel_size, strides, padding=padding, quantizer=quantizer) gold_output = nn.avg_pool(inputs, [1, kernel_size, kernel_size, 1], [1, strides, strides, 1], padding=padding) with tf.Session() as sess: gold_result = gold_output.eval().flatten() result = output.eval().flatten() # print(sess.run(gold_output)) pass failed = False for i in range(len(result)): if result[i] != gold_result[i]: failed = True break print('QAvgPool test:') if failed: