InternLM / InternLM/lmdeploy

[Feature] accelerate QwenEncoder

Open
#3,185 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
8.1k
Forks
748
Avg merge
6d 2h
Merged PRs (30d)
54

Description

### Motivation

I'm doing wok of acceleration of QwenEncoder like the issue in https://github.com/InternLM/lmdeploy/issues/2958.
I use version of 0.6.4 because the decoder is remove in future version.
Library version:
transformers:4.46.3
lmdeploy:0.6.4

### Related resources

The sample code is below.

```
import os
import os.path as osp
import torch
from torch import nn
from lmdeploy import turbomind as tm
from transformers import Qwen2ForCausalLM
import numpy as np
os.environ['TM_LOG_LEVEL'] = 'ERROR'

class Qwen2Encoder(torch.nn.Module):
def __init__(self, pretrain_path):
super().__init__()
print('class Qwen2Encoder(torch.nn.Module): pretrain_path', pretrain_path)
self.model = Qwen2ForCausalLM.from_pretrained(pretrain_path)

def forward_one_step(self, xs, masks, cache=None):
input_masks = masks[:, -1, :]
outs = self.model(
inputs_embeds=xs,
attention_mask=input_masks,
output_hidden_states=True,
return_dict=True,
use_cache=True,
past_key_values=cache,
)
xs = outs.hidden_states[-1]
new_cache = outs.past_key_values
logits = outs.logits

return xs, new_cache, logits

def hf(model_path):
qwen2encoder = Qwen2Encoder(model_path)
lm_input = torch.from_numpy(np.zeros((1, 153, 896))).float()
cache=None

y_pred, cache, logits = qwen2encoder.forward_one_step(lm_input,
masks=torch.tril(torch.ones((1, lm_input.shape[1], lm_input.shape[1]), device=lm_input.device)).to(torch.bool),
cache=cache)
print(logits.shape, logits)

torch.Size([1, 153, 151936]) tensor([[[ 0.9940, 0.8763, -1.3542, ..., -3.0364, -3.0343, -3.0368],
[ 0.9941, 0.8763, -1.3542, ..., -3.0364, -3.0343, -3.0368],
[ 0.9941, 0.8763, -1.3542, ..., -3.0364, -3.0343, -3.0368],
...,
[ 0.9941, 0.8763, -1.3542, ..., -3.0364, -3.0343, -3.0368],
[ 0.9940, 0.8763, -1.3542, ..., -3.0364, -3.0343, -3.0368],
[ 0.9941, 0.8763, -1.3542, ..., -3.0364, -3.0343, -3.0368]]],
grad_fn=)

def main(model_path):
tm_model = tm.TurboMind.from_pretrained(model_path)
generator = tm_model.create_instance()

input_ids = [[]]
input_embeddings = [np.squeeze(np.zeros((1, 153, 896)))] # (1, 153, 896)
input_ids = [0] * 153
input_embedding_ranges = [[0, 153]]
logits = generator.decode(input_ids, steps=[0], input_embeddings=input_embeddings,input_embedding_ranges=input_embedding_ranges, sequence_start=True, sequence_end=False)
print(logits.shape, logits)

if __name__ == '__main__':
hf('E:\work\CosyVoice\pretrained_models\CosyVoice2-0.5B\CosyVoice-BlankEN')

main('E:\work\CosyVoice\pretrained_models\CosyVoice2-0.5B\CosyVoice-BlankEN')
```
and the output:
torch.Size([1, 153, 151936]) tensor([[[ 0.9940, 0.8763, -1.3542, ..., -3.0364, -3.0343, -3.0368],
[ 0.9941, 0.8763, -1.3542, ..., -3.0364, -3.0343, -3.0368],
[ 0.9941, 0.8763, -1.3542, ..., -3.0364, -3.0343, -3.0368],
...,
[ 0.9941, 0.8763, -1.3542, ..., -3.0364, -3.0343, -3.0368],
[ 0.9940, 0.8763, -1.3542, ..., -3.0364, -3.0343, -3.0368],
[ 0.9941, 0.8763, -1.3542, ..., -3.0364, -3.0343, -3.0368]]],
grad_fn=)

[WARNING] gemm_config.in is not found; using default GEMM algo
torch.Size([1, 153, 151936]) tensor([[[ 0.9601, 1.3395, -0.7853, ..., -3.5119, -3.5102, -3.5118],
[ 0.9601, 1.3395, -0.7853, ..., -3.5119, -3.5102, -3.5118],
[ 0.9601, 1.3395, -0.7853, ..., -3.5119, -3.5102, -3.5118],
...,
[ 0.9601, 1.3395, -0.7853, ..., -3.5119, -3.5102, -3.5118],
[ 0.9601, 1.3395, -0.7853, ..., -3.5119, -3.5102, -3.5118],
[ 0.9601, 1.3395, -0.7853, ..., -3.5119, -3.5102, -3.5118]]],
device='cuda:0')

The shape is the same, however,the output is different.
The model can be found here: https://huggingface.co/FunAudioLLM/CosyVoice2-0.5B/tree/main

How could I change my code to make the output same?

### Additional context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.