Skip to content

fix(cli): match m alora add-readme to the repository's adapter layout - #1861

Open
markstur wants to merge 12 commits into
generative-computing:mainfrom
markstur:fix/issue-1620
Open

markstur wants to merge 12 commits into
generative-computing:mainfrom
markstur:fix/issue-1620

Conversation

@markstur

@markstur markstur commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request

Thanks for sending a pull request! Please make sure you read the contributing guidelines.

Issue

Fixes #1620

Description

m alora upload writes weights flat (at the repository root) by default, but m alora add-readme always generated intrinsic usage code that only loads from <name>/<base-model>/<lora|alora>/. Following train → upload → add-readme therefore produced a README whose own example failed with ValueError: Intrinsic … not found.

add-readme now looks at the repository and documents code that works for the layout it finds. upload's default is unchanged.

Behavior

  • New --layout auto|intrinsic|flat (default auto).
    • auto lists the repository's files (read-only, with a 5-second timeout) and classifies it. Nested weights get the existing intrinsic sample. Weights at the root get a plain PEFT sample that builds the training prompt, generates, and decodes only the reply. If the layout can't be determined (repo not created yet, README-only, no token, Hub error) it asks, or exits non-zero before generating when there is no terminal.
    • intrinsic and flat skip detection and make no Hub requests before the upload.
  • Intrinsic repos: checks the directory the loader will read for --basemodel (via adapter_subpath, taking the base model's last path component as LocalHFBackend.base_model_name does). If no weights are there it warns and still generates. The matching directory gives LoRA vs aLoRA, so the sample no longer always says aLoRA. The runnable block builds its backend from --basemodel instead of a hardcoded model.
  • Flat repos: reads the root adapter_config.json (one small file) to say LoRA vs aLoRA, from the value of alora_invocation_tokens, since PEFT writes the key as null for a plain LoRA. If it can't be read, the README makes no claim. The README, the generator prompt and the upload commit message stop calling a flat repo an intrinsic.
  • Hub access: each request is announced before it is made. The config is downloaded into a temporary directory, so nothing is left in the shared Hugging Face cache. Not-found no longer claims the repository is missing, because the Hub reports private repositories the same way.
  • Flat upload prints a warning pointing at --intrinsic --io-yaml.
  • Docs: the upload guide now states that a plain upload cannot be loaded as a Mellea adapter function, next to the existing --intrinsic instruction.

Not in this PR: changing upload's default, the unused --io-yaml on add-readme (#1860), the upload --intrinsic LoRA/alora slot bug (#1873), and README generation failing for hyphenated or org-namespace repo names (#1881).

Both points raised in the #1620 discussion are resolved there: the extra adapter_config.json read for flat repos was approved, and the shared base-model-name helper was deliberately left for later.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code was added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Local run: 647 tests pass across test/cli and the CLI reference generator tests; ruff and mypy are clean; the new lines are at 100% coverage. The flat sample was executed against tiny offline LoRA and aLoRA adapters, the config download against a local fake Hub, and the listing timeout against a silent local server. Not run: the real Hub, a real Granite model, or CI.

Attribution

  • AI coding assistants used

Adding a new component, requirement, sampling strategy, or tool?

If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.

🤖 Generated with Claude Code

`m alora upload` defaults to a flat layout (weights at the repo root), but
`m alora add-readme` always generated intrinsic usage code that only finds
weights under `<name>/<base-model>/<lora|alora>/`. Following
train -> upload -> add-readme therefore produced a README whose own sample
failed with `ValueError: Intrinsic '<name>' ... not found`.

`add-readme` now detects the repository layout and renders matching code:
the intrinsic layout keeps the existing Intrinsic/LocalFileBinding sample,
while a flat repo documents plain PEFT loading and explains how to re-upload
as an intrinsic. When the layout cannot be verified (repo not created yet or
a Hub error), it asks which layout is intended; `--layout` skips detection.

Flat uploads also print a warning pointing at `--intrinsic --io-yaml`.

Closes generative-computing#1620

Assisted-by: Claude Code
Signed-off-by: Mark Sturdevant <[email protected]>
…is no terminal

With `--layout auto`, an undetectable repository layout (repo not created yet,
README-only, no token, Hub error) falls back to asking the user. Without a
terminal, `input()` hangs or raises EOFError, and only after the slow README
generation has already run.

Exit with a usage error before generating, naming `--layout intrinsic|flat`.
An explicit `--layout`, or a detectable repository, never needs a terminal.

Assisted-by: Claude Code
Signed-off-by: Mark Sturdevant <[email protected]>
…ment the right adapter type

Layout classification only checks that files sit under `<name>/`. The loader
resolves `<name>/<base model name>/<lora|alora>/` from the backend's base
model, so a nested upload made for a different base model passed the check and
still failed to load.

Check the loader's own directories (via `adapter_subpath`) for `--basemodel` and
warn, naming them, when no weights are there. The README is still generated.
The base model is looked up by its last path component, as
`LocalHFBackend.base_model_name` does and `m alora upload --intrinsic` writes,
so `myorg/my-finetune` is checked as `my-finetune`.

The directory that matches also identifies the adapter type, so the generated
sample no longer documents every adapter as aLoRA: the binding, `Intrinsic`,
identity, and qualified name follow the detected type. Without detection the
type stays `alora`.

Assisted-by: Claude Code
Signed-off-by: Mark Sturdevant <[email protected]>
The flat-layout sample stopped after `from_pretrained`, so it ran and showed
nothing. Adapters are trained on a fixed prompt shape ending in an invocation
string, and an aLoRA only activates from that string onward, so calling
`generate()` on plain text looks like a broken adapter.

The sample now builds the training prompt (item, "Requirement:" line,
invocation string), generates, and decodes only the reply. The item is the
first dataset row formatted as training formats it, and the invocation string
is read from `--promptfile` the way `m alora train` reads it. Both are embedded
as Python literals, so quotes and braces in the data stay valid.

For a flat repo, read the root `adapter_config.json` (one small download) to
say whether the adapter is a LoRA or an aLoRA, and make no claim if it cannot
be read. PEFT writes `alora_invocation_tokens` for every LoRA config and nulls
it unless activated, so the value decides, not the key's presence.

Assisted-by: Claude Code
Signed-off-by: Mark Sturdevant <[email protected]>
A flat repository is not usable as an intrinsic, but the README generated
for it still called it one in three places: the LLM prompt, which steers the
description the model writes ("a machine learning intrinsic adapter"), the
upload commit message ("Upload intrinsic README."), and the progress line
("Generated INTRINSIC_README.md" for a file named README.md).

For the flat layout, say "adapter" in the prompt and "Upload README." in the
commit; the progress line is now neutral for both layouts. The intrinsic layout
keeps its wording. The `intrinsic_name` field names stay, since the output
schema and template use them as identifiers.

Assisted-by: Claude Code
Signed-off-by: Mark Sturdevant <[email protected]>
The runnable block of the intrinsic README sample built
`LocalHFBackend(IBM_GRANITE_4_1_3B)` whatever `--basemodel` was. The loader
looks for weights under the backend's base model, so for an adapter trained on
anything else the README's own code failed with the same "not found" error as a
flat upload.

Render `--basemodel` into the sample as a Python literal instead, and use the
same literal for the flat sample's tokenizer, so a path with quotes or
backslashes stays valid.

Assisted-by: Claude Code
Signed-off-by: Mark Sturdevant <[email protected]>
The test README says never to write `@pytest.mark.unit`: conftest applies it to
every test without an `integration`, `e2e`, `qualitative`, or `llm` marker. The
alora add-readme and layout tests wrote it explicitly anyway. Remove it from the
tests this branch added; the four already in test_alora_readme_template.py
predate this branch and are left alone. The tests selected by `-m unit` and
`-m integration` across these files are unchanged.

Assisted-by: Claude Code
Signed-off-by: Mark Sturdevant <[email protected]>
…ache behind

`--layout auto` makes read-only Hugging Face requests before anything is
generated or confirmed, but said nothing until they returned, and the flat-repo
config download landed in the shared Hugging Face cache.

Download `adapter_config.json` into a temporary directory that is removed
afterwards, and print a line before the repository listing and before that
download. Say in the `--layout` help, the command docstring, and the example
README that `auto` makes these requests and an explicit `--layout` makes none.

Hugging Face also reports a private repository the token cannot see as "not
found", so stop claiming the repository does not exist; say it may not exist
or the token may lack access.

Assisted-by: Claude Code
Signed-off-by: Mark Sturdevant <[email protected]>
…code

Close the coverage gaps in the new add-readme code: the flat `upload` warning,
the layout question's retry, a failed repository listing, the real terminal
check, and `generate_readme` end to end for both layouts, including session
cleanup when generation fails.

Remove two things coverage showed could never run in `_inspect_repo`: a return
that only an impossible layout value could reach, and an ImportError guard that
duplicates the one `alora_add_readme` already applies before calling it.

Assisted-by: Claude Code
Signed-off-by: Mark Sturdevant <[email protected]>
`list_repo_files` takes no timeout, so a connection that accepts and then
goes quiet (a dead proxy, a captive portal) hung `add-readme` before anything
was generated. Ask for the file list through `model_info` with a 5 second
limit instead — the same list, via `siblings` — and say so when it times out,
falling through to asking which layout to use like any other unverifiable
case. Also state in the docstring that without a token `auto` skips the
requests and asks, and that the token is only required to upload.

Assisted-by: Claude Code
Signed-off-by: Mark Sturdevant <[email protected]>
The upload section says to pass `--intrinsic` "if you intend to use the adapter
as a Mellea adapter function" but never states the consequence of not passing
it: the weights land at the repository root, where `LocalFileBinding` does not
look. State that in the same paragraph, so the "Use the adapter in Mellea"
section below is known to assume an `--intrinsic` upload.

Assisted-by: Claude Code
Signed-off-by: Mark Sturdevant <[email protected]>
PEFT's `save_pretrained` overloads declare `str`, unlike transformers',
which accepts paths. The `Path` argument failed the repo-wide mypy that
the quality job runs, a check my earlier runs scoped to `cli/alora` missed.

Assisted-by: Claude Code
Signed-off-by: Mark Sturdevant <[email protected]>

@planetf1 planetf1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This follows the #1620 discussion closely. The flat sample builds the training prompt and decodes only the reply, the exact-directory check uses adapter_subpath, the listing has a 5-second timeout with tests, and the flat-repo framing is gone. The only things I'd still change are the three doc points inline: the no-token and no-terminal behaviour in the --layout help, the Prerequisites line, and the example README. Everything else I'd merge as is. The upload --intrinsic LoRA/alora bug stays with #1873.

Comment thread cli/alora/commands.py
Comment on lines +373 to +376
help="Adapter layout of the target repository: 'auto' looks it up on "
"Hugging Face with read-only requests and asks when it cannot be "
"verified, 'intrinsic' is <name>/<base-model>/<lora|alora>/, and 'flat' "
"is the repository root. 'intrinsic' and 'flat' make no requests.",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The help covers intrinsic and flat making no requests, but not the two cases that decide what auto does without them. Could it say that without a token auto skips the Hub requests and asks which layout you intend to upload, and that with no terminal it exits and asks for --layout? The docstring has both, but --help is where scripted users will look.

Comment thread cli/alora/commands.py
instead; a token is only required to upload the README.

Prerequisites:
Hugging Face token available (set `HF_TOKEN` or run `hf auth login`).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Hugging Face token available" reads as required from the start, but the paragraph above (line 397) says a token is only needed to upload. Could this say "needed to upload the README, and for --layout auto to inspect the repository", so the two agree?

```

The generator will display the README and ask for confirmation before uploading it to your Hugging Face repo. You can also call the generator programmatically from Python — see `example_readme_generator.py` for an example.
The generator will display the README and ask for confirmation before uploading it to your Hugging Face repo. It detects whether the repository uses the intrinsic layout (`--intrinsic`) or a flat upload, and documents matching usage code; if the repository does not exist yet, it asks which layout you intend to upload. Detection makes read-only requests to Hugging Face with your token before anything is generated or uploaded: a listing of the repository's files and, for a flat repository, its small `adapter_config.json`, which is downloaded to a temporary directory and removed. Pass `--layout intrinsic` or `--layout flat` to skip detection and those requests. You can also call the generator programmatically from Python — see `example_readme_generator.py` for an example.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This describes detection with your token, but not what happens without one or without a terminal. A sentence would close it: without a token, or when the layout can't be determined, it asks which layout you intend to upload, and with no terminal it exits and asks for --layout intrinsic or --layout flat.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

m alora upload's default layout is incompatible with add-readme's generated usage code

2 participants