Skip to content

Plumb max_seq_len into llama model #2566

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

Closed
wants to merge 1 commit into from
Closed
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: 2 additions & 0 deletions examples/models/llama2/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def load_llama_model(
use_sdpa_with_kv_cache: bool = False,
weight_type: WeightType = WeightType.LLAMA,
verbose: bool = False,
max_seq_len: int = 128,
) -> "LlamaEdgeManager":
"""
A helper util that builds a Llama2 model. It returns a LlamaEdgeManager that
Expand All @@ -87,6 +88,7 @@ def load_llama_model(
use_kv_cache=use_kv_cache,
use_sdpa_with_kv_cache=use_sdpa_with_kv_cache,
fairseq2=weight_type == WeightType.FAIRSEQ2,
max_seq_len=max_seq_len,
)
state_dict = model.state_dict()
dtype = state_dict[next(iter(state_dict))].dtype
Expand Down
6 changes: 0 additions & 6 deletions examples/models/llama2/eval_llama_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,6 @@ def build_args_parser() -> argparse.ArgumentParser:
parser.add_argument(
"--limit", type=int, default=5, help="number of samples to evalulate"
)
parser.add_argument(
"--max_seq_length",
type=int,
default=100,
help="maximum length sequence to evaluate",
)

return parser

Expand Down
8 changes: 8 additions & 0 deletions examples/models/llama2/export_llama_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,13 @@ def build_args_parser() -> argparse.ArgumentParser:
help="Override the output filename of the saved pte model file.",
)

parser.add_argument(
"--max_seq_length",
type=int,
default=128,
help="maximum length sequence to evaluate",
)

parser.add_argument("-2", "--fairseq2", action="store_true")
parser.add_argument("-v", "--verbose", action="store_true")
parser.add_argument("-X", "--xnnpack", action="store_true")
Expand Down Expand Up @@ -511,6 +518,7 @@ def _prepare_for_llama_export(modelname: str, args) -> LlamaEdgeManager:
use_sdpa_with_kv_cache=args.use_sdpa_with_kv_cache,
weight_type=weight_type,
verbose=args.verbose,
max_seq_len=args.max_seq_length,
)
.set_output_dir(output_dir_path)
.set_metadata(args.metadata)
Expand Down
4 changes: 3 additions & 1 deletion examples/models/llama2/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def __init__(self, **kwargs):
if "use_sdpa_with_kv_cache" in kwargs
else False
)

self.max_seq_len = kwargs["max_seq_len"] if "max_seq_len" in kwargs else 128
# The example is using a dummy small model with random weights for demo purpose only.
# Follow the instruction in https://github.com/facebookresearch/llama to download the model
device = "cpu"
Expand Down Expand Up @@ -112,7 +114,7 @@ def __init__(self, **kwargs):
)
with open(params_path, "r") as f:
params = json.loads(f.read())
max_seq_len = 128
max_seq_len = self.max_seq_len
max_batch_size = 1
model_args: ModelArgs = ModelArgs(
max_seq_len=max_seq_len,
Expand Down