Unverified Commit fa4abdb3 authored by Jayesh Dewangan's avatar Jayesh Dewangan Committed by GitHub
Browse files

Replace assertions with valueError Exeptions (#14117)

* Replace assertions with valueError Exeptions

* Reformatted
parent 9f53f049
...@@ -75,9 +75,10 @@ def set_recursively(hf_pointer, key, value, full_name, weight_type): ...@@ -75,9 +75,10 @@ def set_recursively(hf_pointer, key, value, full_name, weight_type):
else: else:
hf_shape = hf_pointer.shape hf_shape = hf_pointer.shape
assert ( if hf_shape != value.shape:
hf_shape == value.shape raise ValueError(
), f"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be {value.shape} for {full_name}" f"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be {value.shape} for {full_name}"
)
if weight_type == "weight": if weight_type == "weight":
hf_pointer.weight.data = value hf_pointer.weight.data = value
...@@ -145,28 +146,32 @@ def load_conv_layer(full_name, value, feature_extractor, unused_weights, use_gro ...@@ -145,28 +146,32 @@ def load_conv_layer(full_name, value, feature_extractor, unused_weights, use_gro
if type_id == 0: if type_id == 0:
if "bias" in name: if "bias" in name:
assert ( if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape:
value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape raise ValueError(
), f"{full_name} has size {value.shape}, but {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found." f"{full_name} has size {value.shape}, but {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found."
)
feature_extractor.conv_layers[layer_id].conv.bias.data = value feature_extractor.conv_layers[layer_id].conv.bias.data = value
logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}.") logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}.")
elif "weight" in name: elif "weight" in name:
assert ( if value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape:
value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape raise ValueError(
), f"{full_name} has size {value.shape}, but {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found." f"{full_name} has size {value.shape}, but {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found."
)
feature_extractor.conv_layers[layer_id].conv.weight.data = value feature_extractor.conv_layers[layer_id].conv.weight.data = value
logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}.") logger.info(f"Feat extract conv layer {layer_id} was initialized from {full_name}.")
elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm): elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
if "bias" in name: if "bias" in name:
assert ( if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape:
value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape raise ValueError(
), f"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was found." f"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was found."
)
feature_extractor.conv_layers[layer_id].layer_norm.bias.data = value feature_extractor.conv_layers[layer_id].layer_norm.bias.data = value
logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.") logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.")
elif "weight" in name: elif "weight" in name:
assert ( if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape:
value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape raise ValueError(
), f"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.weight.data.shape} was found." f"{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.weight.data.shape} was found."
)
feature_extractor.conv_layers[layer_id].layer_norm.weight.data = value feature_extractor.conv_layers[layer_id].layer_norm.weight.data = value
logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.") logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.")
else: else:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment