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

add gptfast decoder #11

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

add gptfast decoder #11

wants to merge 3 commits into from

Conversation

Sanster
Copy link

@Sanster Sanster commented Jun 4, 2024

Add decoder with static kv-cache from gpt-fast. Manually checked the results of the images in dataset/mini_pubtabnet/val, but have not actually run the acc/TEDS metrics on the test set.

Benchmark cell detection model with:

  • 3090
  • max_decode_len = 512
  • fp32
image

The main modifications in full_pipeline.ipynb.

  1. Specify which class to use for the decoder:
backbone = ImgLinearBackbone(d_model=d_model, patch_size=patch_size)
encoder = Encoder(
    d_model=d_model,
    nhead=nhead,
    dropout = dropout,
    activation="gelu",
    norm_first=True,
    nlayer=12,
    ff_ratio=4,
)
# decoder_class = Decoder
decoder_class = GPTFastDecoder
  1. Initialize the decoder in load_vocab_and_model, call map_state_dict when using GPTFastDecoder.
def load_vocab_and_model(..., decoder_class: Type[nn.Module]):
    decoder = decoder_class(
         d_model=d_model,
         nhead=nhead,
         dropout = dropout,
         activation="gelu",
         norm_first=True,
         nlayer=4,
         ff_ratio=4,   
    )
    model = EncoderDecoder(
        backbone=backbone,
        encoder=encoder,
        decoder=decoder,
        ...
    )

    state_dict = torch.load(model_weights, map_location="cpu")
    if isinstance(model.decoder, GPTFastDecoder):
        state_dict = map_state_dict(state_dict)
    
    model.load_state_dict(state_dict)
    model = model.to(device)
    return vocab, model
  1. In autoregressive_decode, if GPTFastDecoder is used, setup_caches needs to be called first.
def autoregressive_decode(...):
    model.eval()
    is_gpt_fast = isinstance(model.decoder, GPTFastDecoder)
    if is_gpt_fast:
        with torch.device(image.device):
            model.decoder.setup_caches(max_batch_size=image.shape[0], max_seq_length=max_decode_len, dtype=image.dtype)
    memory = model.encode(image)
    ...

@Sanster Sanster mentioned this pull request Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant