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