Skip to content

Adapter code is undocumented and over-specialized to Intrinsics. #423

Closed

Description

@nrfulton

This issue might be capturing my own misconcptions about the current state of the code-base.

I'm opening the issue to get feedback from @jakelorocco and @frreiss. It's possible that I am misunderstanding things and all that's needed is better documentation. If that's the case, then we can probably iterate on this issue description to explain how things work (or should work).

If I state anything false or misleading in this issue, please quote and explain what I have wrong.

The Immediate Problem

While fixing issues related to our m tune command (see PR #422 ), I found that none of our custom alora stuff has worked since the intrinsics change.

(These examples aren't in the code-base's test suite because they require training and loading a custom model; we should find a way to mock that someone in the future.)

The cause for the break is as follows:

  • all requirement checking adapters must be intrinsics, but
  • all intrinsics must be listed in _INTRINSICS_CATALOG_ENTRIES, and
  • there is no way to add custom stuff to _INTRINSICS_CATALOG_ENTRIES (and even if there were that would be ugly).

The failure point is in the Intrinsic.__init__ call within the AloraRequirement initializer:

        Intrinsic.__init__(
            self,
            intrinsic_name=intrinsic_name,
            intrinsic_kwargs={"requirement": f"{self.description}"},
        )

which calls adapters.catalog.fetch_intrinsic_metadata(intrinsic_name: str) function, which does a lookup against the _INTRINSICS_CATALOG_ENTRIES table. This means that you cannot use any adapter for requirement checking that is not part of the _INTRINSICS_CATALOG_ENTRIES table. That's a problem because we might want custom lora/alora requirement checkers that are implemented as adapters (as in the canonical stembolt example).

I tried to hack around this by adding to the catalog, but that also failed because the adapter must be a GraniteCommonAdapter (huggingface.py:354), which opened another whole can of worms.

Larger Issue

After digging through the current Intrinsic code-base, I think this is a symptom of a larger problem. We have not done a proper job at documenting the architectural choices associated with intrinsics and adapters. We may have also made some bad decisions, but I'm not sure because I am still not quite sure how everything is supposed to work.

I'm capturing some other alora questions here, which aren't directly related to getting m tune working again.

  1. It's unclear to me how you're supposed to write the logic for activating a generic adapter without turning it into an intrinsic. Our current training code doesn't auto-generate the JSONs for eg activation sequences (trivial to fix), and I think you might also need to modify granite.common itself in order to use a custom adapter, at least for our current code base (less trivial to fix / does require some redesign). In any case, it should be possible to train an adapter and use it to implement a component's generation without bundling it as an intrinsic. Open to being
  2. We tout that the primary advantage of aloras is that you re-use kv cache, but then implement aloras in terms of chat rewrites. If you rewite prior history, then you have to recompute kv cache anyways. So why not use a LoRA?
  3. Our code currently allows the use of multiple aLoRAs against a single model. The original paper states that this is not possible. Have changed since then / do we have changes in the pipeline to enable this? If not, we should enforce the invariant that only one alora may be loaded / used with any particular backend. You must unload the old alora before loading the new one, and that triggers a cache invalidation.

Activity

  1. nrfulton commented on Feb 8, 2026

    @nrfulton
    MemberAuthor

    Note: there are other hacky solutions, or perhaps even principled incremental (small bug fix) solutions. The larger issue I'm trying to point out is that the way that this whole system works is opaque and not very well documented.

  2. changed the title [-]Cannot use custom adapters because Intrinsics and Adapters are conflated[/-] [+]Cannot use custom adapters because Intrinsics and Adapters are conflated, and missing documentation[/+] on Feb 8, 2026
  3. added
    bug
    documentation
    question
    on Feb 8, 2026
  4. nrfulton commented on Feb 8, 2026

    @nrfulton
    MemberAuthor

    Below I capture a proposed re-design. I want to first address the top-level concerns; it may not make sense to proceed down this path, so I'm burying it in a comment for now.

    Proposed Solution

    we've conflated intrinsics and adapters, or perhaps prioritized intrinsics over adapters.

    I think that the actual substantive difference between an intrinsic and any other (alora) adapter is that intrinsics ship with three things:

    1. A name
    2. JSON file telling you how to load and activate the intrinsic
    3. some chat rewrite logic implement in granite-common (looked up by name)

    So "Intrinsic", in the current code-base, is just a really name for concept of a JSON file format and unique name which together entail a checkpoint, an activation sequence, and some chat rewrite logic. This means that it's a specific implementation of an Adapter.

    AFAICT, there is no way to use a LoRA or aLoRA adapter that isn't shipped exactly the way that granite intrinsics are shipped. This is a problem because approximately all current users of LoRAs do not do things in the granite intrinsics way. For this reason, our code base should treat intrinsics as a special case of adapters -- not the other way around.

    In mu proposed alternative design, we have Adapters and AdapterBackedComponents. Intrinsics are just special cases of both that implement the Adapter interface / behavior using the name, JSON, granite.common impl discussed above.

    My belief is that

    Intrinsic <: Adapter
    

    and any action : Component backed by an intrinsic is ultimately backed by a particular type of Adapter, so there does not even need to exist an IntrinsicBackedComponent. Those are just AdapterBackedComponents whose adapters happen to be of type Intrinsic. I.e., what we currently call an Intrinsic is just a AdapterBackedComponent whose adapter happens to be an intrinsic.

    I'll briefly sketch the adapter lifecycle and protocol, both because that's the basis of my belief and because we should flesh this out and document it somewhere (probably in docs/rewrite/adapters_and_intrinsics.md with corresponding examples).

    Adapter Lifecycle

    Adapters have a lifecycle that's mostly identical to how things work now. Parts of this protocol might need to be implemented in the backend rather than in the intrinsic. I'm not sure exactly what the split is; I think @jakelorocco has the most context to answer that question.

    Loading (done by backends)

    load
        * as-is
    
    unload
        * as-is
    

    Activation (done by backends)

    activate
        * as-is
    
    detect_activation
        * detects when an adapter has been activated by a token stream. Optional for now since we don't have this functionality atm. But we should design for it.
    

    Deactivation (done by backends)

    deactivate
        * as-is
    
    detect_deactivation
        * detects when an adapter has been deactivated by a token stream. Optional for now since we don't have this functionality atm. 
    

    Preparing (done by Adapaters or backends? IDK)

    Adapter.rewrite(action, context, backend) -> action, context
    
    Adapter.parse_result <- not necessary, used parsed_repr for this???
    

    Specifics of Proposed Change

    AdapterBackedComponent

    The only way to use an adapter (therefore, intrinsic) is by passing an AdapaterBackedComponent as the action.

    The AdapterBackedComponent is just a Component with an adapter : str | Adapter property. The union type works like our model_ids -- if it's a str then we look it up in the _INTRINSICS_CATALOG_ENTRIES but otherwise we use the adapter.

    For example, from the current code base, we would need ALoRARequirement <: AdapaterBackedComponent with adapter="requirement_checking" by default.

    This solves the lookup problem that led to my deep-dive of the curretn adapter/intrinsics code base.

    Intrinsics

    There is an IntrinsicAdapter <: Adapter which uses the Intrinsic JSON format to implement each of the methods. the backend might also dispatch on this subtype to do special handling if necessary (although I think this should not be necessary because Adapter.rewrite should do everything that's intrinsic-specific).

    For example, there is no

    Backend._generate_from_intrinsic
    

    There is only

    Backend._generate_from_adapter_backed_component
    

    which operates over things that implement the AdapterBackedComponent interface using things that implement the Adapter interface.

    @jakelorocco please let me know what you think.

    Other design considerations:

    1. Adapters can only be added to a single backend. Add lifecycle checks that enforce this invariant.
    2. Adapters have only a single type. Is this correct? Or should Adapters have types, like Intrinsic does now? (This is an additional code smell that we're conflating a few different things, imo.)
  5. changed the title [-]Cannot use custom adapters because Intrinsics and Adapters are conflated, and missing documentation[/-] [+]Adapter code is over-specialized to Intrinsics.[/+] on Feb 8, 2026
  6. changed the title [-]Adapter code is over-specialized to Intrinsics.[/-] [+]Adapter code is undocumented and over-specialized to Intrinsics.[/+] on Feb 8, 2026
  7. nrfulton commented on Feb 8, 2026

    @nrfulton
    MemberAuthor

    I went ahead and hacked up some dicts and upload a model to huggingface, leaving a trace of issues as I worked. I now need a io.yaml file but cannot figure out what's supposed to go in those files or what the semantics are even after reviewing all of the ones in the rag lib. Do we have infra for bundling and uploading these intrinsic huggingface repo things?

    Edit: figured out the io.yaml format. It looks like all intrinsics must return json; is that correct?

  8. frreiss commented on Feb 10, 2026

    @frreiss
    Collaborator

    Edit: figured out the io.yaml format. It looks like all intrinsics must return json; is that correct?

    Yes, the returned value must adhere to a JSON Schema schema that is compatible with XGrammar constrained decoding. Attempts to use other output formats have resulted in considerable pain.

  9. frreiss commented on Feb 10, 2026

    @frreiss
    Collaborator

    3. Our code currently allows the use of multiple aLoRAs against a single model. The original paper states that this is not possible. Have changed since then / do we have changes in the pipeline to enable this? If not, we should enforce the invariant that only one alora may be loaded / used with any particular backend. You must unload the old alora before loading the new one, and that triggers a cache invalidation.

    vLLM supports multiple LoRA adapters within a single batch, and the aLoRA fork of vLLM extends this capability to activated LoRA. The aLoRA fork can reuse cached KV prefixes from the base model while generating new tokens with multiple aLoRA adapters in parallel.

    If you are using Hugging Face Transformers for inference, performance is going to be poor regardless of the caching strategy. There is too much latency from alternating between GPU, C++ CPU, and Python CPU computation.

  10. nrfulton commented on Feb 10, 2026

    @nrfulton
    MemberAuthor

    Yes, the returned value must adhere to a JSON Schema schema that is compatible with XGrammar constrained decoding. Attempts to use other output formats have resulted in considerable pain.

    Reasonable; my experience with existing models is similar. The LLMON folks are making good progress, so this appears to be an artifact of training regimes rather than a fundamental limitation (no surprises there, just observing.)

    In the meantime: currently granite common doesn't enforce a grammar but does always insist on the output at least being valid json due to a json.loads call (or similar) during post-processing.

    This means that we can't use the intrinsics code with a lora/alora that was not trained on JSON.

    Could we do one of the following:

    1. include something in io.yaml that specifies "no schema"
    2. Or, interpret template: null as "no grammatical constraint"

    Either way works for me.

    I'm open to counter-arguments.

    We can also move this discussion to granite-common.

  11. nrfulton commented on Feb 10, 2026

    @nrfulton
    MemberAuthor

    vLLM supports multiple LoRA adapters within a single batch, and the aLoRA fork of vLLM extends this capability to activated LoRA. The aLoRA fork can reuse cached KV prefixes from the base model while generating new tokens with multiple aLoRA adapters in parallel.

    Thanks for the rundown, that's helpful.

    But after talking with a few people I realized that the load/unload isn't necessary, because if you want to marshal stuff between aloras I guess you do that like this (pseudocode with hallucinated method names obviously):

    let base_model be a model that prefill : toks -> KV and generate : KV -> toks * KV
    let alora_1 and alora_2 be the base model with different aloras enabled.
    
    kv_0 = base_model.prefill(input_toks)
    toks_from_alora_1, kv_1 = alora_1.generate(kv_0)
    
    # key point: need to prefill on base model with output from alora 1 if you want to include the generation from alora_1 when calling alora_2. But this is okay; it's just a prefill
    kv_2 = base_model.prefill(toks_from_alora_1)
    toks_from_alora_2 = alora_1.generate(kv_0)

    If you are using Hugging Face Transformers for inference, performance is going to be poor regardless of the caching strategy. There is too much latency from alternating between GPU, C++ CPU, and Python CPU computation.

    Yeah, we know.

    We maintain the transformers backend because it is still a lingua franc for many researchers, and we want to be able to build out runtime features against model capabilities that aren't yet available in llama.cpp or ollama or vllm. Not because we expect deployment against that backend.

    The thing I was observing was the cost of the additional prefill below the "key point" comment above, which is essential and not wrt any particular engine.

  12. nrfulton commented on Feb 11, 2026

    @nrfulton
    MemberAuthor

    Additional note: I am not sure how Intrinsic kwargs are supposed to work. Are these the arguments to the intrinsic?

    In any case, we should probably intantiate an IntrinsicThingie (maybe Intrinsic) which is not a Componet but has a __call__ which returns an IntrinsicCall which is a Component.

    But I'm not sure. I'm working an example and will include comments there (102_example.py in the examples/aLora directory, will be merged tmw).

    But I'm definitely open to correction on this. And it's just an ergonomics thing.

    Having the type variable for Component bound at the definition of Intrinsic is also unfortunate, because many intrinsic have a template and an object with fields corresponding to that template should be the parsed_repr type.

  13. jakelorocco commented on Feb 11, 2026

    @jakelorocco
    Contributor

    Additional note: I am not sure how Intrinsic kwargs are supposed to work. Are these the arguments to the intrinsic?

    In any case, we should probably intantiate an IntrinsicThingie (maybe Intrinsic) which is not a Componet but has a __call__ which returns an IntrinsicCall which is a Component.

    But I'm not sure. I'm working an example and will include comments there (102_example.py in the examples/aLora directory, will be merged tmw).

    But I'm definitely open to correction on this. And it's just an ergonomics thing.

    Yes, intrinsic kwargs are kwargs that are passed into the rewrite / transform method when generating using an intrinsic. They are "necessary" for the intrinsic to function correctly. For instance, a requirement check alora needs it's requirement passed as a kwarg to that transform function.

    I would have to see an example of the IntrinsicThingie -> IntrinsicCall to weigh in. I'm not exactly sure what this means / would look like. Our current implementation of the Intrinsic / rag calls was done to have a more "functional" approach. But we can define specific classes for each Intrinsic if desired.

    Having the type variable for Component bound at the definition of Intrinsic is also unfortunate, because many intrinsic have a template and an object with fields corresponding to that template should be the parsed_repr type.

    Agreed, this is an unfortunate limitation of python's type system. We can change the way intrinsics are defined by

    • moving the necessary Intrinsic capabilities to a separate class
    • create a generic IntrinsicComponent which acts as Intrinsic does now (ie as a component with type str)
    • for specific Intrinsics that have complex return types, you would inherit directly from (Component, Intrinsic)
  14. psschwei commented on May 12, 2026

    @psschwei
    Contributor

    superseded

  15. psschwei commented on May 12, 2026

    @psschwei
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugdocumentationquestion

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions