facebookresearch / ToMe

A method to increase the speed and lower the memory footprint of existing vision transformers.
Other
931 stars 67 forks source link

`ToMeTransformer` object has no attribute 'dist_token' #1

Closed Raghvender1205 closed 1 year ago

Raghvender1205 commented 1 year ago

I was trying out the timm_validation.ipynb from the examples directory on my Colab. I load the ViT Base model and uploaded an image. But when i tried patching my model with ToMe it gives out an error

This is the stacktrace

AttributeError                            Traceback (most recent call last)
[<ipython-input-11-6cb8d2faecdd>](https://localhost:8080/#) in <module>
      1 # Patch Model to ToMe
----> 2 tome.patch.timm(model)

1 frames
[/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py](https://localhost:8080/#) in __getattr__(self, name)
   1206                 return modules[name]
   1207         raise AttributeError("'{}' object has no attribute '{}'".format(
-> 1208             type(self).__name__, name))
   1209 
   1210     def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> None:

AttributeError: 'ToMeVisionTransformer' object has no attribute 'dist_token'
dbolya commented 1 year ago

Hmm, you give it a timm model, correct? Might be a version issue. What timm version do you have?

And if you set "distill_token": False on line 133 of tome/patch/timm.py, does it work?

Raghvender1205 commented 1 year ago

I'll check and revert back

Raghvender1205 commented 1 year ago

I installed timm on the colab so timm 0.4.12 it was.

dbolya commented 1 year ago

dist_token shouldn't be undefined in timm 0.4.12, according to the source: https://github.com/rwightman/pytorch-image-models/blob/7096b52a613eefb4f6d8107366611c8983478b19/timm/models/vision_transformer.py#L264

Though, if hard-coding that flag to False works, I can just push a work-around that checks if the transformer has that attribute.

Raghvender1205 commented 1 year ago

on changing the self._tome_info['distill_token'] = False, it seems to breaks as now I am getting this

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[<ipython-input-28-18d855f21157>](https://localhost:8080/#) in <module>
      1 # Run image through ViT model
      2 # Return class idx
----> 3 model(img_tensor).topk(5).indices[0].tolist()

2 frames
[/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py](https://localhost:8080/#) in __getattr__(self, name)
   1206                 return modules[name]
   1207         raise AttributeError("'{}' object has no attribute '{}'".format(
-> 1208             type(self).__name__, name))
   1209 
   1210     def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -> None:

AttributeError: 'ToMeVisionTransformer' object has no attribute '_tome_info'
Raghvender1205 commented 1 year ago

Code Snippet Screenshot

dbolya commented 1 year ago

What I meant was replaced the dictionary here: https://github.com/facebookresearch/ToMe/blob/main/tome/patch/timm.py#L126-L134 with

    model._tome_info = {
        "r": model.r,
        "size": None,
        "source": None,
        "trace_source": trace_source,
        "prop_attn": prop_attn,
        "class_token": model.cls_token is not None,
        "distill_token": False,
    }

You'll have to recreate and re-patch the model.

Raghvender1205 commented 1 year ago

Oh Ok. I did replace but it seems to not work. I think there should be PR for this though.

image

dbolya commented 1 year ago

I've created a PR for the candidate fix. See if you can reset your collab and pull this pr instead: #3

I can't test what this changes because every version of timm I can find has that dist token, so I'm not sure what's up with your environment.

Raghvender1205 commented 1 year ago

I'll check

Raghvender1205 commented 1 year ago

On this branch the example benchmark_timm.ipynb is working

dbolya commented 1 year ago

Great, I'll merge.