'''VGG11/13/16/19 in Pytorch.'''
import torch
import torch.nn as nn
import torch.nn.functional as F
from quant_layers.quant_activation import PACT
from quant_layers.quant_activation import q_act
from quant_layers.quant_activation import cyc_relu
from quant_layers.mod_conv import Mod_conv2d_try
from quant_layers.mod_linear import Mod_Linear
from quant_layers.functions import no_grad_mul
import pdb
import numpy as np

class VGG_7_mod(nn.Module):
    def __init__(self, vgg_name, c_bits):
        super(VGG_7_mod, self).__init__()
        self.conv1 = nn.Conv2d(3, 128, kernel_size=3, padding=0,bias=False)
        self.bn1 = nn.BatchNorm2d(128)
        self.conv2 = Mod_conv2d_try(4, c_bits, 128, 128, kernel_size=3, padding=0,bias=False, carry=False) 
        self.bn2 = nn.BatchNorm2d(128)
        self.conv3 = Mod_conv2d_try(4, c_bits, 128, 256, kernel_size=3, padding=0,bias=False, carry=False) 
        self.bn3 = nn.BatchNorm2d(256)
        self.conv4 = Mod_conv2d_try(4, c_bits, 256, 256, kernel_size=3, padding=0,bias=False, carry=False) 
        self.bn4 = nn.BatchNorm2d(256)
        self.conv5 = Mod_conv2d_try(4, c_bits, 256, 512, kernel_size=3, padding=0,bias=False, carry=False) 
        self.bn5 = nn.BatchNorm2d(512)
        self.conv6 = Mod_conv2d_try(4, c_bits, 512, 512, kernel_size=3, padding=1,bias=False, carry=False) 
        self.bn6 = nn.BatchNorm2d(512)
        self.classifier = nn.Linear(512, 10, True)
        self.mp = nn.MaxPool2d(kernel_size=2, stride=2)
        self.qact = q_act(8)
        self.alpha = 1.#0.0625

    def forward(self, x):
        outrange = 0
        std_carry = 0
        inputs = []
        outputs = []
        out = self.conv1(x)
        out = self.bn1(out)
        out = F.relu(out)
        s1 = 1. 
        out, ofrate2, s2 = self.conv2(out, s1, self.alpha)
        outrange += (F.relu(torch.abs(out/s2) - 2**7)).mean()
        out = self.bn2(out)
        out = F.relu(out)
        s2 = 1.
        out = self.mp(out)
        out, ofrate3, s3 = self.conv3(out, s2, self.alpha)
        outrange += (F.relu(torch.abs(out/s3) - 2**7)).mean()
        out = self.bn3(out)
        out = F.relu(out)
        s3 = 1.
        out, ofrate4, s4 = self.conv4(out, s3, self.alpha)
        outrange += (F.relu(torch.abs(out/s4) - 2**7)).mean()
        out = self.bn4(out)
        out = F.relu(out)
        s4 = 1.
        out = self.mp(out)
        out, ofrate5, s5 = self.conv5(out, s4, self.alpha)
        outrange += (F.relu(torch.abs(out/s5) - 2**7)).mean()
        out = self.bn5(out)
        out = F.relu(out)
        s5 = 1.
        out, ofrate6, s6 = self.conv6(out, s5, self.alpha)
        outrange += (F.relu(torch.abs(out/s6) - 2**7)).mean()
        out = self.bn6(out)
        out = F.relu(out)
        s6 = 1.
        out = self.mp(out)
        out = out.view(out.size(0), -1)
        oflinear = 0
        out = self.classifier(out)
        return out, outrange, ofrate2+ofrate3+ofrate4+ofrate5+ofrate6, oflinear

