import unittest
import numpy as np
from op_test import OpTest


class TestConv2dOp(OpTest):
    def setUp(self):
        self.op_type = "conv2d"
        batch_size = 2
        input_channels = 3
        input_height = 5
        input_width = 5
        output_channels = 6
        filter_height = 3
        filter_width = 3
        stride = 1
        padding = 0
        groups = 3
        output_height = (input_height - filter_height + 2 * padding
                         ) / stride + 1
        output_width = (input_width - filter_width + 2 * padding) / stride + 1
        input = np.random.random((batch_size, input_channels, input_height,
                                  input_width)).astype("float32")

        filter = np.random.random(
            (output_channels, input_channels / groups, filter_height,
             filter_width)).astype("float32")
        output = np.ndarray(
            (batch_size, output_channels, output_height, output_width))

        self.inputs = {'Input': input, 'Filter': filter}
        self.attrs = {'strides': [1, 1], 'paddings': [0, 0], 'groups': groups}

        output_group_channels = output_channels / groups
        input_group_channels = input_channels / groups
        for batchid in xrange(batch_size):
            for group in xrange(groups):
                for outchannelid in range(group * output_group_channels,
                                          (group + 1) * output_group_channels):
                    for rowid in xrange(output_height):
                        for colid in xrange(output_width):
                            start_h = (rowid * stride) - padding
                            start_w = (colid * stride) - padding
                            output_value = 0.0
                            for inchannelid in range(
                                    group * input_group_channels,
                                (group + 1) * input_group_channels):
                                for frowid in xrange(filter_height):
                                    for fcolid in xrange(filter_width):
                                        input_value = 0.0
                                        inrowid = start_h + frowid
                                        incolid = start_w + fcolid
                                        if ((inrowid >= 0 and
                                             inrowid < input_height) and
                                            (incolid >= 0 and
                                             incolid < input_width)):
                                            input_value = input[batchid][
                                                inchannelid][inrowid][incolid]
                                        filter_value = filter[outchannelid][
                                            inchannelid % input_group_channels][
                                                frowid][fcolid]
                                        output_value += input_value * filter_value
                            output[batchid][outchannelid][rowid][
                                colid] = output_value

        self.outputs = {'Output': output}

    def test_check_output(self):
        self.check_output()

    def test_check_grad(self):
        self.check_grad(set(['Input', 'Filter']), 'Output')


if __name__ == '__main__':
    unittest.main()

@@ -98,32 +98,59 @@
 ef setUp(self):%0A
+        self.init_groups()%0A
         self.op_
@@ -386,27 +386,8 @@
 = 0%0A
-        groups = 3%0A
     
@@ -790,24 +790,29 @@
 _channels / 
+self.
 groups, filt
@@ -1049,16 +1049,29 @@
 ttrs = %7B
+%0A            
 'strides
@@ -1080,16 +1080,28 @@
  %5B1, 1%5D,
+%0A           
  'paddin
@@ -1112,16 +1112,28 @@
  %5B0, 0%5D,
+%0A           
  'groups
@@ -1135,22 +1135,36 @@
 roups': 
+self.
 groups
+%0A        
 %7D%0A%0A     
@@ -1200,32 +1200,37 @@
 tput_channels / 
+self.
 groups%0A        i
@@ -1268,16 +1268,21 @@
 nnels / 
+self.
 groups%0A 
@@ -1351,24 +1351,29 @@
 p in xrange(
+self.
 groups):%0A   
@@ -3379,16 +3379,156 @@
 tput')%0A%0A
+    def init_groups(self):%0A        self.groups = 1%0A%0A%0Aclass TestWithGroup(TestConv2dOp):%0A    def init_groups(self):%0A        self.groups = 3%0A%0A
 %0Aif __na

Add test with groups=1.