TensorRT-LLMs/cpp/tensorrt_llm/runtime/loraModule.cpp
Kaiyu Xie 655524dd82
Update TensorRT-LLM (#1168)
* Update TensorRT-LLM

---------

Co-authored-by: Bhuvanesh Sridharan <bhuvan.sridharan@gmail.com>
Co-authored-by: Shixiaowei02 <39303645+Shixiaowei02@users.noreply.github.com>
2024-02-27 17:37:34 +08:00

62 lines
2.5 KiB
C++

/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION. All rights reserved.
*
* 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.
*/
#include "tensorrt_llm/runtime/loraModule.h"
namespace tensorrt_llm::runtime
{
std::vector<LoraModule> LoraModule::createLoraModules(std::vector<std::string> const& loraModuleNames,
SizeType hiddenSize, SizeType mlpHiddenSize, SizeType numAttentionHeads, SizeType numKvAttentionHeads,
SizeType attentionHeadSize, SizeType tpSize)
{
auto const hidden = hiddenSize * tpSize;
auto const mlpHidden = mlpHiddenSize * tpSize;
auto const numHeads = numAttentionHeads * tpSize;
auto const numKvHeads = numKvAttentionHeads * tpSize;
auto const attnHeadSize = attentionHeadSize;
std::vector<LoraModule> modules;
for (auto const& moduleName : loraModuleNames)
{
auto t = toModuleType(moduleName);
switch (t)
{
case ModuleType::kATTN_QKV:
case ModuleType::kCROSS_ATTN_QKV:
modules.emplace_back(
LoraModule(t, hidden, (numHeads * attnHeadSize + 2 * numKvHeads * attnHeadSize), false, true, -1, 0));
break;
case ModuleType::kATTN_Q:
case ModuleType::kATTN_K:
case ModuleType::kATTN_V:
case ModuleType::kCROSS_ATTN_Q:
case ModuleType::kCROSS_ATTN_K:
case ModuleType::kCROSS_ATTN_V: modules.emplace_back(t, hidden, hidden, false, true, -1, 0); break;
case ModuleType::kATTN_DENSE:
case ModuleType::kCROSS_ATTN_DENSE: modules.emplace_back(t, hidden, hidden, false, true, 1, -1); break;
case ModuleType::kMLP_H_TO_4H: modules.emplace_back(t, hidden, mlpHidden, false, true, -1, 0); break;
case ModuleType::kMLP_GATE: modules.emplace_back(t, hidden, mlpHidden, false, true, -1, 0); break;
case ModuleType::kMLP_4H_TO_H: modules.emplace_back(t, mlpHiddenSize, hidden, false, true, 1, -1); break;
case ModuleType::kINVALID: throw std::runtime_error("Invalid loRA module " + moduleName);
}
}
return modules;
}
} // namespace tensorrt_llm::runtime