


'''simple docstring'''
import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

 if is_vision_available():
  from transformers import MaskFormerImageProcessor

if is_vision_available():
 from PIL import Image

class 						_lowerCamelCase					:




 '''simple docstring'''


 def __init__(      self      ,      __lowercase      ,      __lowercase=2      ,      __lowercase=True      ,      __lowercase=False      ,      __lowercase=10      ,      __lowercase=3      ,      __lowercase=32 * 4      ,      __lowercase=32 * 6      ,      __lowercase=4      ,      __lowercase=32      ,      ):

  """simple docstring"""







  __A  :		int       =			parent
  __A  :		Optional[Any]       =			batch_size
  __A  :		Dict       =			is_training
  __A  :		Dict       =			use_auxiliary_loss
  __A  :		List[Any]       =			num_queries
  __A  :		Optional[Any]       =			num_channels
  __A  :		List[str]       =			min_size
  __A  :		List[str]       =			max_size
  __A  :		Optional[int]       =			num_labels
  __A  :		Optional[int]       =			mask_feature_size


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Optional[Any]       =			floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]						).to(
      __lowercase						)

  __A  :		str       =			torch.ones([self.batch_size, self.min_size, self.max_size]      ,      device=__lowercase						)

  __A  :		Tuple       =			(
      torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]      ,      device=__lowercase						) > 0.5
  ).float()
  __A  :		Tuple       =			(torch.rand((self.batch_size, self.num_labels)      ,      device=__lowercase						) > 0.5).long()

  __A  :		Optional[int]       =			self.get_config()
  return config, pixel_values, pixel_mask, mask_labels, class_labels


 def  snake_case__   (      self						):

  """simple docstring"""







  return MaskFormerConfig.from_backbone_and_decoder_configs(
      backbone_config=SwinConfig(
          depths=[1, 1, 1, 1]      ,      )      ,      decoder_config=DetrConfig(
          decoder_ffn_dim=128      ,      num_queries=self.num_queries      ,      decoder_attention_heads=2      ,      d_model=self.mask_feature_size      ,      )      ,      mask_feature_size=self.mask_feature_size      ,      fpn_feature_size=self.mask_feature_size      ,      num_channels=self.num_channels      ,      num_labels=self.num_labels      ,      )


 def  snake_case__   (      self						):

  """simple docstring"""







  __A     ,__A     ,__A     ,__A     ,__A  :		Optional[Any]       =			self.prepare_config_and_inputs()
  __A  :		str       =			{'pixel_values': pixel_values, 'pixel_mask': pixel_mask}
  return config, inputs_dict


 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase						):

  """simple docstring"""







  __A  :		Dict       =			output.encoder_hidden_states
  __A  :		Union[str, Any]       =			output.pixel_decoder_hidden_states
  __A  :		Optional[int]       =			output.transformer_decoder_hidden_states

  self.parent.assertTrue(len(__lowercase						)      ,      len(config.backbone_config.depths						)						)
  self.parent.assertTrue(len(__lowercase						)      ,      len(config.backbone_config.depths						)						)
  self.parent.assertTrue(len(__lowercase						)      ,      config.decoder_config.decoder_layers						)


 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase      ,      __lowercase      ,      __lowercase=False						):

  """simple docstring"""







  with torch.no_grad():
   __A  :		str       =			MaskFormerModel(config=__lowercase						)
   model.to(__lowercase						)
   model.eval()

   __A  :		Optional[Any]       =			model(pixel_values=__lowercase      ,      pixel_mask=__lowercase						)
   __A  :		Tuple       =			model(__lowercase      ,      output_hidden_states=__lowercase						)
  # the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
  # encoder and pixel decoder
  self.parent.assertEqual(
      output.transformer_decoder_last_hidden_state.shape      ,      (self.batch_size, self.num_queries, self.mask_feature_size)      ,      )
  # let's ensure the other two hidden state exists
  self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None						)
  self.parent.assertTrue(output.encoder_last_hidden_state is not None						)

  if output_hidden_states:
   self.check_output_hidden_state(__lowercase      ,      __lowercase						)







 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase      ,      __lowercase      ,      __lowercase      ,      __lowercase						):

  """simple docstring"""







  __A  :		Dict       =			MaskFormerForInstanceSegmentation(config=__lowercase						)
  model.to(__lowercase						)
  model.eval()

  def comm_check_on_output(__lowercase						):
   # let's still check that all the required stuff is there
   self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None						)
   self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None						)
   self.parent.assertTrue(result.encoder_last_hidden_state is not None						)
   # okay, now we need to check the logits shape
   # due to the encoder compression, masks have a //4 spatial size
   self.parent.assertEqual(
       result.masks_queries_logits.shape      ,      (self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)      ,      )
   # + 1 for null class
   self.parent.assertEqual(
       result.class_queries_logits.shape      ,      (self.batch_size, self.num_queries, self.num_labels + 1)						)

  with torch.no_grad():
   __A  :		List[str]       =			model(pixel_values=__lowercase      ,      pixel_mask=__lowercase						)
   __A  :		Optional[Any]       =			model(__lowercase						)

   comm_check_on_output(__lowercase						)

   __A  :		int       =			model(
       pixel_values=__lowercase      ,      pixel_mask=__lowercase      ,      mask_labels=__lowercase      ,      class_labels=__lowercase						)

  comm_check_on_output(__lowercase						)

  self.parent.assertTrue(result.loss is not None						)
  self.parent.assertEqual(result.loss.shape      ,      torch.Size([1]						)						)


@require_torch
class 						_lowerCamelCase					(    snake_case_    ,		snake_case_    ,		unittest.TestCase				):




 '''simple docstring'''
 __lowercase				:		Union[str, Any]    							=						(MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
 __lowercase				:		int    							=						(
     {'''feature-extraction''': MaskFormerModel, '''image-segmentation''': MaskFormerForInstanceSegmentation}
     if is_torch_available()
     else {}
 )

 __lowercase				:		Dict    							=						False
 __lowercase				:		Any    							=						False
 __lowercase				:		Tuple    							=						False
 __lowercase				:		Any    							=						False


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Optional[int]       =			MaskFormerModelTester(self						)
  __A  :		Optional[Any]       =			ConfigTester(self      ,      config_class=__lowercase      ,      has_text_modality=__lowercase						)


 def  snake_case__   (      self						):

  """simple docstring"""







  self.config_tester.run_common_tests()


 def  snake_case__   (      self						):

  """simple docstring"""







  __A     ,__A  :		Dict       =			self.model_tester.prepare_config_and_inputs_for_common()
  self.model_tester.create_and_check_maskformer_model(__lowercase      ,      **__lowercase      ,      output_hidden_states=__lowercase						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Any       =			self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*__lowercase						)


 @unittest.skip(reason='MaskFormer does not use inputs_embeds'						)
 def  snake_case__   (      self						):

  """simple docstring"""







  pass


 @unittest.skip(reason='MaskFormer does not have a get_input_embeddings method'						)
 def  snake_case__   (      self						):

  """simple docstring"""







  pass


 @unittest.skip(reason='MaskFormer is not a generative model'						)
 def  snake_case__   (      self						):

  """simple docstring"""







  pass


 @unittest.skip(reason='MaskFormer does not use token embeddings'						)
 def  snake_case__   (      self						):

  """simple docstring"""







  pass


 @require_torch_multi_gpu
 @unittest.skip(
     reason='MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'						)
 def  snake_case__   (      self						):

  """simple docstring"""







  pass


 @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'						)
 def  snake_case__   (      self						):

  """simple docstring"""







  pass


 def  snake_case__   (      self						):

  """simple docstring"""







  __A     ,__A  :		List[Any]       =			self.model_tester.prepare_config_and_inputs_for_common()

  for model_class in self.all_model_classes:
   __A  :		List[str]       =			model_class(__lowercase						)
   __A  :		List[str]       =			inspect.signature(model.forward						)
   # signature.parameters is an OrderedDict => so arg_names order is deterministic
   __A  :		List[str]       =			[*signature.parameters.keys()]

   __A  :		List[str]       =			['pixel_values']
   self.assertListEqual(arg_names[:1]      ,      __lowercase						)


 @slow
 def  snake_case__   (      self						):

  """simple docstring"""







  for model_name in ["facebook/maskformer-swin-small-coco"]:
   __A  :		List[Any]       =			MaskFormerModel.from_pretrained(__lowercase						)
   self.assertIsNotNone(__lowercase						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Optional[int]       =			(self.model_tester.min_size,) * 2
  __A  :		Tuple       =			{
      'pixel_values': torch.randn((2, 3, *size)      ,      device=__lowercase						),
      'mask_labels': torch.randn((2, 10, *size)      ,      device=__lowercase						),
      'class_labels': torch.zeros(2      ,      10      ,      device=__lowercase						).long(),
  }

  __A  :		str       =			MaskFormerForInstanceSegmentation(MaskFormerConfig()						).to(__lowercase						)
  __A  :		Any       =			model(**__lowercase						)
  self.assertTrue(outputs.loss is not None						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A     ,__A  :		int       =			self.model_tester.prepare_config_and_inputs_for_common()
  self.model_tester.create_and_check_maskformer_model(__lowercase      ,      **__lowercase      ,      output_hidden_states=__lowercase						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A     ,__A  :		Dict       =			self.model_tester.prepare_config_and_inputs_for_common()

  for model_class in self.all_model_classes:
   __A  :		Dict       =			model_class(__lowercase						).to(__lowercase						)
   __A  :		Optional[Any]       =			model(**__lowercase      ,      output_attentions=__lowercase						)
   self.assertTrue(outputs.attentions is not None						)


 def  snake_case__   (      self						):

  """simple docstring"""







  if not self.model_tester.is_training:
   return
  # only MaskFormerForInstanceSegmentation has the loss
  __A  :		str       =			self.all_model_classes[1]
  __A     ,__A     ,__A     ,__A     ,__A  :		Any       =			self.model_tester.prepare_config_and_inputs()

  __A  :		int       =			model_class(__lowercase						)
  model.to(__lowercase						)
  model.train()

  __A  :		Union[str, Any]       =			model(__lowercase      ,      mask_labels=__lowercase      ,      class_labels=__lowercase						).loss
  loss.backward()







 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[Any]       =			self.all_model_classes[1]
  __A     ,__A     ,__A     ,__A     ,__A  :		Dict       =			self.model_tester.prepare_config_and_inputs()
  __A  :		Any       =			True
  __A  :		str       =			True

  __A  :		Optional[Any]       =			model_class(__lowercase						)
  model.to(__lowercase						)
  model.train()

  __A  :		List[str]       =			model(__lowercase      ,      mask_labels=__lowercase      ,      class_labels=__lowercase						)

  __A  :		Optional[Any]       =			outputs.encoder_hidden_states[0]
  encoder_hidden_states.retain_grad()

  __A  :		Optional[int]       =			outputs.pixel_decoder_hidden_states[0]
  pixel_decoder_hidden_states.retain_grad()
  # we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
  __A  :		List[Any]       =			outputs.transformer_decoder_hidden_states[0]
  transformer_decoder_hidden_states.retain_grad()

  __A  :		Dict       =			outputs.attentions[0]
  attentions.retain_grad()

  outputs.loss.backward(retain_graph=__lowercase						)

  self.assertIsNotNone(encoder_hidden_states.grad						)
  self.assertIsNotNone(pixel_decoder_hidden_states.grad						)
  self.assertIsNotNone(transformer_decoder_hidden_states.grad						)
  self.assertIsNotNone(attentions.grad						)


UpperCAmelCase_ :     Optional[int]								=      1E-4


def 			_lowercase       (				):
 __A  :		Optional[int]       =			Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
 return image


@require_vision
@slow
class 						_lowerCamelCase					(    unittest.TestCase				):




 '''simple docstring'''


 @cached_property
 def  snake_case__   (      self						):

  """simple docstring"""







  return (
      MaskFormerImageProcessor.from_pretrained('facebook/maskformer-swin-small-coco'						)
      if is_vision_available()
      else None
  )


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Optional[int]       =			MaskFormerModel.from_pretrained('facebook/maskformer-swin-small-coco'						).to(__lowercase						)
  __A  :		List[Any]       =			self.default_image_processor
  __A  :		Union[str, Any]       =			prepare_img()
  __A  :		List[str]       =			image_processor(__lowercase      ,      return_tensors='pt'						).to(__lowercase						)
  __A  :		Optional[int]       =			inputs['pixel_values'].shape
  # check size is divisible by 32
  self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0						)
  # check size
  self.assertEqual(__lowercase      ,      (1, 3, 800, 1_088)						)

  with torch.no_grad():
   __A  :		List[Any]       =			model(**__lowercase						)

  __A  :		Optional[Any]       =			torch.tensor(
      [[-0.0_4_8_2, 0.9_2_2_8, 0.4_9_5_1], [-0.2_5_4_7, 0.8_0_1_7, 0.8_5_2_7], [-0.0_0_6_9, 0.3_3_8_5, -0.0_0_8_9]]						).to(__lowercase						)
  self.assertTrue(
      torch.allclose(
          outputs.encoder_last_hidden_state[0, 0, :3, :3]      ,      __lowercase      ,      atol=__lowercase						)						)

  __A  :		str       =			torch.tensor(
      [[-0.8_4_2_2, -0.8_4_3_4, -0.9_7_1_8], [-1.0_1_4_4, -0.5_5_6_5, -0.4_1_9_5], [-1.0_0_3_8, -0.4_4_8_4, -0.1_9_6_1]]						).to(__lowercase						)
  self.assertTrue(
      torch.allclose(
          outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]      ,      __lowercase      ,      atol=__lowercase						)						)

  __A  :		Optional[int]       =			torch.tensor(
      [[0.2_8_5_2, -0.0_1_5_9, 0.9_7_3_5], [0.6_2_5_4, 0.1_8_5_8, 0.8_5_2_9], [-0.0_6_8_0, -0.4_1_1_6, 1.8_4_1_3]]						).to(__lowercase						)
  self.assertTrue(
      torch.allclose(
          outputs.transformer_decoder_last_hidden_state[0, :3, :3]      ,      __lowercase      ,      atol=__lowercase						)						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Any       =			(
      MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-swin-small-coco'						)
      .to(__lowercase						)
      .eval()
  )
  __A  :		List[str]       =			self.default_image_processor
  __A  :		Tuple       =			prepare_img()
  __A  :		Union[str, Any]       =			image_processor(__lowercase      ,      return_tensors='pt'						).to(__lowercase						)
  __A  :		Union[str, Any]       =			inputs['pixel_values'].shape
  # check size is divisible by 32
  self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0						)
  # check size
  self.assertEqual(__lowercase      ,      (1, 3, 800, 1_088)						)

  with torch.no_grad():
   __A  :		Dict       =			model(**__lowercase						)
  # masks_queries_logits
  __A  :		Optional[Any]       =			outputs.masks_queries_logits
  self.assertEqual(
      masks_queries_logits.shape      ,      (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)      ,      )
  __A  :		Optional[int]       =			[
      [-1.3_7_3_7_1_2_4, -1.7_7_2_4_9_3_7, -1.9_3_6_4_2_3_3],
      [-1.5_9_7_7_2_8_1, -1.9_8_6_7_9_3_9, -2.1_5_2_3_6_9_5],
      [-1.5_7_9_5_3_9_8, -1.9_2_6_9_8_3_2, -2.0_9_3_9_4_2],
  ]
  __A  :		Tuple       =			torch.tensor(__lowercase						).to(__lowercase						)
  self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]      ,      __lowercase      ,      atol=__lowercase						)						)
  # class_queries_logits
  __A  :		Optional[int]       =			outputs.class_queries_logits
  self.assertEqual(
      class_queries_logits.shape      ,      (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)						)
  __A  :		str       =			torch.tensor(
      [
          [1.6_512E00, -5.2_572E00, -3.3_519E00],
          [3.6_169E-02, -5.9_025E00, -2.9_313E00],
          [1.0_766E-04, -7.7_630E00, -5.1_263E00],
      ]						).to(__lowercase						)
  self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]      ,      __lowercase      ,      atol=__lowercase						)						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Dict       =			(
      MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-resnet101-coco-stuff'						)
      .to(__lowercase						)
      .eval()
  )
  __A  :		List[str]       =			self.default_image_processor
  __A  :		Union[str, Any]       =			prepare_img()
  __A  :		Optional[int]       =			image_processor(__lowercase      ,      return_tensors='pt'						).to(__lowercase						)
  __A  :		int       =			inputs['pixel_values'].shape
  # check size is divisible by 32
  self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0						)
  # check size
  self.assertEqual(__lowercase      ,      (1, 3, 800, 1_088)						)

  with torch.no_grad():
   __A  :		int       =			model(**__lowercase						)
  # masks_queries_logits
  __A  :		Union[str, Any]       =			outputs.masks_queries_logits
  self.assertEqual(
      masks_queries_logits.shape      ,      (1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)      ,      )
  __A  :		Optional[int]       =			[[-0.9_0_4_6, -2.6_3_6_6, -4.6_0_6_2], [-3.4_1_7_9, -5.7_8_9_0, -8.8_0_5_7], [-4.9_1_7_9, -7.6_5_6_0, -1_0.7_7_1_1]]
  __A  :		List[str]       =			torch.tensor(__lowercase						).to(__lowercase						)
  self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]      ,      __lowercase      ,      atol=__lowercase						)						)
  # class_queries_logits
  __A  :		List[str]       =			outputs.class_queries_logits
  self.assertEqual(
      class_queries_logits.shape      ,      (1, model.config.decoder_config.num_queries, model.config.num_labels + 1)						)
  __A  :		str       =			torch.tensor(
      [[4.7_1_8_8, -3.2_5_8_5, -2.8_8_5_7], [6.6_8_7_1, -2.9_1_8_1, -1.2_4_8_7], [7.2_4_4_9, -2.2_7_6_4, -2.1_8_7_4]]						).to(__lowercase						)
  self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]      ,      __lowercase      ,      atol=__lowercase						)						)







 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Optional[int]       =			(
      MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-swin-small-coco'						)
      .to(__lowercase						)
      .eval()
  )
  __A  :		str       =			self.default_image_processor

  __A  :		Optional[int]       =			image_processor(
      [np.zeros((3, 800, 1_333)						), np.zeros((3, 800, 1_333)						)]      ,      segmentation_maps=[np.zeros((384, 384)						).astype(np.floataa						), np.zeros((384, 384)						).astype(np.floataa						)]      ,      return_tensors='pt'      ,      )

  __A  :		Tuple       =			inputs['pixel_values'].to(__lowercase						)
  __A  :		Tuple       =			[el.to(__lowercase						) for el in inputs['mask_labels']]
  __A  :		List[str]       =			[el.to(__lowercase						) for el in inputs['class_labels']]

  with torch.no_grad():
   __A  :		List[Any]       =			model(**__lowercase						)

  self.assertTrue(outputs.loss is not None						)










'''simple docstring'''
import os
from pathlib import Path
from unittest.mock import patch

import pytest
import zstandard as zstd

from datasets.download.download_config import DownloadConfig
from datasets.utils.file_utils import (
    OfflineModeIsEnabled,
    cached_path,
    fsspec_get,
    fsspec_head,
    ftp_get,
    ftp_head,
    get_from_cache,
    http_get,
    http_head,
)


UpperCAmelCase_ :     int								=      '\\n    Text data.\n    Second line of data.'

UpperCAmelCase_ :     Tuple								=      'file'


@pytest.fixture(scope='session'			)
def 			_lowercase       (				UpperCamelCase__       :					Any			):
 __A  :		Optional[Any]       =			tmp_path_factory.mktemp('data'			) / (FILE_PATH + '.zstd')
 __A  :		Optional[int]       =			bytes(UpperCamelCase__,				'utf-8'			)
 with zstd.open(UpperCamelCase__,				'wb'			) as f:
  f.write(UpperCamelCase__			)
 return path


@pytest.fixture
def 			_lowercase       (				UpperCamelCase__       :					Optional[Any]			):
 with open(os.path.join(tmpfs.local_root_dir,				UpperCamelCase__			),				'w'			) as f:
  f.write(UpperCamelCase__			)
 return FILE_PATH


@pytest.mark.parametrize('compression_format',				['gzip', 'xz', 'zstd']			)
def 			_lowercase       (				UpperCamelCase__       :					List[Any],				UpperCamelCase__       :					Tuple,				UpperCamelCase__       :					Union[str, Any],				UpperCamelCase__       :					str,				UpperCamelCase__       :					Tuple,				UpperCamelCase__       :					List[str]			):
 __A  :		Any       =			{'gzip': gz_file, 'xz': xz_file, 'zstd': zstd_path}
 __A  :		str       =			input_paths[compression_format]
 __A  :		str       =			tmp_path / 'cache'
 __A  :		int       =			DownloadConfig(cache_dir=UpperCamelCase__,				extract_compressed_file=UpperCamelCase__			)
 __A  :		Tuple       =			cached_path(UpperCamelCase__,				download_config=UpperCamelCase__			)
 with open(UpperCamelCase__			) as f:
  __A  :		Union[str, Any]       =			f.read()
 with open(UpperCamelCase__			) as f:
  __A  :		int       =			f.read()
 assert extracted_file_content == expected_file_content


@pytest.mark.parametrize('default_extracted',				[True, False]			)
@pytest.mark.parametrize('default_cache_dir',				[True, False]			)
def 			_lowercase       (				UpperCamelCase__       :					List[Any],				UpperCamelCase__       :					Optional[int],				UpperCamelCase__       :					str,				UpperCamelCase__       :					int,				UpperCamelCase__       :					Dict			):
 __A  :		Optional[Any]       =			'custom_cache'
 __A  :		Dict       =			'custom_extracted_dir'
 __A  :		int       =			tmp_path / 'custom_extracted_path'
 if default_extracted:
  __A  :		List[str]       =			('downloads' if default_cache_dir else custom_cache_dir, 'extracted')
 else:
  monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_DIR',				UpperCamelCase__			)
  monkeypatch.setattr('datasets.config.EXTRACTED_DATASETS_PATH',				str(UpperCamelCase__			)			)
  __A  :		Optional[int]       =			custom_extracted_path.parts[-2:] if default_cache_dir else (custom_cache_dir, custom_extracted_dir)

 __A  :		Optional[Any]       =			xz_file
 __A  :		List[Any]       =			(
     DownloadConfig(extract_compressed_file=UpperCamelCase__			)
     if default_cache_dir
     else DownloadConfig(cache_dir=tmp_path / custom_cache_dir,				extract_compressed_file=UpperCamelCase__			)
 )
 __A  :		Tuple       =			cached_path(UpperCamelCase__,				download_config=UpperCamelCase__			)
 assert Path(UpperCamelCase__			).parent.parts[-2:] == expected


def 			_lowercase       (				UpperCamelCase__       :					Optional[int]			):
 # absolute path
 __A  :		Any       =			str(Path(UpperCamelCase__			).resolve()			)
 assert cached_path(UpperCamelCase__			) == text_file
 # relative path
 __A  :		Optional[Any]       =			str(Path(UpperCamelCase__			).resolve().relative_to(Path(os.getcwd()			)			)			)
 assert cached_path(UpperCamelCase__			) == text_file


def 			_lowercase       (				UpperCamelCase__       :					List[str]			):
 # absolute path
 __A  :		Any       =			str(tmp_path.resolve() / '__missing_file__.txt'			)
 with pytest.raises(UpperCamelCase__			):
  cached_path(UpperCamelCase__			)
 # relative path
 __A  :		Tuple       =			'./__missing_file__.txt'
 with pytest.raises(UpperCamelCase__			):
  cached_path(UpperCamelCase__			)


def 			_lowercase       (				UpperCamelCase__       :					Tuple			):
 __A  :		Optional[int]       =			get_from_cache(f"""tmp://{tmpfs_file}"""			)
 with open(UpperCamelCase__			) as f:
  __A  :		Tuple       =			f.read()
 assert output_file_content == FILE_CONTENT


@patch('datasets.config.HF_DATASETS_OFFLINE',				UpperCamelCase__			)
def 			_lowercase       (				):
 with pytest.raises(UpperCamelCase__			):
  cached_path('https://huggingface.co'			)


@patch('datasets.config.HF_DATASETS_OFFLINE',				UpperCamelCase__			)
def 			_lowercase       (				UpperCamelCase__       :					Optional[Any]			):
 __A  :		Optional[int]       =			tmp_path_factory.mktemp('data'			) / 'file.html'
 with pytest.raises(UpperCamelCase__			):
  http_get('https://huggingface.co',				temp_file=UpperCamelCase__			)
 with pytest.raises(UpperCamelCase__			):
  http_head('https://huggingface.co'			)


@patch('datasets.config.HF_DATASETS_OFFLINE',				UpperCamelCase__			)
def 			_lowercase       (				UpperCamelCase__       :					Any			):
 __A  :		Union[str, Any]       =			tmp_path_factory.mktemp('data'			) / 'file.html'
 with pytest.raises(UpperCamelCase__			):
  ftp_get('ftp://huggingface.co',				temp_file=UpperCamelCase__			)
 with pytest.raises(UpperCamelCase__			):
  ftp_head('ftp://huggingface.co'			)


@patch('datasets.config.HF_DATASETS_OFFLINE',				UpperCamelCase__			)
def 			_lowercase       (				UpperCamelCase__       :					Optional[Any]			):
 __A  :		Optional[int]       =			tmp_path_factory.mktemp('data'			) / 'file.html'
 with pytest.raises(UpperCamelCase__			):
  fsspec_get('s3://huggingface.co',				temp_file=UpperCamelCase__			)
 with pytest.raises(UpperCamelCase__			):
  fsspec_head('s3://huggingface.co'			)






