Skip to content
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
16 changes: 14 additions & 2 deletions examples/ingress/torch/hub_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
"""

import argparse
import faulthandler
import sys
from transformers import AutoModel
from lighthouse.ingress.torch import import_from_model


def load_from_hf(model_name: str) -> str:
def load_from_hf(model_name: str, download_timeout: int) -> str:
# The download can stall indefinitely (e.g. behind a proxy): exit with the
# stalled stack instead.
# The test fails on timeout.
faulthandler.dump_traceback_later(download_timeout, exit=True)
model = AutoModel.from_pretrained(model_name)
faulthandler.cancel_dump_traceback_later()
model.eval()
# Exporting to a static graph: drop the KV cache so the output is plain tensors.
if hasattr(model.config, "use_cache"):
Expand All @@ -35,8 +41,14 @@ def load_from_hf(model_name: str) -> str:
default="hf-internal-testing/tiny-random-BertModel",
help="Hugging Face model id (e.g. 'google-bert/bert-base-uncased').",
)
parser.add_argument(
"--download-timeout",
type=int,
default=30,
help="Seconds to wait for the model download before giving up.",
)
args = parser.parse_args()

print(f"Loading model from Hugging Face: {args.model}", file=sys.stderr)
mlir_module = load_from_hf(args.model)
mlir_module = load_from_hf(args.model, args.download_timeout)
print(mlir_module)
12 changes: 12 additions & 0 deletions lit.cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ def find_filecheck() -> str:
f"env PYTHONPATH={shlex.quote(pythonpath)} {python}",
)

# Keep network access in case of proxy usage.
for var in [
"http_proxy",
"https_proxy",
"no_proxy",
"HTTP_PROXY",
"HTTPS_PROXY",
"NO_PROXY",
]:
if var in os.environ:
config.environment[var] = os.environ[var]

for tool_dir, _, files in os.walk(project_root + "/tools"):
for file in files:
tool_path = os.path.join(tool_dir, file)
Expand Down
Loading