# Copyright 2019 Google LLC
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import pytest
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import DepthwiseConv2D
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model

from qkeras.estimate import print_qstats
from qkeras.utils import model_quantize
from qkeras import QConv2D
from qkeras.quantizers import *


def create_network():
  xi = Input((28, 28, 1))
  x = Conv2D(32, (3, 3))(xi)
  x = Activation("relu")(x)
  x = Conv2D(32, (3, 3), activation="relu")(x)
  x = Activation("softmax")(x)
  return Model(inputs=xi, outputs=x)


def create_mix_network():

  xi = Input((28, 28, 1))
  x = QConv2D(32, (3, 3), kernel_quantizer=binary())(xi)
  x = Activation("relu")(x)
  x = Conv2D(32, (3, 3))(x)
  x = Activation("softmax")(x)
  return Model(inputs=xi, outputs=x)


def create_network_with_bn():
  """Creates a network contains both QConv2D and QDepthwiseConv2D layers."""

  xi = Input((28, 28, 1))
  x = Conv2D(32, (3, 3))(xi)
  x = BatchNormalization()(x)
  x = Activation("relu")(x)
  x = DepthwiseConv2D((3, 3), activation="relu")(x)
  x = BatchNormalization()(x)
  x = Activation("softmax")(x)
  return Model(inputs=xi, outputs=x)


def test_conversion_print_qstats():
  # this tests if references in tensorflow are working properly.
  m = create_network()
  d = {
      "QConv2D": {
          "kernel_quantizer": "binary",
          "bias_quantizer": "binary"
      },
      "QActivation": {
          "relu": "ternary"
      }
  }
  qq = model_quantize(m, d, 4)
  qq.summary()
  print_qstats(qq)

  # test if print_qstats works with unquantized layers
  print_qstats(m)

  # test if print_qstats works with mixture of quantized and unquantized layers
  m1 = create_mix_network()
  print_qstats(m1)

  m2 = create_network_with_bn()
  d2 = {
      "QConv2D": {
          "kernel_quantizer": "binary",
          "bias_quantizer": "binary"
      },
      "QActivation": {
          "relu": "ternary"
      },
      "QConv2DBatchnorm": {
          "kernel_quantizer": "ternary",
          "bias_quantizer": "ternary",
      },
      "QDepthwiseConv2DBatchnorm": {
          "depthwise_quantizer": "ternary",
          "bias_quantizer": "ternary",
      },
  }
  m2 = model_quantize(m2, d2, 4, enable_bn_folding=True)
  m2.summary()
  print_qstats(m2)


if __name__ == "__main__":
  pytest.main([__file__])

tensorflow.keras.layers.DepthwiseConv2D tensorflow.keras.layers.Activation tensorflow.keras.models.Model tensorflow.keras.layers.Conv2D tensorflow.keras.layers.BatchNormalization tensorflow.keras.layers.Input
[(34, 'tensorflow.keras.layers.Input', 'Input', (['(28, 28, 1)'], {}), False, 'from tensorflow.keras.layers import Input\n'), (39, 'tensorflow.keras.models.Model', 'Model', ([], {'inputs': 'xi', 'outputs': 'x'}), False, 'from tensorflow.keras.models import Model\n'), (44, 'tensorflow.keras.layers.Input', 'Input', (['(28, 28, 1)'], {}), False, 'from tensorflow.keras.layers import Input\n'), (49, 'tensorflow.keras.models.Model', 'Model', ([], {'inputs': 'xi', 'outputs': 'x'}), False, 'from tensorflow.keras.models import Model\n'), (55, 'tensorflow.keras.layers.Input', 'Input', (['(28, 28, 1)'], {}), False, 'from tensorflow.keras.layers import Input\n'), (62, 'tensorflow.keras.models.Model', 'Model', ([], {'inputs': 'xi', 'outputs': 'x'}), False, 'from tensorflow.keras.models import Model\n'), (77, 'qkeras.utils.model_quantize', 'model_quantize', (['m', 'd', '(4)'], {}), False, 'from qkeras.utils import model_quantize\n'), (79, 'qkeras.estimate.print_qstats', 'print_qstats', (['qq'], {}), False, 'from qkeras.estimate import print_qstats\n'), (82, 'qkeras.estimate.print_qstats', 'print_qstats', (['m'], {}), False, 'from qkeras.estimate import print_qstats\n'), (86, 'qkeras.estimate.print_qstats', 'print_qstats', (['m1'], {}), False, 'from qkeras.estimate import print_qstats\n'), (106, 'qkeras.utils.model_quantize', 'model_quantize', (['m2', 'd2', '(4)'], {'enable_bn_folding': '(True)'}), False, 'from qkeras.utils import model_quantize\n'), (108, 'qkeras.estimate.print_qstats', 'print_qstats', (['m2'], {}), False, 'from qkeras.estimate import print_qstats\n'), (112, 'pytest.main', 'pytest.main', (['[__file__]'], {}), False, 'import pytest\n'), (35, 'tensorflow.keras.layers.Conv2D', 'Conv2D', (['(32)', '(3, 3)'], {}), False, 'from tensorflow.keras.layers import Conv2D\n'), (36, 'tensorflow.keras.layers.Activation', 'Activation', (['"""relu"""'], {}), False, 'from tensorflow.keras.layers import Activation\n'), (37, 'tensorflow.keras.layers.Conv2D', 'Conv2D', (['(32)', '(3, 3)'], {'activation': '"""relu"""'}), False, 'from tensorflow.keras.layers import Conv2D\n'), (38, 'tensorflow.keras.layers.Activation', 'Activation', (['"""softmax"""'], {}), False, 'from tensorflow.keras.layers import Activation\n'), (46, 'tensorflow.keras.layers.Activation', 'Activation', (['"""relu"""'], {}), False, 'from tensorflow.keras.layers import Activation\n'), (47, 'tensorflow.keras.layers.Conv2D', 'Conv2D', (['(32)', '(3, 3)'], {}), False, 'from tensorflow.keras.layers import Conv2D\n'), (48, 'tensorflow.keras.layers.Activation', 'Activation', (['"""softmax"""'], {}), False, 'from tensorflow.keras.layers import Activation\n'), (56, 'tensorflow.keras.layers.Conv2D', 'Conv2D', (['(32)', '(3, 3)'], {}), False, 'from tensorflow.keras.layers import Conv2D\n'), (57, 'tensorflow.keras.layers.BatchNormalization', 'BatchNormalization', ([], {}), False, 'from tensorflow.keras.layers import BatchNormalization\n'), (58, 'tensorflow.keras.layers.Activation', 'Activation', (['"""relu"""'], {}), False, 'from tensorflow.keras.layers import Activation\n'), (59, 'tensorflow.keras.layers.DepthwiseConv2D', 'DepthwiseConv2D', (['(3, 3)'], {'activation': '"""relu"""'}), False, 'from tensorflow.keras.layers import DepthwiseConv2D\n'), (60, 'tensorflow.keras.layers.BatchNormalization', 'BatchNormalization', ([], {}), False, 'from tensorflow.keras.layers import BatchNormalization\n'), (61, 'tensorflow.keras.layers.Activation', 'Activation', (['"""softmax"""'], {}), False, 'from tensorflow.keras.layers import Activation\n')]