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 support of kwargs to Backbone.from_preset and fix the dtype forwarding in Task.from_preset #1742

Merged
merged 1 commit into from
Aug 9, 2024

Conversation

james77777778
Copy link
Collaborator

@james77777778 james77777778 commented Aug 7, 2024

Currently, CausalLM.from_preset(..., dtype="bfloat16") has no effect because it doesn't forward dtype to the backbone.
This PR fixes that issue and also adds dtype support to Backbone.from_preset.

Additionally, I have updated the logic in load_serialized_object to support dtype when using DTypePolicyMap, ensuring that the pre-quantized preset will obey dtype:

import keras_nlp

llama_lm = keras_nlp.models.CausalLM.from_preset(
    "llama2_instruct_7b_en_int8", load_weights=False, dtype="bfloat16"
)
assert llama_lm.backbone.token_embedding.compute_dtype == "bfloat16"

@james77777778 james77777778 changed the title Add dtype argument to Backbone.from_preset and fix the dtype forwarding in CausalLM.from_preset Add dtype argument to Backbone.from_preset and fix dtype forwarding in CausalLM.from_preset Aug 7, 2024
Copy link
Member

@mattdangerw mattdangerw left a comment

Choose a reason for hiding this comment

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

Looks good i think! just one small comment

backbone = load_serialized_object(preset, CONFIG_FILE)
# Forward `config_overrides` and `dtype`.
config_overrides = {}
if "config_overrides" in kwargs:
Copy link
Member

Choose a reason for hiding this comment

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

I believe the kwargs were supposed to ask as the config override directly (though it look like this broke at some point).

So if you wanted to set bert dropout, for example, you could do

model = keras_nlp.models.BertBackbone.from_preset("bert_base_en_uncased", dropout=0.5)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, it is a bit weird to see config_overrides and I think there is a dangerous default value (config_overrides={}) for it in load_serialized_object.

I can try to fix it.

@james77777778 james77777778 changed the title Add dtype argument to Backbone.from_preset and fix dtype forwarding in CausalLM.from_preset Add support of kwargs to Backbone.from_preset and fix the dtype forwarding in Task.from_preset Aug 8, 2024
@james77777778
Copy link
Collaborator Author

james77777778 commented Aug 8, 2024

@mattdangerw

I have updated Backbone.from_preset to support kwargs forwarding.
Now, this example works:

import keras_nlp


llama_lm = keras_nlp.models.CausalLM.from_preset(
    "llama2_instruct_7b_en_int8", load_weights=False, dtype="bfloat16"
)
assert llama_lm.backbone.token_embedding.compute_dtype == "bfloat16"

bert_backbone = keras_nlp.models.BertBackbone.from_preset(
    "bert_base_en_uncased", load_weights=False, dtype="bfloat16", dropout=0.5
)
assert bert_backbone.token_embedding.compute_dtype == "bfloat16"
assert bert_backbone.dropout == 0.5

Copy link
Member

@mattdangerw mattdangerw left a comment

Choose a reason for hiding this comment

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

Thanks! This LGTM

@mattdangerw mattdangerw merged commit f5676ee into keras-team:master Aug 9, 2024
7 checks passed
@james77777778 james77777778 deleted the fix-dtype branch August 10, 2024 08:19
divyashreepathihalli pushed a commit that referenced this pull request Aug 12, 2024
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.

2 participants