





def 						A_							(	_lowerCAmelCase				,  _lowerCAmelCase				,  _lowerCAmelCase				,  _lowerCAmelCase				,  _lowerCAmelCase	)		->					int:
 if index == number_of_items:
  return 0
 UpperCamelCase      :   int			      =  0
 UpperCamelCase      :   Dict			      =  0
 UpperCamelCase      :   str			      =  knapsack(_lowerCAmelCase				,  _lowerCAmelCase				,  _lowerCAmelCase				,  _lowerCAmelCase				,  index + 1	)
 if weights[index] <= max_weight:
  UpperCamelCase      :   Optional[Any]			      =  values[index] + knapsack(
      _lowerCAmelCase				,  _lowerCAmelCase				,  _lowerCAmelCase				,  max_weight - weights[index]				,  index + 1	)
 return max(_lowerCAmelCase				,  _lowerCAmelCase	)


if __name__ == "__main__":
    import doctest

    doctest.testmod()







import math
import os
import unittest

from transformers import MegatronBertConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        MODEL_FOR_PRETRAINING_MAPPING,
        MegatronBertForCausalLM,
        MegatronBertForMaskedLM,
        MegatronBertForMultipleChoice,
        MegatronBertForNextSentencePrediction,
        MegatronBertForPreTraining,
        MegatronBertForQuestionAnswering,
        MegatronBertForSequenceClassification,
        MegatronBertForTokenClassification,
        MegatronBertModel,
    )



class    A__  :





      def __init__(					self			,				A_			,				A_=13			,				A_=7			,				A_=True			,				A_=True			,				A_=True			,				A_=True			,				A_=99			,				A_=64			,				A_=32			,				A_=5			,				A_=4			,				A_=37			,				A_="gelu"			,				A_=0.1			,				A_=0.1			,				A_=512			,				A_=16			,				A_=2			,				A_=0.02			,				A_=3			,				A_=4			,				A_=None			,				):
       '''simple docstring'''




       UpperCamelCase      :   Dict			      =  parent
       UpperCamelCase      :   int			      =  batch_size
       UpperCamelCase      :   Dict			      =  seq_length
       UpperCamelCase      :   Union[str, Any]			      =  is_training
       UpperCamelCase      :   int			      =  use_input_mask
       UpperCamelCase      :   Optional[Any]			      =  use_token_type_ids
       UpperCamelCase      :   Optional[Any]			      =  use_labels
       UpperCamelCase      :   str			      =  vocab_size
       UpperCamelCase      :   Union[str, Any]			      =  hidden_size
       UpperCamelCase      :   Any			      =  embedding_size
       UpperCamelCase      :   List[Any]			      =  num_hidden_layers
       UpperCamelCase      :   Optional[int]			      =  num_attention_heads
       UpperCamelCase      :   Optional[Any]			      =  intermediate_size
       UpperCamelCase      :   Dict			      =  hidden_act
       UpperCamelCase      :   Union[str, Any]			      =  hidden_dropout_prob
       UpperCamelCase      :   Optional[Any]			      =  attention_probs_dropout_prob
       UpperCamelCase      :   List[str]			      =  max_position_embeddings
       UpperCamelCase      :   List[Any]			      =  type_vocab_size
       UpperCamelCase      :   Any			      =  type_sequence_label_size
       UpperCamelCase      :   Optional[int]			      =  initializer_range
       UpperCamelCase      :   str			      =  num_labels
       UpperCamelCase      :   List[str]			      =  num_choices
       UpperCamelCase      :   Tuple			      =  scope





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   int			      =  ids_tensor([self.batch_size, self.seq_length]			,				self.vocab_size  )

       UpperCamelCase      :   List[str]			      =  None
       if self.use_input_mask:
        UpperCamelCase      :   List[str]			      =  random_attention_mask([self.batch_size, self.seq_length]  )

       UpperCamelCase      :   Tuple			      =  None
       if self.use_token_type_ids:
        UpperCamelCase      :   Dict			      =  ids_tensor([self.batch_size, self.seq_length]			,				self.type_vocab_size  )

       UpperCamelCase      :   Dict			      =  None
       UpperCamelCase      :   Union[str, Any]			      =  None
       UpperCamelCase      :   Dict			      =  None
       if self.use_labels:
        UpperCamelCase      :   Optional[Any]			      =  ids_tensor([self.batch_size]			,				self.type_sequence_label_size  )
        UpperCamelCase      :   Tuple			      =  ids_tensor([self.batch_size, self.seq_length]			,				self.num_labels  )
        UpperCamelCase      :   Optional[int]			      =  ids_tensor([self.batch_size]			,				self.num_choices  )

       UpperCamelCase      :   Optional[Any]			      =  self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       return MegatronBertConfig(
           vocab_size=self.vocab_size			,				hidden_size=self.hidden_size			,				num_hidden_layers=self.num_hidden_layers			,				num_attention_heads=self.num_attention_heads			,				intermediate_size=self.intermediate_size			,				embedding_size=self.embedding_size			,				hidden_act=self.hidden_act			,				hidden_dropout_prob=self.hidden_dropout_prob			,				attention_probs_dropout_prob=self.attention_probs_dropout_prob			,				max_position_embeddings=self.max_position_embeddings			,				type_vocab_size=self.type_vocab_size			,				is_decoder=A_			,				initializer_range=self.initializer_range			,				)





      def 			__UpperCamelCase(					self			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   Optional[int]			      =  MegatronBertModel(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   Optional[Any]			      =  model(A_			,				attention_mask=A_			,				token_type_ids=A_  )
       UpperCamelCase      :   Dict			      =  model(A_			,				token_type_ids=A_  )
       UpperCamelCase      :   Union[str, Any]			      =  model(A_  )

       self.parent.assertEqual(result.last_hidden_state.shape			,				(self.batch_size, self.seq_length, self.hidden_size)  )
       self.parent.assertEqual(result.pooler_output.shape			,				(self.batch_size, self.hidden_size)  )





      def 			__UpperCamelCase(					self			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   List[str]			      =  MegatronBertForMaskedLM(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   Optional[int]			      =  model(A_			,				attention_mask=A_			,				token_type_ids=A_			,				labels=A_  )
       self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.seq_length, self.vocab_size)  )





      def 			__UpperCamelCase(					self			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   int			      =  MegatronBertForCausalLM(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   Dict			      =  model(A_			,				attention_mask=A_			,				token_type_ids=A_			,				labels=A_  )
       self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.seq_length, self.vocab_size)  )





      def 			__UpperCamelCase(					self			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   int			      =  MegatronBertForNextSentencePrediction(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   Dict			      =  model(
           A_			,				attention_mask=A_			,				token_type_ids=A_			,				labels=A_			,				)
       self.parent.assertEqual(result.logits.shape			,				(self.batch_size, 2)  )





      def 			__UpperCamelCase(					self			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   Optional[int]			      =  MegatronBertForPreTraining(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   str			      =  model(
           A_			,				attention_mask=A_			,				token_type_ids=A_			,				labels=A_			,				next_sentence_label=A_			,				)
       self.parent.assertEqual(result.prediction_logits.shape			,				(self.batch_size, self.seq_length, self.vocab_size)  )
       self.parent.assertEqual(result.seq_relationship_logits.shape			,				(self.batch_size, 2)  )





      def 			__UpperCamelCase(					self			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   str			      =  MegatronBertForQuestionAnswering(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   Union[str, Any]			      =  model(
           A_			,				attention_mask=A_			,				token_type_ids=A_			,				start_positions=A_			,				end_positions=A_			,				)
       self.parent.assertEqual(result.start_logits.shape			,				(self.batch_size, self.seq_length)  )
       self.parent.assertEqual(result.end_logits.shape			,				(self.batch_size, self.seq_length)  )





      def 			__UpperCamelCase(					self			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   List[str]			      =  self.num_labels
       UpperCamelCase      :   Optional[int]			      =  MegatronBertForSequenceClassification(A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   List[Any]			      =  model(A_			,				attention_mask=A_			,				token_type_ids=A_			,				labels=A_  )
       self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.num_labels)  )





      def 			__UpperCamelCase(					self			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   Union[str, Any]			      =  self.num_labels
       UpperCamelCase      :   List[str]			      =  MegatronBertForTokenClassification(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   List[str]			      =  model(A_			,				attention_mask=A_			,				token_type_ids=A_			,				labels=A_  )
       self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.seq_length, self.num_labels)  )





      def 			__UpperCamelCase(					self			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_			,				A_  ):
       '''simple docstring'''




       UpperCamelCase      :   int			      =  self.num_choices
       UpperCamelCase      :   int			      =  MegatronBertForMultipleChoice(config=A_  )
       model.to(A_  )
       model.eval()
       UpperCamelCase      :   Optional[int]			      =  input_ids.unsqueeze(1  ).expand(-1			,				self.num_choices			,				-1  ).contiguous()
       UpperCamelCase      :   Tuple			      =  token_type_ids.unsqueeze(1  ).expand(-1			,				self.num_choices			,				-1  ).contiguous()
       UpperCamelCase      :   List[str]			      =  input_mask.unsqueeze(1  ).expand(-1			,				self.num_choices			,				-1  ).contiguous()
       UpperCamelCase      :   Tuple			      =  model(
           A_			,				attention_mask=A_			,				token_type_ids=A_			,				labels=A_			,				)
       self.parent.assertEqual(result.logits.shape			,				(self.batch_size, self.num_choices)  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Optional[Any]			      =  self.prepare_config_and_inputs()
       (
           (
           UpperCamelCase
       )      ,       (
           UpperCamelCase
       )      ,       (
           UpperCamelCase
       )      ,       (
           UpperCamelCase
       )      ,       (
           UpperCamelCase
       )      ,       (
           UpperCamelCase
       )      ,       (
           UpperCamelCase
       )      ,       
       )      :   Any			      =  config_and_inputs
       UpperCamelCase      :   Optional[int]			      =  {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
       return config, inputs_dict



@require_torch
class    A__  (			__snake_case			,  __snake_case			,  unittest.TestCase     ):
      _UpperCAmelCase      :Tuple		       =  (
          (
              MegatronBertModel,
              MegatronBertForMaskedLM,
              MegatronBertForCausalLM,
              MegatronBertForMultipleChoice,
              MegatronBertForNextSentencePrediction,
              MegatronBertForPreTraining,
              MegatronBertForQuestionAnswering,
              MegatronBertForSequenceClassification,
              MegatronBertForTokenClassification,
          )
          if is_torch_available()
          else ()
      )
      _UpperCAmelCase      :Optional[Any]		       =  (
          {
              'feature-extraction': MegatronBertModel,
              'fill-mask': MegatronBertForMaskedLM,
              'question-answering': MegatronBertForQuestionAnswering,
              'text-classification': MegatronBertForSequenceClassification,
              'text-generation': MegatronBertForCausalLM,
              'token-classification': MegatronBertForTokenClassification,
              'zero-shot': MegatronBertForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      _UpperCAmelCase      :Optional[Any]		       =  True
      # test_resize_embeddings = False
      _UpperCAmelCase      :Optional[Any]		       =  False





      def 			__UpperCamelCase(					self			,				A_			,				A_			,				A_=False  ):
       '''simple docstring'''




       UpperCamelCase      :   Any			      =  super()._prepare_for_class(A_			,				A_			,				return_labels=A_  )

       if return_labels:
        if model_class in get_values(A_  ):
         UpperCamelCase      :   str			      =  torch.zeros(
             (self.model_tester.batch_size, self.model_tester.seq_length)			,				dtype=torch.long			,				device=A_  )
         UpperCamelCase      :   str			      =  torch.zeros(
             self.model_tester.batch_size			,				dtype=torch.long			,				device=A_  )
       return inputs_dict





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Optional[int]			      =  MegatronBertModelTester(self  )
       UpperCamelCase      :   Optional[int]			      =  ConfigTester(self			,				config_class=A_			,				hidden_size=37  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       self.config_tester.run_common_tests()





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   List[str]			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_model(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   int			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_masked_lm(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Optional[Any]			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_multiple_choice(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Any			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_next_sequence_prediction(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   int			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_pretraining(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Dict			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_question_answering(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Union[str, Any]			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_sequence_classification(*A_  )





      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   Optional[int]			      =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_megatron_bert_for_token_classification(*A_  )



def 						A_							(	_lowerCAmelCase	)		->					int:
 return torch.tensor(
     _lowerCAmelCase				,  dtype=torch.long				,  device=_lowerCAmelCase				,  )


__lowerCamelCase			:	Optional[int]					    =	1E-4



@require_torch
@require_sentencepiece
@require_tokenizers
class    A__  (			unittest.TestCase     ):





      @slow
      @unittest.skip("Model is not available."  )
      def 			__UpperCamelCase(					self  ):
       '''simple docstring'''




       UpperCamelCase      :   List[str]			      =  "nvidia/megatron-bert-uncased-345m"
       if "MYDIR" in os.environ:
        UpperCamelCase      :   Union[str, Any]			      =  os.path.join(os.environ["MYDIR"]			,				A_  )
       UpperCamelCase      :   List[Any]			      =  MegatronBertModel.from_pretrained(A_  )
       model.to(A_  )
       model.half()
       UpperCamelCase      :   Optional[Any]			      =  _long_tensor([[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]  )
       with torch.no_grad():
        UpperCamelCase      :   Tuple			      =  model(A_  )[0]
       UpperCamelCase      :   Dict			      =  torch.Size((1, 9, 1024)  )
       self.assertEqual(output.shape			,				A_  )

       UpperCamelCase      :   Union[str, Any]			      =  [-0.60_40, -0.25_17, -0.10_25, 0.34_20, -0.67_58, -0.00_17, -0.10_89, -0.19_90, 0.57_28]
       for ii in range(3  ):
        for jj in range(3  ):
         UpperCamelCase      :   List[str]			      =  output[0, ii, jj]
         UpperCamelCase      :   List[Any]			      =  expected[3 * ii + jj]
         UpperCamelCase      :   Optional[Any]			      =  "ii={} jj={} a={} b={}".format(A_			,				A_			,				A_			,				A_  )
         self.assertTrue(math.isclose(A_			,				A_			,				rel_tol=A_			,				abs_tol=A_  )			,				msg=A_  )
