Skip to content

feat(backends): populate mot.raw.response in HuggingFace _generate_from_raw #1331

Closed

Description

@ajbozarth

Deferred from the mot.raw migration (#1329, closes #1215).

Summary

HuggingFaceBackend._generate_from_raw runs one batched model.generate() for all prompts and produces a single GenerateDecoderOnlyOutput. The per-MOT mot.raw.response is left None (only mot.raw.provider is set), because slicing a per-index view out of the batched output is non-trivial.

This makes HF inconsistent with the other four backends and with HF's own chat path:

  • Chat path (processing) sets mot.raw.response to that MOT's own GenerateDecoderOnlyOutput.
  • Raw path leaves it None.
  • ollama / openai / watsonx / litellm populate mot.raw.response on both paths.

Why it was deferred

A faithful per-MOT slice means indexing row i out of every batch-dimensioned field on GenerateDecoderOnlyOutput — sequences, the scores step-tuple, logits, attentions, hidden_states, and the past_key_values DynamicCache. The cache especially is not cleanly sliceable. That is error-prone surgery that did not belong in a mechanical _meta → mot.raw refactor.

(The original _generate_from_raw wrote nothing to _meta either, so leaving raw.response = None is faithful to prior behavior — no regression.)

Proposed work

Construct a per-MOT GenerateDecoderOnlyOutput (or a minimal equivalent) holding only sequence i, so raw-completion MOTs expose their backend-native response like every other backend/path. Decide how to handle the shared past_key_values cache (likely omit it from the per-MOT slice).

Acceptance

  • HF _generate_from_raw populates mot.raw.response with a per-MOT view.
  • Unit test asserting each batched MOT's raw.response corresponds to its own sequence.

Activity

  1. added
    enhancement
    area/backends
    on Jun 23, 2026
  2. added
    p2
    on Jun 24, 2026
  3. cptnm3 commented on Jul 31, 2026

    @cptnm3
    Contributor

    @ajbozarth can I pick this one?

  4. ajbozarth commented on Jul 31, 2026

    @ajbozarth
    MemberAuthor

    I have no issues with you picking this up @cptnm3 but @jakelorocco might have some insight on design

  5. jakelorocco commented on Jul 31, 2026

    @jakelorocco
    Contributor

    I don't have much insight here. I think this should be a best effort approach that flags any shortcomings clearly but doesn't try to over-engineer to get around any.

    We should also make sure that we don't inflate memory usage when doing this. The huggingface backend has always been a glut when it comes to memory usage and storage due to keeping track of all the tensors / cache. We should make sure that however the splitting happens, that we don't duplicate the data being split. Additionally, we need to make sure any caching / references get cleaned up correctly with the explicit cache clearing and garbage collecting done with the regular chat endpoint. The main point here is look at the regular chat endpoint, processing, and post-processing functions to make sure we keep the needed cleanup logic. We also should have some debugger code, etc... that will help with keeping track of the memory usage. I don't know where that is but we can point you to it when needed.

    Thanks for offering to pick up this work @cptnm3!

  6. cptnm3 commented on Aug 5, 2026

    @cptnm3
    Contributor

    Thanks @jakelorocco for your comments! I did look at the documentations for GenerateDecoderOnlyOutput, it has below structure -
    ( sequences: LongTensor scores: tuple[torch.FloatTensor] | None = None logits: tuple[torch.FloatTensor] | None = None attentions: tuple[tuple[torch.FloatTensor]] | None = None hidden_states: tuple[tuple[torch.FloatTensor]] | None = None past_key_values: transformers.cache_utils.Cache | None = None )
    I would say it is safe to slice only the fields that are safe and indexable. Set everything else to None on the per-MOT object. Which means the mot.raw.response will have data populated for sequences, scores and logits as they are row-indexable 2D tensors. The other keys attentions, hidden_states and past_key_values will be populated with None along with a debug level warning.

    I would like to know your thoughts on the above key choices. attentions and hidden_states are nested tuples but this extraction would come at an added complexity - would you rather have these keys populated with added complexity?
    past_key_values is a DynamicCache and there is no straight way of extracting per row value.

  7. cptnm3 commented on Aug 5, 2026

    @cptnm3
    Contributor

    To keep the memory in check, I propose to use the views instead of the clones for sequences, scores, and logits this will make sure that there is no duplication of data on memory. The views will hold the memory until the lifetime of mot.raw.response.

    Since _generate_from_raw does not use cache KV, we could run cleanup right after the existing loop for where this new construction logic will go in. We can clean up the named outputs within the _generate_from_raw method similar to cleanup in post_processing. This would ensure only mot.raw.response holds the memory.

  8. cptnm3 commented on Aug 5, 2026

    @cptnm3
    Contributor

    @jakelorocco please let me know if the above points meet your expectations. Please feel free to correct me if any of my analysis points are not accurate. Thank you!

  9. jakelorocco commented on Aug 5, 2026

    @jakelorocco
    Contributor

    Thank you for the detailed breakdown! That all sounds good to me. Ensuring we use views and don't copy the data works for me. I haven't looked at the copy and deepcopy logic for mots recently; when you make these changes, can you please add test for mots that ensures these views (and I suppose all the other huggingface specific fields here) get copied as we expect them?

  10. cptnm3 commented on Aug 5, 2026

    @cptnm3
    Contributor

    Thanks @jakelorocco! I'll open a PR with all these changes.

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

Metadata

Metadata

Assignees

Labels

area/backendsenhancementp2

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions