Fix broken PhotoMaker v1 path: version-aware num_tokens + pass pm_version="v1" where the v1 checkpoint is loaded - #231
Open
Linxiushen wants to merge 1 commit into
Conversation
…sion="v1" where the v1 checkpoint is loaded
load_photomaker_adapter() defaults pm_version to "v2" and hardcodes
self.num_tokens = 2, but the README quick start, both demo notebooks and
predict.py all download photomaker-v1.bin and call it without
pm_version. So the documented v1 flow loads a v1 checkpoint under the v2
code path: setup fails on the state dict, and once that is past, the v1
encoder produces one embedding per ID image while the pipeline reserved
two class-token slots, so PhotoMakerIDEncoder.forward raises
RuntimeError: Sizes of tensors must match except in dimension 1.
Expected size 2 but got size 1 for tensor number 1 in the list.
Make num_tokens version-aware (`2 if pm_version == "v2" else 1`, which
restores the pre-v2 v1 semantics) and pass pm_version="v1" at the four
call sites that load the v1 checkpoint: the README snippet, both
notebooks and predict.py.
The same hardcoded num_tokens is fixed in pipeline_controlnet.py and
pipeline_t2i_adapter.py for consistency; those two have no v1 caller in
the repo today, so they are latent rather than currently broken.
v2 behaviour is unchanged: the expression is identically 2 for "v2".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
load_photomaker_adapter()defaultspm_version="v2"and then hardcodesself.num_tokens = 2. But the v1 flow the README still documents, both demo notebooks, andpredict.pyall downloadphotomaker-v1.binand call the loader withoutpm_version, so a v1 checkpoint is loaded down the v2 code path. Two failures follow:PhotoMakerIDEncoder.forwardraises:So fixing only the version argument just moves the crash later; both parts are needed, which is why this is one PR rather than two.
Reachability, stated plainly: the README quick start (lines 108-160) and the two notebooks it links are the affected paths, and copying them fails immediately. I am not claiming the hosted Replicate endpoint is down: its image was built in 2024-01 from v1-era code and is frozen, and
cog.yamlpinsdiffusers==0.25.0, which cannot even import today'sphotomaker/pipeline.py(diffusers.callbacksonly exists from 0.28). Thepredict.pyline is still worth fixing, but not because production is broken.Fix
self.num_tokens = 2 if pm_version == "v2" else 1, which restores the v1 semantics the code had before the v2 commit ([class_token] * num_id_images).pm_version="v1"at the four call sites that loadphotomaker-v1.bin: the README snippet, both notebooks,predict.py.num_tokensis fixed inpipeline_controlnet.pyandpipeline_t2i_adapter.pyfor consistency. Stated plainly: those two have no v1 caller in the repo today (bothinference_pmv2_*scripts fetchphotomaker-v2.binand use thev2default), so they are latent, not currently broken.The loader signature and its
pm_version="v2"default are unchanged, so every v2 caller (the fourinference_pmv2*.pyscripts andgradio_demo/app_v2.py) is untouched.Verification
zsh verify.shin my scratch dir reproduces the whole thing; the important parts:num_id_imagesin {1,2,3,4,5,8} (30 combinations), comparingclean_input_idsandclass_tokens_maskbetween the pristine and patched trees: all identical. End-to-end forward through the real, unmodifiedPhotoMakerIDEncoder_CLIPInsightfaceExtendtokenwith a fixed seed: max absolute difference 0.0,torch.equaltrue.num_id_imagesof 1, 2, 4 and 8, the pristine tree raises theRuntimeErrorabove every time; patched, all four returnout=(1, 77, 2048).json.dumprewrite), so the diff is one added and one changed line each and both still parse.README.mdis CRLF and the patch preserves that.ruffwith the repo'spyproject.tomlsettings reports the same 221 findings before and after, with an identical per-rule breakdown; no new diagnostics.No test file: the repo has none,
requirements.txthas no pytest, and there is no CI, so testing this end to end would mean mocking the whole diffusers pipeline or downloading multi-GB weights. The differential script above covers it instead.Note for whoever picks this up: open PR #200 touches
photomaker/pipeline.pyheavily, so if that lands first this needs a trivial textual rebase.This fix was developed with AI assistance (Claude); the change was reviewed and verified locally before submission.