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

Add use_pretrained attribute for AutoTransformers #3498

Merged
merged 6 commits into from
Aug 5, 2023
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
8 changes: 7 additions & 1 deletion ludwig/schema/encoders/text_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -2574,7 +2574,7 @@ def module_name():
)

use_pretrained: bool = schema_utils.Boolean(
default=False,
default=True,
description="Whether to use the pretrained weights for the model. If false, the model will train from "
"scratch which is very computationally expensive.",
parameter_metadata=ENCODER_METADATA["FlauBERT"]["use_pretrained"],
arnavgarg1 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -3087,6 +3087,12 @@ def __post_init__(self):
def module_name():
return "AutoTransformer"

@property
def use_pretrained(self) -> bool:
# Always set this to True since we always want to use the pretrained weights
# We don't currently support training from scratch for AutoTransformers
return True

type: str = schema_utils.ProtectedString(
"auto_transformer",
description=ENCODER_METADATA["AutoTransformer"]["type"].long_description,
Expand Down
23 changes: 20 additions & 3 deletions tests/ludwig/encoders/test_text_encoders.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ def test_hf_ludwig_model_e2e(tmpdir, csv_filename, encoder_name):
"vocab_size": 30,
"min_len": 1,
"type": encoder_name,
"use_pretrained": True,
}
)
]
Expand Down Expand Up @@ -138,7 +137,6 @@ def test_hf_ludwig_model_reduce_options(tmpdir, csv_filename, encoder_name, redu
"vocab_size": 30,
"min_len": 1,
"type": encoder_name,
"use_pretrained": True,
"reduce_output": reduce_output,
},
)
Expand Down Expand Up @@ -210,7 +208,6 @@ def test_hf_ludwig_model_auto_transformers(tmpdir, csv_filename, pretrained_mode
"min_len": 1,
"type": "auto_transformer",
"pretrained_model_name_or_path": pretrained_model_name_or_path,
"use_pretrained": True,
},
)
]
Expand Down Expand Up @@ -292,3 +289,23 @@ def test_tfidf_encoder(vocab_size: int):
inputs = torch.randint(2, (batch_size, sequence_length)).to(DEVICE)
outputs = text_encoder(inputs)
assert outputs[ENCODER_OUTPUT].shape[1:] == text_encoder.output_shape


def test_hf_auto_transformer_use_pretrained():
"""This test ensures that use_pretrained is always True when using the auto_transformer text encoder even if a
user explicitly sets it to False."""
config = {
"input_features": [
text_feature(
encoder={
"type": "auto_transformer",
"use_pretrained": False,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ideally this should be an error if we were more strict with our config validation rules. We should instead just leave this out of the config.

"pretrained_model_name_or_path": "hf-internal-testing/tiny-random-bloom",
},
)
],
"output_features": [category_feature(decoder={"vocab_size": 2})],
}

model = LudwigModel(config=config, backend=LocalTestBackend())
assert model.config_obj.input_features[0].encoder.use_pretrained
Loading