





'''simple docstring'''




import subprocess
import sys

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







class   A_	(  lowerCAmelCase_			):




 @require_torch
 def      lowercase	(					self       :    str    ):
      # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
      # `transformers` is loaded, and it's too late for inside pytest - so we are changing it
      # while running an external program

      # python one-liner segments

      # this must be loaded before socket.socket is monkey-patched
      _UpperCAmelCase     							=   "\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

      _UpperCAmelCase     							=   "\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        "

      _UpperCAmelCase     							=   "\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
      _UpperCAmelCase     							=   "hf-internal-testing/tiny-random-bert"
      BertConfig.from_pretrained(snake_case_    )
      BertModel.from_pretrained(snake_case_    )
      BertTokenizer.from_pretrained(snake_case_    )
      pipeline(task="fill-mask"	,			model=snake_case_    )

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

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




 @require_torch
 def      lowercase	(					self       :    List[Any]    ):
      # python one-liner segments
      # this must be loaded before socket.socket is monkey-patched
      _UpperCAmelCase     							=   "\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        "

      _UpperCAmelCase     							=   "\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        "

      _UpperCAmelCase     							=   "\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
      _UpperCAmelCase     							=   "hf-internal-testing/tiny-random-bert"
      BertConfig.from_pretrained(snake_case_    )
      BertModel.from_pretrained(snake_case_    )
      BertTokenizer.from_pretrained(snake_case_    )
      pipeline(task="fill-mask"	,			model=snake_case_    )

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

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




 @require_torch
 def      lowercase	(					self       :    Tuple    ):
      # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
      # `transformers` is loaded, and it's too late for inside pytest - so we are changing it
      # while running an external program

      # python one-liner segments

      # this must be loaded before socket.socket is monkey-patched
      _UpperCAmelCase     							=   "\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        "

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

      _UpperCAmelCase     							=   "\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
      _UpperCAmelCase     							=   [sys.executable, "-c", "\n".join([load, run]    )]

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

      # next emulate no network
      _UpperCAmelCase     							=   [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
      _UpperCAmelCase     							=   "1"
      _UpperCAmelCase     							=   subprocess.run(snake_case_	,			env=snake_case_	,			check=snake_case_	,			capture_output=snake_case_    )
      self.assertEqual(result.returncode	,			0	,			result.stderr    )
      self.assertIn("success"	,			result.stdout.decode()    )




 @require_torch
 def      lowercase	(					self       :    Optional[Any]    ):
      _UpperCAmelCase     							=   "\nfrom transformers import pipeline\n        "
      _UpperCAmelCase     							=   "\nmname = \"hf-internal-testing/tiny-random-bert\"\npipe = pipeline(model=mname)\n        "

      _UpperCAmelCase     							=   "\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error(\"Offline mode is enabled\")\nsocket.socket = offline_socket\n        "
      _UpperCAmelCase     							=   self.get_env()
      _UpperCAmelCase     							=   "1"
      _UpperCAmelCase     							=   [sys.executable, "-c", "\n".join([load, mock, run]    )]
      _UpperCAmelCase     							=   subprocess.run(snake_case_	,			env=snake_case_	,			check=snake_case_	,			capture_output=snake_case_    )
      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      lowercase	(					self       :    Optional[int]    ):
      _UpperCAmelCase     							=   "\nfrom transformers import AutoModel\n        "
      _UpperCAmelCase     							=   "\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
      _UpperCAmelCase     							=   [sys.executable, "-c", "\n".join([load, run]    )]

      # should succeed
      _UpperCAmelCase     							=   self.get_env()
      _UpperCAmelCase     							=   subprocess.run(snake_case_	,			env=snake_case_	,			check=snake_case_	,			capture_output=snake_case_    )
      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
      _UpperCAmelCase     							=   "1"
      _UpperCAmelCase     							=   subprocess.run(snake_case_	,			env=snake_case_	,			check=snake_case_	,			capture_output=snake_case_    )
      self.assertEqual(result.returncode	,			0	,			result.stderr    )
      self.assertIn("success"	,			result.stdout.decode()    )











'''simple docstring'''




from __future__ import annotations

from collections.abc import Callable
from typing import Any, Generic, TypeVar

__SCREAMING_SNAKE_CASE     :Optional[int]       =				TypeVar('''T''')







class   A_	(  Generic[T]			):




 def __init__(					self       :    List[Any]	,			snake_case_       :    list[T]	,			snake_case_       :    Callable[[T, T], T]    ):
      _UpperCAmelCase     							=   None

      _UpperCAmelCase     							=   len(snake_case_    )
      _UpperCAmelCase     							=   [any_type for _ in range(self.N    )] + arr
      _UpperCAmelCase     							=   fnc
      self.build()




 def      lowercase	(					self       :    List[Any]    ):
      for p in range(self.N - 1	,			0	,			-1    ):
           _UpperCAmelCase     							=   self.fn(self.st[p * 2]	,			self.st[p * 2 + 1]    )




 def      lowercase	(					self       :    Optional[Any]	,			snake_case_       :    int	,			snake_case_       :    T    ):
      p += self.N
      _UpperCAmelCase     							=   v
      while p > 1:
           _UpperCAmelCase     							=   p // 2
           _UpperCAmelCase     							=   self.fn(self.st[p * 2]	,			self.st[p * 2 + 1]    )




 def      lowercase	(					self       :    Any	,			snake_case_       :    int	,			snake_case_       :    int    ):  # noqa: E741
      _UpperCAmelCase							,  _UpperCAmelCase     							=   l + self.N, r + self.N

      _UpperCAmelCase     							=   None
      while l <= r:
           if l % 2 == 1:
                _UpperCAmelCase     							=   self.st[l] if res is None else self.fn(snake_case_	,			self.st[l]    )
           if r % 2 == 0:
                _UpperCAmelCase     							=   self.st[r] if res is None else self.fn(snake_case_	,			self.st[r]    )
           _UpperCAmelCase							,  _UpperCAmelCase     							=   (l + 1) // 2, (r - 1) // 2
      return res


if __name__ == "__main__":
  from functools import reduce

  __SCREAMING_SNAKE_CASE     :Union[str, Any]       =				[1, 10, -2, 9, -3, 8, 4, -7, 5, 6, 11, -12]

  __SCREAMING_SNAKE_CASE     :List[str]       =				{
      0: 7,
      1: 2,
      2: 6,
      3: -14,
      4: 5,
      5: 4,
      6: 7,
      7: -10,
      8: 9,
      9: 10,
      10: 12,
      11: 1,
  }

  __SCREAMING_SNAKE_CASE     :Any       =				SegmentTree(test_array, min)
  __SCREAMING_SNAKE_CASE     :Any       =				SegmentTree(test_array, max)
  __SCREAMING_SNAKE_CASE     :Any       =				SegmentTree(test_array, lambda a, b: a + b)
  def 	UpperCAmelCase_							(					)		->       None:


       '''simple docstring'''



       for i in range(len(__lowercase       )       ):
            for j in range(__lowercase			,     len(__lowercase       )       ):
                 _UpperCAmelCase     							=   reduce(__lowercase			,     test_array[i : j + 1]       )
                 _UpperCAmelCase     							=   reduce(__lowercase			,     test_array[i : j + 1]       )
                 _UpperCAmelCase     							=   reduce(lambda __lowercase			,     __lowercase       : a + b			,     test_array[i : j + 1]       )
                 assert min_range == min_segment_tree.query(__lowercase			,     __lowercase       )
                 assert max_range == max_segment_tree.query(__lowercase			,     __lowercase       )
                 assert sum_range == sum_segment_tree.query(__lowercase			,     __lowercase       )

  test_all_segments()

  for index, value in test_updates.items():
    __SCREAMING_SNAKE_CASE     :str       =				value
    min_segment_tree.update(index, value)
    max_segment_tree.update(index, value)
    sum_segment_tree.update(index, value)
    test_all_segments()




