예제 #1
0
    def forward(self, inps):
        src = inps[0]
        weight = inps[1]
        try:
            bias = inps[2]
        except IndexError:
            bias = None

        if bias is not None:
            if bias.shape.ndim == 3:
                bias = F.expand_dims(bias, axis=0)
            elif bias.shape.ndim == 1:
                bias = F.expand_dims(bias, axis=[0, 2, 3])
            else:
                raise Exception(f"Invalid Conv2d bias's shape {bias.shape}")

        if self.param["groups"] != 1:
            groups = self.param["groups"]
            IC = src.shape.numpy()[1]
            OC = weight.shape.numpy()[0]
            FH = weight.shape.numpy()[2]
            FW = weight.shape.numpy()[3]
            target_shape = [groups, int(OC / groups), int(IC / groups), FH, FW]
            weight = F.reshape(weight, target_shape)

        return F.conv2d(
            src,
            weight,
            bias,
            stride=self.param["stride"],
            padding=self.param["padding"],
            dilation=self.param["dilation"],
            groups=self.param["groups"],
        )
예제 #2
0
 def forward(self, x):
     x1 = self.block_0(x)
     x2 = self.block_1(x)
     y = x1 + x2
     y = F.reshape(y, (-1))
     y = y * 3
     return y
예제 #3
0
def test_host_compute_elemwise():
    a = mge.tensor([[1, 2], [2, 3], [3, 4]], dtype="float32")
    b = mge.tensor([1, 1], dtype="int32")
    # check DepType::VALUE is still known
    c = b * 2
    with pytest.raises(RuntimeError):
        d = F.reshape(a, c)
예제 #4
0
def test_level1_infer_shape_with_unknown():
    config_async_level(2)
    a = mge.tensor([[1, 2, 2, 3]], dtype="float32")
    b = mge.tensor([1, 1])
    multi2 = mge.tensor(np.array([[2, 0], [0, 2]]), dtype="float32")
    c = F.matmul(b, multi2)
    # make DepType::SHAPE unknown
    d = F.reshape(a, c)
    e = mge.tensor([[1, 2]], dtype="float32")
    config_async_level(1)
    # test src no shape, throw in level1
    with pytest.raises(RuntimeError):
        f = F.reshape(d, b)
    with pytest.raises(RuntimeError):
        g = F.matmul(d, e)
    config_async_level(2)
예제 #5
0
def test_level1_infer_value():
    config_async_level(1)
    a = mge.tensor([[1, 2], [2, 3], [3, 4]], dtype="float32")
    b = mge.tensor([1, 1], dtype="float32")
    # make DepType::VALUE unknown
    c = b * 2
    with pytest.raises(RuntimeError):
        d = F.reshape(a, c)
예제 #6
0
 def forward(self, inps):
     target_shape = inps[1].numpy()
     valid_test = [i for i, k in enumerate(target_shape) if k == -1]
     assert (
         len(valid_test) <=
         1), "Target Shape of ReShape Opr only contains '-1' Once At Most"
     if 0 not in target_shape and -1 not in target_shape:
         return F.reshape(inps[0], target_shape)
     else:
         tshape = []
         inp_shape = inps[0].shape.numpy()
         for i, k in enumerate(target_shape):
             if k == 0:
                 tshape.append(inp_shape[i])
             else:
                 tshape.append(k)
         return F.reshape(inps[0], tshape)
예제 #7
0
 def forward(self, input):
     A = input.shape[0]
     shape = astensor1d((A, A),
                        self.data,
                        dtype="int32",
                        device=input.device)
     x = F.reshape(self.data, shape)
     o = input + x
     return o
예제 #8
0
    def forward(self, x):
        x = F.reshape(x, (1, 3, 224, 224))
        x = self.extract_features(x)["res5"]

        x = F.avg_pool2d(x, 7)
        x = F.flatten(x, 1)
        x = self.fc(x)

        return x
예제 #9
0
def test_level1_infer_value():
    config_async_level(1)
    a = mge.tensor([[1, 2], [2, 3], [3, 4]], dtype="float32")
    b = mge.tensor([1, 1], dtype="float32")
    identity = mge.tensor(np.array([[1, 0], [0, 1]]), dtype="float32")
    # make DepType::VALUE unknown
    c = F.matmul(b, identity)
    with pytest.raises(RuntimeError):
        d = F.reshape(a, c)
    config_async_level(2)
예제 #10
0
def test_level1_infer_shape_with_unknown():
    config_async_level(2)
    a = mge.tensor([[1, 2, 2, 3]], dtype="float32")
    b = mge.tensor([1, 1])
    c = b * 2
    # make DepType::SHAPE unknown
    d = F.reshape(a, c)
    config_async_level(1)
    e = mge.tensor([[1, 2]], dtype="float32")
    with pytest.raises(RuntimeError):
        f = F.matmul(d, e)
예제 #11
0
def test_reshape():
    x = np.arange(6, dtype="float32")
    xx = tensor(x)
    y = x.reshape(1, 2, 3)

    for shape in [
        (1, 2, 3),
        (1, -1, 3),
        (1, tensor(-1), 3),
            np.array([1, -1, 3], dtype="int32"),
            tensor([1, -1, 3]),
    ]:
        yy = F.reshape(xx, shape)
        np.testing.assert_equal(yy.numpy(), y)
예제 #12
0
 def __init__(self, output_size=5, hidden_size=64, layers=4):
     super().__init__()
     self.hidden_size = hidden_size
     self.base = ConvBase(output_size=hidden_size,
                          hidden=hidden_size,
                          channels=1,
                          max_pool=False,
                          layers=layers)
     self.features = M.Sequential(
         kl.Lambda(lambda x: F.reshape(x, (-1, 1, 28, 28))),
         self.base,
         kl.Lambda(lambda x: kf.mean(x, axis=[2, 3])),
         kl.Flatten(),
     )
     self.classifier = M.Linear(hidden_size, output_size, bias=True)
예제 #13
0
def test_reshape(is_varnode):
    if is_varnode:
        network = Network()
    else:
        network = None

    x = np.arange(6, dtype="float32")
    xx = make_tensor(x, network)
    y = x.reshape(1, 2, 3)

    for shape in [
        (1, 2, 3),
        (1, -1, 3),
        (1, make_tensor(-1, network), 3),
            np.array([1, -1, 3], dtype="int32"),
            make_tensor([1, -1, 3], network),
    ]:
        yy = F.reshape(xx, shape)
        np.testing.assert_equal(yy.numpy(), y)
예제 #14
0
 def forward(self, now_LR, pre_h_SD):
     """
     now_LR: B,3,H,W
     pre_h_SD: B,48,H,W
     """
     batch, C, H, W = pre_h_SD.shape
     kernels = self.conv(now_LR)  # [B, k*k, H, W]
     batchwise_ans = []
     for idx in range(batch):
         kernel = kernels[idx]  # [k*k, H, W]
         kernel = F.dimshuffle(kernel, (1, 2, 0))  # [H, W , k*k]
         kernel = F.reshape(kernel, (H, W, 1, self.K, self.K, 1))
         kernel = F.broadcast_to(kernel, (C, H, W, 1, self.K, self.K, 1))
         batchwise_ans.append(
             F.local_conv2d(
                 F.add_axis(pre_h_SD[idx], 0), kernel, [1, 1], [1, 1],
                 [1, 1]))  # [1, C, H, W]      some bug with padding
     similarity_matrix = F.concat(batchwise_ans, axis=0)  # [B,C,H,W]
     del batchwise_ans
     similarity_matrix = F.sigmoid(similarity_matrix)
     return F.multiply(pre_h_SD, similarity_matrix)
예제 #15
0
 def forward(self, x):
     x = F.reshape(x, self.out_shape)
     x = F.reshape(x, self.out_shape1)
     x = F.reshape(x, self.out_shape2)
     return x
예제 #16
0
 def convert_to_nchw4(var):
     var = F.reshape(var, (var.shape[0], var.shape[1] // 4, 4,
                           var.shape[2], var.shape[3]))
     var = F.transpose(var, (0, 1, 3, 4, 2))
     return var
예제 #17
0
 def func(x, shp):
     return F.reshape(x, shp)