





import time
from dataclasses import dataclass
from multiprocessing import Pool
from unittest import TestCase
from unittest.mock import patch

import multiprocess
import numpy as np
import pytest

from datasets.utils.py_utils import (
    NestedDataStructure,
    asdict,
    iflatmap_unordered,
    map_nested,
    temp_seed,
    temporary_assignment,
    zip_dict,
)

from .utils import require_tf, require_torch




def 		lowerCAmelCase_    (  __UpperCAmelCase:			List[str]      )      ->	str:  # picklable for multiprocessing
    return x.sum()




def 		lowerCAmelCase_    (  __UpperCAmelCase:			List[str]      )      ->	int:  # picklable for multiprocessing
    return i + 1






@dataclass
class     lowercase__       :

   '''simple docstring'''


   a						:							int
   a						:							str






class     lowercase__       (	__lowerCamelCase				):

   '''simple docstring'''


   def       UpperCamelCase__  (    self							) ->      Optional[Any]:






       """simple docstring"""
       UpperCamelCase__							:				int          =      {}
       UpperCamelCase__							:				Any          =      []
       UpperCamelCase__							:				Dict          =      1
       UpperCamelCase__							:				Optional[int]          =      [1, 2]
       UpperCamelCase__							:				Optional[Any]          =      {'''a''': 1, '''b''': 2}
       UpperCamelCase__							:				Dict          =      {'''a''': [1, 2], '''b''': [3, 4]}
       UpperCamelCase__							:				List[str]          =      {'''a''': {'''1''': 1}, '''b''': 2}
       UpperCamelCase__							:				List[Any]          =      {'''a''': 1, '''b''': 2, '''c''': 3, '''d''': 4}
       UpperCamelCase__							:				Union[str, Any]          =      {}
       UpperCamelCase__							:				Optional[int]          =      []
       UpperCamelCase__							:				Tuple          =      2
       UpperCamelCase__							:				List[str]          =      [2, 3]
       UpperCamelCase__							:				List[Any]          =      {'''a''': 2, '''b''': 3}
       UpperCamelCase__							:				Any          =      {'''a''': [2, 3], '''b''': [4, 5]}
       UpperCamelCase__							:				Tuple          =      {'''a''': {'''1''': 2}, '''b''': 3}
       UpperCamelCase__							:				Optional[Any]          =      {'''a''': 2, '''b''': 3, '''c''': 4, '''d''': 5}
       self.assertEqual(map_nested(__magic_name__,		__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__							),		__magic_name__							)

       UpperCamelCase__							:				Dict          =      2
       self.assertEqual(map_nested(__magic_name__,		__magic_name__,		num_proc=__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__,		num_proc=__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__,		num_proc=__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__,		num_proc=__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__,		num_proc=__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__,		num_proc=__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__,		num_proc=__magic_name__							),		__magic_name__							)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__,		num_proc=__magic_name__							),		__magic_name__							)

       UpperCamelCase__							:				int          =      {'''a''': np.eye(2							), '''b''': np.zeros(3							), '''c''': np.ones(2							)}
       UpperCamelCase__							:				List[str]          =      {'''a''': 2, '''b''': 0, '''c''': 2}
       UpperCamelCase__							:				Union[str, Any]          =      {
           '''a''': np.eye(2							).astype(__magic_name__							),
           '''b''': np.zeros(3							).astype(__magic_name__							),
           '''c''': np.ones(2							).astype(__magic_name__							),
       }
       self.assertEqual(map_nested(__magic_name__,		__magic_name__,		map_numpy=__magic_name__							),		__magic_name__							)
       self.assertEqual(
           {k: v.tolist() for k, v in map_nested(__magic_name__,		__magic_name__,		map_numpy=__magic_name__							).items()},		{k: v.tolist() for k, v in expected_map_nested_sna_int.items()},		)
       self.assertEqual(map_nested(__magic_name__,		__magic_name__,		map_numpy=__magic_name__,		num_proc=__magic_name__							),		__magic_name__							)
       self.assertEqual(
           {k: v.tolist() for k, v in map_nested(__magic_name__,		__magic_name__,		map_numpy=__magic_name__,		num_proc=__magic_name__							).items()},		{k: v.tolist() for k, v in expected_map_nested_sna_int.items()},		)
       with self.assertRaises(__magic_name__							):  # can't pickle a local lambda
           map_nested(lambda __magic_name__							: x + 1,		__magic_name__,		num_proc=__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      List[Any]:






       """simple docstring"""
       UpperCamelCase__							:				List[str]          =      {'''a''': 1, '''b''': 2}
       UpperCamelCase__							:				List[Any]          =      {'''a''': 3, '''b''': 4}
       UpperCamelCase__							:				Optional[int]          =      {'''a''': 5, '''b''': 6}
       UpperCamelCase__							:				Any          =      sorted([('''a''', (1, 3, 5)), ('''b''', (2, 4, 6))]							)
       self.assertEqual(sorted(zip_dict(__magic_name__,		__magic_name__,		__magic_name__							)							),		__magic_name__							)




   def       UpperCamelCase__  (    self							) ->      Optional[Any]:






       """simple docstring"""






       class     lowercase__       :

          '''simple docstring'''


          a						:							Optional[Any]   		=		"bar"




       UpperCamelCase__							:				Tuple          =      Foo()
       self.assertEqual(foo.my_attr,		'''bar'''							)
       with temporary_assignment(__magic_name__,		'''my_attr''',		'''BAR'''							):
           self.assertEqual(foo.my_attr,		'''BAR'''							)
       self.assertEqual(foo.my_attr,		'''bar'''							)




@pytest.mark.parametrize(
    '''iterable_length, num_proc, expected_num_proc'''		,   [
        (1, None, 1),
        (1, 1, 1),
        (2, None, 1),
        (2, 1, 1),
        (2, 2, 1),
        (2, 3, 1),
        (3, 2, 1),
        (16, 16, 16),
        (16, 17, 16),
        (17, 16, 16),
    ]		,   )
def 		lowerCAmelCase_    (  __UpperCAmelCase:			str		,   __UpperCAmelCase:			Optional[Any]		,   __UpperCAmelCase:			List[str]      )      ->	Tuple:
    with patch('''datasets.utils.py_utils._single_map_nested'''      ) as mock_single_map_nested, patch(
        '''datasets.parallel.parallel.Pool'''      ) as mock_multiprocessing_pool:
        UpperCamelCase__							:				str          =      {f"{i}": i for i in range(__UpperCAmelCase      )}
        UpperCamelCase__							:				List[Any]          =      map_nested(lambda __UpperCAmelCase      : x + 10		,   __UpperCAmelCase		,   num_proc=__UpperCAmelCase		,   parallel_min_length=16      )
        if expected_num_proc == 1:
            assert mock_single_map_nested.called
            assert not mock_multiprocessing_pool.called
        else:
            assert not mock_single_map_nested.called
            assert mock_multiprocessing_pool.called
            assert mock_multiprocessing_pool.call_args[0][0] == expected_num_proc






class     lowercase__       (	__lowerCamelCase				):

   '''simple docstring'''


   @require_tf
   def       UpperCamelCase__  (    self							) ->      int:






       """simple docstring"""
       import tensorflow as tf
       from tensorflow.keras import layers

       UpperCamelCase__							:				List[str]          =      layers.Dense(2							)

       def gen_random_output():
           UpperCamelCase__							:				Optional[int]          =      tf.random.uniform((1, 3)							)
           return model(__magic_name__							).numpy()

       with temp_seed(42,		set_tensorflow=__magic_name__							):
           UpperCamelCase__							:				Tuple          =      gen_random_output()
       with temp_seed(42,		set_tensorflow=__magic_name__							):
           UpperCamelCase__							:				List[Any]          =      gen_random_output()
       UpperCamelCase__							:				int          =      gen_random_output()

       np.testing.assert_equal(__magic_name__,		__magic_name__							)
       self.assertGreater(np.abs(outa - outa							).sum(),		0							)




   @require_torch
   def       UpperCamelCase__  (    self							) ->      Any:






       """simple docstring"""
       import torch

       def gen_random_output():
           UpperCamelCase__							:				Dict          =      torch.nn.Linear(3,		2							)
           UpperCamelCase__							:				Tuple          =      torch.rand(1,		3							)
           return model(__magic_name__							).detach().numpy()

       with temp_seed(42,		set_pytorch=__magic_name__							):
           UpperCamelCase__							:				str          =      gen_random_output()
       with temp_seed(42,		set_pytorch=__magic_name__							):
           UpperCamelCase__							:				Dict          =      gen_random_output()
       UpperCamelCase__							:				str          =      gen_random_output()

       np.testing.assert_equal(__magic_name__,		__magic_name__							)
       self.assertGreater(np.abs(outa - outa							).sum(),		0							)




   def       UpperCamelCase__  (    self							) ->      Tuple:






       """simple docstring"""
       def gen_random_output():
           return np.random.rand(1,		3							)

       with temp_seed(42							):
           UpperCamelCase__							:				Tuple          =      gen_random_output()
       with temp_seed(42							):
           UpperCamelCase__							:				int          =      gen_random_output()
       UpperCamelCase__							:				str          =      gen_random_output()

       np.testing.assert_equal(__magic_name__,		__magic_name__							)
       self.assertGreater(np.abs(outa - outa							).sum(),		0							)




@pytest.mark.parametrize('''input_data'''		,   [{}]      )
def 		lowerCAmelCase_    (  __UpperCAmelCase:			List[str]      )      ->	Optional[int]:
    UpperCamelCase__							:				List[Any]          =      NestedDataStructure(__UpperCAmelCase      ).data
    assert output_data == input_data




@pytest.mark.parametrize(
    '''data, expected_output'''		,   [
        ({}, []),
        ([], []),
        ('''foo''', ['''foo''']),
        (['''foo''', '''bar'''], ['''foo''', '''bar''']),
        ([['''foo''', '''bar''']], ['''foo''', '''bar''']),
        ([[['''foo'''], ['''bar''']]], ['''foo''', '''bar''']),
        ([[['''foo'''], '''bar''']], ['''foo''', '''bar''']),
        ({'''a''': 1, '''b''': 2}, [1, 2]),
        ({'''a''': [1, 2], '''b''': [3, 4]}, [1, 2, 3, 4]),
        ({'''a''': [[1, 2]], '''b''': [[3, 4]]}, [1, 2, 3, 4]),
        ({'''a''': [[1, 2]], '''b''': [3, 4]}, [1, 2, 3, 4]),
        ({'''a''': [[[1], [2]]], '''b''': [[[3], [4]]]}, [1, 2, 3, 4]),
        ({'''a''': [[[1], [2]]], '''b''': [[3, 4]]}, [1, 2, 3, 4]),
        ({'''a''': [[[1], [2]]], '''b''': [3, 4]}, [1, 2, 3, 4]),
        ({'''a''': [[[1], [2]]], '''b''': [3, [4]]}, [1, 2, 3, 4]),
        ({'''a''': {'''1''': 1}, '''b''': 2}, [1, 2]),
        ({'''a''': {'''1''': [1]}, '''b''': 2}, [1, 2]),
        ({'''a''': {'''1''': [1]}, '''b''': [2]}, [1, 2]),
    ]		,   )
def 		lowerCAmelCase_    (  __UpperCAmelCase:			Tuple		,   __UpperCAmelCase:			List[str]      )      ->	Tuple:
    UpperCamelCase__							:				Union[str, Any]          =      NestedDataStructure(__UpperCAmelCase      ).flatten()
    assert output == expected_output




def 		lowerCAmelCase_    (  )      ->	str:
    UpperCamelCase__							:				List[Any]          =      A(x=1		,   y='''foobar'''      )
    UpperCamelCase__							:				str          =      {'''x''': 1, '''y''': '''foobar'''}
    assert asdict(__UpperCAmelCase      ) == expected_output

    UpperCamelCase__							:				int          =      {'''a''': {'''b''': A(x=10		,   y='''foo'''      )}, '''c''': [A(x=20		,   y='''bar'''      )]}
    UpperCamelCase__							:				Tuple          =      {'''a''': {'''b''': {'''x''': 10, '''y''': '''foo'''}}, '''c''': [{'''x''': 20, '''y''': '''bar'''}]}
    assert asdict(__UpperCAmelCase      ) == expected_output

    with pytest.raises(__UpperCAmelCase      ):
        asdict([1, A(x=10		,   y='''foo'''      )]      )




def 		lowerCAmelCase_    (  __UpperCAmelCase:			str      )      ->	int:
    return text.split()




def 		lowerCAmelCase_    (  __UpperCAmelCase:			str      )      ->	Optional[int]:
    yield (time.time(), content)
    time.sleep(2      )
    yield (time.time(), content)





def 		lowerCAmelCase_    (  )      ->	Optional[int]:
    with Pool(2      ) as pool:
        UpperCamelCase__							:				Optional[Any]          =      list(iflatmap_unordered(__UpperCAmelCase		,   _split_text		,   kwargs_iterable=[{'''text''': '''hello there'''}] * 10      )      )
        assert out.count('''hello'''      ) == 10
        assert out.count('''there'''      ) == 10
        assert len(__UpperCAmelCase      ) == 20

    # check multiprocess from pathos (uses dill for pickling)
    with multiprocess.Pool(2      ) as pool:
        UpperCamelCase__							:				Tuple          =      list(iflatmap_unordered(__UpperCAmelCase		,   _split_text		,   kwargs_iterable=[{'''text''': '''hello there'''}] * 10      )      )
        assert out.count('''hello'''      ) == 10
        assert out.count('''there'''      ) == 10
        assert len(__UpperCAmelCase      ) == 20

    # check that we get items as fast as possible
    with Pool(2      ) as pool:
        UpperCamelCase__							:				Dict          =      []
        for yield_time, content in iflatmap_unordered(
            __UpperCAmelCase		,   _aseconds_generator_of_aitems_with_timing		,   kwargs_iterable=[{'''content''': '''a'''}, {'''content''': '''b'''}]      ):
            assert yield_time < time.time() + 0.1, "we should each item directly after it was yielded"
            out.append(__UpperCAmelCase      )
        assert out.count('''a'''      ) == 2
        assert out.count('''b'''      ) == 2
        assert len(__UpperCAmelCase      ) == 4








from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCAmelCase_     							= logging.get_logger(__name__)

UpperCAmelCase_     							= {
    'google/mobilenet_v2_1.4_224': 'https://huggingface.co/google/mobilenet_v2_1.4_224/resolve/main/config.json',
    'google/mobilenet_v2_1.0_224': 'https://huggingface.co/google/mobilenet_v2_1.0_224/resolve/main/config.json',
    'google/mobilenet_v2_0.75_160': 'https://huggingface.co/google/mobilenet_v2_0.75_160/resolve/main/config.json',
    'google/mobilenet_v2_0.35_96': 'https://huggingface.co/google/mobilenet_v2_0.35_96/resolve/main/config.json',
    # See all MobileNetV2 models at https://huggingface.co/models?filter=mobilenet_v2
}






class     lowercase__       (	__lowerCamelCase				):

   '''simple docstring'''


   a						:							List[str]   		=		"mobilenet_v2"




   def __init__(    self,		__magic_name__=3,		__magic_name__=224,		__magic_name__=1.0,		__magic_name__=8,		__magic_name__=8,		__magic_name__=6,		__magic_name__=32,		__magic_name__=True,		__magic_name__=True,		__magic_name__="relu6",		__magic_name__=True,		__magic_name__=0.8,		__magic_name__=0.02,		__magic_name__=0.001,		__magic_name__=255,		**__magic_name__,		) ->      List[Any]:






       """simple docstring"""
       super().__init__(**__magic_name__							)

       if depth_multiplier <= 0:
           raise ValueError('''depth_multiplier must be greater than zero.'''							)

       UpperCamelCase__							:				Union[str, Any]          =      num_channels
       UpperCamelCase__							:				int          =      image_size
       UpperCamelCase__							:				int          =      depth_multiplier
       UpperCamelCase__							:				Tuple          =      depth_divisible_by
       UpperCamelCase__							:				List[str]          =      min_depth
       UpperCamelCase__							:				Optional[int]          =      expand_ratio
       UpperCamelCase__							:				Optional[int]          =      output_stride
       UpperCamelCase__							:				Tuple          =      first_layer_is_expansion
       UpperCamelCase__							:				Union[str, Any]          =      finegrained_output
       UpperCamelCase__							:				str          =      hidden_act
       UpperCamelCase__							:				Optional[Any]          =      tf_padding
       UpperCamelCase__							:				Optional[int]          =      classifier_dropout_prob
       UpperCamelCase__							:				int          =      initializer_range
       UpperCamelCase__							:				Union[str, Any]          =      layer_norm_eps
       UpperCamelCase__							:				Tuple          =      semantic_loss_ignore_index






class     lowercase__       (	__lowerCamelCase				):

   '''simple docstring'''


   a						:							Union[str, Any]   		=		version.parse("1.11"				)




   @property
   def       UpperCamelCase__  (    self							) ->      Mapping[str, Mapping[int, str]]:






       """simple docstring"""
       return OrderedDict([('''pixel_values''', {0: '''batch'''})]							)




   @property
   def       UpperCamelCase__  (    self							) ->      Mapping[str, Mapping[int, str]]:






       """simple docstring"""
       if self.task == "image-classification":
           return OrderedDict([('''logits''', {0: '''batch'''})]							)
       else:
           return OrderedDict([('''last_hidden_state''', {0: '''batch'''}), ('''pooler_output''', {0: '''batch'''})]							)




   @property
   def       UpperCamelCase__  (    self							) ->      float:






       """simple docstring"""
       return 1E-4

