



'''simple docstring'''





from __future__ import annotations

import math




def 							lowerCamelCase	(					_snake_case	:    list  ,_snake_case	:    list							):





    '''simple docstring'''


    if len(_snake_case							) != 2 or len(a[0]							) != 2 or len(_snake_case							) != 2 or len(b[0]							) != 2:
        raise Exception("Matrices are not 2x2"							)
    lowercase__									=    [
        [a[0][0] * b[0][0] + a[0][1] * b[1][0], a[0][0] * b[0][1] + a[0][1] * b[1][1]],
        [a[1][0] * b[0][0] + a[1][1] * b[1][0], a[1][0] * b[0][1] + a[1][1] * b[1][1]],
    ]
    return new_matrix




def 							lowerCamelCase	(					_snake_case	:    list  ,_snake_case	:    list							):





    '''simple docstring'''


    return [
        [matrix_a[row][col] + matrix_b[row][col] for col in range(len(matrix_a[row]							)							)]
        for row in range(len(_snake_case							)							)
    ]




def 							lowerCamelCase	(					_snake_case	:    list  ,_snake_case	:    list							):





    '''simple docstring'''


    return [
        [matrix_a[row][col] - matrix_b[row][col] for col in range(len(matrix_a[row]							)							)]
        for row in range(len(_snake_case							)							)
    ]




def 							lowerCamelCase	(					_snake_case	:    list							):





    '''simple docstring'''


    if len(_snake_case							) % 2 != 0 or len(a[0]							) % 2 != 0:
        raise Exception("Odd matrices are not supported!"							)

    lowercase__									=    len(_snake_case							)
    lowercase__									=    matrix_length // 2

    lowercase__									=    [[a[i][j] for j in range(_snake_case  ,_snake_case							)] for i in range(_snake_case							)]
    lowercase__									=    [
        [a[i][j] for j in range(_snake_case  ,_snake_case							)] for i in range(_snake_case  ,_snake_case							)
    ]

    lowercase__									=    [[a[i][j] for j in range(_snake_case							)] for i in range(_snake_case							)]
    lowercase__									=    [[a[i][j] for j in range(_snake_case							)] for i in range(_snake_case  ,_snake_case							)]

    return top_left, top_right, bot_left, bot_right




def 							lowerCamelCase	(					_snake_case	:    list							):





    '''simple docstring'''


    return len(_snake_case							), len(matrix[0]							)




def 							lowerCamelCase	(					_snake_case	:    list							):





    '''simple docstring'''


    print("\n".join(str(_snake_case							) for line in matrix							)							)




def 							lowerCamelCase	(					_snake_case	:    list  ,_snake_case	:    list							):





    '''simple docstring'''


    if matrix_dimensions(_snake_case							) == (2, 2):
        return default_matrix_multiplication(_snake_case  ,_snake_case							)

    lowercase__							,							lowercase__							,							lowercase__							,							lowercase__									=    split_matrix(_snake_case							)
    lowercase__							,							lowercase__							,							lowercase__							,							lowercase__									=    split_matrix(_snake_case							)

    lowercase__									=    actual_strassen(_snake_case  ,matrix_subtraction(_snake_case  ,_snake_case							)							)
    lowercase__									=    actual_strassen(matrix_addition(_snake_case  ,_snake_case							)  ,_snake_case							)
    lowercase__									=    actual_strassen(matrix_addition(_snake_case  ,_snake_case							)  ,_snake_case							)
    lowercase__									=    actual_strassen(_snake_case  ,matrix_subtraction(_snake_case  ,_snake_case							)							)
    lowercase__									=    actual_strassen(matrix_addition(_snake_case  ,_snake_case							)  ,matrix_addition(_snake_case  ,_snake_case							)							)
    lowercase__									=    actual_strassen(matrix_subtraction(_snake_case  ,_snake_case							)  ,matrix_addition(_snake_case  ,_snake_case							)							)
    lowercase__									=    actual_strassen(matrix_subtraction(_snake_case  ,_snake_case							)  ,matrix_addition(_snake_case  ,_snake_case							)							)

    lowercase__									=    matrix_addition(matrix_subtraction(matrix_addition(_snake_case  ,_snake_case							)  ,_snake_case							)  ,_snake_case							)
    lowercase__									=    matrix_addition(_snake_case  ,_snake_case							)
    lowercase__									=    matrix_addition(_snake_case  ,_snake_case							)
    lowercase__									=    matrix_subtraction(matrix_subtraction(matrix_addition(_snake_case  ,_snake_case							)  ,_snake_case							)  ,_snake_case							)

    # construct the new matrix from our 4 quadrants
    lowercase__									=    []
    for i in range(len(_snake_case							)							):
        new_matrix.append(top_left[i] + top_right[i]							)
    for i in range(len(_snake_case							)							):
        new_matrix.append(bot_left[i] + bot_right[i]							)
    return new_matrix




def 							lowerCamelCase	(					_snake_case	:    list  ,_snake_case	:    list							):





    '''simple docstring'''


    if matrix_dimensions(_snake_case							)[1] != matrix_dimensions(_snake_case							)[0]:
        lowercase__									=    (
            "Unable to multiply these matrices, please check the dimensions.\n"
            f'''Matrix A: {matrixa}\n'''
            f'''Matrix B: {matrixa}'''
        )
        raise Exception(_snake_case							)
    lowercase__									=    matrix_dimensions(_snake_case							)
    lowercase__									=    matrix_dimensions(_snake_case							)

    if dimensiona[0] == dimensiona[1] and dimensiona[0] == dimensiona[1]:
        return [matrixa, matrixa]

    lowercase__									=    max(*_snake_case  ,*_snake_case							)
    lowercase__									=    int(math.pow(2  ,math.ceil(math.loga(_snake_case							)							)							)							)
    lowercase__									=    matrixa
    lowercase__									=    matrixa

    # Adding zeros to the matrices so that the arrays dimensions are the same and also
    # power of 2
    for i in range(0  ,_snake_case							):
        if i < dimensiona[0]:
            for _ in range(dimensiona[1]  ,_snake_case							):
                new_matrixa[i].append(0							)
        else:
            new_matrixa.append([0] * maxim							)
        if i < dimensiona[0]:
            for _ in range(dimensiona[1]  ,_snake_case							):
                new_matrixa[i].append(0							)
        else:
            new_matrixa.append([0] * maxim							)

    lowercase__									=    actual_strassen(_snake_case  ,_snake_case							)

    # Removing the additional zeros
    for i in range(0  ,_snake_case							):
        if i < dimensiona[0]:
            for _ in range(dimensiona[1]  ,_snake_case							):
                final_matrix[i].pop()
        else:
            final_matrix.pop()
    return final_matrix


if __name__ == "__main__":
 SCREAMING_SNAKE_CASE__          =  [
     [2, 3, 4, 5],
     [6, 4, 3, 1],
     [2, 3, 6, 7],
     [3, 1, 2, 4],
     [2, 3, 4, 5],
     [6, 4, 3, 1],
     [2, 3, 6, 7],
     [3, 1, 2, 4],
     [2, 3, 4, 5],
     [6, 2, 3, 1],
 ]
 SCREAMING_SNAKE_CASE__          =  [[0, 2, 1, 1], [16, 2, 3, 3], [2, 2, 7, 7], [13, 11, 22, 4]]
 print(strassen(matrixa, matrixa))









'''simple docstring'''





import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 (
     CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
     CTRLForSequenceClassification,
     CTRLLMHeadModel,
     CTRLModel,
 )


class 					snake_case			:



       def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_=14  ,UpperCAmelCase_=7  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=99  ,UpperCAmelCase_=32  ,UpperCAmelCase_=5  ,UpperCAmelCase_=4  ,UpperCAmelCase_=37  ,UpperCAmelCase_="gelu"  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=512  ,UpperCAmelCase_=16  ,UpperCAmelCase_=2  ,UpperCAmelCase_=0.02  ,UpperCAmelCase_=3  ,UpperCAmelCase_=4  ,UpperCAmelCase_=None  ,)	->				Dict:
           lowercase__									=    parent
           lowercase__									=    batch_size
           lowercase__									=    seq_length
           lowercase__									=    is_training
           lowercase__									=    use_token_type_ids
           lowercase__									=    use_input_mask
           lowercase__									=    use_labels
           lowercase__									=    use_mc_token_ids
           lowercase__									=    vocab_size
           lowercase__									=    hidden_size
           lowercase__									=    num_hidden_layers
           lowercase__									=    num_attention_heads
           lowercase__									=    intermediate_size
           lowercase__									=    hidden_act
           lowercase__									=    hidden_dropout_prob
           lowercase__									=    attention_probs_dropout_prob
           lowercase__									=    max_position_embeddings
           lowercase__									=    type_vocab_size
           lowercase__									=    type_sequence_label_size
           lowercase__									=    initializer_range
           lowercase__									=    num_labels
           lowercase__									=    num_choices
           lowercase__									=    scope
           lowercase__									=    self.vocab_size - 1



       def 				_a   (					self   )	->				Any:
           lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size   )

           lowercase__									=    None
           if self.use_input_mask:
               lowercase__									=    random_attention_mask([self.batch_size, self.seq_length]   )

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

           lowercase__									=    None
           if self.use_mc_token_ids:
               lowercase__									=    ids_tensor([self.batch_size, self.num_choices]  ,self.seq_length   )

           lowercase__									=    None
           lowercase__									=    None
           lowercase__									=    None
           if self.use_labels:
               lowercase__									=    ids_tensor([self.batch_size]  ,self.type_sequence_label_size   )
               lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.num_labels   )
               lowercase__									=    ids_tensor([self.batch_size]  ,self.num_choices   )

           lowercase__									=    self.get_config()

           lowercase__									=    ids_tensor([self.num_hidden_layers, self.num_attention_heads]  ,2   )

           return (
               config,
               input_ids,
               input_mask,
               head_mask,
               token_type_ids,
               mc_token_ids,
               sequence_labels,
               token_labels,
               choice_labels,
           )



       def 				_a   (					self   )	->				Optional[int]:
           return CTRLConfig(
               vocab_size=self.vocab_size  ,n_embd=self.hidden_size  ,n_layer=self.num_hidden_layers  ,n_head=self.num_attention_heads  ,n_positions=self.max_position_embeddings  ,pad_token_id=self.pad_token_id  ,)



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,*UpperCAmelCase_   )	->				List[Any]:
           lowercase__									=    CTRLModel(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()

           model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,head_mask=UpperCAmelCase_   )
           model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_   )
           lowercase__									=    model(UpperCAmelCase_   )
           self.parent.assertEqual(result.last_hidden_state.shape  ,(self.batch_size, self.seq_length, self.hidden_size)   )
           self.parent.assertEqual(len(result.past_key_values   )  ,config.n_layer   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,*UpperCAmelCase_   )	->				Optional[int]:
           lowercase__									=    CTRLLMHeadModel(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()

           lowercase__									=    model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,labels=UpperCAmelCase_   )
           self.parent.assertEqual(result.loss.shape  ,()   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.vocab_size)   )



       def 				_a   (					self   )	->				Union[str, Any]:
           lowercase__									=    self.prepare_config_and_inputs()

           (
               (
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							
           )									=    config_and_inputs

           lowercase__									=    {"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

           return config, inputs_dict







       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,*UpperCAmelCase_   )	->				Optional[int]:
           lowercase__									=    self.num_labels
           lowercase__									=    CTRLForSequenceClassification(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    ids_tensor([self.batch_size]  ,self.type_sequence_label_size   )
           lowercase__									=    model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,labels=UpperCAmelCase_   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.num_labels)   )





@require_torch
class 					snake_case			(UpperCamelCase		,   UpperCamelCase		,   UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:Union[str, Any]     =       (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
       lowerCAmelCase__	:Dict     =       (CTRLLMHeadModel,) if is_torch_available() else ()
       lowerCAmelCase__	:int     =       (
           {
               "feature-extraction": CTRLModel,
               "text-classification": CTRLForSequenceClassification,
               "text-generation": CTRLLMHeadModel,
               "zero-shot": CTRLForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       lowerCAmelCase__	:str     =       True
       lowerCAmelCase__	:Tuple     =       False
       lowerCAmelCase__	:List[Any]     =       False



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Tuple:
           if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
               # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
               # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
               # config could not be created.
               return True

           return False



       def 				_a   (					self   )	->				str:
           lowercase__									=    CTRLModelTester(self   )
           lowercase__									=    ConfigTester(self  ,config_class=UpperCAmelCase_  ,n_embd=37   )



       def 				_a   (					self   )	->				List[Any]:
           super().tearDown()
           # clean-up as much as possible GPU memory occupied by PyTorch
           gc.collect()
           torch.cuda.empty_cache()



       def 				_a   (					self   )	->				Tuple:
           self.config_tester.run_common_tests()



       def 				_a   (					self   )	->				Any:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_ctrl_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				Optional[Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_lm_head_model(*UpperCAmelCase_   )



       @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."   )
       def 				_a   (					self   )	->				Tuple:
           pass



       @slow
       def 				_a   (					self   )	->				List[str]:
           for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__									=    CTRLModel.from_pretrained(UpperCAmelCase_   )
               self.assertIsNotNone(UpperCAmelCase_   )







       @unittest.skip("The model doesn't support left padding"   )  # and it's not used enough to be worth fixing :)
       def 				_a   (					self   )	->				Tuple:
           pass





@require_torch
class 					snake_case			(unittest.TestCase ):



       def 				_a   (					self   )	->				int:
           super().tearDown()
           # clean-up as much as possible GPU memory occupied by PyTorch
           gc.collect()
           torch.cuda.empty_cache()







       @slow
       def 				_a   (					self   )	->				int:
           lowercase__									=    CTRLLMHeadModel.from_pretrained("ctrl"   )
           model.to(UpperCAmelCase_   )
           lowercase__									=    torch.tensor(
               [[11_859, 0, 1_611, 8]]  ,dtype=torch.long  ,device=UpperCAmelCase_   )  # Legal the president is
           lowercase__									=    [
               11_859,
               0,
               1_611,
               8,
               5,
               150,
               26_449,
               2,
               19,
               348,
               469,
               3,
               2_595,
               48,
               20_740,
               246_533,
               246_533,
               19,
               30,
               5,
           ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

           lowercase__									=    model.generate(UpperCAmelCase_  ,do_sample=UpperCAmelCase_   )
           self.assertListEqual(output_ids[0].tolist()  ,UpperCAmelCase_   )




