Skip to content
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

[Feature] Add AttentionContext #730

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion xtuner/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright (c) OpenMMLab. All rights reserved.
from .attention_context import AttentionContext
from .constants import (DEFAULT_IMAGE_TOKEN, DEFAULT_PAD_TOKEN_INDEX,
IGNORE_INDEX, IMAGE_TOKEN_INDEX)
from .stop_criteria import StopWordStoppingCriteria
Expand All @@ -7,5 +8,5 @@
__all__ = [
'IGNORE_INDEX', 'DEFAULT_PAD_TOKEN_INDEX', 'PROMPT_TEMPLATE',
'DEFAULT_IMAGE_TOKEN', 'SYSTEM_TEMPLATE', 'StopWordStoppingCriteria',
'IMAGE_TOKEN_INDEX'
'IMAGE_TOKEN_INDEX', 'AttentionContext'
]
51 changes: 51 additions & 0 deletions xtuner/utils/attention_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from mmengine.utils import ManagerMixin


class MessageHub(ManagerMixin):

def __init__(self, name: str = '', **kwargs):
super().__init__(name, **kwargs)
self._cumulative_len = None
self._max_seqlen = None

def update(self, seqlen_list):
cumulative_len = [0]
max_seqlen = 0
for seqlen in seqlen_list:
cumulative_len.append(cumulative_len[-1] + seqlen)
max_seqlen = max(max_seqlen, seqlen)
self._cumulative_len = cumulative_len
self._max_seqlen = max_seqlen

def clear(self):
self._cumulative_len = None
self._max_seqlen = None

@property
def cumulative_len(self):
return self._cumulative_len

@property
def max_seqlen(self):
return self._max_seqlen


class AttentionContext:

message_hub = MessageHub.get_instance('attention_context')

@classmethod
def update(cls, seqlen_list):
cls.message_hub.update(seqlen_list)

@classmethod
def clear(cls):
cls.message_hub.clear()

@classmethod
def get_max_seqlen(cls):
return cls.message_hub.max_seqlen

@classmethod
def get_cumulative_len(cls):
return cls.message_hub.cumulative_len
Loading