mirror of
https://github.com/NVIDIA/TensorRT-LLM.git
synced 2026-02-03 01:31:30 +08:00
304 lines
14 KiB
Python
304 lines
14 KiB
Python
# SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import torch
|
|
|
|
import tensorrt_llm
|
|
from tensorrt_llm.quantization import QuantMode
|
|
|
|
|
|
def fromfile(dir_path, name, shape=None, dtype=None):
|
|
p = dir_path + '/' + name
|
|
if Path(p).exists():
|
|
t = np.fromfile(p, dtype=dtype)
|
|
if shape is not None:
|
|
t = t.reshape(shape)
|
|
return t
|
|
return None
|
|
|
|
|
|
def sinusoids(length, channels, max_timescale=10000):
|
|
"""Returns sinusoids for positional embedding"""
|
|
assert channels % 2 == 0
|
|
log_timescale_increment = np.log(max_timescale) / (channels // 2 - 1)
|
|
inv_timescales = torch.exp(-log_timescale_increment *
|
|
torch.arange(channels // 2))
|
|
scaled_time = torch.arange(length)[:, np.newaxis] * inv_timescales[
|
|
np.newaxis, :]
|
|
return torch.cat([torch.sin(scaled_time), torch.cos(scaled_time)], dim=1)
|
|
|
|
|
|
def trans_weight(weight):
|
|
return np.ascontiguousarray(weight)
|
|
|
|
|
|
def load_encoder_weight(tensorrt_llm_whisper, model_metadata: dict,
|
|
model_params: dict, n_layer: int):
|
|
tensorrt_llm.logger.info('Loading encoder weights from PT...')
|
|
|
|
quant_mode = getattr(tensorrt_llm_whisper, 'quant_mode', QuantMode(0))
|
|
if quant_mode.is_int8_weight_only():
|
|
plugin_weight_only_quant_type = torch.int8
|
|
elif quant_mode.is_int4_weight_only():
|
|
plugin_weight_only_quant_type = torch.quint4x2
|
|
|
|
# Do we use INT4/INT8 weight-only?
|
|
use_weight_only = quant_mode.is_weight_only()
|
|
|
|
tensorrt_llm_whisper.positional_embedding.value = sinusoids(
|
|
model_metadata['n_audio_ctx'], model_metadata['n_audio_state']).numpy()
|
|
|
|
tensorrt_llm_whisper.conv1.weight.value = torch.unsqueeze(
|
|
model_params['encoder.conv1.weight'], -1).numpy()
|
|
tensorrt_llm_whisper.conv1.bias.value = model_params[
|
|
'encoder.conv1.bias'].numpy()
|
|
tensorrt_llm_whisper.conv2.weight.value = torch.unsqueeze(
|
|
model_params['encoder.conv2.weight'], -1).numpy()
|
|
tensorrt_llm_whisper.conv2.bias.value = model_params[
|
|
'encoder.conv2.bias'].numpy()
|
|
|
|
for i in range(n_layer):
|
|
tensorrt_llm_whisper.encoder_layers[
|
|
i].attention_layernorm.weight.value = model_params[
|
|
'encoder.blocks.' + str(i) + '.attn_ln.weight'].numpy()
|
|
tensorrt_llm_whisper.encoder_layers[
|
|
i].attention_layernorm.bias.value = model_params[
|
|
'encoder.blocks.' + str(i) + '.attn_ln.bias'].numpy()
|
|
|
|
t = torch.cat([
|
|
model_params['encoder.blocks.' + str(i) + '.attn.query.weight'],
|
|
model_params['encoder.blocks.' + str(i) + '.attn.key.weight'],
|
|
model_params['encoder.blocks.' + str(i) + '.attn.value.weight']
|
|
],
|
|
dim=0).numpy()
|
|
|
|
if t is not None:
|
|
dst = tensorrt_llm_whisper.encoder_layers[i].attention.qkv.weight
|
|
if use_weight_only:
|
|
processed_torch_weights, torch_weight_scales = torch.ops.fastertransformer.symmetric_quantize_last_axis_of_batched_matrix(
|
|
torch.tensor(np.ascontiguousarray(t.transpose(1, 0))),
|
|
plugin_weight_only_quant_type)
|
|
# workaround for trt not supporting int8 inputs in plugins currently
|
|
dst.value = processed_torch_weights.view(
|
|
dtype=torch.float32).numpy()
|
|
scales = tensorrt_llm_whisper.encoder_layers[
|
|
i].attention.qkv.per_channel_scale
|
|
scales.value = torch_weight_scales.numpy()
|
|
else:
|
|
dst.value = t
|
|
|
|
bias_shape = model_params['encoder.blocks.' + str(i) +
|
|
'.attn.query.bias'].shape
|
|
dtype = model_params['encoder.blocks.' + str(i) +
|
|
'.attn.query.bias'].dtype
|
|
fused_bias = torch.cat([
|
|
model_params['encoder.blocks.' + str(i) + '.attn.query.bias'],
|
|
torch.zeros([*bias_shape], dtype=dtype),
|
|
model_params['encoder.blocks.' + str(i) + '.attn.value.bias']
|
|
],
|
|
dim=0).numpy()
|
|
tensorrt_llm_whisper.encoder_layers[
|
|
i].attention.qkv.bias.value = fused_bias
|
|
|
|
t = trans_weight(model_params['encoder.blocks.' + str(i) +
|
|
'.attn.out.weight'].numpy())
|
|
if t is not None:
|
|
dst = tensorrt_llm_whisper.encoder_layers[i].attention.dense.weight
|
|
if use_weight_only:
|
|
processed_torch_weights, torch_weight_scales = torch.ops.fastertransformer.symmetric_quantize_last_axis_of_batched_matrix(
|
|
torch.tensor(np.ascontiguousarray(t.transpose(1, 0))),
|
|
plugin_weight_only_quant_type)
|
|
# workaround for trt not supporting int8 inputs in plugins currently
|
|
dst.value = processed_torch_weights.view(
|
|
dtype=torch.float32).numpy()
|
|
scales = tensorrt_llm_whisper.encoder_layers[
|
|
i].attention.dense.per_channel_scale
|
|
scales.value = torch_weight_scales.numpy()
|
|
else:
|
|
dst.value = t
|
|
tensorrt_llm_whisper.encoder_layers[
|
|
i].attention.dense.bias.value = trans_weight(
|
|
model_params['encoder.blocks.' + str(i) +
|
|
'.attn.out.bias'].numpy())
|
|
|
|
tensorrt_llm_whisper.encoder_layers[
|
|
i].mlp_layernorm.weight.value = model_params[
|
|
'encoder.blocks.' + str(i) + '.mlp_ln.weight'].numpy()
|
|
tensorrt_llm_whisper.encoder_layers[
|
|
i].mlp_layernorm.bias.value = model_params['encoder.blocks.' +
|
|
str(i) +
|
|
'.mlp_ln.bias'].numpy()
|
|
|
|
t = trans_weight(model_params['encoder.blocks.' + str(i) +
|
|
'.mlp.0.weight'].numpy())
|
|
if t is not None:
|
|
dst = tensorrt_llm_whisper.encoder_layers[i].mlp.fc.weight
|
|
if use_weight_only:
|
|
processed_torch_weights, torch_weight_scales = torch.ops.fastertransformer.symmetric_quantize_last_axis_of_batched_matrix(
|
|
torch.tensor(np.ascontiguousarray(t.transpose(1, 0))),
|
|
plugin_weight_only_quant_type)
|
|
# workaround for trt not supporting int8 inputs in plugins currently
|
|
dst.value = processed_torch_weights.view(
|
|
dtype=torch.float32).numpy()
|
|
scales = tensorrt_llm_whisper.encoder_layers[
|
|
i].mlp.fc.per_channel_scale
|
|
scales.value = torch_weight_scales.numpy()
|
|
else:
|
|
dst.value = t
|
|
tensorrt_llm_whisper.encoder_layers[i].mlp.fc.bias.value = trans_weight(
|
|
model_params['encoder.blocks.' + str(i) + '.mlp.0.bias'].numpy())
|
|
|
|
t = trans_weight(model_params['encoder.blocks.' + str(i) +
|
|
'.mlp.2.weight'].numpy())
|
|
if t is not None:
|
|
dst = tensorrt_llm_whisper.encoder_layers[i].mlp.proj.weight
|
|
if use_weight_only:
|
|
processed_torch_weights, torch_weight_scales = torch.ops.fastertransformer.symmetric_quantize_last_axis_of_batched_matrix(
|
|
torch.tensor(np.ascontiguousarray(t.transpose(1, 0))),
|
|
plugin_weight_only_quant_type)
|
|
# workaround for trt not supporting int8 inputs in plugins currently
|
|
dst.value = processed_torch_weights.view(
|
|
dtype=torch.float32).numpy()
|
|
scales = tensorrt_llm_whisper.blocks[i].mlp2.per_channel_scale
|
|
scales.value = torch_weight_scales.numpy()
|
|
else:
|
|
dst.value = t
|
|
tensorrt_llm_whisper.encoder_layers[
|
|
i].mlp.proj.bias.value = trans_weight(
|
|
model_params['encoder.blocks.' + str(i) +
|
|
'.mlp.2.bias'].numpy())
|
|
|
|
tensorrt_llm_whisper.ln_post.weight.value = model_params[
|
|
'encoder.ln_post.weight'].numpy()
|
|
tensorrt_llm_whisper.ln_post.bias.value = model_params[
|
|
'encoder.ln_post.bias'].numpy()
|
|
|
|
|
|
def fuse_qkv(q, k, v):
|
|
qkv_weight = np.concatenate((q, k, v))
|
|
return qkv_weight
|
|
|
|
|
|
def load_decoder_weight(
|
|
tllm_model,
|
|
model_params: dict,
|
|
):
|
|
tllm_model.embedding.vocab_embedding.weight.value = trans_weight(
|
|
model_params['decoder.token_embedding.weight'].numpy())
|
|
tllm_model.lm_head.weight.value = trans_weight(
|
|
model_params['decoder.token_embedding.weight'].numpy())
|
|
if tllm_model.embedding.position_embedding:
|
|
tllm_model.embedding.position_embedding.weight.value = trans_weight(
|
|
model_params['decoder.positional_embedding'].numpy())
|
|
|
|
for i in range(tllm_model.num_layers):
|
|
layer = tllm_model.decoder_layers[i]
|
|
|
|
# self attn
|
|
layer.self_attention.qkv.weight.value = fuse_qkv(
|
|
trans_weight(model_params['decoder.blocks.' + str(i) +
|
|
'.attn.query.weight'].numpy()),
|
|
trans_weight(model_params['decoder.blocks.' + str(i) +
|
|
'.attn.key.weight'].numpy()),
|
|
trans_weight(model_params['decoder.blocks.' + str(i) +
|
|
'.attn.value.weight'].numpy()))
|
|
layer.self_attention.dense.weight.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) +
|
|
'.attn.out.weight'].numpy())
|
|
|
|
if tllm_model.has_attention_qkvo_bias:
|
|
bias_shape = model_params['decoder.blocks.' + str(i) +
|
|
'.attn.query.bias'].shape
|
|
dtype = model_params['decoder.blocks.' + str(i) +
|
|
'.attn.query.bias'].dtype
|
|
layer.self_attention.qkv.bias.value = fuse_qkv(
|
|
trans_weight(model_params['decoder.blocks.' + str(i) +
|
|
'.attn.query.bias'].numpy()),
|
|
torch.zeros([*bias_shape], dtype=dtype).numpy(),
|
|
trans_weight(model_params['decoder.blocks.' + str(i) +
|
|
'.attn.value.bias'].numpy()))
|
|
layer.self_attention.dense.bias.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) +
|
|
'.attn.out.bias'].numpy())
|
|
|
|
layer.self_attention_layernorm.weight.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) +
|
|
'.attn_ln.weight'].numpy())
|
|
layer.self_attention_layernorm.bias.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) + '.attn_ln.bias'].numpy())
|
|
|
|
# cross attn
|
|
cross_attn_qkv_weight = fuse_qkv(
|
|
trans_weight(model_params['decoder.blocks.' + str(i) +
|
|
'.cross_attn.query.weight'].numpy()),
|
|
trans_weight(model_params['decoder.blocks.' + str(i) +
|
|
'.cross_attn.key.weight'].numpy()),
|
|
trans_weight(model_params['decoder.blocks.' + str(i) +
|
|
'.cross_attn.value.weight'].numpy()))
|
|
layer.cross_attention.qkv.weight.value = cross_attn_qkv_weight
|
|
layer.cross_attention.dense.weight.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) +
|
|
'.cross_attn.out.weight'].numpy())
|
|
|
|
if tllm_model.has_attention_qkvo_bias:
|
|
bias_shape = model_params['decoder.blocks.' + str(i) +
|
|
'.cross_attn.query.bias'].shape
|
|
dtype = model_params['decoder.blocks.' + str(i) +
|
|
'.cross_attn.query.bias'].dtype
|
|
cross_attn_qkv_bias = fuse_qkv(
|
|
trans_weight(model_params['decoder.blocks.' + str(i) +
|
|
'.cross_attn.query.bias'].numpy()),
|
|
torch.zeros([*bias_shape], dtype=dtype).numpy(),
|
|
trans_weight(model_params['decoder.blocks.' + str(i) +
|
|
'.cross_attn.value.bias'].numpy()))
|
|
|
|
layer.cross_attention.qkv.bias.value = cross_attn_qkv_bias
|
|
|
|
layer.cross_attention.dense.bias.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) +
|
|
'.cross_attn.out.bias'].numpy())
|
|
|
|
layer.cross_attention_layernorm.weight.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) +
|
|
'.cross_attn_ln.weight'].numpy())
|
|
layer.cross_attention_layernorm.bias.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) +
|
|
'.cross_attn_ln.bias'].numpy())
|
|
layer.mlp.fc.weight.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) + '.mlp.0.weight'].numpy())
|
|
layer.mlp.proj.weight.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) + '.mlp.2.weight'].numpy())
|
|
|
|
if tllm_model.has_mlp_bias:
|
|
layer.mlp.fc.bias.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) +
|
|
'.mlp.0.bias'].numpy())
|
|
layer.mlp.proj.bias.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) +
|
|
'.mlp.2.bias'].numpy())
|
|
|
|
layer.mlp_layernorm.weight.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) + '.mlp_ln.weight'].numpy())
|
|
layer.mlp_layernorm.bias.value = trans_weight(
|
|
model_params['decoder.blocks.' + str(i) + '.mlp_ln.bias'].numpy())
|
|
|
|
if tllm_model.final_layernorm:
|
|
tllm_model.final_layernorm.weight.value = trans_weight(
|
|
model_params['decoder.ln.weight'].numpy())
|
|
tllm_model.final_layernorm.bias.value = trans_weight(
|
|
model_params['decoder.ln.bias'].numpy())
|