
import os

import jsonlines
import numpy as np
from tqdm import tqdm


__lowercase										= 2048
__lowercase										= 4096
__lowercase										= 42
__lowercase										= os.environ.pop('''PROCESS_TRAIN''', '''false''')
__lowercase										= {'''null''': 0, '''short''': 1, '''long''': 2, '''yes''': 3, '''no''': 4}



def lowerCamelCase  (       SCREAMING_SNAKE_CASE     ):
    '''simple docstring'''







    def choose_first(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE=False     ):
        assert isinstance(_lowerCamelCase  ,      _lowerCamelCase     )
        if len(_lowerCamelCase     ) == 1:
            __UpperCamelCase					:Any        =		answer[0]
            return {k: [answer[k]] for k in answer} if is_long_answer else answer
        for a in answer:
            if is_long_answer:
                __UpperCamelCase					:Union[str, Any]        =		{k: [a[k]] for k in a}
            if len(a['''start_token''']     ) > 0:
                break
        return a

    __UpperCamelCase					:Union[str, Any]        =		{"""id""": example["""id"""]}
    __UpperCamelCase					:Any        =		example["""annotations"""]
    __UpperCamelCase					:List[Any]        =		annotation["""yes_no_answer"""]
    if 0 in yes_no_answer or 1 in yes_no_answer:
        __UpperCamelCase					:Tuple        =		["""yes"""] if 1 in yes_no_answer else ["""no"""]
        __UpperCamelCase					:Optional[Any]        =		[]
        __UpperCamelCase					:Tuple        =		[]
        __UpperCamelCase					:Optional[Any]        =		["""<cls>"""]
    else:
        __UpperCamelCase					:Union[str, Any]        =		["""short"""]
        __UpperCamelCase					:Optional[Any]        =		choose_first(annotation['''short_answers''']     )
        if len(out['''start_token''']     ) == 0:
            # answer will be long if short is not available
            __UpperCamelCase					:Dict        =		["""long"""]
            __UpperCamelCase					:str        =		choose_first(annotation['''long_answer''']  ,      is_long_answer=_lowerCamelCase     )
            __UpperCamelCase					:Optional[int]        =		[]
        answer.update(_lowerCamelCase     )

    # disregard some samples
    if len(answer['''start_token''']     ) > 1 or answer["start_token"] == answer["end_token"]:
        __UpperCamelCase					:List[Any]        =		True
    else:
        __UpperCamelCase					:int        =		False

    __UpperCamelCase					:Any        =		["""start_token""", """end_token""", """start_byte""", """end_byte""", """text"""]
    if not all(isinstance(answer[k]  ,      _lowerCamelCase     ) for k in cols     ):
        raise ValueError('''Issue in ID'''  ,      example['''id''']     )

    return answer



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE=False     ):
    '''simple docstring'''







    __UpperCamelCase					:List[Any]        =		_get_single_answer(_lowerCamelCase     )
    # bytes are of no use
    del answer["start_byte"]
    del answer["end_byte"]

    # handle yes_no answers explicitly
    if answer["category"][0] in ["yes", "no"]:  # category is list with one element
        __UpperCamelCase					:Optional[Any]        =		example["""document"""]["""tokens"""]
        __UpperCamelCase					:Union[str, Any]        =		[]
        for i in range(len(doc['''token''']     )     ):
            if not doc["is_html"][i]:
                context.append(doc['''token'''][i]     )
        return {
            "context": " ".join(_lowerCamelCase     ),
            "answer": {
                "start_token": -100,  # ignore index in cross-entropy
                "end_token": -100,  # ignore index in cross-entropy
                "category": answer["category"],
                "span": answer["category"],  # extra
            },
        }

    # later, help in removing all no answers
    if answer["start_token"] == [-1]:
        return {
            "context": "None",
            "answer": {
                "start_token": -1,
                "end_token": -1,
                "category": "null",
                "span": "None",  # extra
            },
        }

    # handling normal samples

    __UpperCamelCase					:Union[str, Any]        =		["""start_token""", """end_token"""]
    answer.update({k: answer[k][0] if len(answer[k]     ) > 0 else answer[k] for k in cols}     )  # e.g. [10] == 10

    __UpperCamelCase					:str        =		example["""document"""]["""tokens"""]
    __UpperCamelCase					:Any        =		answer["""start_token"""]
    __UpperCamelCase					:Optional[int]        =		answer["""end_token"""]

    __UpperCamelCase					:int        =		[]
    for i in range(len(doc['''token''']     )     ):
        if not doc["is_html"][i]:
            context.append(doc['''token'''][i]     )
        else:
            if answer["start_token"] > i:
                start_token -= 1
            if answer["end_token"] > i:
                end_token -= 1
    __UpperCamelCase					:Dict        =		""" """.join(context[start_token:end_token]     )

    # checking above code
    if assertion:
        __UpperCamelCase					:str        =		doc["""is_html"""][answer["""start_token"""] : answer["""end_token"""]]
        __UpperCamelCase					:Union[str, Any]        =		doc["""token"""][answer["""start_token"""] : answer["""end_token"""]]
        __UpperCamelCase					:Dict        =		""" """.join([old[i] for i in range(len(_lowerCamelCase     )     ) if not is_html[i]]     )
        if new != old:
            print('''ID:'''  ,      example['''id''']     )
            print('''New:'''  ,      _lowerCamelCase  ,      end='''\n'''     )
            print('''Old:'''  ,      _lowerCamelCase  ,      end='''\n\n'''     )

    return {
        "context": " ".join(_lowerCamelCase     ),
        "answer": {
            "start_token": start_token,
            "end_token": end_token - 1,  # this makes it inclusive
            "category": answer["category"],  # either long or short
            "span": new,  # extra
        },
    }



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE=2_048  ,      SCREAMING_SNAKE_CASE=4_096  ,      SCREAMING_SNAKE_CASE=True     ):
    '''simple docstring'''







    __UpperCamelCase					:List[Any]        =		get_context_and_ans(_lowerCamelCase  ,      assertion=_lowerCamelCase     )
    __UpperCamelCase					:Dict        =		out["""answer"""]

    # later, removing these samples
    if answer["start_token"] == -1:
        return {
            "example_id": example["id"],
            "input_ids": [[-1]],
            "labels": {
                "start_token": [-1],
                "end_token": [-1],
                "category": ["null"],
            },
        }

    __UpperCamelCase					:List[Any]        =		tokenizer(example['''question''']['''text''']  ,      out['''context''']     ).input_ids
    __UpperCamelCase					:Optional[int]        =		input_ids.index(tokenizer.sep_token_id     ) + 1

    # return yes/no
    if answer["category"][0] in ["yes", "no"]:  # category is list with one element
        __UpperCamelCase					:int        =		[]
        __UpperCamelCase					:List[str]        =		[]
        __UpperCamelCase					:Optional[int]        =		input_ids[:q_len]
        __UpperCamelCase					:Dict        =		range(_lowerCamelCase  ,      len(_lowerCamelCase     )  ,      max_length - doc_stride     )
        for i in doc_start_indices:
            __UpperCamelCase					:Dict        =		i + max_length - q_len
            __UpperCamelCase					:Tuple        =		input_ids[i:end_index]
            inputs.append(q_indices + slice     )
            category.append(answer['''category'''][0]     )
            if slice[-1] == tokenizer.sep_token_id:
                break

        return {
            "example_id": example["id"],
            "input_ids": inputs,
            "labels": {
                "start_token": [-100] * len(_lowerCamelCase     ),
                "end_token": [-100] * len(_lowerCamelCase     ),
                "category": category,
            },
        }

    __UpperCamelCase					:Optional[int]        =		out["""context"""].split()
    __UpperCamelCase					:Optional[int]        =		splitted_context[answer["""end_token"""]]
    __UpperCamelCase					:Any        =		len(
        tokenizer(
            ''' '''.join(splitted_context[: answer['''start_token''']]     )  ,      add_special_tokens=_lowerCamelCase  ,      ).input_ids     )
    __UpperCamelCase					:Tuple        =		len(
        tokenizer(''' '''.join(splitted_context[: answer['''end_token''']]     )  ,      add_special_tokens=_lowerCamelCase     ).input_ids     )

    answer["start_token"] += q_len
    answer["end_token"] += q_len

    # fixing end token
    __UpperCamelCase					:Tuple        =		len(tokenizer(_lowerCamelCase  ,      add_special_tokens=_lowerCamelCase     ).input_ids     )
    if num_sub_tokens > 1:
        answer["end_token"] += num_sub_tokens - 1

    __UpperCamelCase					:Union[str, Any]        =		input_ids[answer["""start_token"""] : answer["""end_token"""] + 1]  # right & left are inclusive
    __UpperCamelCase					:Optional[int]        =		answer["""start_token"""]
    __UpperCamelCase					:List[Any]        =		answer["""end_token"""]

    if assertion:
        __UpperCamelCase					:Any        =		tokenizer.decode(_lowerCamelCase     )
        if answer["span"] != new:
            print('''ISSUE IN TOKENIZATION'''     )
            print('''OLD:'''  ,      answer['''span''']     )
            print('''NEW:'''  ,      _lowerCamelCase  ,      end='''\n\n'''     )

    if len(_lowerCamelCase     ) <= max_length:
        return {
            "example_id": example["id"],
            "input_ids": [input_ids],
            "labels": {
                "start_token": [answer["start_token"]],
                "end_token": [answer["end_token"]],
                "category": answer["category"],
            },
        }

    __UpperCamelCase					:List[Any]        =		input_ids[:q_len]
    __UpperCamelCase					:Tuple        =		range(_lowerCamelCase  ,      len(_lowerCamelCase     )  ,      max_length - doc_stride     )

    __UpperCamelCase					:Optional[Any]        =		[]
    __UpperCamelCase					:List[Any]        =		[]
    __UpperCamelCase					:Optional[int]        =		[]
    __UpperCamelCase					:Tuple        =		[]  # null, yes, no, long, short
    for i in doc_start_indices:
        __UpperCamelCase					:Dict        =		i + max_length - q_len
        __UpperCamelCase					:Union[str, Any]        =		input_ids[i:end_index]
        inputs.append(q_indices + slice     )
        assert len(inputs[-1]     ) <= max_length, "Issue in truncating length"

        if start_token >= i and end_token <= end_index - 1:
            __UpperCamelCase					:str        =		start_token - i + q_len
            __UpperCamelCase					:int        =		end_token - i + q_len
            answers_category.append(answer['''category'''][0]     )  # ["short"] -> "short"
        else:
            __UpperCamelCase					:Optional[Any]        =		-100
            __UpperCamelCase					:Optional[Any]        =		-100
            answers_category.append('''null'''     )
        __UpperCamelCase					:List[str]        =		inputs[-1][start_token : end_token + 1]

        answers_start_token.append(_lowerCamelCase     )
        answers_end_token.append(_lowerCamelCase     )
        if assertion:
            if new != old and new != [tokenizer.cls_token_id]:
                print('''ISSUE in strided for ID:'''  ,      example['''id''']     )
                print('''New:'''  ,      tokenizer.decode(_lowerCamelCase     )     )
                print('''Old:'''  ,      tokenizer.decode(_lowerCamelCase     )  ,      end='''\n\n'''     )
        if slice[-1] == tokenizer.sep_token_id:
            break

    return {
        "example_id": example["id"],
        "input_ids": inputs,
        "labels": {
            "start_token": answers_start_token,
            "end_token": answers_end_token,
            "category": answers_category,
        },
    }



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE=2_048  ,      SCREAMING_SNAKE_CASE=4_096  ,      SCREAMING_SNAKE_CASE=False     ):
    '''simple docstring'''







    __UpperCamelCase					:Union[str, Any]        =		get_strided_contexts_and_ans(
        _lowerCamelCase  ,      _lowerCamelCase  ,      doc_stride=_lowerCamelCase  ,      max_length=_lowerCamelCase  ,      assertion=_lowerCamelCase  ,      )

    return example



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
    '''simple docstring'''







    with jsonlines.open(_lowerCamelCase  ,      '''a'''     ) as writer:
        for example in tqdm(_lowerCamelCase  ,      total=len(_lowerCamelCase     )  ,      desc='''Saving samples ... '''     ):
            __UpperCamelCase					:int        =		example["""labels"""]
            for ids, start, end, cat in zip(
                example['''input_ids''']  ,      labels['''start_token''']  ,      labels['''end_token''']  ,      labels['''category''']  ,      ):
                if start == -1 and end == -1:
                    continue  # leave waste samples with no answer
                if cat == "null" and np.random.rand() < 0.6:
                    continue  # removing 50 % samples
                writer.write(
                    {
                        '''input_ids''': ids,
                        '''start_token''': start,
                        '''end_token''': end,
                        '''category''': CATEGORY_MAPPING[cat],
                    }     )


if __name__ == "__main__":
    from datasets import load_dataset

    from transformers import BigBirdTokenizer

    __lowercase										= load_dataset('''natural_questions''')
    __lowercase										= BigBirdTokenizer.from_pretrained('''google/bigbird-roberta-base''')

    __lowercase										= data['''train''' if PROCESS_TRAIN == '''true''' else '''validation''']

    __lowercase										= {
        '''tokenizer''': tokenizer,
        '''doc_stride''': DOC_STRIDE,
        '''max_length''': MAX_LENGTH,
        '''assertion''': False,
    }
    __lowercase										= data.map(prepare_inputs, fn_kwargs=fn_kwargs)
    __lowercase										= data.remove_columns(['''annotations''', '''document''', '''id''', '''question'''])
    print(data)

    np.random.seed(SEED)
    __lowercase										= '''nq-training.jsonl''' if PROCESS_TRAIN == '''true''' else '''nq-validation.jsonl'''
    save_to_disk(data, file_name=cache_file_name)






from typing import Optional, Union

import torch
from torch import nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...modeling_outputs import BaseModelOutputWithPoolingAndNoAttention, ImageClassifierOutputWithNoAttention
from ...modeling_utils import PreTrainedModel
from ...utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward, logging
from .configuration_mobilenet_va import MobileNetVaConfig


__lowercase										= logging.get_logger(__name__)


# General docstring
__lowercase										= '''MobileNetV1Config'''

# Base docstring
__lowercase										= '''google/mobilenet_v1_1.0_224'''
__lowercase										= [1, 1024, 7, 7]

# Image classification docstring
__lowercase										= '''google/mobilenet_v1_1.0_224'''
__lowercase										= '''tabby, tabby cat'''


__lowercase										= [
    '''google/mobilenet_v1_1.0_224''',
    '''google/mobilenet_v1_0.75_192''',
    # See all MobileNetV1 models at https://huggingface.co/models?filter=mobilenet_v1
]



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE=None     ):
    '''simple docstring'''







    __UpperCamelCase					:Tuple        =		{}

    if isinstance(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
        __UpperCamelCase					:Dict        =		model.mobilenet_va
    else:
        __UpperCamelCase					:str        =		model

    __UpperCamelCase					:int        =		'''MobilenetV1/Conv2d_0/'''
    __UpperCamelCase					:str        =		backbone.conv_stem.convolution.weight
    __UpperCamelCase					:int        =		backbone.conv_stem.normalization.bias
    __UpperCamelCase					:Union[str, Any]        =		backbone.conv_stem.normalization.weight
    __UpperCamelCase					:Optional[int]        =		backbone.conv_stem.normalization.running_mean
    __UpperCamelCase					:Optional[int]        =		backbone.conv_stem.normalization.running_var

    for i in range(13     ):
        __UpperCamelCase					:Optional[Any]        =		i + 1
        __UpperCamelCase					:Optional[int]        =		i * 2

        __UpperCamelCase					:List[Any]        =		backbone.layer[pt_index]
        __UpperCamelCase					:Tuple        =		f"""MobilenetV1/Conv2d_{tf_index}_depthwise/"""
        __UpperCamelCase					:Any        =		pointer.convolution.weight
        __UpperCamelCase					:Dict        =		pointer.normalization.bias
        __UpperCamelCase					:List[str]        =		pointer.normalization.weight
        __UpperCamelCase					:Any        =		pointer.normalization.running_mean
        __UpperCamelCase					:List[str]        =		pointer.normalization.running_var

        __UpperCamelCase					:Union[str, Any]        =		backbone.layer[pt_index + 1]
        __UpperCamelCase					:List[str]        =		f"""MobilenetV1/Conv2d_{tf_index}_pointwise/"""
        __UpperCamelCase					:Optional[Any]        =		pointer.convolution.weight
        __UpperCamelCase					:Dict        =		pointer.normalization.bias
        __UpperCamelCase					:int        =		pointer.normalization.weight
        __UpperCamelCase					:Optional[int]        =		pointer.normalization.running_mean
        __UpperCamelCase					:Optional[int]        =		pointer.normalization.running_var

    if isinstance(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
        __UpperCamelCase					:Any        =		'''MobilenetV1/Logits/Conv2d_1c_1x1/'''
        __UpperCamelCase					:Union[str, Any]        =		model.classifier.weight
        __UpperCamelCase					:int        =		model.classifier.bias

    return tf_to_pt_map



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
    '''simple docstring'''







    try:
        import numpy as np
        import tensorflow as tf
    except ImportError:
        logger.error(
            '''Loading a TensorFlow models in PyTorch, requires TensorFlow to be installed. Please see '''
            '''https://www.tensorflow.org/install/ for installation instructions.'''     )
        raise

    # Load weights from TF model
    __UpperCamelCase					:Any        =		tf.train.list_variables(SCREAMING_SNAKE_CASE     )
    __UpperCamelCase					:List[str]        =		{}
    for name, shape in init_vars:
        logger.info(f"""Loading TF weight {name} with shape {shape}"""     )
        __UpperCamelCase					:str        =		tf.train.load_variable(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     )
        __UpperCamelCase					:List[str]        =		array

    # Build TF to PyTorch weights loading map
    __UpperCamelCase					:Optional[Any]        =		_build_tf_to_pytorch_map(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     )

    for name, pointer in tf_to_pt_map.items():
        logger.info(f"""Importing {name}"""     )
        if name not in tf_weights:
            logger.info(f"""{name} not in tf pre-trained weights, skipping"""     )
            continue

        __UpperCamelCase					:Optional[Any]        =		tf_weights[name]

        if "depthwise_weights" in name:
            logger.info('''Transposing depthwise'''     )
            __UpperCamelCase					:Optional[int]        =		np.transpose(SCREAMING_SNAKE_CASE  ,      (2, 3, 0, 1)     )
        elif "weights" in name:
            logger.info('''Transposing'''     )
            if len(pointer.shape     ) == 2:  # copying into linear layer
                __UpperCamelCase					:Tuple        =		array.squeeze().transpose()
            else:
                __UpperCamelCase					:Union[str, Any]        =		np.transpose(SCREAMING_SNAKE_CASE  ,      (3, 2, 0, 1)     )

        if pointer.shape != array.shape:
            raise ValueError(f"""Pointer shape {pointer.shape} and array shape {array.shape} mismatched"""     )

        logger.info(f"""Initialize PyTorch weight {name} {array.shape}"""     )
        __UpperCamelCase					:Optional[int]        =		torch.from_numpy(SCREAMING_SNAKE_CASE     )

        tf_weights.pop(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     )
        tf_weights.pop(name + '''/RMSProp'''  ,      SCREAMING_SNAKE_CASE     )
        tf_weights.pop(name + '''/RMSProp_1'''  ,      SCREAMING_SNAKE_CASE     )
        tf_weights.pop(name + '''/ExponentialMovingAverage'''  ,      SCREAMING_SNAKE_CASE     )

    logger.info(f"""Weights not copied to PyTorch model: {', '.join(tf_weights.keys()     )}"""     )
    return model



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE     ):
    '''simple docstring'''







    __UpperCamelCase  ,	__UpperCamelCase					:str        =		features.shape[-2:]
    __UpperCamelCase  ,	__UpperCamelCase					:Union[str, Any]        =		conv_layer.stride
    __UpperCamelCase  ,	__UpperCamelCase					:Union[str, Any]        =		conv_layer.kernel_size

    if in_height % stride_height == 0:
        __UpperCamelCase					:Optional[int]        =		max(kernel_height - stride_height  ,      0     )
    else:
        __UpperCamelCase					:List[Any]        =		max(kernel_height - (in_height % stride_height)  ,      0     )

    if in_width % stride_width == 0:
        __UpperCamelCase					:List[str]        =		max(kernel_width - stride_width  ,      0     )
    else:
        __UpperCamelCase					:Tuple        =		max(kernel_width - (in_width % stride_width)  ,      0     )

    __UpperCamelCase					:Any        =		pad_along_width // 2
    __UpperCamelCase					:Tuple        =		pad_along_width - pad_left
    __UpperCamelCase					:Union[str, Any]        =		pad_along_height // 2
    __UpperCamelCase					:str        =		pad_along_height - pad_top

    __UpperCamelCase					:Optional[Any]        =		(pad_left, pad_right, pad_top, pad_bottom)
    return nn.functional.pad(SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      '''constant'''  ,      0.0     )


class      lowerCamelCase_					(       nn.Module			):






    '''simple docstring'''






    def __init__(     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase = 1			,			__lowercase = 1			,			__lowercase = False			,			__lowercase = True			,			__lowercase = True			,			)	->     None:
        super().__init__()
        __UpperCamelCase					:str        =		config

        if in_channels % groups != 0:
            raise ValueError(f"""Input channels ({in_channels}) are not divisible by {groups} groups.""")
        if out_channels % groups != 0:
            raise ValueError(f"""Output channels ({out_channels}) are not divisible by {groups} groups.""")

        __UpperCamelCase					:Any        =		0 if config.tf_padding else int((kernel_size - 1) / 2)

        __UpperCamelCase					:List[Any]        =		nn.Convad(
            in_channels=__lowercase			,			out_channels=__lowercase			,			kernel_size=__lowercase			,			stride=__lowercase			,			padding=__lowercase			,			groups=__lowercase			,			bias=__lowercase			,			padding_mode='''zeros'''			,			)

        if use_normalization:
            __UpperCamelCase					:str        =		nn.BatchNormad(
                num_features=__lowercase			,			eps=config.layer_norm_eps			,			momentum=0.99_97			,			affine=__lowercase			,			track_running_stats=__lowercase			,			)
        else:
            __UpperCamelCase					:Tuple        =		None

        if use_activation:
            if isinstance(__lowercase			,			__lowercase):
                __UpperCamelCase					:Union[str, Any]        =		ACTaFN[use_activation]
            elif isinstance(config.hidden_act			,			__lowercase):
                __UpperCamelCase					:Dict        =		ACTaFN[config.hidden_act]
            else:
                __UpperCamelCase					:List[Any]        =		config.hidden_act
        else:
            __UpperCamelCase					:Optional[Any]        =		None







    def       UpperCamelCase__       (     self			,			__lowercase)	->     torch.Tensor:
        if self.config.tf_padding:
            __UpperCamelCase					:Any        =		apply_tf_padding(__lowercase			,			self.convolution)
        __UpperCamelCase					:str        =		self.convolution(__lowercase)
        if self.normalization is not None:
            __UpperCamelCase					:Any        =		self.normalization(__lowercase)
        if self.activation is not None:
            __UpperCamelCase					:List[str]        =		self.activation(__lowercase)
        return features


class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    a__     :						List[str]      =				MobileNetVaConfig
    a__     :						Dict      =				load_tf_weights_in_mobilenet_va
    a__     :						Tuple      =				"""mobilenet_v1"""
    a__     :						Optional[Any]      =				"""pixel_values"""
    a__     :						int      =				False







    def       UpperCamelCase__       (     self			,			__lowercase)	->     None:
        if isinstance(__lowercase			,			(nn.Linear, nn.Convad)):
            module.weight.data.normal_(mean=0.0			,			std=self.config.initializer_range)
            if module.bias is not None:
                module.bias.data.zero_()
        elif isinstance(__lowercase			,			nn.BatchNormad):
            module.bias.data.zero_()
            module.weight.data.fill_(1.0)
__lowercase										= r'''
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`MobileNetV1Config`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
'''

__lowercase										= r'''
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`MobileNetV1ImageProcessor.__call__`] for details.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
'''


@add_start_docstrings(
    """The bare MobileNetV1 model outputting raw hidden-states without any specific head on top."""						,     UpperCAmelCase_						,     )
class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    def __init__(     self			,			__lowercase			,			__lowercase = True)	->     Optional[Any]:
        super().__init__(__lowercase)
        __UpperCamelCase					:List[str]        =		config

        __UpperCamelCase					:Any        =		32
        __UpperCamelCase					:List[str]        =		max(int(depth * config.depth_multiplier)			,			config.min_depth)

        __UpperCamelCase					:Union[str, Any]        =		MobileNetVaConvLayer(
            __lowercase			,			in_channels=config.num_channels			,			out_channels=__lowercase			,			kernel_size=3			,			stride=2			,			)

        __UpperCamelCase					:str        =		[1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1, 2, 1]

        __UpperCamelCase					:Any        =		nn.ModuleList()
        for i in range(13):
            __UpperCamelCase					:str        =		out_channels

            if strides[i] == 2 or i == 0:
                depth *= 2
                __UpperCamelCase					:Tuple        =		max(int(depth * config.depth_multiplier)			,			config.min_depth)

            self.layer.append(
                MobileNetVaConvLayer(
                    __lowercase			,			in_channels=__lowercase			,			out_channels=__lowercase			,			kernel_size=3			,			stride=strides[i]			,			groups=__lowercase			,			))

            self.layer.append(
                MobileNetVaConvLayer(
                    __lowercase			,			in_channels=__lowercase			,			out_channels=__lowercase			,			kernel_size=1			,			))

        __UpperCamelCase					:str        =		nn.AdaptiveAvgPoolad((1, 1)) if add_pooling_layer else None

        # Initialize weights and apply final processing
        self.post_init()







    def       UpperCamelCase__       (     self			,			__lowercase)	->     Union[str, Any]:
        raise NotImplementedError







    @add_start_docstrings_to_model_forward(__lowercase)
    @add_code_sample_docstrings(
        checkpoint=_CHECKPOINT_FOR_DOC			,			output_type=__lowercase			,			config_class=_CONFIG_FOR_DOC			,			modality='''vision'''			,			expected_output=_EXPECTED_OUTPUT_SHAPE			,			)
    def       UpperCamelCase__       (     self			,			__lowercase = None			,			__lowercase = None			,			__lowercase = None			,			)	->     Union[tuple, BaseModelOutputWithPoolingAndNoAttention]:
        __UpperCamelCase					:Union[str, Any]        =		(
            output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
        )
        __UpperCamelCase					:str        =		return_dict if return_dict is not None else self.config.use_return_dict

        if pixel_values is None:
            raise ValueError('''You have to specify pixel_values''')

        __UpperCamelCase					:int        =		self.conv_stem(__lowercase)

        __UpperCamelCase					:List[str]        =		() if output_hidden_states else None

        for i, layer_module in enumerate(self.layer):
            __UpperCamelCase					:Optional[Any]        =		layer_module(__lowercase)

            if output_hidden_states:
                __UpperCamelCase					:int        =		all_hidden_states + (hidden_states,)

        __UpperCamelCase					:Any        =		hidden_states

        if self.pooler is not None:
            __UpperCamelCase					:str        =		torch.flatten(self.pooler(__lowercase)			,			start_dim=1)
        else:
            __UpperCamelCase					:Tuple        =		None

        if not return_dict:
            return tuple(v for v in [last_hidden_state, pooled_output, all_hidden_states] if v is not None)

        return BaseModelOutputWithPoolingAndNoAttention(
            last_hidden_state=__lowercase			,			pooler_output=__lowercase			,			hidden_states=__lowercase			,			)


@add_start_docstrings(
    """
    MobileNetV1 model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    """						,     UpperCAmelCase_						,     )
class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    def __init__(     self			,			__lowercase)	->     None:
        super().__init__(__lowercase)

        __UpperCamelCase					:int        =		config.num_labels
        __UpperCamelCase					:Optional[int]        =		MobileNetVaModel(__lowercase)

        __UpperCamelCase					:Optional[Any]        =		self.mobilenet_va.layer[-1].convolution.out_channels

        # Classifier head
        __UpperCamelCase					:str        =		nn.Dropout(config.classifier_dropout_prob			,			inplace=__lowercase)
        __UpperCamelCase					:Dict        =		nn.Linear(__lowercase			,			config.num_labels) if config.num_labels > 0 else nn.Identity()

        # Initialize weights and apply final processing
        self.post_init()







    @add_start_docstrings_to_model_forward(__lowercase)
    @add_code_sample_docstrings(
        checkpoint=_IMAGE_CLASS_CHECKPOINT			,			output_type=__lowercase			,			config_class=_CONFIG_FOR_DOC			,			expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT			,			)
    def       UpperCamelCase__       (     self			,			__lowercase = None			,			__lowercase = None			,			__lowercase = None			,			__lowercase = None			,			)	->     Union[tuple, ImageClassifierOutputWithNoAttention]:
        __UpperCamelCase					:List[Any]        =		return_dict if return_dict is not None else self.config.use_return_dict

        __UpperCamelCase					:Tuple        =		self.mobilenet_va(__lowercase			,			output_hidden_states=__lowercase			,			return_dict=__lowercase)

        __UpperCamelCase					:List[str]        =		outputs.pooler_output if return_dict else outputs[1]

        __UpperCamelCase					:Union[str, Any]        =		self.classifier(self.dropout(__lowercase))

        __UpperCamelCase					:int        =		None
        if labels is not None:
            if self.config.problem_type is None:
                if self.num_labels == 1:
                    __UpperCamelCase					:Tuple        =		'''regression'''
                elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
                    __UpperCamelCase					:Union[str, Any]        =		'''single_label_classification'''
                else:
                    __UpperCamelCase					:Optional[Any]        =		'''multi_label_classification'''

            if self.config.problem_type == "regression":
                __UpperCamelCase					:Any        =		MSELoss()
                if self.num_labels == 1:
                    __UpperCamelCase					:List[str]        =		loss_fct(logits.squeeze()			,			labels.squeeze())
                else:
                    __UpperCamelCase					:Dict        =		loss_fct(__lowercase			,			__lowercase)
            elif self.config.problem_type == "single_label_classification":
                __UpperCamelCase					:Optional[int]        =		CrossEntropyLoss()
                __UpperCamelCase					:str        =		loss_fct(logits.view(-1			,			self.num_labels)			,			labels.view(-1))
            elif self.config.problem_type == "multi_label_classification":
                __UpperCamelCase					:Dict        =		BCEWithLogitsLoss()
                __UpperCamelCase					:List[str]        =		loss_fct(__lowercase			,			__lowercase)

        if not return_dict:
            __UpperCamelCase					:Tuple        =		(logits,) + outputs[2:]
            return ((loss,) + output) if loss is not None else output

        return ImageClassifierOutputWithNoAttention(
            loss=__lowercase			,			logits=__lowercase			,			hidden_states=outputs.hidden_states			,			)





