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 tests for qwen + allow uninitialized weights in Llama model #8552

Open
wants to merge 6 commits into
base: jz/export_qwen
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .ci/scripts/gather_test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def model_should_run_on_event(model: str, event: str) -> bool:
We put higher priority and fast models to pull request and rest to push.
"""
if event == "pull_request":
return model in ["mv3", "vit"]
return model in ["mv3", "vit", "qwen2_5"] # TODO: remove, just to test the ci
elif event == "push":
# These are super slow. Only run it periodically
return model not in ["dl3", "edsr", "emformer_predict"]
Expand Down
12 changes: 11 additions & 1 deletion .ci/scripts/test_model.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,17 @@ test_model() {
# Install requirements for llama vision.
bash examples/models/llama3_2_vision/install_requirements.sh
fi
# python3 -m examples.portable.scripts.export --model_name="llama2" should works too
if [[ "${MODEL_NAME}" == "qwen2_5" ]]; then
# Install requirements for export_llama
bash examples/models/llama/install_requirements.sh
# Test export_llama script: python3 -m examples.models.llama.export_llama.
# Use Llama random checkpoint with Qwen 2.5 1.5b model configuration.
"${PYTHON_EXECUTABLE}" -m examples.models.llama.export_llama --model "${MODEL_NAME}" -c examples/models/llama/params/demo_rand_params.pth -p examples/models/qwen2_5/1_5b_config.json
rm "./${MODEL_NAME}.pte"
return # Skip running with portable executor runnner since portable doesn't support Qwen's biased linears.
fi

# Export a basic .pte and run the model.
"${PYTHON_EXECUTABLE}" -m examples.portable.scripts.export --model_name="${MODEL_NAME}" "${STRICT}"
run_portable_executor_runner
}
Expand Down
1 change: 1 addition & 0 deletions examples/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"resnet50": ("resnet", "ResNet50Model"),
"llava": ("llava", "LlavaModel"),
"efficient_sam": ("efficient_sam", "EfficientSAM"),
"qwen2_5": ("qwen2_5", "Qwen2_5Model"),
}

__all__ = [
Expand Down
2 changes: 2 additions & 0 deletions examples/models/llama/export_llama_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,15 @@
verbosity_setting = None


# All models that leverage the transformer architecture defined in llama_transformer.py.
EXECUTORCH_DEFINED_MODELS = [
"stories110m",
"llama2",
"llama3",
"llama3_1",
"llama3_2",
"static_llama",
"qwen2_5",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I accidentally deleted the original comment about ordering, but I was going to say that I think this is clearer to list all the llama models first

]
TORCHTUNE_DEFINED_MODELS = ["llama3_2_vision"]

Expand Down
25 changes: 17 additions & 8 deletions examples/models/llama/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,23 @@ def __init__(self, **kwargs):
eviction_batch_size=eviction_batch_size,
)

# assign=True: load params/buffers by assignment instead of performing an in-place copy.
# Because we are using device="meta", tensors do not have memory associated with them
# and an in-place copy is a no-op. Use assign=True in load_state_dict for this scenario.
missing, unexpected = self.model_.load_state_dict(
checkpoint,
strict=False,
assign=True,
) # self.model_ = Transformer(gptconf)
missing, unexpected = None, None
try:
# assign=True: load params/buffers by assignment instead of performing an in-place copy.
# Because we are using device="meta", tensors do not have memory associated with them
# and an in-place copy is a no-op. Use assign=True in load_state_dict for this scenario.
missing, unexpected = self.model_.load_state_dict(
checkpoint,
strict=False,
assign=True,
) # self.model_ = Transformer(gptconf)
except RuntimeError as e:
Comment on lines +240 to +249
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So it doesn't error out when loading examples/models/llama/params/demo_rand_params.pth or any checkpoint that is incompatible with the model architecture. We also have no way to not specify a checkpoint, I looked into removing the default val for that arg but it's going to take some work since it's relied on internally in a lot of places

print(
"Could not load checkpoint into mode, defaulting to random uninitialized weights."
)
print(f"Error: {e}")
# Need to provide concrete (empty) values for meta-initialized tensors for quantization.
self.model_.to_empty(device="cpu")

if missing:
missing_weights = [fqn for fqn in missing if fqn.endswith(".weight")]
Expand Down
14 changes: 14 additions & 0 deletions examples/models/qwen2_5/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

from executorch.example.models.llama.model import Llama2Model


class Qwen2_5Model(Llama2Model):
def __init__(self, **kwargs):
super().__init__(**kwargs)


__all__ = [
"Qwen2_5Model",
]
Loading