codingutf8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license testing suite for the pytorch levit model import unittest import warnings from math import ceil floor from transformers import levitconfig from transformers fileutils import cachedproperty istorchavailable isvisionavailable from transformers models auto import getvalues from transformers testingutils import requiretorch requirevision slow torchdevice from testconfigurationcommon import configtester from testmodelingcommon import modeltestermixin floatstensor idstensor from testpipelinemixin import pipelinetestermixin if istorchavailable import torch from transformers import modelforimageclassificationmapping modelmapping levitforimageclassification levitforimageclassificationwithteacher levitmodel from transformers models levit modelinglevit import levitpretrainedmodelarchivelist if isvisionavailable from pil import image from transformers import levitimageprocessor class levitconfigtesterconfigtester def createandtestconfigcommonpropertiesself config self configclassself inputsdict self parent asserttruehasattrconfig hiddensizes self parent asserttruehasattrconfig numattentionheads class levitmodeltester def init self parent batchsize13 imagesize64 numchannels3 kernelsize3 stride2 padding1 patchsize16 hiddensizes16 32 48 numattentionheads1 2 3 depths2 3 4 keydim8 8 8 droppathrate0 mlpratio2 2 2 attentionratio2 2 2 initializerrange0 02 istrainingtrue uselabelstrue numlabels2 check self parent parent self batchsize batchsize self imagesize imagesize self numchannels numchannels self kernelsize kernelsize self stride stride self padding padding self hiddensizes hiddensizes self numattentionheads numattentionheads self depths depths self keydim keydim self droppathrate droppathrate self patchsize patchsize self attentionratio attentionratio self mlpratio mlpratio self initializerrange initializerrange self downops subsample keydim0 hiddensizes0 keydim0 4 2 2 subsample keydim0 hiddensizes1 keydim0 4 2 2 self istraining istraining self uselabels uselabels self numlabels numlabels self initializerrange initializerrange def prepareconfigandinputsself pixelvalues floatstensorself batchsize self numchannels self imagesize self imagesize labels none if self uselabels labels idstensorself batchsize self numlabels config self getconfig return config pixelvalues labels def getconfigself return levitconfig imagesizeself imagesize numchannelsself numchannels kernelsizeself kernelsize strideself stride paddingself padding patchsizeself patchsize hiddensizesself hiddensizes numattentionheadsself numattentionheads depthsself depths keydimself keydim droppathrateself droppathrate mlpratioself mlpratio attentionratioself attentionratio initializerrangeself initializerrange downopsself downops def createandcheckmodelself config pixelvalues labels model levitmodelconfigconfig model totorchdevice model eval result modelpixelvalues imagesize self imagesize self imagesize height width imagesize0 imagesize1 for in range4 height floorheight 2 self padding self kernelsize self stride 1 width floorwidth 2 self padding self kernelsize self stride 1 self parent assertequal result lasthiddenstate shape self batchsize ceilheight 4 ceilwidth 4 self hiddensizes1 def createandcheckforimageclassificationself config pixelvalues labels config numlabels self numlabels model levitforimageclassificationconfig model totorchdevice model eval result modelpixelvalues labelslabels self parent assertequalresult logits shape self batchsize self numlabels def prepareconfigandinputsforcommonself configandinputs self prepareconfigandinputs config pixelvalues labels configandinputs inputsdict pixelvalues pixelvalues return config inputsdict requiretorch class levitmodeltestmodeltestermixin pipelinetestermixin unittest testcase allmodelclasses levitmodel levitforimageclassification levitforimageclassificationwithteacher if istorchavailable else pipelinemodelmapping featureextraction levitmodel imageclassification levitforimageclassification levitforimageclassificationwithteacher if istorchavailable else testpruning false testtorchscript false testresizeembeddings false testheadmasking false hasattentions false def setupself self modeltester levitmodeltesterself self configtester configtesterself configclasslevitconfig hastextmodalityfalse hiddensize37 def testconfigself self createandtestconfigcommonproperties self configtester createandtestconfigtojsonstring self configtester createandtestconfigtojsonfile self configtester createandtestconfigfromandsavepretrained self configtester createandtestconfigwithnumlabels self configtester checkconfigcanbeinitwithoutparams self configtester checkconfigargumentsinit def createandtestconfigcommonpropertiesself return unittest skipreasonlevit does not use inputsembeds def testinputsembedsself pass unittest skipreasonlevit does not support input and output embeddings def testmodelcommonattributesself pass unittest skipreasonlevit does not output attentions def testattentionoutputsself pass def testhiddenstatesoutputself def checkhiddenstatesoutputinputsdict config modelclass model modelclassconfig model totorchdevice model eval with torch nograd outputs modelself prepareforclassinputsdict modelclass hiddenstates outputs hiddenstates expectednumlayers lenself modeltester depths 1 self assertequallenhiddenstates expectednumlayers imagesize self modeltester imagesize self modeltester imagesize height width imagesize0 imagesize1 for in range4 height floor height 2 self modeltester padding self modeltester kernelsize self modeltester stride 1 width floor width 2 self modeltester padding self modeltester kernelsize self modeltester stride 1 verify the first hidden states first block self assertlistequal listhiddenstates0 shape2 height width self modeltester hiddensizes0 config inputsdict self modeltester prepareconfigandinputsforcommon for modelclass in self allmodelclasses inputsdictoutputhiddenstates true checkhiddenstatesoutputinputsdict config modelclass check that outputhiddenstates also work using config del inputsdictoutputhiddenstates config outputhiddenstates true checkhiddenstatesoutputinputsdict config modelclass def prepareforclassself inputsdict modelclass returnlabelsfalse inputsdict super prepareforclassinputsdict modelclass returnlabelsreturnlabels if returnlabels if modelclass name levitforimageclassificationwithteacher del inputsdictlabels return inputsdict def testmodelself configandinputs self modeltester prepareconfigandinputs self modeltester createandcheckmodelconfigandinputs def testforimageclassificationself configandinputs self modeltester prepareconfigandinputs self modeltester createandcheckforimageclassificationconfigandinputs special case for levitforimageclassificationwithteacher model def testtrainingself if not self modeltester istraining return config inputsdict self modeltester prepareconfigandinputsforcommon config returndict true for modelclass in self allmodelclasses levitforimageclassificationwithteacher supports inferenceonly if modelclass in getvaluesmodelmapping or modelclass name levitforimageclassificationwithteacher continue model modelclassconfig model totorchdevice model train inputs self prepareforclassinputsdict modelclass returnlabelstrue loss modelinputs loss loss backward def testtraininggradientcheckpointingself config inputsdict self modeltester prepareconfigandinputsforcommon if not self modeltester istraining return config usecache false config returndict true for modelclass in self allmodelclasses if modelclass in getvaluesmodelmapping or not modelclass supportsgradientcheckpointing continue levitforimageclassificationwithteacher supports inferenceonly if modelclass name levitforimageclassificationwithteacher continue model modelclassconfig model gradientcheckpointingenable model totorchdevice model train inputs self prepareforclassinputsdict modelclass returnlabelstrue loss modelinputs loss loss backward def testproblemtypesself config inputsdict self modeltester prepareconfigandinputsforcommon problemtypes title multilabelclassification numlabels 2 dtype torch float title singlelabelclassification numlabels 1 dtype torch long title regression numlabels 1 dtype torch float for modelclass in self allmodelclasses if modelclass not in getvaluesmodelforimageclassificationmapping or modelclass name levitforimageclassificationwithteacher continue for problemtype in problemtypes with self subtestmsgftesting modelclass with problemtype title config problemtype problemtypetitle config numlabels problemtypenumlabels model modelclassconfig model totorchdevice model train inputs self prepareforclassinputsdict modelclass returnlabelstrue if problemtypenumlabels 1 inputslabels inputslabels unsqueeze1 repeat1 problemtypenumlabels inputslabels inputslabels toproblemtypedtype this tests that we do not trigger the warning form pytorch using a target size that is different to the input size this will likely lead to incorrect results due to broadcasting please ensure they have the same size which is a symptom something in wrong for the regression problem see https github comhuggingfacetransformersissues11780 with warnings catchwarningsrecordtrue as warninglist loss modelinputs loss for w in warninglist if using a target size that is different to the input size in strw message raise valueerror fsomething is going wrong in the regression problem intercepted w message loss backward slow def testmodelfrompretrainedself for modelname in levitpretrainedmodelarchivelist 1 model levitmodel frompretrainedmodelname self assertisnotnonemodel we will verify our results on an image of cute cats def prepareimg image image open testsfixturestestssamplescoco000000039769 png return image requiretorch requirevision class levitmodelintegrationtestunittest testcase cachedproperty def defaultimageprocessorself return levitimageprocessor frompretrainedlevitpretrainedmodelarchivelist0 slow def testinferenceimageclassificationheadself model levitforimageclassificationwithteacher frompretrainedlevitpretrainedmodelarchivelist0 to torchdevice imageprocessor self defaultimageprocessor image prepareimg inputs imageprocessorimagesimage returntensorspt totorchdevice forward pass with torch nograd outputs modelinputs verify the logits expectedshape torch size1 1000 self assertequaloutputs logits shape expectedshape expectedslice torch tensor1 0448 0 3745 1 8317 totorchdevice self asserttruetorch allcloseoutputs logits0 3 expectedslice atol1e4 coding utf 8 2022 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license testing suite for the pytorch levit model check here we also overwrite some of the tests of test_modeling_common py as levit does not use input_ids inputs_embeds attention_mask and seq_length verify the first hidden states first block check that output_hidden_states also work using config special case for levitforimageclassificationwithteacher model levitforimageclassificationwithteacher supports inference only levitforimageclassificationwithteacher supports inference only this tests that we do not trigger the warning form pytorch using a target size that is different to the input size this will likely lead to incorrect results due to broadcasting please ensure they have the same size which is a symptom something in wrong for the regression problem see https github com huggingface transformers issues 11780 we will verify our results on an image of cute cats forward pass verify the logits
import unittest
import warnings
from math import ceil, floor

from transformers import LevitConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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


if is_torch_available():
    import torch

    from transformers import (
        MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
        MODEL_MAPPING,
        LevitForImageClassification,
        LevitForImageClassificationWithTeacher,
        LevitModel,
    )
    from transformers.models.levit.modeling_levit import LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import LevitImageProcessor


class LevitConfigTester(ConfigTester):
    def create_and_test_config_common_properties(self):
        config = self.config_class(**self.inputs_dict)
        self.parent.assertTrue(hasattr(config, "hidden_sizes"))
        self.parent.assertTrue(hasattr(config, "num_attention_heads"))


class LevitModelTester:
    def __init__(
        self,
        parent,
        batch_size=13,
        image_size=64,
        num_channels=3,
        kernel_size=3,
        stride=2,
        padding=1,
        patch_size=16,
        hidden_sizes=[16, 32, 48],
        num_attention_heads=[1, 2, 3],
        depths=[2, 3, 4],
        key_dim=[8, 8, 8],
        drop_path_rate=0,
        mlp_ratio=[2, 2, 2],
        attention_ratio=[2, 2, 2],
        initializer_range=0.02,
        is_training=True,
        use_labels=True,
        num_labels=2,  
    ):
        self.parent = parent
        self.batch_size = batch_size
        self.image_size = image_size
        self.num_channels = num_channels
        self.kernel_size = kernel_size
        self.stride = stride
        self.padding = padding
        self.hidden_sizes = hidden_sizes
        self.num_attention_heads = num_attention_heads
        self.depths = depths
        self.key_dim = key_dim
        self.drop_path_rate = drop_path_rate
        self.patch_size = patch_size
        self.attention_ratio = attention_ratio
        self.mlp_ratio = mlp_ratio
        self.initializer_range = initializer_range
        self.down_ops = [
            ["Subsample", key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
            ["Subsample", key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
        ]
        self.is_training = is_training
        self.use_labels = use_labels
        self.num_labels = num_labels
        self.initializer_range = initializer_range

    def prepare_config_and_inputs(self):
        pixel_values = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

        labels = None
        if self.use_labels:
            labels = ids_tensor([self.batch_size], self.num_labels)

        config = self.get_config()
        return config, pixel_values, labels

    def get_config(self):
        return LevitConfig(
            image_size=self.image_size,
            num_channels=self.num_channels,
            kernel_size=self.kernel_size,
            stride=self.stride,
            padding=self.padding,
            patch_size=self.patch_size,
            hidden_sizes=self.hidden_sizes,
            num_attention_heads=self.num_attention_heads,
            depths=self.depths,
            key_dim=self.key_dim,
            drop_path_rate=self.drop_path_rate,
            mlp_ratio=self.mlp_ratio,
            attention_ratio=self.attention_ratio,
            initializer_range=self.initializer_range,
            down_ops=self.down_ops,
        )

    def create_and_check_model(self, config, pixel_values, labels):
        model = LevitModel(config=config)
        model.to(torch_device)
        model.eval()
        result = model(pixel_values)
        image_size = (self.image_size, self.image_size)
        height, width = image_size[0], image_size[1]
        for _ in range(4):
            height = floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1)
            width = floor(((width + 2 * self.padding - self.kernel_size) / self.stride) + 1)
        self.parent.assertEqual(
            result.last_hidden_state.shape,
            (self.batch_size, ceil(height / 4) * ceil(width / 4), self.hidden_sizes[-1]),
        )

    def create_and_check_for_image_classification(self, config, pixel_values, labels):
        config.num_labels = self.num_labels
        model = LevitForImageClassification(config)
        model.to(torch_device)
        model.eval()
        result = model(pixel_values, labels=labels)
        self.parent.assertEqual(result.logits.shape, (self.batch_size, self.num_labels))

    def prepare_config_and_inputs_for_common(self):
        config_and_inputs = self.prepare_config_and_inputs()
        config, pixel_values, labels = config_and_inputs
        inputs_dict = {"pixel_values": pixel_values}
        return config, inputs_dict


@require_torch
class LevitModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
    

    all_model_classes = (
        (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher)
        if is_torch_available()
        else ()
    )
    pipeline_model_mapping = (
        {
            "feature-extraction": LevitModel,
            "image-classification": (LevitForImageClassification, LevitForImageClassificationWithTeacher),
        }
        if is_torch_available()
        else {}
    )

    test_pruning = False
    test_torchscript = False
    test_resize_embeddings = False
    test_head_masking = False
    has_attentions = False

    def setUp(self):
        self.model_tester = LevitModelTester(self)
        self.config_tester = ConfigTester(self, config_class=LevitConfig, has_text_modality=False, hidden_size=37)

    def test_config(self):
        self.create_and_test_config_common_properties()
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.create_and_test_config_with_num_labels()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()

    def create_and_test_config_common_properties(self):
        return

    @unittest.skip(reason="Levit does not use inputs_embeds")
    def test_inputs_embeds(self):
        pass

    @unittest.skip(reason="Levit does not support input and output embeddings")
    def test_model_common_attributes(self):
        pass

    @unittest.skip(reason="Levit does not output attentions")
    def test_attention_outputs(self):
        pass

    def test_hidden_states_output(self):
        def check_hidden_states_output(inputs_dict, config, model_class):
            model = model_class(config)
            model.to(torch_device)
            model.eval()

            with torch.no_grad():
                outputs = model(**self._prepare_for_class(inputs_dict, model_class))

            hidden_states = outputs.hidden_states

            expected_num_layers = len(self.model_tester.depths) + 1
            self.assertEqual(len(hidden_states), expected_num_layers)

            image_size = (self.model_tester.image_size, self.model_tester.image_size)
            height, width = image_size[0], image_size[1]
            for _ in range(4):
                height = floor(
                    (
                        (height + 2 * self.model_tester.padding - self.model_tester.kernel_size)
                        / self.model_tester.stride
                    )
                    + 1
                )
                width = floor(
                    (
                        (width + 2 * self.model_tester.padding - self.model_tester.kernel_size)
                        / self.model_tester.stride
                    )
                    + 1
                )
            
            self.assertListEqual(
                list(hidden_states[0].shape[-2:]),
                [
                    height * width,
                    self.model_tester.hidden_sizes[0],
                ],
            )

        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            inputs_dict["output_hidden_states"] = True
            check_hidden_states_output(inputs_dict, config, model_class)

            
            del inputs_dict["output_hidden_states"]
            config.output_hidden_states = True

            check_hidden_states_output(inputs_dict, config, model_class)

    def _prepare_for_class(self, inputs_dict, model_class, return_labels=False):
        inputs_dict = super()._prepare_for_class(inputs_dict, model_class, return_labels=return_labels)

        if return_labels:
            if model_class.__name__ == "LevitForImageClassificationWithTeacher":
                del inputs_dict["labels"]

        return inputs_dict

    def test_model(self):
        config_and_inputs = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*config_and_inputs)

    def test_for_image_classification(self):
        config_and_inputs = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*config_and_inputs)

    
    def test_training(self):
        if not self.model_tester.is_training:
            return

        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
        config.return_dict = True

        for model_class in self.all_model_classes:
            
            if (
                model_class in get_values(MODEL_MAPPING)
                or model_class.__name__ == "LevitForImageClassificationWithTeacher"
            ):
                continue
            model = model_class(config)
            model.to(torch_device)
            model.train()
            inputs = self._prepare_for_class(inputs_dict, model_class, return_labels=True)
            loss = model(**inputs).loss
            loss.backward()

    def test_training_gradient_checkpointing(self):
        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
        if not self.model_tester.is_training:
            return

        config.use_cache = False
        config.return_dict = True

        for model_class in self.all_model_classes:
            if model_class in get_values(MODEL_MAPPING) or not model_class.supports_gradient_checkpointing:
                continue
            
            if model_class.__name__ == "LevitForImageClassificationWithTeacher":
                continue
            model = model_class(config)
            model.gradient_checkpointing_enable()
            model.to(torch_device)
            model.train()
            inputs = self._prepare_for_class(inputs_dict, model_class, return_labels=True)
            loss = model(**inputs).loss
            loss.backward()

    def test_problem_types(self):
        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()

        problem_types = [
            {"title": "multi_label_classification", "num_labels": 2, "dtype": torch.float},
            {"title": "single_label_classification", "num_labels": 1, "dtype": torch.long},
            {"title": "regression", "num_labels": 1, "dtype": torch.float},
        ]

        for model_class in self.all_model_classes:
            if (
                model_class
                not in [
                    *get_values(MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING),
                ]
                or model_class.__name__ == "LevitForImageClassificationWithTeacher"
            ):
                continue

            for problem_type in problem_types:
                with self.subTest(msg=f"Testing {model_class} with {problem_type['title']}"):
                    config.problem_type = problem_type["title"]
                    config.num_labels = problem_type["num_labels"]

                    model = model_class(config)
                    model.to(torch_device)
                    model.train()

                    inputs = self._prepare_for_class(inputs_dict, model_class, return_labels=True)

                    if problem_type["num_labels"] > 1:
                        inputs["labels"] = inputs["labels"].unsqueeze(1).repeat(1, problem_type["num_labels"])

                    inputs["labels"] = inputs["labels"].to(problem_type["dtype"])

                    
                    
                    
                    
                    with warnings.catch_warnings(record=True) as warning_list:
                        loss = model(**inputs).loss
                    for w in warning_list:
                        if "Using a target size that is different to the input size" in str(w.message):
                            raise ValueError(
                                f"Something is going wrong in the regression problem: intercepted {w.message}"
                            )

                    loss.backward()

    @slow
    def test_model_from_pretrained(self):
        for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            model = LevitModel.from_pretrained(model_name)
            self.assertIsNotNone(model)



def prepare_img():
    image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
    return image


@require_torch
@require_vision
class LevitModelIntegrationTest(unittest.TestCase):
    @cached_property
    def default_image_processor(self):
        return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0])

    @slow
    def test_inference_image_classification_head(self):
        model = LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]).to(
            torch_device
        )

        image_processor = self.default_image_processor
        image = prepare_img()
        inputs = image_processor(images=image, return_tensors="pt").to(torch_device)

        
        with torch.no_grad():
            outputs = model(**inputs)

        
        expected_shape = torch.Size((1, 1000))
        self.assertEqual(outputs.logits.shape, expected_shape)

        expected_slice = torch.tensor([1.0448, -0.3745, -1.8317]).to(torch_device)

        self.assertTrue(torch.allclose(outputs.logits[0, :3], expected_slice, atol=1e-4))