You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feat(backends): populate mot.raw.response in HuggingFace _generate_from_raw #1331
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_valuesDynamicCache. 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.
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 @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.
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.
@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!
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?
Activity
cptnm3 commentedon Jul 31, 2026
@ajbozarth can I pick this one?
ajbozarth commentedon Jul 31, 2026
I have no issues with you picking this up @cptnm3 but @jakelorocco might have some insight on design
jakelorocco commentedon Jul 31, 2026
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!
cptnm3 commentedon Aug 5, 2026
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.responsewill have data populated forsequences,scoresandlogitsas they are row-indexable 2D tensors. The other keysattentions,hidden_statesandpast_key_valueswill be populated withNonealong with a debug level warning.I would like to know your thoughts on the above key choices.
attentionsandhidden_statesare nested tuples but this extraction would come at an added complexity - would you rather have these keys populated with added complexity?past_key_valuesis aDynamicCacheand there is no straight way of extracting per row value.cptnm3 commentedon Aug 5, 2026
To keep the memory in check, I propose to use the
viewsinstead of theclonesforsequences,scores, andlogitsthis will make sure that there is no duplication of data on memory. The views will hold the memory until the lifetime ofmot.raw.response.Since
_generate_from_rawdoes 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_rawmethod similar to cleanup inpost_processing. This would ensure onlymot.raw.responseholds the memory.cptnm3 commentedon Aug 5, 2026
@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!
jakelorocco commentedon Aug 5, 2026
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?
cptnm3 commentedon Aug 5, 2026
Thanks @jakelorocco! I'll open a PR with all these changes.