





from math import ceil


def       UpperCamelCase__(			UpperCamelCase__     :			str					,	UpperCamelCase__     :			Any    )->Tuple:
       A__		   =		list(range(0					,	UpperCamelCase__    )    )

       A__		   =		[item for sublist in list(device_map.values()    ) for item in sublist]

       # Duplicate check
       A__		   =		[]
       for i in device_map_blocks:
              if device_map_blocks.count(UpperCamelCase__    ) > 1 and i not in duplicate_blocks:
                     duplicate_blocks.append(UpperCamelCase__    )
    # Missing blocks
       A__		   =		[i for i in blocks if i not in device_map_blocks]
       A__		   =		[i for i in device_map_blocks if i not in blocks]

       if len(UpperCamelCase__    ) != 0:
              raise ValueError(
                  '''Duplicate attention blocks specified in device_map. Attention blocks must be specified to one device.'''
                  ''' These attention blocks were specified more than once: ''' + str(UpperCamelCase__    )    )
       if len(UpperCamelCase__    ) != 0:
              raise ValueError(
                  '''There are attention blocks for this model that are not specified in the device_map. Add these attention '''
                  '''blocks to a device on the device_map: ''' + str(UpperCamelCase__    )    )
       if len(UpperCamelCase__    ) != 0:
              raise ValueError(
                  '''The device_map contains more attention blocks than this model has. Remove these from the device_map:'''
                  + str(UpperCamelCase__    )    )


def       UpperCamelCase__(			UpperCamelCase__     :			Optional[Any]					,	UpperCamelCase__     :			Optional[int]    )->List[str]:
       A__		   =		list(range(UpperCamelCase__    )    )
       A__		   =		int(ceil(n_layers / len(UpperCamelCase__    )    )    )
       A__		   =		[layers[i : i + n_blocks] for i in range(0					,	UpperCamelCase__					,	UpperCamelCase__    )]

       return dict(zip(UpperCamelCase__					,	UpperCamelCase__    )    )












import argparse

import torch

from transformers import RemBertConfig, RemBertModel, load_tf_weights_in_rembert
from transformers.utils import logging


logging.set_verbosity_info()


def       UpperCamelCase__(			UpperCamelCase__     :			Any					,	UpperCamelCase__     :			int					,	UpperCamelCase__     :			List[Any]    )->Optional[int]:
       # Initialise PyTorch model
       A__		   =		RemBertConfig.from_json_file(UpperCamelCase__    )
       print('''Building PyTorch model from configuration: {}'''.format(str(UpperCamelCase__    )    )    )
       A__		   =		RemBertModel(UpperCamelCase__    )

       # Load weights from tf checkpoint
       load_tf_weights_in_rembert(UpperCamelCase__					,	UpperCamelCase__					,	UpperCamelCase__    )

       # Save pytorch-model
       print('''Save PyTorch model to {}'''.format(UpperCamelCase__    )    )
       torch.save(model.state_dict()					,	UpperCamelCase__    )


if __name__ == "__main__":
       a__: List[Any]							   =						argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           '--tf_checkpoint_path', default=None, type=str, required=True, help='Path to the TensorFlow checkpoint path.'
       )
       parser.add_argument(
           '--rembert_config_file',
           default=None,
           type=str,
           required=True,
           help=(
               'The config json file corresponding to the pre-trained RemBERT model. \n'
               'This specifies the model architecture.'
           ),
       )
       parser.add_argument(
           '--pytorch_dump_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
       )
       a__: Union[str, Any]							   =						parser.parse_args()
       convert_rembert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.rembert_config_file, args.pytorch_dump_path)





