code: 
  
  
  import json
  import os
  import unittest
  
  from transformers import BartTokenizer, BartTokenizerFast, BatchEncoding
  from transformers.models.roberta.tokenization_roberta import VOCAB_FILES_NAMES
  from transformers.testing_utils import require_tokenizers, require_torch
  from transformers.utils import cached_property
  
  from ...test_tokenization_common import TokenizerTesterMixin, filter_roberta_detectors
  
  @require_tokenizers
  class 						snake_case			(       lowerCAmelCase__	, unittest.TestCase	):
  			'''simple docstring'''
  
  			UpperCAmelCase       :       Any       = BartTokenizer
  			UpperCAmelCase       :       str       = BartTokenizerFast
  			UpperCAmelCase       :       Any       = True
  			UpperCAmelCase       :       Optional[int]       = filter_roberta_detectors
  
  			def _lowercase (	self     :     Tuple					)					->					Dict:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								super().setUp()
  								SCREAMING_SNAKE_CASE_	      =       [
  								    '''l''',
  								    '''o''',
  								    '''w''',
  								    '''e''',
  								    '''r''',
  								    '''s''',
  								    '''t''',
  								    '''i''',
  								    '''d''',
  								    '''n''',
  								    '''\u0120''',
  								    '''\u0120l''',
  								    '''\u0120n''',
  								    '''\u0120lo''',
  								    '''\u0120low''',
  								    '''er''',
  								    '''\u0120lowest''',
  								    '''\u0120newer''',
  								    '''\u0120wider''',
  								    '''<unk>''',
  								]
  								SCREAMING_SNAKE_CASE_	      =       dict(zip(lowerCAmelCase_							,							range(len(lowerCAmelCase_					)					)					)					)
  								SCREAMING_SNAKE_CASE_	      =       ['''#version: 0.2''', '''\u0120 l''', '''\u0120l o''', '''\u0120lo w''', '''e r''', '''''']
  								SCREAMING_SNAKE_CASE_	      =       {'''unk_token''': '''<unk>'''}
  
  								SCREAMING_SNAKE_CASE_	      =       os.path.join(self.tmpdirname							,							VOCAB_FILES_NAMES['''vocab_file''']					)
  								SCREAMING_SNAKE_CASE_	      =       os.path.join(self.tmpdirname							,							VOCAB_FILES_NAMES['''merges_file''']					)
  								with open(self.vocab_file							,							'''w'''							,							encoding='''utf-8'''					) as fp:
  													fp.write(json.dumps(lowerCAmelCase_					) + '''\n'''					)
  								with open(self.merges_file							,							'''w'''							,							encoding='''utf-8'''					) as fp:
  													fp.write('''\n'''.join(lowerCAmelCase_					)					)
  
  			def _lowercase (	self     :     List[str]							,							**lowerCAmelCase_     :     Optional[int]					)					->					Union[str, Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								kwargs.update(self.special_tokens_map					)
  								return self.tokenizer_class.from_pretrained(self.tmpdirname							,							**lowerCAmelCase_					)
  
  			def _lowercase (	self     :     Any							,							**lowerCAmelCase_     :     Optional[int]					)					->					int:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								kwargs.update(self.special_tokens_map					)
  								return self.rust_tokenizer_class.from_pretrained(self.tmpdirname							,							**lowerCAmelCase_					)
  
  			def _lowercase (	self     :     List[str]							,							lowerCAmelCase_     :     List[str]					)					->					int:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								return "lower newer", "lower newer"
  
  			@cached_property
  			def _lowercase (	self     :     List[str]					)					->					List[str]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								return BartTokenizer.from_pretrained('''facebook/bart-large'''					)
  
  			@cached_property
  			def _lowercase (	self     :     str					)					->					Dict:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								return BartTokenizerFast.from_pretrained('''facebook/bart-large'''					)
  
  			@require_torch
  			def _lowercase (	self     :     Optional[int]					)					->					Optional[int]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_	      =       ['''A long paragraph for summarization.''', '''Another paragraph for summarization.''']
  								SCREAMING_SNAKE_CASE_	      =       [0, 250, 251, 17_818, 13, 39_186, 1_938, 4, 2]
  
  								for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
  													SCREAMING_SNAKE_CASE_	      =       tokenizer(lowerCAmelCase_							,							max_length=len(lowerCAmelCase_					)							,							padding=lowerCAmelCase_							,							return_tensors='''pt'''					)
  													self.assertIsInstance(lowerCAmelCase_							,							lowerCAmelCase_					)
  
  													self.assertEqual((2, 9)							,							batch.input_ids.shape					)
  													self.assertEqual((2, 9)							,							batch.attention_mask.shape					)
  													SCREAMING_SNAKE_CASE_	      =       batch.input_ids.tolist()[0]
  													self.assertListEqual(lowerCAmelCase_							,							lowerCAmelCase_					)
  													# Test that special tokens are reset
  
  			@require_torch
  			def _lowercase (	self     :     Dict					)					->					List[Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_	      =       ['''A long paragraph for summarization.''', '''Another paragraph for summarization.''']
  								for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
  													SCREAMING_SNAKE_CASE_	      =       tokenizer(lowerCAmelCase_							,							padding=lowerCAmelCase_							,							return_tensors='''pt'''					)
  													# check if input_ids are returned and no labels
  													self.assertIn('''input_ids'''							,							lowerCAmelCase_					)
  													self.assertIn('''attention_mask'''							,							lowerCAmelCase_					)
  													self.assertNotIn('''labels'''							,							lowerCAmelCase_					)
  													self.assertNotIn('''decoder_attention_mask'''							,							lowerCAmelCase_					)
  
  			@require_torch
  			def _lowercase (	self     :     List[Any]					)					->					Dict:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_	      =       [
  								    '''Summary of the text.''',
  								    '''Another summary.''',
  								]
  								for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
  													SCREAMING_SNAKE_CASE_	      =       tokenizer(text_target=lowerCAmelCase_							,							max_length=32							,							padding='''max_length'''							,							return_tensors='''pt'''					)
  													self.assertEqual(32							,							targets['''input_ids'''].shape[1]					)
  
  			@require_torch
  			def _lowercase (	self     :     Optional[int]					)					->					List[str]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
  													SCREAMING_SNAKE_CASE_	      =       tokenizer(
  													    ['''I am a small frog''' * 1_024, '''I am a small frog''']							,							padding=lowerCAmelCase_							,							truncation=lowerCAmelCase_							,							return_tensors='''pt'''					)
  													self.assertIsInstance(lowerCAmelCase_							,							lowerCAmelCase_					)
  													self.assertEqual(batch.input_ids.shape							,							(2, 1_024)					)
  
  			@require_torch
  			def _lowercase (	self     :     Any					)					->					Union[str, Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_	      =       ['''A long paragraph for summarization.''']
  								SCREAMING_SNAKE_CASE_	      =       [
  								    '''Summary of the text.''',
  								]
  								for tokenizer in [self.default_tokenizer, self.default_tokenizer_fast]:
  													SCREAMING_SNAKE_CASE_	      =       tokenizer(lowerCAmelCase_							,							return_tensors='''pt'''					)
  													SCREAMING_SNAKE_CASE_	      =       tokenizer(text_target=lowerCAmelCase_							,							return_tensors='''pt'''					)
  													SCREAMING_SNAKE_CASE_	      =       inputs['''input_ids''']
  													SCREAMING_SNAKE_CASE_	      =       targets['''input_ids''']
  													self.assertTrue((input_ids[:, 0] == tokenizer.bos_token_id).all().item()					)
  													self.assertTrue((labels[:, 0] == tokenizer.bos_token_id).all().item()					)
  													self.assertTrue((input_ids[:, -1] == tokenizer.eos_token_id).all().item()					)
  													self.assertTrue((labels[:, -1] == tokenizer.eos_token_id).all().item()					)
  
  			def _lowercase (	self     :     List[Any]					)					->					Union[str, Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								pass
  
  
  
  
  			def _lowercase (	self     :     List[str]					)					->					int:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
  													with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})'''					):
  																		SCREAMING_SNAKE_CASE_	      =       self.rust_tokenizer_class.from_pretrained(lowerCAmelCase_							,							**lowerCAmelCase_					)
  																		SCREAMING_SNAKE_CASE_	      =       self.tokenizer_class.from_pretrained(lowerCAmelCase_							,							**lowerCAmelCase_					)
  																		SCREAMING_SNAKE_CASE_	      =       '''A, <mask> AllenNLP sentence.'''
  																		SCREAMING_SNAKE_CASE_	      =       tokenizer_r.encode_plus(lowerCAmelCase_							,							add_special_tokens=lowerCAmelCase_							,							return_token_type_ids=lowerCAmelCase_					)
  																		SCREAMING_SNAKE_CASE_	      =       tokenizer_p.encode_plus(lowerCAmelCase_							,							add_special_tokens=lowerCAmelCase_							,							return_token_type_ids=lowerCAmelCase_					)
  
  																		# token_type_ids should put 0 everywhere
  																		self.assertEqual(sum(tokens_r['''token_type_ids''']					)							,							sum(tokens_p['''token_type_ids''']					)					)
  
  																		# attention_mask should put 1 everywhere, so sum over length should be 1
  																		self.assertEqual(
  																		    sum(tokens_r['''attention_mask''']					) / len(tokens_r['''attention_mask''']					)							,							sum(tokens_p['''attention_mask''']					) / len(tokens_p['''attention_mask''']					)							,							)
  
  																		SCREAMING_SNAKE_CASE_	      =       tokenizer_r.convert_ids_to_tokens(tokens_r['''input_ids''']					)
  																		SCREAMING_SNAKE_CASE_	      =       tokenizer_p.convert_ids_to_tokens(tokens_p['''input_ids''']					)
  
  																		# Rust correctly handles the space before the mask while python doesnt
  																		self.assertSequenceEqual(tokens_p['''input_ids''']							,							[0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2]					)
  																		self.assertSequenceEqual(tokens_r['''input_ids''']							,							[0, 250, 6, 50_264, 3_823, 487, 21_992, 3_645, 4, 2]					)
  
  																		self.assertSequenceEqual(
  																		    lowerCAmelCase_							,							['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>''']					)
  																		self.assertSequenceEqual(
  																		    lowerCAmelCase_							,							['''<s>''', '''A''', ''',''', '''<mask>''', '''ĠAllen''', '''N''', '''LP''', '''Ġsentence''', '''.''', '''</s>''']					)
  
  
  
style_context: 
  
  
  import json
  import os
  from collections.abc import Mapping
  from typing import Dict, List, Optional, Union
  
  import numpy as np
  import torch
  
  from .imports import is_safetensors_available
  
  
  
  
  
  
  
  def 							UpperCAmelCase				(     UpperCAmelCase				,UpperCAmelCase				,UpperCAmelCase				,UpperCAmelCase=None      )->		Dict:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  					SCREAMING_SNAKE_CASE_	      =       None
  					# Check the string instead of the dtype to be compatible with versions of PyTorch that don't have bfloat16.
  					if str(weight.dtype      ) == "torch.bfloat16":
  										# Need to reinterpret the underlined data as int16 since NumPy does not handle bfloat16s.
  										SCREAMING_SNAKE_CASE_	      =       weight.view(torch.intaa      )
  										SCREAMING_SNAKE_CASE_	      =       '''bfloat16'''
  					SCREAMING_SNAKE_CASE_	      =       weight.cpu().numpy()
  					SCREAMING_SNAKE_CASE_	      =       os.path.join(UpperCAmelCase				,f'''{weight_name}.dat'''      )
  					if index is not None:
  										if dtype is None:
  															SCREAMING_SNAKE_CASE_	      =       str(array.dtype      )
  										SCREAMING_SNAKE_CASE_	      =       {'''dtype''': dtype, '''shape''': list(array.shape      )}
  					if array.ndim == 0:
  										SCREAMING_SNAKE_CASE_	      =       array[None]
  					SCREAMING_SNAKE_CASE_	      =       np.memmap(UpperCAmelCase				,dtype=array.dtype				,mode='''w+'''				,shape=array.shape      )
  					SCREAMING_SNAKE_CASE_	      =       array[:]
  					file_array.flush()
  					return index
  
  
  
  
  
  
  
  def 							UpperCAmelCase				(     UpperCAmelCase				,UpperCAmelCase      )->		List[str]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  					SCREAMING_SNAKE_CASE_	      =       tuple(weight_info['''shape''']      )
  					if shape == ():
  										# NumPy memory-mapped arrays can't have 0 dims so it was saved as 1d tensor
  										SCREAMING_SNAKE_CASE_	      =       (1,)
  
  					SCREAMING_SNAKE_CASE_	      =       weight_info['''dtype''']
  					if dtype == "bfloat16":
  										# NumPy does not support bfloat16 so this was saved as a int16
  										SCREAMING_SNAKE_CASE_	      =       '''int16'''
  
  					SCREAMING_SNAKE_CASE_	      =       np.memmap(UpperCAmelCase				,dtype=UpperCAmelCase				,shape=UpperCAmelCase				,mode='''r'''      )
  
  					if len(weight_info['''shape''']      ) == 0:
  										SCREAMING_SNAKE_CASE_	      =       weight[0]
  					SCREAMING_SNAKE_CASE_	      =       torch.tensor(UpperCAmelCase      )
  					if weight_info["dtype"] == "bfloat16":
  										SCREAMING_SNAKE_CASE_	      =       weight.view(torch.bfloataa      )
  
  					return weight
  
  
  
  
  
  
  
  def 							UpperCAmelCase				(     UpperCAmelCase				,UpperCAmelCase      )->		str:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  					if index is None or len(UpperCAmelCase      ) == 0:
  										# Nothing to save
  										return
  
  					SCREAMING_SNAKE_CASE_	      =       os.path.join(UpperCAmelCase				,'''index.json'''      )
  					if os.path.isfile(UpperCAmelCase      ):
  										with open(UpperCAmelCase				,'''r'''				,encoding='''utf-8'''      ) as f:
  															SCREAMING_SNAKE_CASE_	      =       json.load(UpperCAmelCase      )
  					else:
  										SCREAMING_SNAKE_CASE_	      =       {}
  					current_index.update(UpperCAmelCase      )
  
  					with open(UpperCAmelCase				,'''w'''				,encoding='''utf-8'''      ) as f:
  										json.dump(UpperCAmelCase				,UpperCAmelCase				,indent=2      )
  
  
  
  
  
  
  
  def 							UpperCAmelCase				(     UpperCAmelCase				,UpperCAmelCase      )->		Optional[Any]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  					os.makedirs(UpperCAmelCase				,exist_ok=UpperCAmelCase      )
  					SCREAMING_SNAKE_CASE_	      =       {}
  					for name, parameter in state_dict.items():
  										SCREAMING_SNAKE_CASE_	      =       offload_weight(UpperCAmelCase				,UpperCAmelCase				,UpperCAmelCase				,index=UpperCAmelCase      )
  
  					# Update index
  					save_offload_index(UpperCAmelCase				,UpperCAmelCase      )
  
  class 						snake_case			(       lowerCAmelCase__	):
  			'''simple docstring'''
  
  			def __init__(	self     :     int							,							lowerCAmelCase_     :     Mapping							,							lowerCAmelCase_     :     str					)					->					Dict:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								SCREAMING_SNAKE_CASE_	      =       dataset
  								SCREAMING_SNAKE_CASE_	      =       prefix
  
  			def __getitem__(	self     :     Optional[int]							,							lowerCAmelCase_     :     Optional[int]					)					->					Union[str, Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								return self.dataset[F'''{self.prefix}{key}''']
  
  			def __iter__(	self     :     Tuple					)					->					str:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								return iter([key for key in self.dataset if key.startswith(self.prefix					)]					)
  
  
  
  
  			def __len__(	self     :     Dict					)					->					Optional[int]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								return len(self.dataset					)
  
  class 						snake_case			(       lowerCAmelCase__	):
  			'''simple docstring'''
  
  			def __init__(	self     :     int							,							lowerCAmelCase_     :     Dict[str, torch.Tensor] = None							,							lowerCAmelCase_     :     Optional[Union[str, os.PathLike]] = None							,							lowerCAmelCase_     :     Mapping = None							,							lowerCAmelCase_     :     int=None							,							)					->					Tuple:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								if state_dict is None and save_folder is None:
  													raise ValueError('''Need either a `state_dict` or a `save_folder` containing offloaded weights.'''					)
  
  								SCREAMING_SNAKE_CASE_	      =       {} if state_dict is None else state_dict
  								SCREAMING_SNAKE_CASE_	      =       save_folder
  								if index is None and save_folder is not None:
  													with open(os.path.join(lowerCAmelCase_							,							'''index.json'''					)					) as f:
  																		SCREAMING_SNAKE_CASE_	      =       json.load(lowerCAmelCase_					)
  								SCREAMING_SNAKE_CASE_	      =       {} if index is None else index
  								SCREAMING_SNAKE_CASE_	      =       list(self.state_dict.keys()					)
  								self.all_keys.extend([key for key in self.index if key not in self.all_keys]					)
  								SCREAMING_SNAKE_CASE_	      =       device
  
  			def __getitem__(	self     :     str							,							lowerCAmelCase_     :     str					)					->					Union[str, Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								if key in self.state_dict:
  													return self.state_dict[key]
  								SCREAMING_SNAKE_CASE_	      =       self.index[key]
  								if weight_info.get('''safetensors_file'''					) is not None:
  													if not is_safetensors_available():
  																		raise ImportError('''These offloaded weights require the use of safetensors: `pip install safetensors`.'''					)
  
  													from safetensors import safe_open
  
  													SCREAMING_SNAKE_CASE_	      =       '''cpu''' if self.device is None else self.device
  													with safe_open(weight_info['''safetensors_file''']							,							framework='''pt'''							,							device=lowerCAmelCase_					) as f:
  																		SCREAMING_SNAKE_CASE_	      =       f.get_tensor(weight_info.get('''weight_name'''							,							lowerCAmelCase_					)					)
  
  													if "dtype" in weight_info:
  																		return tensor.to(getattr(lowerCAmelCase_							,							weight_info['''dtype''']					)					)
  													else:
  																		return tensor
  
  								SCREAMING_SNAKE_CASE_	      =       os.path.join(self.save_folder							,							F'''{key}.dat'''					)
  								return load_offloaded_weight(lowerCAmelCase_							,							lowerCAmelCase_					)
  
  			def __iter__(	self     :     str					)					->					Optional[int]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								return iter(self.all_keys					)
  
  
  
  
  			def __len__(	self     :     Optional[Any]					)					->					Optional[Any]:
  
  
  
  
  								"""simple docstring"""
  
  
  
  
  
  
  								return len(self.all_keys					)
  
  
  
  
  
  
  
  def 							UpperCAmelCase				(     UpperCAmelCase				,UpperCAmelCase      )->		List[str]:
  
  
  
  
  
  
  
  					'''simple docstring'''
  
  
  
  					SCREAMING_SNAKE_CASE_	      =       {}
  					for module_name in submodule_names:
  										# We want to catch module_name parameter (module_name.xxx) or potentially module_name, but not any of the
  										# submodules that could being like module_name (transformers.h.1 and transformers.h.10 for instance)
  										result.update(
  										    {
  										        key: param
  										        for key, param in state_dict.items()
  										        if key == module_name or key.startswith(module_name + '''.'''      )
  										    }      )
  					return result
  
  
  