Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty assert hunt #6056

Merged
merged 7 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/transformers/commands/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def __init__(self, args: Namespace):
self.framework = "tf" if is_tf_available() else "torch"

os.makedirs(args.output, exist_ok=True)
assert os.path.isdir(args.output)
self.output = args.output

self.column_label = args.column_label
Expand Down
27 changes: 17 additions & 10 deletions src/transformers/convert_marian_to_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def write_model_card(
extra_markdown = f"### {hf_model_name}\n\n* source languages: {s}\n* target languages: {t}\n* OPUS readme: [{opus_name}]({readme_url})\n"
# combine with opus markdown
opus_readme_path = Path(f"{repo_path}{opus_name}/README.md")
assert opus_readme_path.exists(), opus_readme_path
assert opus_readme_path.exists(), f"Readme file {opus_readme_path} not found"
content = opus_readme_path.open().read()
content = content.split("\n# ")[-1] # Get the lowest level 1 header in the README -- the most recent model.
content = "*".join(content.split("*")[1:])
Expand Down Expand Up @@ -231,7 +231,9 @@ def fetch_test_set(test_set_url):
src = lmap(str.strip, lns[::4])
gold = lmap(str.strip, lns[1::4])
mar_model = lmap(str.strip, lns[2::4])
assert len(gold) == len(mar_model) == len(src)
assert (
len(gold) == len(mar_model) == len(src)
), f"Gold, marian and source lengths {len(gold)}, {len(mar_model)}, {len(src)} mismatched"
os.remove(fname)
return src, mar_model, gold

Expand Down Expand Up @@ -374,20 +376,21 @@ def __init__(self, source_dir):
self.state_dict = np.load(npz_path)
cfg = load_config_from_state_dict(self.state_dict)
assert cfg["dim-vocabs"][0] == cfg["dim-vocabs"][1]
assert "Wpos" not in self.state_dict
assert "Wpos" not in self.state_dict, "Wpos key in state dictionary"
self.state_dict = dict(self.state_dict)
self.wemb, self.final_bias = add_emb_entries(self.state_dict["Wemb"], self.state_dict[BIAS_KEY], 1)
self.pad_token_id = self.wemb.shape[0] - 1
cfg["vocab_size"] = self.pad_token_id + 1
# self.state_dict['Wemb'].sha
self.state_keys = list(self.state_dict.keys())
if "Wtype" in self.state_dict:
raise ValueError("found Wtype key")
assert "Wtype" not in self.state_dict, "Wtype key in state dictionary"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you sure ? it seems to me that the assert clause activates when the key is in the state dict, not the reverse, so that's what the error message should display

Copy link
Contributor

Choose a reason for hiding this comment

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

no you're correct. I don't see the value of the change but my suggestion is horrible.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I was just trying to harmonize things, not much of a change from raising an error to having an assert clause

self._check_layer_entries()
self.source_dir = source_dir
self.cfg = cfg
hidden_size, intermediate_shape = self.state_dict["encoder_l1_ffn_W1"].shape
assert hidden_size == cfg["dim-emb"] == 512
assert (
hidden_size == cfg["dim-emb"] == 512
), f"Hidden size {hidden_size} and configured size {cfg['dim_emb']} mismatched or not 512"

# Process decoder.yml
decoder_yml = cast_marian_config(load_yaml(source_dir / "decoder.yml"))
Expand Down Expand Up @@ -448,7 +451,7 @@ def sub_keys(self, layer_prefix):
def load_marian_model(self) -> MarianMTModel:
state_dict, cfg = self.state_dict, self.hf_config

assert cfg.static_position_embeddings
assert cfg.static_position_embeddings, "config.static_position_embeddings should be True"
model = MarianMTModel(cfg)

assert "hidden_size" not in cfg.to_dict()
Expand Down Expand Up @@ -476,7 +479,9 @@ def load_marian_model(self) -> MarianMTModel:
raise NotImplementedError("Need to convert layernorm_embedding")

assert not self.extra_keys, f"Failed to convert {self.extra_keys}"
assert model.model.shared.padding_idx == self.pad_token_id
assert (
model.model.shared.padding_idx == self.pad_token_id
), f"Padding tokens {model.model.shared.padding_idx} and {self.pad_token_id} mismatched"
return model


Expand All @@ -500,7 +505,9 @@ def convert(source_dir: Path, dest_dir):
save_tokenizer(tokenizer, dest_dir)

opus_state = OpusState(source_dir)
assert opus_state.cfg["vocab_size"] == len(tokenizer.encoder)
assert opus_state.cfg["vocab_size"] == len(
tokenizer.encoder
), f"Original vocab size {opus_state.cfg['vocab_size']} and new vocab size {len(tokenizer.encoder)} mismatched"
# save_json(opus_state.cfg, dest_dir / "marian_original_config.json")
# ^^ Save human readable marian config for debugging

Expand All @@ -517,7 +524,7 @@ def convert(source_dir: Path, dest_dir):
args = parser.parse_args()

source_dir = Path(args.src)
assert source_dir.exists()
assert source_dir.exists(), f"Source directory {source_dir} not found"
dest_dir = f"converted-{source_dir.name}" if args.dest is None else args.dest
convert(source_dir, dest_dir)

Expand Down
4 changes: 2 additions & 2 deletions src/transformers/data/datasets/language_modeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TextDataset(Dataset):
def __init__(
self, tokenizer: PreTrainedTokenizer, file_path: str, block_size: int, overwrite_cache=False,
):
assert os.path.isfile(file_path)
assert os.path.isfile(file_path), f"Input file path {file_path} not found"

block_size = block_size - tokenizer.num_special_tokens_to_add(pair=False)

Expand Down Expand Up @@ -82,7 +82,7 @@ class LineByLineTextDataset(Dataset):
"""

def __init__(self, tokenizer: PreTrainedTokenizer, file_path: str, block_size: int):
assert os.path.isfile(file_path)
assert os.path.isfile(file_path), f"Input file path {file_path} not found"
# Here, we do not cache the features, operating under the assumption
# that we will soon use fast multithreaded tokenizers from the
# `tokenizers` repo everywhere =)
Expand Down
8 changes: 6 additions & 2 deletions src/transformers/data/metrics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ def pearson_and_spearman(preds, labels):
}

def glue_compute_metrics(task_name, preds, labels):
assert len(preds) == len(labels)
assert len(preds) == len(
labels
), f"Predictions and labels have mismatched lengths {len(preds)} and {len(labels)}"
if task_name == "cola":
return {"mcc": matthews_corrcoef(labels, preds)}
elif task_name == "sst-2":
Expand All @@ -78,7 +80,9 @@ def glue_compute_metrics(task_name, preds, labels):
raise KeyError(task_name)

def xnli_compute_metrics(task_name, preds, labels):
assert len(preds) == len(labels)
assert len(preds) == len(
labels
), f"Predictions and labels have mismatched lengths {len(preds)} and {len(labels)}"
if task_name == "xnli":
return {"acc": simple_accuracy(preds, labels)}
else:
Expand Down
8 changes: 4 additions & 4 deletions src/transformers/data/metrics/squad_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ def compute_predictions_logits(
if not nbest:
nbest.append(_NbestPrediction(text="empty", start_logit=0.0, end_logit=0.0))

assert len(nbest) >= 1
assert len(nbest) >= 1, "No valid predictions"

total_scores = []
best_non_null_entry = None
Expand All @@ -544,7 +544,7 @@ def compute_predictions_logits(
output["end_logit"] = entry.end_logit
nbest_json.append(output)

assert len(nbest_json) >= 1
assert len(nbest_json) >= 1, "No valid predictions"

if not version_2_with_negative:
all_predictions[example.qas_id] = nbest_json[0]["text"]
Expand Down Expand Up @@ -739,8 +739,8 @@ def compute_predictions_log_probs(
output["end_log_prob"] = entry.end_log_prob
nbest_json.append(output)

assert len(nbest_json) >= 1
assert best_non_null_entry is not None
assert len(nbest_json) >= 1, "No valid predictions"
assert best_non_null_entry is not None, "No valid predictions"

score_diff = score_null
scores_diff_json[example.qas_id] = score_diff
Expand Down
8 changes: 6 additions & 2 deletions src/transformers/data/processors/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ def add_examples_from_csv(
def add_examples(
self, texts_or_text_and_labels, labels=None, ids=None, overwrite_labels=False, overwrite_examples=False
):
assert labels is None or len(texts_or_text_and_labels) == len(labels)
assert ids is None or len(texts_or_text_and_labels) == len(ids)
assert labels is None or len(texts_or_text_and_labels) == len(
labels
), f"Text and labels have mismatched lengths {len(texts_or_text_and_labels)} and {len(labels)}"
assert ids is None or len(texts_or_text_and_labels) == len(
ids
), f"Text and ids have mismatched lengths {len(texts_or_text_and_labels)} and {len(ids)}"
if ids is None:
ids = [None] * len(texts_or_text_and_labels)
if labels is None:
Expand Down
8 changes: 6 additions & 2 deletions src/transformers/data/processors/xnli.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def get_train_examples(self, data_dir):
text_a = line[0]
text_b = line[1]
label = "contradiction" if line[2] == "contradictory" else line[2]
assert isinstance(text_a, str) and isinstance(text_b, str) and isinstance(label, str)
assert isinstance(text_a, str), f"Training input {text_a} is not a string"
assert isinstance(text_b, str), f"Training input {text_b} is not a string"
assert isinstance(label, str), f"Training label {label} is not a string"
examples.append(InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
return examples

Expand All @@ -63,7 +65,9 @@ def get_test_examples(self, data_dir):
text_a = line[6]
text_b = line[7]
label = line[1]
assert isinstance(text_a, str) and isinstance(text_b, str) and isinstance(label, str)
assert isinstance(text_a, str), f"Training input {text_a} is not a string"
assert isinstance(text_b, str), f"Training input {text_b} is not a string"
assert isinstance(label, str), f"Training label {label} is not a string"
examples.append(InputExample(guid=guid, text_a=text_a, text_b=text_b, label=label))
return examples

Expand Down
4 changes: 3 additions & 1 deletion src/transformers/modeling_albert.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ def load_tf_weights_in_albert(model, config, tf_checkpoint_path):
elif m_name == "kernel":
array = np.transpose(array)
try:
assert pointer.shape == array.shape
assert (
pointer.shape == array.shape
), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched"
except AssertionError as e:
e.args += (pointer.shape, array.shape)
raise
Expand Down
4 changes: 3 additions & 1 deletion src/transformers/modeling_bert.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ def load_tf_weights_in_bert(model, config, tf_checkpoint_path):
elif m_name == "kernel":
array = np.transpose(array)
try:
assert pointer.shape == array.shape
assert (
pointer.shape == array.shape
), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched"
except AssertionError as e:
e.args += (pointer.shape, array.shape)
raise
Expand Down
4 changes: 3 additions & 1 deletion src/transformers/modeling_electra.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def load_tf_weights_in_electra(model, config, tf_checkpoint_path, discriminator_
elif m_name == "kernel":
array = np.transpose(array)
try:
assert pointer.shape == array.shape, original_name
assert (
pointer.shape == array.shape
), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched"
except AssertionError as e:
e.args += (pointer.shape, array.shape)
raise
Expand Down
4 changes: 3 additions & 1 deletion src/transformers/modeling_gpt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ def load_tf_weights_in_gpt2(model, config, gpt2_checkpoint_path):
num = int(scope_names[1])
pointer = pointer[num]
try:
assert pointer.shape == array.shape
assert (
pointer.shape == array.shape
), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched"
except AssertionError as e:
e.args += (pointer.shape, array.shape)
raise
Expand Down
4 changes: 3 additions & 1 deletion src/transformers/modeling_mobilebert.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ def load_tf_weights_in_mobilebert(model, config, tf_checkpoint_path):
elif m_name == "kernel":
array = np.transpose(array)
try:
assert pointer.shape == array.shape
assert (
pointer.shape == array.shape
), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched"
except AssertionError as e:
e.args += (pointer.shape, array.shape)
raise
Expand Down
8 changes: 6 additions & 2 deletions src/transformers/modeling_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,16 @@ def load_tf_weights_in_openai_gpt(model, config, openai_checkpoint_folder_path):
num = int(scope_names[1])
pointer = pointer[num]
try:
assert pointer.shape == array.shape
assert (
pointer.shape == array.shape
), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched"
except AssertionError as e:
e.args += (pointer.shape, array.shape)
raise
try:
assert pointer.shape == array.shape
assert (
pointer.shape == array.shape
), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched"
except AssertionError as e:
e.args += (pointer.shape, array.shape)
raise
Expand Down
4 changes: 3 additions & 1 deletion src/transformers/modeling_t5.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ def load_tf_weights_in_t5(model, config, tf_checkpoint_path):
logger.info("Transposing numpy weight of shape {} for {}".format(array.shape, name))
array = np.transpose(array)
try:
assert pointer.shape == array.shape
assert (
pointer.shape == array.shape
), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched"
except AssertionError as e:
e.args += (pointer.shape, array.shape)
raise
Expand Down
4 changes: 3 additions & 1 deletion src/transformers/modeling_tf_albert.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ def __init__(self, config, **kwargs):
)

self.num_attention_heads = config.num_attention_heads
assert config.hidden_size % config.num_attention_heads == 0
assert (
config.hidden_size % config.num_attention_heads == 0
), f"Hidden size {config.hidden_size} not dividable by number of heads {config.num_attention_heads}"
self.attention_head_size = int(config.hidden_size / config.num_attention_heads)
self.all_head_size = self.num_attention_heads * self.attention_head_size

Expand Down
12 changes: 7 additions & 5 deletions src/transformers/modeling_tf_distilbert.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def __init__(self, config, **kwargs):
self.dim = config.dim
self.dropout = tf.keras.layers.Dropout(config.attention_dropout)

assert self.dim % self.n_heads == 0
assert self.dim % self.n_heads == 0, f"Hidden size {self.dim} not dividable by number of heads {self.n_heads}"

self.q_lin = tf.keras.layers.Dense(
config.dim, kernel_initializer=get_initializer(config.initializer_range), name="q_lin"
Expand Down Expand Up @@ -311,7 +311,9 @@ def __init__(self, config, **kwargs):
self.dropout = tf.keras.layers.Dropout(config.dropout)
self.activation = config.activation

assert config.dim % config.n_heads == 0
assert (
config.dim % config.n_heads == 0
), f"Hidden size {config.dim} not dividable by number of heads {config.n_heads}"

self.attention = TFMultiHeadSelfAttention(config, name="attention")
self.sa_layer_norm = tf.keras.layers.LayerNormalization(epsilon=1e-12, name="sa_layer_norm")
Expand Down Expand Up @@ -395,11 +397,11 @@ def call(self, inputs, training=False):
hidden_state = layer_outputs[-1]

if cast_bool_to_primitive(output_attentions) is True:
assert len(layer_outputs) == 2
assert len(layer_outputs) == 2, f"Incorrect number of outputs {len(layer_outputs)} instead of 2"
attentions = layer_outputs[0]
all_attentions = all_attentions + (attentions,)
else:
assert len(layer_outputs) == 1
assert len(layer_outputs) == 1, f"Incorrect number of outputs {len(layer_outputs)} instead of 1"

# Add last layer
if cast_bool_to_primitive(output_hidden_states) is True:
Expand Down Expand Up @@ -1024,7 +1026,7 @@ def __init__(self, config, *inputs, **kwargs):
self.qa_outputs = tf.keras.layers.Dense(
config.num_labels, kernel_initializer=get_initializer(config.initializer_range), name="qa_outputs"
)
assert config.num_labels == 2
assert config.num_labels == 2, f"Incorrect number of labels {config.num_labels} instead of 2"
self.dropout = tf.keras.layers.Dropout(config.qa_dropout)

@add_start_docstrings_to_callable(DISTILBERT_INPUTS_DOCSTRING)
Expand Down
12 changes: 9 additions & 3 deletions src/transformers/modeling_tf_flaubert.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ def call(

# check inputs
# assert shape_list(lengths)[0] == bs
tf.debugging.assert_equal(shape_list(lengths)[0], bs)
tf.debugging.assert_equal(
shape_list(lengths)[0], bs
), f"Expected batch size {shape_list(lengths)[0]} and received batch size {bs} mismatched"
# assert lengths.max().item() <= slen
# input_ids = input_ids.transpose(0, 1) # batch size as dimension 0
# assert (src_enc is None) == (src_len is None)
Expand All @@ -201,13 +203,17 @@ def call(
position_ids = tf.expand_dims(tf.range(slen), axis=0)
else:
# assert shape_list(position_ids) == [bs, slen] # (slen, bs)
tf.debugging.assert_equal(shape_list(position_ids), [bs, slen])
tf.debugging.assert_equal(
shape_list(position_ids), [bs, slen]
), f"Position id shape {shape_list(position_ids)} and input shape {[bs, slen]} mismatched"
# position_ids = position_ids.transpose(0, 1)

# langs
if langs is not None:
# assert shape_list(langs) == [bs, slen] # (slen, bs)
tf.debugging.assert_equal(shape_list(langs), [bs, slen])
tf.debugging.assert_equal(
shape_list(langs), [bs, slen]
), f"Lang shape {shape_list(langs)} and input shape {[bs, slen]} mismatched"
# langs = langs.transpose(0, 1)

# Prepare head mask if needed
Expand Down
4 changes: 3 additions & 1 deletion src/transformers/modeling_tf_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def __init__(self, nx, n_ctx, config, scale=False, **kwargs):

n_state = nx # in Attention: n_state=768 (nx=n_embd)
# [switch nx => n_state from Block to Attention to keep identical to TF implem]
assert n_state % config.n_head == 0
assert (
n_state % config.n_head == 0
), f"Hidden dimension {n_state} not dividable by number of heads {config.n_head}"
self.n_ctx = n_ctx
self.n_head = config.n_head
self.split_size = n_state
Expand Down
3 changes: 1 addition & 2 deletions src/transformers/modeling_tf_xlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,7 @@ def relative_positional_encoding(self, qlen, klen, bsz=None, dtype=None):
bwd_pos_seq = tf.clip_by_value(bwd_pos_seq, -self.clamp_len, self.clamp_len)

if bsz is not None:
# With bi_data, the batch size should be divisible by 2.
assert bsz % 2 == 0
assert bsz % 2 == 0, f"With bi_data, the batch size {bsz} should be divisible by 2"
fwd_pos_emb = self.positional_embedding(fwd_pos_seq, inv_freq, bsz // 2)
bwd_pos_emb = self.positional_embedding(bwd_pos_seq, inv_freq, bsz // 2)
else:
Expand Down
Loading