jalammar / ecco

Explain, analyze, and visualize NLP language models. Ecco creates interactive visualizations directly in Jupyter notebooks explaining the behavior of Transformer-based language models (like GPT2, BERT, RoBERTA, T5, and T0).
https://ecco.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.96k stars 167 forks source link

[bugfix] fix comparison of transformers version in lm generate #62

Closed joaonadkarni closed 2 years ago

joaonadkarni commented 2 years ago

Problem: The comparison of versions in the lm generate method is buggy as it is just comparing strings. For e.g. "4.6.1" > "4.13" when just comparing strings, but that is not the intended behavior when comparing versions.

Solution: Use the version.parse functionality of the packaging package (that is already installed as a requirement of one of the required packages present in the requirements.txt). This will make the string version to be binded to a Version object that has the comparison methods implemented with the intended behavior for version comparison.

Local setup to reproduce bug: Code:

import ecco

lm = ecco.from_pretrained(
    'tscholak/1zha5ono',
    model_config={'embedding': 'shared.weight',
    'type': 'enc-dec',
    'activations': ['wo'],
    'token_prefix': '▁',
    'partial_token_prefix': ''}
)
output = lm.generate("some test string", max_length=500, do_sample=False, attribution=['grad_x_input'])

Error: image Relevant Packages version:

ecco: 0.1.1
transformers: 4.6.1
jalammar commented 2 years ago

Beautiful! Thank you for this!