





"""simple docstring"""






from typing import Tuple, Union

from ...modeling_outputs import BackboneOutput
from ...modeling_utils import PreTrainedModel
from ...utils import is_timm_available, is_torch_available, requires_backends
from ...utils.backbone_utils import BackboneMixin
from .configuration_timm_backbone import TimmBackboneConfig


if is_timm_available():
    import timm


if is_torch_available():
    from torch import Tensor



class 	lowerCAmelCase					(      __lowerCamelCase			,	__lowerCamelCase							):
       lowerCAmelCase__						 =  """pixel_values"""
       lowerCAmelCase__						 =  False
       lowerCAmelCase__						 =  TimmBackboneConfig






       def __init__(   self    ,  a__    ,  **a__     ):
              requires_backends(self    ,  'timm'     )
              super().__init__(SCREAMING_SNAKE_CASE_     )
              _UpperCAmelCase     =     config

              if config.backbone is None:
                     raise ValueError('backbone is not set in the config. Please set it to a timm model name.'     )

              if config.backbone not in timm.list_models():
                     raise ValueError(f"""backbone {config.backbone} is not supported by timm."""     )

              if hasattr(SCREAMING_SNAKE_CASE_    ,  'out_features'     ) and config.out_features is not None:
                     raise ValueError('out_features is not supported by TimmBackbone. Please use out_indices instead.'     )

              _UpperCAmelCase     =     getattr(SCREAMING_SNAKE_CASE_    ,  'use_pretrained_backbone'    ,  SCREAMING_SNAKE_CASE_     )
              if pretrained is None:
                     raise ValueError('use_pretrained_backbone is not set in the config. Please set it to True or False.'     )

              # We just take the final layer by default. This matches the default for the transformers models.
              _UpperCAmelCase     =     config.out_indices if getattr(SCREAMING_SNAKE_CASE_    ,  'out_indices'    ,  SCREAMING_SNAKE_CASE_     ) is not None else (-1,)

              _UpperCAmelCase     =     timm.create_model(
                  config.backbone    ,  pretrained=SCREAMING_SNAKE_CASE_    ,  features_only=config.features_only    ,  in_chans=config.num_channels    ,  out_indices=SCREAMING_SNAKE_CASE_    ,  **SCREAMING_SNAKE_CASE_    ,  )
              # These are used to control the output of the model when called. If output_hidden_states is True, then
              # return_layers is modified to include all layers.
              _UpperCAmelCase     =     self._backbone.return_layers
              _UpperCAmelCase     =     {layer['module']: str(SCREAMING_SNAKE_CASE_     ) for i, layer in enumerate(self._backbone.feature_info.info     )}
              super()._init_backbone(SCREAMING_SNAKE_CASE_     )






       @classmethod
       def 							__A (   cls    ,  a__    ,  *a__    ,  **a__     ):
              requires_backends(cls    ,  ['vision', 'timm']     )
              from ...models.timm_backbone import TimmBackboneConfig

              _UpperCAmelCase     =     kwargs.pop('config'    ,  TimmBackboneConfig()     )

              _UpperCAmelCase     =     kwargs.pop('use_timm_backbone'    ,  SCREAMING_SNAKE_CASE_     )
              if not use_timm:
                     raise ValueError('use_timm_backbone must be True for timm backbones'     )

              _UpperCAmelCase     =     kwargs.pop('num_channels'    ,  config.num_channels     )
              _UpperCAmelCase     =     kwargs.pop('features_only'    ,  config.features_only     )
              _UpperCAmelCase     =     kwargs.pop('use_pretrained_backbone'    ,  config.use_pretrained_backbone     )
              _UpperCAmelCase     =     kwargs.pop('out_indices'    ,  config.out_indices     )
              _UpperCAmelCase     =     TimmBackboneConfig(
                  backbone=SCREAMING_SNAKE_CASE_    ,  num_channels=SCREAMING_SNAKE_CASE_    ,  features_only=SCREAMING_SNAKE_CASE_    ,  use_pretrained_backbone=SCREAMING_SNAKE_CASE_    ,  out_indices=SCREAMING_SNAKE_CASE_    ,  )
              return super()._from_config(SCREAMING_SNAKE_CASE_    ,  **SCREAMING_SNAKE_CASE_     )






       def 							__A (   self    ,  a__     ):
              pass






       def 							__A (   self    ,  a__    ,  a__=None    ,  a__=None    ,  a__=None    ,  **a__     ):
              _UpperCAmelCase     =     return_dict if return_dict is not None else self.config.use_return_dict
              _UpperCAmelCase     =     (
                  output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
              )
              _UpperCAmelCase     =     output_attentions if output_attentions is not None else self.config.output_attentions

              if output_attentions:
                     raise ValueError('Cannot output attentions for timm backbones at the moment'     )

              if output_hidden_states:
                     # We modify the return layers to include all the stages of the backbone
                     _UpperCAmelCase     =     self._all_layers
                     _UpperCAmelCase     =     self._backbone(SCREAMING_SNAKE_CASE_    ,  **SCREAMING_SNAKE_CASE_     )
                     _UpperCAmelCase     =     self._return_layers
                     _UpperCAmelCase     =     tuple(hidden_states[i] for i in self.out_indices     )
              else:
                     _UpperCAmelCase     =     self._backbone(SCREAMING_SNAKE_CASE_    ,  **SCREAMING_SNAKE_CASE_     )
                     _UpperCAmelCase     =     None

              _UpperCAmelCase     =     tuple(SCREAMING_SNAKE_CASE_     )
              _UpperCAmelCase     =     tuple(SCREAMING_SNAKE_CASE_     ) if hidden_states is not None else None

              if not return_dict:
                     _UpperCAmelCase     =     (feature_maps,)
                     if output_hidden_states:
                            _UpperCAmelCase     =     output + (hidden_states,)
                     return output

              return BackboneOutput(feature_maps=SCREAMING_SNAKE_CASE_    ,  hidden_states=SCREAMING_SNAKE_CASE_    ,  attentions=SCREAMING_SNAKE_CASE_     )









"""simple docstring"""






def       __lowerCamelCase    (       SCREAMING_SNAKE_CASE,SCREAMING_SNAKE_CASE							)		->       str:






       """simple docstring"""




       if a < 0 or b < 0:
              raise ValueError('the value of both inputs must be positive'							)

       _UpperCAmelCase     =     str(bin(SCREAMING_SNAKE_CASE							)							)[2:]  # remove the leading "0b"
       _UpperCAmelCase     =     str(bin(SCREAMING_SNAKE_CASE							)							)[2:]  # remove the leading "0b"

       _UpperCAmelCase     =     max(len(SCREAMING_SNAKE_CASE							),len(SCREAMING_SNAKE_CASE							)							)

       return "0b" + "".join(
           str(int(char_a == '1' and char_b == '1'							)							)
           for char_a, char_b in zip(a_binary.zfill(SCREAMING_SNAKE_CASE							),b_binary.zfill(SCREAMING_SNAKE_CASE							)							)							)


if __name__ == "__main__":
    import doctest

    doctest.testmod()






