|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "module.hpp" |
| 4 | +#include "../context/context.hpp" |
| 5 | +#include "../tensor.hpp" |
| 6 | +#include <memory> |
| 7 | + |
| 8 | +namespace infinicore::nn { |
| 9 | + |
| 10 | +class RoPE : public Module { |
| 11 | +public: |
| 12 | + /** |
| 13 | + * @brief RoPE algorithm type |
| 14 | + */ |
| 15 | + enum class Algo { |
| 16 | + GPT_J = 0, // GPT-J style RoPE algorithm (Interleave even and odd dimensions) |
| 17 | + GPT_NEOX = 1, // GPT-NeoX style RoPE algorithm (First half dimensions for sin, second half for cos) |
| 18 | + }; |
| 19 | + |
| 20 | + /** |
| 21 | + * @brief Construct a RoPE layer |
| 22 | + * |
| 23 | + * @param head_dim Dimension of each attention head (must be even) |
| 24 | + * @param max_seq_len Maximum sequence length for pre-computed cache |
| 25 | + * @param theta Base frequency for rotary embeddings (default: 10000.0) |
| 26 | + * @param algo RoPE algorithm type (default: Algo::GPT_J) |
| 27 | + * @param dtype Data type for sin/cos cache (default: DataType::F32) |
| 28 | + * @param device Device to create the cache on |
| 29 | + */ |
| 30 | + RoPE(size_t head_dim, |
| 31 | + size_t max_seq_len, |
| 32 | + double theta = 10000.0, |
| 33 | + Algo algo = Algo::GPT_J, |
| 34 | + const DataType &dtype = DataType::F32, |
| 35 | + const Device &device = Device()); |
| 36 | + |
| 37 | + /** |
| 38 | + * @brief Forward pass: apply RoPE to a tensor |
| 39 | + * |
| 40 | + * @param x Input tensor of shape (..., head_dim) where ... is any number of dimensions |
| 41 | + * @param pos Position IDs tensor of shape (*,) typically [seq_len] or [batch, seq_len] |
| 42 | + * @return Rotated tensor with same shape as input |
| 43 | + * |
| 44 | + * Applies rotary position embeddings to the input tensor. |
| 45 | + * For attention mechanisms, call this method separately for query and key tensors. |
| 46 | + * |
| 47 | + * Common input shapes: |
| 48 | + * - [batch, num_heads, seq_len, head_dim] |
| 49 | + * - [batch, seq_len, num_heads, head_dim] |
| 50 | + * - [seq_len, head_dim] |
| 51 | + */ |
| 52 | + Tensor forward(const Tensor &x, const Tensor &pos) const; |
| 53 | + |
| 54 | + // Module information |
| 55 | + size_t head_dim() const { return head_dim_; } |
| 56 | + size_t max_seq_len() const { return max_seq_len_; } |
| 57 | + double theta() const { return theta_; } |
| 58 | + Algo algo() const { return algo_; } |
| 59 | + DataType dtype() const { return dtype_; } |
| 60 | + |
| 61 | + // String representation |
| 62 | + std::string extra_repr() const; |
| 63 | + |
| 64 | +protected: |
| 65 | + // Buffers (sin and cos cache tables) - not exposed in state_dict |
| 66 | + INFINICORE_NN_BUFFER(sin_cache); |
| 67 | + INFINICORE_NN_BUFFER(cos_cache); |
| 68 | + |
| 69 | +private: |
| 70 | + void initialize_cache(); |
| 71 | + |
| 72 | + size_t head_dim_; // Dimension of each attention head |
| 73 | + size_t max_seq_len_; // Maximum sequence length |
| 74 | + double theta_; // Base frequency for rotary embeddings |
| 75 | + Algo algo_; // RoPE algorithm type |
| 76 | + DataType dtype_; // Data type for cache tables |
| 77 | +}; |
| 78 | + |
| 79 | +} // namespace infinicore::nn |
0 commit comments