"""simple docstring"""
from __future__ import annotations

import math
import random
from collections.abc import Collection
from typing import overload



class   lowerCAmelCase_    :



				"""simple docstring"""

				def __init__(				self		,    lowerCAmelCase = None    ):






										"""simple docstring"""

										if components is None:
																snake_case								=			[]
										snake_case								=			list(lowerCAmelCase    )

				def __len__(				self    ):






										"""simple docstring"""

										return len(self.__components    )

				def __str__(				self    ):






										"""simple docstring"""

										return "(" + ",".join(map(lowerCAmelCase		,    self.__components    )    ) + ")"

				def __add__(				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										snake_case								=			len(self    )
										if size == len(lowerCAmelCase    ):
																snake_case								=			[self.__components[i] + other.component(lowerCAmelCase    ) for i in range(lowerCAmelCase    )]
																return Vector(lowerCAmelCase    )
										else:
																raise Exception('must have the same size'    )

				def __sub__(				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										snake_case								=			len(self    )
										if size == len(lowerCAmelCase    ):
																snake_case								=			[self.__components[i] - other.component(lowerCAmelCase    ) for i in range(lowerCAmelCase    )]
																return Vector(lowerCAmelCase    )
										else:  # error case
																raise Exception('must have the same size'    )

				@overload
				def __mul__(				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										...

				@overload
				def __mul__(				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										...

				def __mul__(				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										if isinstance(lowerCAmelCase		,    (float, int)    ):
																snake_case								=			[c * other for c in self.__components]
																return Vector(lowerCAmelCase    )
										elif isinstance(lowerCAmelCase		,    lowerCAmelCase    ) and len(self    ) == len(lowerCAmelCase    ):
																snake_case								=			len(self    )
																snake_case								=			[self.__components[i] * other.component(lowerCAmelCase    ) for i in range(lowerCAmelCase    )]
																return sum(lowerCAmelCase    )
										else:  # error case
																raise Exception('invalid operand!'    )

				def 		snake_case (				self    ):






										"""simple docstring"""

										return Vector(self.__components    )

				def 		snake_case (				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										if isinstance(lowerCAmelCase		,    lowerCAmelCase    ) and -len(self.__components    ) <= i < len(self.__components    ):
																return self.__components[i]
										else:
																raise Exception('index out of range'    )

				def 		snake_case (				self		,    lowerCAmelCase		,    lowerCAmelCase    ):






										"""simple docstring"""

										assert -len(self.__components    ) <= pos < len(self.__components    )
										snake_case								=			value

				def 		snake_case (				self    ):






										"""simple docstring"""

										if len(self.__components    ) == 0:
																raise Exception('Vector is empty'    )
										snake_case								=			[c**2 for c in self.__components]
										return math.sqrt(sum(lowerCAmelCase    )    )

				def 		snake_case (				self		,    lowerCAmelCase		,    lowerCAmelCase = False    ):






										"""simple docstring"""

										snake_case								=			self * other
										snake_case								=			self.euclidean_length() * other.euclidean_length()
										if deg:
																return math.degrees(math.acos(num / den    )    )
										else:
																return math.acos(num / den    )





def    lowerCAmelCase__    (			_UpperCamelCase       :				int					)					->				Vector:





						"""simple docstring"""






						assert isinstance(_UpperCamelCase ,			_UpperCamelCase					)
						return Vector([0] * dimension					)





def    lowerCAmelCase__    (			_UpperCamelCase       :				int ,			_UpperCamelCase       :				int					)					->				Vector:





						"""simple docstring"""






						assert isinstance(_UpperCamelCase ,			_UpperCamelCase					) and (isinstance(_UpperCamelCase ,			_UpperCamelCase					))
						snake_case								=			[0] * dimension
						snake_case								=			1
						return Vector(_UpperCamelCase					)





def    lowerCAmelCase__    (			_UpperCamelCase       :				float ,			_UpperCamelCase       :				Vector ,			_UpperCamelCase       :				Vector					)					->				Vector:





						"""simple docstring"""






						assert (
						    isinstance(_UpperCamelCase ,			_UpperCamelCase					)
						    and isinstance(_UpperCamelCase ,			_UpperCamelCase					)
						    and (isinstance(_UpperCamelCase ,			(int, float)					))
						)
						return x * scalar + y





def    lowerCAmelCase__    (			_UpperCamelCase       :				int ,			_UpperCamelCase       :				int ,			_UpperCamelCase       :				int					)					->				Vector:





						"""simple docstring"""






						random.seed(_UpperCamelCase					)
						snake_case								=			[random.randint(_UpperCamelCase ,			_UpperCamelCase					) for _ in range(_UpperCamelCase					)]
						return Vector(_UpperCamelCase					)



class   lowerCAmelCase_    :



				"""simple docstring"""

				def __init__(				self		,    lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase    ):






										"""simple docstring"""

										snake_case								=			matrix
										snake_case								=			w
										snake_case								=			h

				def __str__(				self    ):






										"""simple docstring"""

										snake_case								=			''
										for i in range(self.__height    ):
																ans += "|"
																for j in range(self.__width    ):
																						if j < self.__width - 1:
																												ans += str(self.__matrix[i][j]    ) + ","
																						else:
																												ans += str(self.__matrix[i][j]    ) + "|\n"
										return ans

				def __add__(				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										if self.__width == other.width() and self.__height == other.height():
																snake_case								=			[]
																for i in range(self.__height    ):
																						snake_case								=			[
																						    self.__matrix[i][j] + other.component(lowerCAmelCase		,    lowerCAmelCase    )
																						    for j in range(self.__width    )
																						]
																						matrix.append(lowerCAmelCase    )
																return Matrix(lowerCAmelCase		,    self.__width		,    self.__height    )
										else:
																raise Exception('matrix must have the same dimension!'    )

				def __sub__(				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										if self.__width == other.width() and self.__height == other.height():
																snake_case								=			[]
																for i in range(self.__height    ):
																						snake_case								=			[
																						    self.__matrix[i][j] - other.component(lowerCAmelCase		,    lowerCAmelCase    )
																						    for j in range(self.__width    )
																						]
																						matrix.append(lowerCAmelCase    )
																return Matrix(lowerCAmelCase		,    self.__width		,    self.__height    )
										else:
																raise Exception('matrices must have the same dimension!'    )

				@overload
				def __mul__(				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										...

				@overload
				def __mul__(				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										...

				def __mul__(				self		,    lowerCAmelCase    ):






										"""simple docstring"""

										if isinstance(lowerCAmelCase		,    lowerCAmelCase    ):  # matrix-vector
																if len(lowerCAmelCase    ) == self.__width:
																						snake_case								=			zero_vector(self.__height    )
																						for i in range(self.__height    ):
																												snake_case								=			[
																												    self.__matrix[i][j] * other.component(lowerCAmelCase    )
																												    for j in range(self.__width    )
																												]
																												ans.change_component(lowerCAmelCase		,    sum(lowerCAmelCase    )    )
																						return ans
																else:
																						raise Exception(
																						    'vector must have the same size as the '
																						    'number of columns of the matrix!'    )
										elif isinstance(lowerCAmelCase		,    (int, float)    ):  # matrix-scalar
																snake_case								=			[
																    [self.__matrix[i][j] * other for j in range(self.__width    )]
																    for i in range(self.__height    )
																]
																return Matrix(lowerCAmelCase		,    self.__width		,    self.__height    )
										return None

				def 		snake_case (				self    ):






										"""simple docstring"""

										return self.__height

				def 		snake_case (				self    ):






										"""simple docstring"""

										return self.__width

				def 		snake_case (				self		,    lowerCAmelCase		,    lowerCAmelCase    ):






										"""simple docstring"""

										if 0 <= x < self.__height and 0 <= y < self.__width:
																return self.__matrix[x][y]
										else:
																raise Exception('change_component: indices out of bounds'    )

				def 		snake_case (				self		,    lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase    ):






										"""simple docstring"""

										if 0 <= x < self.__height and 0 <= y < self.__width:
																snake_case								=			value
										else:
																raise Exception('change_component: indices out of bounds'    )

				def 		snake_case (				self		,    lowerCAmelCase		,    lowerCAmelCase    ):






										"""simple docstring"""

										if self.__height != self.__width:
																raise Exception('Matrix is not square'    )
										snake_case								=			self.__matrix[:x] + self.__matrix[x + 1 :]
										for i in range(len(lowerCAmelCase    )    ):
																snake_case								=			minor[i][:y] + minor[i][y + 1 :]
										return Matrix(lowerCAmelCase		,    self.__width - 1		,    self.__height - 1    ).determinant()

				def 		snake_case (				self		,    lowerCAmelCase		,    lowerCAmelCase    ):






										"""simple docstring"""

										if self.__height != self.__width:
																raise Exception('Matrix is not square'    )
										if 0 <= x < self.__height and 0 <= y < self.__width:
																return (-1) ** (x + y) * self.minor(lowerCAmelCase		,    lowerCAmelCase    )
										else:
																raise Exception('Indices out of bounds'    )

				def 		snake_case (				self    ):






										"""simple docstring"""

										if self.__height != self.__width:
																raise Exception('Matrix is not square'    )
										if self.__height < 1:
																raise Exception('Matrix has no element'    )
										elif self.__height == 1:
																return self.__matrix[0][0]
										elif self.__height == 2:
																return (
																    self.__matrix[0][0] * self.__matrix[1][1]
																    - self.__matrix[0][1] * self.__matrix[1][0]
																)
										else:
																snake_case								=			[
																    self.__matrix[0][y] * self.cofactor(0		,    lowerCAmelCase    ) for y in range(self.__width    )
																]
																return sum(lowerCAmelCase    )





def    lowerCAmelCase__    (			_UpperCamelCase       :				int					)					->				Matrix:





						"""simple docstring"""






						snake_case								=			[[0] * n for _ in range(_UpperCamelCase					)]
						return Matrix(_UpperCamelCase ,			_UpperCamelCase ,			_UpperCamelCase					)





def    lowerCAmelCase__    (			_UpperCamelCase       :				int ,			_UpperCamelCase       :				int ,			_UpperCamelCase       :				int ,			_UpperCamelCase       :				int					)					->				Matrix:





						"""simple docstring"""






						random.seed(_UpperCamelCase					)
						snake_case								=			[
						    [random.randint(_UpperCamelCase ,			_UpperCamelCase					) for _ in range(_UpperCamelCase					)] for _ in range(_UpperCamelCase					)
						]
						return Matrix(_UpperCamelCase ,			_UpperCamelCase ,			_UpperCamelCase					)



"""simple docstring"""
import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch



class   lowerCAmelCase_    (			lowerCAmelCase					):



				"""simple docstring"""

				@require_torch
				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

										snake_case								=			'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

										snake_case								=			'\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n        '

										# Force fetching the files so that we can use the cache
										snake_case								=			'hf-internal-testing/tiny-random-bert'
										BertConfig.from_pretrained(lowerCAmelCase    )
										BertModel.from_pretrained(lowerCAmelCase    )
										BertTokenizer.from_pretrained(lowerCAmelCase    )
										pipeline(task='fill-mask'		,    model=lowerCAmelCase    )

										# baseline - just load from_pretrained with normal network
										snake_case								=			[sys.executable, '-c', '\n'.join([load, run, mock]    )]

										# should succeed
										snake_case								=			self.get_env()
										# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
										snake_case								=			'1'
										snake_case								=			subprocess.run(lowerCAmelCase		,    env=lowerCAmelCase		,    check=lowerCAmelCase		,    capture_output=lowerCAmelCase    )
										self.assertEqual(result.returncode		,    0		,    result.stderr    )
										self.assertIn('success'		,    result.stdout.decode()    )

				@require_torch
				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

										snake_case								=			'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

										snake_case								=			'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n        '

										# Force fetching the files so that we can use the cache
										snake_case								=			'hf-internal-testing/tiny-random-bert'
										BertConfig.from_pretrained(lowerCAmelCase    )
										BertModel.from_pretrained(lowerCAmelCase    )
										BertTokenizer.from_pretrained(lowerCAmelCase    )
										pipeline(task='fill-mask'		,    model=lowerCAmelCase    )

										# baseline - just load from_pretrained with normal network
										snake_case								=			[sys.executable, '-c', '\n'.join([load, run, mock]    )]

										# should succeed
										snake_case								=			self.get_env()
										snake_case								=			subprocess.run(lowerCAmelCase		,    env=lowerCAmelCase		,    check=lowerCAmelCase		,    capture_output=lowerCAmelCase    )
										self.assertEqual(result.returncode		,    0		,    result.stderr    )
										self.assertIn('success'		,    result.stdout.decode()    )

				@require_torch
				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			'\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        '

										snake_case								=			'\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n        '

										snake_case								=			'\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n        '

										# baseline - just load from_pretrained with normal network
										snake_case								=			[sys.executable, '-c', '\n'.join([load, run]    )]

										# should succeed
										snake_case								=			self.get_env()
										snake_case								=			subprocess.run(lowerCAmelCase		,    env=lowerCAmelCase		,    check=lowerCAmelCase		,    capture_output=lowerCAmelCase    )
										self.assertEqual(result.returncode		,    0		,    result.stderr    )
										self.assertIn('success'		,    result.stdout.decode()    )

										# next emulate no network
										snake_case								=			[sys.executable, '-c', '\n'.join([load, mock, run]    )]

										# Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
										# env["TRANSFORMERS_OFFLINE"] = "0"
										# result = subprocess.run(cmd, env=env, check=False, capture_output=True)
										# self.assertEqual(result.returncode, 1, result.stderr)

										# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
										snake_case								=			'1'
										snake_case								=			subprocess.run(lowerCAmelCase		,    env=lowerCAmelCase		,    check=lowerCAmelCase		,    capture_output=lowerCAmelCase    )
										self.assertEqual(result.returncode		,    0		,    result.stderr    )
										self.assertIn('success'		,    result.stdout.decode()    )

				@require_torch
				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			'\nfrom transformers import pipeline\n        '
										snake_case								=			'\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n        '

										snake_case								=			'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n        '
										snake_case								=			self.get_env()
										snake_case								=			'1'
										snake_case								=			[sys.executable, '-c', '\n'.join([load, mock, run]    )]
										snake_case								=			subprocess.run(lowerCAmelCase		,    env=lowerCAmelCase		,    check=lowerCAmelCase		,    capture_output=lowerCAmelCase    )
										self.assertEqual(result.returncode		,    1		,    result.stderr    )
										self.assertIn(
										    'You cannot infer task automatically within `pipeline` when using offline mode'		,    result.stderr.decode().replace('\n'		,    ''    )		,    )

				@require_torch
				def 		snake_case (				self    ):






										"""simple docstring"""

										snake_case								=			'\nfrom transformers import AutoModel\n        '
										snake_case								=			'\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n        '

										# baseline - just load from_pretrained with normal network
										snake_case								=			[sys.executable, '-c', '\n'.join([load, run]    )]

										# should succeed
										snake_case								=			self.get_env()
										snake_case								=			subprocess.run(lowerCAmelCase		,    env=lowerCAmelCase		,    check=lowerCAmelCase		,    capture_output=lowerCAmelCase    )
										self.assertEqual(result.returncode		,    0		,    result.stderr    )
										self.assertIn('success'		,    result.stdout.decode()    )

										# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
										snake_case								=			'1'
										snake_case								=			subprocess.run(lowerCAmelCase		,    env=lowerCAmelCase		,    check=lowerCAmelCase		,    capture_output=lowerCAmelCase    )
										self.assertEqual(result.returncode		,    0		,    result.stderr    )
										self.assertIn('success'		,    result.stdout.decode()    )


