File size: 3,248 Bytes
4a28d4d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
#include "src/turbomind/engine/model_executor.h"
#include <memory>
#include "src/turbomind/core/allocator.h"
#include "src/turbomind/core/check.h"
#include "src/turbomind/core/copy.h"
#include "src/turbomind/engine/batch.h"
#include "src/turbomind/models/language_model.h"
#include "src/turbomind/models/llama/llama_utils.h"
#include "src/turbomind/utils/anomaly_handler.h"
// #include "dbg.h"
namespace turbomind {
using std::shared_ptr;
using std::unique_ptr;
struct ModelExecutor::Impl {
LanguageModel& model_;
LlamaLinear& linear_;
const int device_id_;
Queue<unique_ptr<BatchData>>& inbound_;
Queue<unique_ptr<BatchData>>& outbound_;
std::thread internal_thread_;
void InternalThreadEntry()
{
check_cuda_error(cudaSetDevice(device_id_));
Stream stream = Stream::create();
Allocator h_alloc = Allocator(kCPU);
Allocator d_alloc = Allocator(stream, false);
AnomalyHandler::instance().Init(0, 1000, 0, 1000, stream.handle());
core::ContextGuard ctx{stream, h_alloc, d_alloc};
unique_ptr<BatchData> d;
while (inbound_.pop(d)) {
TM_CHECK_NOTNULL(d);
core::Context::stream().Wait(d->ready);
Run(*d);
d->done.Record(core::Context::stream());
outbound_.push(std::move(d));
}
}
void Run(BatchData& d)
{
auto batch = &d;
BatchCopy copy;
TensorMap env{{"batch", d.buf()}, {"copy", copy.buf()}};
model_.Run(BatchOp::kPrepare, d.phase, env);
// dbg(copy);
copy.Run();
model_.Run(BatchOp::kForward, d.phase, env);
model_.Run(BatchOp::kUnprep, d.phase, env);
// dbg(copy);
copy.Run();
// TM_CHECK(0);
AnomalyHandler::instance().Summarize([](...) {});
AnomalyHandler::instance().Reset();
}
Impl(LanguageModel& model,
Context& context,
int device_id,
Queue<unique_ptr<BatchData>>& inbound,
Queue<unique_ptr<BatchData>>& outbound):
model_{model}, linear_{*context.linear}, device_id_{device_id}, inbound_{inbound}, outbound_{outbound}
{
}
~Impl()
{
if (internal_thread_.joinable()) {
internal_thread_.join();
}
}
void Start()
{
internal_thread_ = std::thread(&Impl::InternalThreadEntry, this);
}
};
ModelExecutor::~ModelExecutor() = default;
ModelExecutor::ModelExecutor() = default;
ModelExecutor::ModelExecutor(ModelExecutor&&) noexcept = default;
ModelExecutor& ModelExecutor::operator=(ModelExecutor&&) noexcept = default;
ModelExecutor::ModelExecutor(LanguageModel& model,
Context& context,
int device_id,
Queue<unique_ptr<BatchData>>& inbound,
Queue<unique_ptr<BatchData>>& outbound):
impl_{std::make_unique<Impl>(model, context, device_id, inbound, outbound)}
{
}
void ModelExecutor::Start()
{
return impl_->Start();
}
} // namespace turbomind
|