# Copyright 2021 Tencent
# SPDX-License-Identifier: BSD-3-Clause

import torch
import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

        self.w3 = nn.Parameter(torch.rand(16))
        self.b3 = nn.Parameter(torch.rand(16))
        self.w4 = nn.Parameter(torch.rand(12))
        self.b4 = nn.Parameter(torch.rand(12))
        self.w5 = nn.Parameter(torch.rand(32))
        self.b5 = nn.Parameter(torch.rand(32))

    def forward(self, x, y, z, w0, b0, w1, b1, w2, b2):
        x = F.group_norm(x, 2, w0, b0)
        x = F.group_norm(x, 1, None, None)
        x = F.group_norm(x, 4, self.w3, self.b3)

        y = F.group_norm(y, 3, w1, b1, eps=1e-4)
        y = F.group_norm(y, 4, None, None)
        y = F.group_norm(y, 6, self.w4, self.b4)

        z = F.group_norm(z, 32, w2, b2)
        z = F.group_norm(z, 4, None, None, eps=1e-2)
        z = F.group_norm(z, 8, self.w5, self.b5)
        return x, y, z

def test():
    net = Model()
    net.eval()

    torch.manual_seed(0)
    x = torch.rand(1, 16)
    y = torch.rand(12, 12, 16)
    z = torch.rand(1, 32, 12, 16)
    w0 = torch.rand(16)
    b0 = torch.rand(16)
    w1 = torch.rand(12)
    b1 = torch.rand(12)
    w2 = torch.rand(32)
    b2 = torch.rand(32)

    a0, a1, a2 = net(x, y, z, w0, b0, w1, b1, w2, b2)

    # export torchscript
    mod = torch.jit.trace(net, (x, y, z, w0, b0, w1, b1, w2, b2))
    mod.save("test_F_group_norm.pt")

    # torchscript to pnnx
    import os
    os.system("../src/pnnx test_F_group_norm.pt inputshape=[1,16],[12,12,16],[1,32,12,16],[16],[16],[12],[12],[32],[32]")

    # pnnx inference
    import test_F_group_norm_pnnx
    b0, b1, b2 = test_F_group_norm_pnnx.test_inference()

    return torch.equal(a0, b0) and torch.equal(a1, b1) and torch.equal(a2, b2)

if __name__ == "__main__":
    if test():
        exit(0)
    else:
        exit(1)
