Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/infinicore/nn/rope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class RoPE : public Module {
private:
void initialize_cache();

size_t rotary_dim_; // Dimension of each attention head
size_t rotary_dim_; // Number of dimensions to apply rotation to (must be even).
size_t head_dim_; // Dimension of each attention head
size_t max_seq_len_; // Maximum sequence length
double theta_; // Base frequency for rotary embeddings
Algo algo_; // RoPE algorithm type
Expand Down
27 changes: 20 additions & 7 deletions src/infinicore/nn/rope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../utils.hpp"
#include "infinicore/ops.hpp"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <stdexcept>
Expand All @@ -20,6 +21,7 @@ RoPE::RoPE(size_t head_dim,
const Device &device,
std::shared_ptr<RopeScalingConfig> scaling)
: rotary_dim_(rotary_dim),
head_dim_(head_dim),
max_seq_len_(max_seq_len),
theta_(theta),
algo_(algo),
Expand All @@ -29,7 +31,7 @@ RoPE::RoPE(size_t head_dim,
if (rotary_dim % 2 != 0) {
throw std::invalid_argument("rotary_dim must be even for RoPE, got " + std::to_string(rotary_dim));
}

assert((rotary_dim > 0) && (rotary_dim <= head_dim_));
device_ = device;

// Initialize cache tables
Expand Down Expand Up @@ -120,23 +122,34 @@ void RoPE::initialize_cache() {
}

Tensor RoPE::forward(const Tensor &x, const Tensor &pos, bool in_place) const {
Tensor y;
if (in_place) {
Tensor y = Tensor(x);
op::rope_(y, x, pos, sin_cache_, cos_cache_, algo_);
return y;
y = Tensor(x);
} else {
y = Tensor::empty(x->shape(), x->dtype(), x->device());
if (rotary_dim_ < head_dim_) {
y->copy_from(x);
}
}

return op::rope(x, pos, sin_cache_, cos_cache_, algo_);
size_t ndim = x->ndim();
op::rope_(y->narrow({{ndim - 1, 0, rotary_dim_}}),
x->narrow({{ndim - 1, 0, rotary_dim_}}),
pos, sin_cache_, cos_cache_, algo_);
return y;
}

Tensor RoPE::forward(const Tensor &y, const Tensor &x, const Tensor &pos) const {
op::rope_(y, x, pos, sin_cache_, cos_cache_, algo_);
size_t ndim = x->ndim();
op::rope_(y->narrow({{ndim - 1, 0, rotary_dim_}}),
x->narrow({{ndim - 1, 0, rotary_dim_}}),
pos, sin_cache_, cos_cache_, algo_);
return y;
}

std::string RoPE::extra_repr() const {
std::string algo_str = (algo_ == Algo::GPT_J) ? "GPT_J" : "GPT_NEOX";
return "RoPE(rotary_dim=" + std::to_string(rotary_dim_) + ", max_seq_len=" + std::to_string(max_seq_len_) + ", theta=" + std::to_string(theta_) + ", algo=" + algo_str + ", dtype=" + std::to_string(static_cast<int>(dtype_)) + ")";
return "RoPE(head_dim=" + std::to_string(head_dim_) + ", rotary_dim=" + std::to_string(rotary_dim_) + ", max_seq_len=" + std::to_string(max_seq_len_) + ", theta=" + std::to_string(theta_) + ", algo=" + algo_str + ", dtype=" + std::to_string(static_cast<int>(dtype_)) + ")";
}

} // namespace infinicore::nn
Loading