llama_for_causal_lm.cpp 3.31 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
5
#include "infinicore/nn/linear.hpp"
#include "infinicore/ops.hpp"
namespace infinilm::models::llama {
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * @deprecated This function is deprecated and will be REMOVED in the next major release (v0.2.0).
 *
 * ⚠️ DEVELOPMENT POLICY:
 *   - NO new development or feature additions permitted on this interface
 *   - Only critical bug fixes (security/stability) allowed until removal
 *   - All new code MUST migrate to the polymorphic overload below
 *
 * Replacement: Use the polymorphic overload of this same function name with updated signature
 * Reason: Legacy signature lacks support for dynamic quantization modes.
 * Removal target: v0.2.0 (Q2 2026)
 */
Your Name's avatar
Your Name committed
18
19
20
LlamaForCausalLM::LlamaForCausalLM(const LlamaConfig &config,
                                   const infinicore::Device &device,
                                   engine::distributed::RankInfo rank_info) {
21

Ceng's avatar
Ceng committed
22
    // Initialize module's device_ member
23
    device_ = device;
24
    const auto &dtype{config.dtype};
25
    // Initialize base model
26
    INFINICORE_NN_MODULE_INIT(model, config, device, rank_info);
27
28
29
30
31
32
33
34

    // 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);
}

35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
LlamaForCausalLM::LlamaForCausalLM(std::shared_ptr<infinilm::config::ModelConfig> model_config,
                                   const infinicore::Device &device,
                                   engine::distributed::RankInfo rank_info) {

    // Initialize module's device_ member
    device_ = device;
    const auto &dtype{model_config->get_dtype()};

    // Initialize base model
    INFINICORE_NN_MODULE_INIT(model, model_config, device, rank_info);
    // 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, model_config->get<size_t>("hidden_size"), model_config->get<size_t>("vocab_size"), false,
                              dtype, device);
}

53
LlamaForCausalLM::Output LlamaForCausalLM::forward(const Input &input) const {
54
55
    auto input_ids = input.input_ids.value();
    auto position_ids = input.position_ids.value();
56
57
    auto past_sequence_lengths = input.past_sequence_lengths;
    auto total_sequence_length = input.total_sequence_lengths;
58
59
60
    auto input_offsets = input.input_offsets;
    auto block_tables = input.block_tables;
    auto slot_mapping = input.slot_mapping;
61

62
    // 1. Forward through base model to get hidden states
63
64
    auto hidden_states = model_->forward(
        input_ids, position_ids, past_sequence_lengths, total_sequence_length, input_offsets, block_tables, slot_mapping);
65
66
67

    // 2. Apply language modeling head to get logits
    auto logits = lm_head_->forward(hidden_states);
68
    return {logits};
69
70
}

PanZezhong's avatar
PanZezhong committed
71
void LlamaForCausalLM::reset_cache(const cache::CacheConfig *cache_config) {
72
73
74
75
76
77
    cache_config_ = cache_config->unique_copy();
    model_->reset_cache(cache_config_.get());
}

const cache::CacheConfig *LlamaForCausalLM::get_cache_config() const {
    return cache_config_.get();
78
79
}

80
} // namespace infinilm::models::llama