Repository navigation
strip padding from text latent cache for variable length models #1345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c34720d
strip padding from Z-Image text latent cache
BitcrushedHeart c325f4a
fix ruff lint errors: remove unused imports, sort import blocks
BitcrushedHeart 2c4ab4a
only strip/pad text cache when latent_caching is enabled
BitcrushedHeart e7fab80
Rename and move prune/pad modules to MGDS, extend to Chroma and Qwen
BitcrushedHeart 5853b35
Update mgds dependency to a new commit version
dxqb 13e06d5
Merge branch 'master' into zimagecache
dxqb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
|
BitcrushedHeart marked this conversation as resolved.
Resolved
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import torch | ||
| import torch.nn.functional as F | ||
| from mgds.PipelineModule import PipelineModule | ||
| from mgds.pipelineModuleTypes.RandomAccessPipelineModule import RandomAccessPipelineModule | ||
|
|
||
|
|
||
| class PadTokens( | ||
| PipelineModule, | ||
| RandomAccessPipelineModule, | ||
| ): | ||
| def __init__( | ||
| self, | ||
| tokens_name: str, | ||
| tokens_mask_name: str, | ||
| hidden_state_name: str, | ||
| max_length: int, | ||
| ): | ||
| super().__init__() | ||
| self.tokens_name = tokens_name | ||
| self.tokens_mask_name = tokens_mask_name | ||
| self.hidden_state_name = hidden_state_name | ||
| self.max_length = max_length | ||
|
|
||
| def length(self) -> int: | ||
| return self._get_previous_length(self.tokens_name) | ||
|
|
||
| def get_inputs(self) -> list[str]: | ||
| return [self.tokens_name, self.tokens_mask_name, self.hidden_state_name] | ||
|
|
||
| def get_outputs(self) -> list[str]: | ||
| return [self.tokens_name, self.tokens_mask_name, self.hidden_state_name] | ||
|
|
||
| def get_item(self, variation: int, index: int, requested_name: str = None) -> dict: | ||
| tokens = self._get_previous_item(variation, self.tokens_name, index) | ||
| tokens_mask = self._get_previous_item(variation, self.tokens_mask_name, index) | ||
| hidden_state = self._get_previous_item(variation, self.hidden_state_name, index) | ||
|
|
||
| current_length = tokens.shape[0] | ||
|
|
||
| if current_length >= self.max_length: | ||
| return { | ||
| self.tokens_name: tokens, | ||
| self.tokens_mask_name: tokens_mask, | ||
| self.hidden_state_name: hidden_state, | ||
| } | ||
|
|
||
| pad_length = self.max_length - current_length | ||
|
|
||
| tokens = F.pad(tokens, (0, pad_length), value=0) | ||
| tokens_mask = F.pad(tokens_mask, (0, pad_length), value=0) | ||
| # hidden_state shape: [N, hidden_dim] -> [max_length, hidden_dim] | ||
| hidden_state = F.pad(hidden_state, (0, 0, 0, pad_length), value=0.0) | ||
|
|
||
| return { | ||
| self.tokens_name: tokens, | ||
| self.tokens_mask_name: tokens_mask, | ||
| self.hidden_state_name: hidden_state, | ||
| } |
|
BitcrushedHeart marked this conversation as resolved.
Resolved
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import torch | ||
| from mgds.PipelineModule import PipelineModule | ||
| from mgds.pipelineModuleTypes.RandomAccessPipelineModule import RandomAccessPipelineModule | ||
|
|
||
|
|
||
| class StripPaddedTokens( | ||
|
BitcrushedHeart marked this conversation as resolved.
Resolved
|
||
| PipelineModule, | ||
| RandomAccessPipelineModule, | ||
| ): | ||
| def __init__( | ||
| self, | ||
| tokens_name: str, | ||
| tokens_mask_name: str, | ||
| hidden_state_name: str, | ||
| ): | ||
| super().__init__() | ||
| self.tokens_name = tokens_name | ||
| self.tokens_mask_name = tokens_mask_name | ||
| self.hidden_state_name = hidden_state_name | ||
|
|
||
| def length(self) -> int: | ||
| return self._get_previous_length(self.tokens_name) | ||
|
|
||
| def get_inputs(self) -> list[str]: | ||
| return [self.tokens_name, self.tokens_mask_name, self.hidden_state_name] | ||
|
|
||
| def get_outputs(self) -> list[str]: | ||
| return [self.tokens_name, self.tokens_mask_name, self.hidden_state_name] | ||
|
|
||
| def get_item(self, variation: int, index: int, requested_name: str = None) -> dict: | ||
| tokens = self._get_previous_item(variation, self.tokens_name, index) | ||
| tokens_mask = self._get_previous_item(variation, self.tokens_mask_name, index) | ||
| hidden_state = self._get_previous_item(variation, self.hidden_state_name, index) | ||
|
|
||
| bool_mask = tokens_mask.bool() | ||
|
|
||
| tokens = tokens[bool_mask] | ||
| tokens_mask = tokens_mask[bool_mask] | ||
| hidden_state = hidden_state[bool_mask] | ||
|
|
||
| return { | ||
| self.tokens_name: tokens, | ||
| self.tokens_mask_name: tokens_mask, | ||
| self.hidden_state_name: hidden_state, | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.