

from __future__ import annotations

from collections import deque





class       snake_case_  :







			'''simple docstring'''

			def __init__(	self					:							Union[str, Any]				,  _UpperCamelCase					:							list[str]				) ->Optional[int]:
									snake_case_  =      []
									self.adlist.append(
									    {'''value''': '''''', '''next_states''': [], '''fail_state''': 0, '''output''': []}				)

									for keyword in keywords:
															self.add_keyword(_UpperCamelCase				)
									self.set_fail_transitions()







			def 							snake_case__(	self					:							int				,  _UpperCamelCase					:							int				,  _UpperCamelCase					:							str				) ->int | None:
									for state in self.adlist[current_state]["next_states"]:
															if char == self.adlist[state]["value"]:
																					return state
									return None







			def 							snake_case__(	self					:							Optional[Any]				,  _UpperCamelCase					:							str				) ->None:
									snake_case_  =      0
									for character in keyword:
															snake_case_  =      self.find_next_state(_UpperCamelCase				,  _UpperCamelCase				)
															if next_state is None:
																					self.adlist.append(
																					    {
																					        '''value''': character,
																					        '''next_states''': [],
																					        '''fail_state''': 0,
																					        '''output''': [],
																					    }				)
																					self.adlist[current_state]["next_states"].append(len(self.adlist				) - 1				)
																					snake_case_  =      len(self.adlist				) - 1
															else:
																					snake_case_  =      next_state
									self.adlist[current_state]["output"].append(_UpperCamelCase				)







			def 							snake_case__(	self					:							List[Any]				) ->None:
									snake_case_  =      deque()
									for node in self.adlist[0]["next_states"]:
															q.append(_UpperCamelCase				)
															snake_case_  =      0
									while q:
															snake_case_  =      q.popleft()
															for child in self.adlist[r]["next_states"]:
																					q.append(_UpperCamelCase				)
																					snake_case_  =      self.adlist[r]['''fail_state''']
																					while (
																					    self.find_next_state(_UpperCamelCase				,  self.adlist[child]['''value''']				) is None
																					    and state != 0
																					):
																											snake_case_  =      self.adlist[state]['''fail_state''']
																					snake_case_  =      self.find_next_state(
																					    _UpperCamelCase				,  self.adlist[child]['''value''']				)
																					if self.adlist[child]["fail_state"] is None:
																											snake_case_  =      0
																					snake_case_  =      (
																					    self.adlist[child]['''output''']
																					    + self.adlist[self.adlist[child]['''fail_state''']]['''output''']
																					)







			def 							snake_case__(	self					:							Any				,  _UpperCamelCase					:							str				) ->dict[str, list[int]]:
									snake_case_  =      {}  # returns a dict with keywords and list of its occurrences
									snake_case_  =      0
									for i in range(len(_UpperCamelCase				)				):
															while (
															    self.find_next_state(_UpperCamelCase				,  string[i]				) is None
															    and current_state != 0
															):
																					snake_case_  =      self.adlist[current_state]['''fail_state''']
															snake_case_  =      self.find_next_state(_UpperCamelCase				,  string[i]				)
															if next_state is None:
																					snake_case_  =      0
															else:
																					snake_case_  =      next_state
																					for key in self.adlist[current_state]["output"]:
																											if key not in result:
																																	snake_case_  =      []
																											result[key].append(i - len(_UpperCamelCase				) + 1				)
									return result


if __name__ == "__main__":
					import doctest

					doctest.testmod()






"""simple docstring"""


from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


__magic_name__       :					List[str]						=			logging.get_logger(__name__)

__magic_name__       :					Any						=			{
    """EleutherAI/gpt-j-6B""": """https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json""",
    # See all GPT-J models at https://huggingface.co/models?filter=gpt_j
}






class 							lowercase__			(   __SCREAMING_SNAKE_CASE	):

			"""simple docstring"""
			__lowerCAmelCase     :				Optional[int]										=  """gptj"""
			__lowerCAmelCase     :				Optional[Any]										=  {
			    """max_position_embeddings""": """n_positions""",
			    """hidden_size""": """n_embd""",
			    """num_attention_heads""": """n_head""",
			    """num_hidden_layers""": """n_layer""",
			}







			def __init__(      self						,   _A=5_0_4_0_0						,   _A=2_0_4_8						,   _A=4_0_9_6						,   _A=2_8						,   _A=1_6						,   _A=6_4						,   _A=None						,   _A="gelu_new"						,   _A=0.0						,   _A=0.0						,   _A=0.0						,   _A=1e-5						,   _A=0.02						,   _A=True						,   _A=5_0_2_5_6						,   _A=5_0_2_5_6						,   _A=False						,   **_A						,   ):






									'''simple docstring'''







									UpperCamelCase				:		Tuple				   =			vocab_size
									UpperCamelCase				:		Any				   =			n_positions
									UpperCamelCase				:		List[str]				   =			n_embd
									UpperCamelCase				:		List[str]				   =			n_layer
									UpperCamelCase				:		Optional[int]				   =			n_head
									UpperCamelCase				:		int				   =			n_inner
									UpperCamelCase				:		Optional[Any]				   =			rotary_dim
									UpperCamelCase				:		Optional[int]				   =			activation_function
									UpperCamelCase				:		str				   =			resid_pdrop
									UpperCamelCase				:		Union[str, Any]				   =			embd_pdrop
									UpperCamelCase				:		Optional[Any]				   =			attn_pdrop
									UpperCamelCase				:		Optional[int]				   =			layer_norm_epsilon
									UpperCamelCase				:		Any				   =			initializer_range
									UpperCamelCase				:		Optional[int]				   =			use_cache

									UpperCamelCase				:		List[Any]				   =			bos_token_id
									UpperCamelCase				:		List[str]				   =			eos_token_id

									super().__init__(
									    bos_token_id=_A						,   eos_token_id=_A						,   tie_word_embeddings=_A						,   **_A    )






class 							lowercase__			(   __SCREAMING_SNAKE_CASE	):

			"""simple docstring"""







			def __init__(      self						,   _A						,   _A = "default"						,   _A = None						,   _A = False						,   ):






									'''simple docstring'''







									super().__init__(_A						,   task=_A						,   patching_specs=_A						,   use_past=_A    )
									if not getattr(self._config						,   """pad_token_id"""						,   _A    ):
															# TODO: how to do that better?
															UpperCamelCase				:		Optional[Any]				   =			0







			@property
			def   _a      (      self    ):






									'''simple docstring'''







									UpperCamelCase				:		List[str]				   =			OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}    )
									if self.use_past:
															self.fill_with_past_key_values_(_A						,   direction="""inputs"""    )
															UpperCamelCase				:		Any				   =			{0: """batch""", 1: """past_sequence + sequence"""}
									else:
															UpperCamelCase				:		List[Any]				   =			{0: """batch""", 1: """sequence"""}

									return common_inputs







			@property
			def   _a      (      self    ):






									'''simple docstring'''







									return self._config.n_layer







			@property
			def   _a      (      self    ):






									'''simple docstring'''







									return self._config.n_head







			def   _a      (      self						,   _A						,   _A = -1						,   _A = -1						,   _A = False						,   _A = None						,   ):






									'''simple docstring'''







									UpperCamelCase				:		Optional[Any]				   =			super(_A						,   self    ).generate_dummy_inputs(
									    _A						,   batch_size=_A						,   seq_length=_A						,   is_pair=_A						,   framework=_A    )

									# We need to order the input in the way they appears in the forward()
									UpperCamelCase				:		Optional[Any]				   =			OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}    )

									# Need to add the past_keys
									if self.use_past:
															if not is_torch_available():
																					raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""    )
															else:
																					import torch

																					UpperCamelCase   ,						UpperCamelCase				:		Dict				   =			common_inputs["""input_ids"""].shape
																					# Not using the same length for past_key_values
																					UpperCamelCase				:		Dict				   =			seqlen + 2
																					UpperCamelCase				:		Union[str, Any]				   =			(
																					    batch,
																					    self.num_attention_heads,
																					    past_key_values_length,
																					    self._config.hidden_size // self.num_attention_heads,
																					)
																					UpperCamelCase				:		List[Any]				   =			[
																					    (torch.zeros(_A    ), torch.zeros(_A    )) for _ in range(self.num_layers    )
																					]

									UpperCamelCase				:		str				   =			common_inputs["""attention_mask"""]
									if self.use_past:
															UpperCamelCase				:		Any				   =			ordered_inputs["""attention_mask"""].dtype
															UpperCamelCase				:		List[Any]				   =			torch.cat(
															    [ordered_inputs["""attention_mask"""], torch.ones(_A						,   _A						,   dtype=_A    )]						,   dim=1    )

									return ordered_inputs







			@property
			def   _a      (      self    ):






									'''simple docstring'''







									return 1_3



