Skip to content

fix(serve): set DJL HF cache defaults before _prepare_for_mode - #6435

Open
AkshayShah03 wants to merge 2 commits into
aws:masterfrom
AkshayShah03:fix/djl-hf-cache-before-prepare
Open

AkshayShah03 wants to merge 2 commits into
aws:masterfrom
AkshayShah03:fix/djl-hf-cache-before-prepare

Conversation

@AkshayShah03

Copy link
Copy Markdown

Issue #, if available: Relates to #5698

Description of changes:

#5733 made the DJL builder default HF_HOME and HUGGINGFACE_HUB_CACHE to /tmp, because /opt/ml/model is read-only on the endpoint when artifacts are mounted there. The setdefault calls run after _prepare_for_mode, though. For SAGEMAKER_ENDPOINT, _prepare_for_mode merges the DJL server's default env into env_vars with setdefault:

_DEFAULT_ENV_VARS = {..., "HF_HOME": "/opt/ml/model/", "HUGGINGFACE_HUB_CACHE": "/opt/ml/model/", ...}

So the /tmp defaults never took effect, and the endpoint still caches Hub downloads under the read-only /opt/ml/model, the #5698 symptom. #5733's tests mock _prepare_for_mode, which hid this.

This change applies the /tmp defaults before _prepare_for_mode. User-provided values still win because both places use setdefault. TRANSFORMERS_CACHE (also /opt/ml/model/ in the DJL defaults) is left as is, matching #5733's scope.

Testing:

  • New test_endpoint_build_hf_cache_is_tmp_with_real_prepare_for_mode runs the real _prepare_for_mode with only the S3 upload mocked. It fails before this change: HF_HOME is /opt/ml/model/.
  • Server builder tests: no new failures. The same 15 tests fail on master.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

aws#5733 made the DJL builder default HF_HOME and HUGGINGFACE_HUB_CACHE to
/tmp, because /opt/ml/model is read-only when artifacts are mounted there.
The setdefault calls ran after _prepare_for_mode, which for
SAGEMAKER_ENDPOINT merges the DJL server's default env (HF_HOME and
HUGGINGFACE_HUB_CACHE = /opt/ml/model/) into env_vars with setdefault, so
the /tmp defaults never took effect and the endpoint still cached HF
downloads under the read-only /opt/ml/model.

Apply the /tmp defaults before _prepare_for_mode. The existing tests mock
_prepare_for_mode; the new test runs it for real (S3 upload mocked).

Relates to aws#5698
The DJL server defaults set TRANSFORMERS_CACHE, HF_HOME and
HUGGINGFACE_HUB_CACHE to /opt/ml/model/. The previous commit stops the
read-only /opt/ml/model/ default from winning for HF_HOME and
HUGGINGFACE_HUB_CACHE, but left TRANSFORMERS_CACHE at /opt/ml/model/.

transformers uses TRANSFORMERS_CACHE as the default explicit cache_dir for
from_pretrained, and an explicit cache_dir overrides HF_HUB_CACHE. So a
container that loads through transformers still writes its Hub cache under
the read-only /opt/ml/model/ even with HF_HOME and HUGGINGFACE_HUB_CACHE
pointing at /tmp. That matches aws#5698, where the reporter set both of those
by hand (which setdefault already honored) and still saw
"OSError: [Errno 30] Read-only file system: /opt/ml/model/models--..." .

Default TRANSFORMERS_CACHE to /tmp alongside the other two, with the same
setdefault so a user-provided value still wins.

Relates to aws#5698

This branch is waiting to be deployed

1 waiting deployment
manual-approval — 8cb59d4f Waiting Oct 23, 2026 by mohamedzeidan2021 via wait-for-approval #2113
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