llama_for_causal_lm.cpp 2.58 KB
Newer Older
1
#include "llama_for_causal_lm.hpp"
Your Name's avatar
Your Name committed
2
#include "infinicore/context/context.hpp"
3
4
#include "infinicore/nn/linear.hpp"
#include "infinicore/ops.hpp"
Ceng's avatar
Ceng committed
5
#include <iostream>
6
7
8

namespace infinilm::models::llama {

Your Name's avatar
Your Name committed
9
10
11
12
LlamaForCausalLM::LlamaForCausalLM(const LlamaConfig &config,
                                   const infinicore::Device &device,
                                   infinicore::DataType dtype,
                                   engine::distributed::RankInfo rank_info) {
13

Ceng's avatar
Ceng committed
14
    // Initialize module's device_ member
15
    device_ = device;
Ceng's avatar
Ceng committed
16

17
    // Initialize base model
Your Name's avatar
Your Name committed
18
    INFINICORE_NN_MODULE_INIT(model, config, device, dtype, rank_info);
19
20
21
22
23
24
25
26
27

    // Initialize language modeling head
    // Note: If tie_word_embeddings is true, we would share weights with embed_tokens
    // For now, we create a separate linear layer
    INFINICORE_NN_MODULE_INIT(lm_head, config.hidden_size, config.vocab_size, false,
                              dtype, device);
}

infinicore::Tensor LlamaForCausalLM::forward(const infinicore::Tensor &input_ids,
28
                                             const infinicore::Tensor &position_ids,
Ceng's avatar
Ceng committed
29
                                             void *kv_cache) const {
30
    // 1. Forward through base model to get hidden states
31
    auto position_ids_device = position_ids->to(device_);
Ceng's avatar
Ceng committed
32
    auto hidden_states = model_->forward(input_ids, position_ids_device, kv_cache);
33
34
35
36

    // 2. Apply language modeling head to get logits
    auto logits = lm_head_->forward(hidden_states);

Ceng's avatar
Ceng committed
37
38
39
40
41
42
43
    // 3. CRITICAL: Synchronize the C++ backend's context after forward pass
    // This ensures all C++ backend operations complete before returning to Python
    if (device_.getType() != infinicore::Device::Type::CPU) {
        infinicore::context::setDevice(device_, false);
        infinicore::context::syncStream();
    }

44
45
46
    return logits;
}

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
infinicore::Tensor LlamaForCausalLM::forward(std::vector<std::any> args) const {
    if (args.size() < 2) {
        throw std::invalid_argument("LlamaForCausalLM::forward requires at least 2 arguments: input_ids and position_ids");
    }

    // Extract input tensors from args
    const auto &input_ids = std::any_cast<const infinicore::Tensor &>(args[0]);
    const auto &position_ids = std::any_cast<const infinicore::Tensor &>(args[1]);

    // Optional KV caches
    std::vector<void *> *kv_caches = nullptr;
    if (args.size() >= 3) {
        kv_caches = std::any_cast<std::vector<void *> *>(args[2]);
    }

    return forward(input_ids, position_ids, kv_caches);
}

Ceng's avatar
Ceng committed
65
66
67
68
void LlamaForCausalLM::reset_cache(size_t pos) {
    model_->reset_cache(pos);
}

69
} // namespace infinilm::models::llama