# Copyright (c) 2023 Graphcore Ltd.
# 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 poptransformer import ops
from .base_layer import BaseLayer


class BaseLayerNorm(BaseLayer):

    norm_fn_map = {'group': ops.group_norm, 'ce': ops.layer_norm_ce}

    def __init__(self, context, name, input_size, eps):
        super().__init__(context, name)
        self.input_size = input_size
        self.eps = eps
        self.collect_bind_layer_weights()

    def collect_bind_layer_weights(self):
        weight_key = '.'.join([self.context, 'weight'])
        weight_np = self.get_param_from_state_dict(weight_key, [self.input_size])
        self.weight_id = self.add_initialized_input_tensor(weight_np, weight_key)
        bias_key = '.'.join([self.context, 'bias'])
        bias_np = self.get_param_from_state_dict(bias_key, [self.input_size])
        self.bias_id = self.add_initialized_input_tensor(bias_np, bias_key)

    def __call__(self, graph, x, sequence_length, norm_type):
        with graph.nameScope(self.context):
            norm_fn = self.norm_fn_map.get(norm_type, None)
            if not norm_fn:
                raise ValueError(f"Invalid norm_fn {norm_type}, options: {self.norm_fn_map.keys()}")
            x = norm_fn(
                graph, x, self.weight_id, self.bias_id, self.eps, self.batch_size, sequence_length, self.input_size)
            return x


class TPLayerNorm(BaseLayerNorm):
    pass


class LayerNorm(TPLayerNorm, BaseLayerNorm):

    layer_class_map = {
        'tp': TPLayerNorm,
        'shard': BaseLayerNorm}

    def __init__(self, context, name, input_size, eps):
        model_type = self.model_type
        self.layer_class = self.layer_class_map.get(model_type, None)
        if not self.layer_class:
            raise ValueError(f"Invalid model_type {model_type}, options: {self.layer_class_map.keys()}")
        self.logger.debug(f'initializing model type: {self.layer_class.__name__}')
        super().__init__(context, name, input_size, eps)

    def __call__(self, graph, x, sequence_length, norm_type):
        return self.layer_class.__call__(self, graph, x, sequence_length, norm_type)

    def collect_bind_layer_weights(self):
        return self.layer_class.collect_bind_layer_weights(self)
