-
Notifications
You must be signed in to change notification settings - Fork 23
[llama4 -> llama4_jax] Refactor to be a proper installable Python package #9
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
6fd621f
2286f87
d5c936a
3812791
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ __pycache__/ | |
build/** | ||
|
||
.venv | ||
.vscode | ||
.vscode |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[project] | ||
name = "llama4_jax" | ||
version = "0.1.0" | ||
description = "" | ||
description = "Pure JAX implementation of Llama 4 inference, including a checkpoint converter for the weights." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
authors = [ | ||
{ name = "Robert Dyro" }, | ||
] | ||
|
@@ -13,6 +13,7 @@ dependencies = [ | |
"jax", | ||
"torch", | ||
"transformers", # for the model config and the tokenizer | ||
"tune_jax", | ||
"tqdm", | ||
"numpy", | ||
"orbax-checkpoint", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,24 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import sys | ||
from pathlib import Path | ||
from pprint import pprint | ||
from argparse import ArgumentParser | ||
import dataclasses | ||
import os.path | ||
import shutil | ||
|
||
try: | ||
from llama4_jax import model as l4jax | ||
from llama4_jax import chkpt_utils as utils | ||
except ImportError: | ||
sys.path.append(str(Path(__file__).parent.absolute())) | ||
|
||
from llama4_jax import model as l4jax | ||
from llama4_jax import chkpt_utils as utils | ||
|
||
from transformers import AutoConfig | ||
from safetensors import safe_open | ||
from tqdm import tqdm | ||
|
||
from llama4_jax import model as l4jax | ||
from llama4_jax import chkpt_utils as utils | ||
|
||
|
||
def main(model_path: str | Path, ckpt_path: str | Path): | ||
model_path, ckpt_path = Path(model_path).expanduser(), Path(ckpt_path).expanduser() | ||
files = list(model_path.glob("**/*safetensors")) | ||
files = list(model_path.glob(os.path.join("**", "*safetensors"))) | ||
assert len(files) > 1 | ||
config_files = list(model_path.glob("**/config.json")) | ||
config_files = list(model_path.glob(os.path.join("**", "config.json"))) | ||
assert len(config_files) == 1, "Must have only one `config.json` file in the model path" | ||
config = AutoConfig.from_pretrained(config_files[0]).text_config | ||
cfg = l4jax.hf_to_jax_config(config) | ||
|
@@ -44,9 +37,9 @@ def main(model_path: str | Path, ckpt_path: str | Path): | |
|
||
additional_files = ["config.json", "tokenizer.json", "tokenizer_config.json"] | ||
for additional_file in additional_files: | ||
full_paths = list(model_path.glob(f"**/{additional_file}")) | ||
full_paths = list(model_path.glob(os.path.join("**", additional_file))) | ||
|
||
if len(full_paths) != 1: | ||
print(f"Found more than 1 file for {additional_file}") | ||
print("Found more than 1 file for", additional_file) | ||
if len(full_paths) == 0: | ||
continue | ||
full_path = full_paths[0] | ||
|
@@ -56,11 +49,12 @@ def main(model_path: str | Path, ckpt_path: str | Path): | |
if __name__ == "__main__": | ||
parser = ArgumentParser() | ||
parser.add_argument( | ||
"--source-path", default="~/DeepSeek-R1-Distill-Llama-70B", required=True, help="HF model directory path" | ||
"--source-path", default=os.path.join(os.path.expanduser("~"), "DeepSeek-R1-Distill-Llama-70B"), | ||
required=True, help="HF model directory path" | ||
) | ||
parser.add_argument( | ||
"--dest-path", | ||
default="~/DeepSeek-R1-Distill-Llama-3.1-70B-Instruct", | ||
default=os.path.join(os.path.expanduser("~"), "DeepSeek-R1-Distill-Llama-3.1-70B-Instruct"), | ||
required=True, | ||
help="JAX model model directory (to be created).", | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import os | ||
from argparse import ArgumentParser | ||
from pathlib import Path | ||
|
||
|
@@ -12,19 +12,20 @@ | |
|
||
|
||
def main(model_id: str, dest_root_path: str | Path): | ||
local_dir = Path(dest_root_path).expanduser().absolute() / str(model_id).replace("/", "--") | ||
local_dir = Path(dest_root_path).expanduser().absolute() / str(model_id).replace(os.path.sep, "--") | ||
snapshot_download(repo_id=model_id, local_dir=local_dir) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = ArgumentParser() | ||
parser.add_argument( | ||
"--model-id", required=True, help=f"HuggingFace model / repo id. Examples include: {example_models}" | ||
"--model-id", required=True, | ||
help=f"HuggingFace model / repo id. Examples include: {example_models}" | ||
) | ||
parser.add_argument( | ||
"--dest-root-path", | ||
required=True, | ||
default="~/", | ||
default=os.path.join(os.path.expanduser("~"), ""), | ||
|
||
help="Destination root directory, the model will be saved into its own directory.", | ||
) | ||
args = parser.parse_args() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,10 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import sys | ||
import os.path | ||
from pathlib import Path | ||
from argparse import ArgumentParser | ||
|
||
try: | ||
from llama4_jax import chkpt_utils as utils | ||
except ImportError: | ||
sys.path.append(str(Path(__file__).parent.absolute())) | ||
|
||
from llama4_jax import chkpt_utils as utils | ||
from llama4_jax import chkpt_utils as utils | ||
|
||
|
||
def main(path: str | Path, suffix: str): | ||
|
@@ -21,7 +16,8 @@ def main(path: str | Path, suffix: str): | |
if __name__ == "__main__": | ||
parser = ArgumentParser() | ||
parser.add_argument( | ||
"--path", default="~/DeepSeek-R1-Distill-Llama-70B", required=True, help="Existing JAX model checkpoint path" | ||
"--path", default=os.path.join(os.path.expanduser("~"), "DeepSeek-R1-Distill-Llama-70B"), | ||
|
||
required=True, help="Existing JAX model checkpoint path" | ||
) | ||
parser.add_argument( | ||
"--suffix", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep
main.py
separate, I think an explicit script might be more clear that it's just a starting point for a larger program rather than default behavior for the llama4 moduleThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rdyro It's your call. But it is very nonstandard Python, the
__main__.py
properly integrates… seeing itself as within Python module; being relocatable; and installable.One other idea is to create a hierarchy:
All packages are installable. There are no
main.py
s. Outside ofjax_examples_cli
there are no__main__.py
s. In the Command Line Interface it finds installed packages and/or packages within a specific location (e.g.,os.getcwd()
orJAX_LLM_EXAMPLES
env var); indicating where to find packages compatible with thejax_examples_cli/__main__.py
.Already your
main.py
s are very similar, it wouldn't be hard to hoist them up. Usage would be:jax_examples_cli --model <deepseek-r1-jax | llama4_jax> --ckpt_path …