Skip to content

Conversation

@bglearning
Copy link
Contributor

Add Type Hints to methods in modeling_utils.py

Note: The coverage isn't 100%. Mostly skipped internal methods (and some I wasn't sure of).

Bijay Gurung added 2 commits April 24, 2020 22:45
Add Type Hints to methods in `modeling_utils.py`

Note: The coverage isn't 100%. Mostly skipped internal methods.
Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, thanks!

@patrickvonplaten
Copy link
Contributor

I think this is good for merge, no? @julien-c

@patrickvonplaten patrickvonplaten linked an issue May 4, 2020 that may be closed by this pull request
import logging
import os
from typing import Callable, Tuple
from typing import Callable, Sequence, Tuple
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we usually use typing.Iterable, any reason to use Sequence here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to Iterable

raise NotImplementedError

def set_input_embeddings(self, value):
def set_input_embeddings(self, value: nn.Module):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this not an nn.Layer or even a nn.Embedding?

Copy link
Contributor Author

@bglearning bglearning May 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going through some tests, looks like it's expected to be either nn.Embedding or AdaptiveEmbedding (from modeling_transfo_xl.py). The docstring has nn.Module (as "A module mapping vocabulary to hidden states"). Maybe it is so to have it be general? Not sure.

output_embeddings.out_features = input_embeddings.num_embeddings

def resize_token_embeddings(self, new_num_tokens=None):
def resize_token_embeddings(self, new_num_tokens: int = None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional[int] = None?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

self.tie_weights()

def prune_heads(self, heads_to_prune):
def prune_heads(self, heads_to_prune: dict):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def prune_heads(self, heads_to_prune: dict):
def prune_heads(self, heads_to_prune: Dict):

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it.

num_return_sequences: int = None,
attention_mask: torch.LongTensor = None,
decoder_start_token_id: int = None,
use_cache: bool = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of those should be Optional[...]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done



def calc_banned_bad_words_ids(prev_input_ids, bad_words_ids):
def calc_banned_bad_words_ids(prev_input_ids: Sequence, bad_words_ids: Sequence) -> Sequence:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parametrize by their generic type, i.e. Sequence[int]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@codecov-io
Copy link

codecov-io commented May 9, 2020

Codecov Report

Merging #3948 into master will decrease coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3948      +/-   ##
==========================================
- Coverage   78.39%   78.38%   -0.02%     
==========================================
  Files         120      120              
  Lines       19925    19925              
==========================================
- Hits        15620    15618       -2     
- Misses       4305     4307       +2     
Impacted Files Coverage Δ
src/transformers/modeling_utils.py 90.96% <100.00%> (-0.13%) ⬇️
src/transformers/modeling_tf_utils.py 92.77% <0.00%> (-0.17%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7b75aa9...99848e8. Read the comment docs.

@bglearning bglearning requested a review from julien-c May 14, 2020 18:12
@julien-c julien-c merged commit e19b978 into huggingface:master May 22, 2020
@julien-c
Copy link
Member

Thanks @bglearning!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Type Hints for modeling_utils.py

5 participants