axonml_nn/layers/
embedding.rs1use std::collections::HashMap;
9
10use axonml_autograd::Variable;
11use axonml_tensor::Tensor;
12
13use crate::init::normal;
14use crate::module::Module;
15use crate::parameter::Parameter;
16
17pub struct Embedding {
30 pub weight: Parameter,
32 num_embeddings: usize,
34 embedding_dim: usize,
36 padding_idx: Option<usize>,
38}
39
40impl Embedding {
41 pub fn new(num_embeddings: usize, embedding_dim: usize) -> Self {
43 Self::with_options(num_embeddings, embedding_dim, None)
44 }
45
46 pub fn with_options(
48 num_embeddings: usize,
49 embedding_dim: usize,
50 padding_idx: Option<usize>,
51 ) -> Self {
52 let mut weight_data = normal(&[num_embeddings, embedding_dim], 0.0, 1.0);
54
55 if let Some(pad_idx) = padding_idx {
57 let mut data = weight_data.to_vec();
58 for i in 0..embedding_dim {
59 data[pad_idx * embedding_dim + i] = 0.0;
60 }
61 weight_data = Tensor::from_vec(data, &[num_embeddings, embedding_dim]).unwrap();
62 }
63
64 Self {
65 weight: Parameter::named("weight", weight_data, true),
66 num_embeddings,
67 embedding_dim,
68 padding_idx,
69 }
70 }
71
72 pub fn from_pretrained(weights: Tensor<f32>, freeze: bool) -> Self {
74 let shape = weights.shape();
75 let num_embeddings = shape[0];
76 let embedding_dim = shape[1];
77
78 Self {
79 weight: Parameter::named("weight", weights, !freeze),
80 num_embeddings,
81 embedding_dim,
82 padding_idx: None,
83 }
84 }
85
86 pub fn num_embeddings(&self) -> usize {
88 self.num_embeddings
89 }
90
91 pub fn embedding_dim(&self) -> usize {
93 self.embedding_dim
94 }
95
96 pub fn lookup(&self, indices: &Variable) -> Variable {
104 let indices_data = indices.data();
105 let indices_vec = indices_data.to_vec();
106 let indices_shape = indices_data.shape().to_vec();
107
108 let weight_vec = self.weight.data().to_vec();
109
110 let mut output_shape = indices_shape.clone();
112 output_shape.push(self.embedding_dim);
113 let output_size: usize = output_shape.iter().product();
114
115 let mut output_data = vec![0.0f32; output_size];
116
117 for (i, &idx_f) in indices_vec.iter().enumerate() {
118 let idx = idx_f as usize;
119 let safe_idx = if idx >= self.num_embeddings {
122 #[cfg(debug_assertions)]
123 eprintln!(
124 "Warning: embedding index {} out of range (max {}), using padding index 0",
125 idx, self.num_embeddings - 1
126 );
127 0
128 } else {
129 idx
130 };
131
132 for d in 0..self.embedding_dim {
133 output_data[i * self.embedding_dim + d] = weight_vec[safe_idx * self.embedding_dim + d];
134 }
135 }
136
137 Variable::new(
138 Tensor::from_vec(output_data, &output_shape).unwrap(),
139 self.weight.requires_grad(),
140 )
141 }
142}
143
144impl Module for Embedding {
145 fn forward(&self, input: &Variable) -> Variable {
146 self.lookup(input)
147 }
148
149 fn parameters(&self) -> Vec<Parameter> {
150 vec![self.weight.clone()]
151 }
152
153 fn named_parameters(&self) -> HashMap<String, Parameter> {
154 let mut params = HashMap::new();
155 params.insert("weight".to_string(), self.weight.clone());
156 params
157 }
158
159 fn name(&self) -> &'static str {
160 "Embedding"
161 }
162}
163
164impl std::fmt::Debug for Embedding {
165 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
166 f.debug_struct("Embedding")
167 .field("num_embeddings", &self.num_embeddings)
168 .field("embedding_dim", &self.embedding_dim)
169 .field("padding_idx", &self.padding_idx)
170 .finish()
171 }
172}
173
174#[cfg(test)]
179mod tests {
180 use super::*;
181
182 #[test]
183 fn test_embedding_creation() {
184 let emb = Embedding::new(1000, 128);
185 assert_eq!(emb.num_embeddings(), 1000);
186 assert_eq!(emb.embedding_dim(), 128);
187 }
188
189 #[test]
190 fn test_embedding_lookup() {
191 let emb = Embedding::new(10, 4);
192 let indices = Variable::new(Tensor::from_vec(vec![0.0, 1.0, 2.0], &[3]).unwrap(), false);
193 let output = emb.forward(&indices);
194 assert_eq!(output.shape(), vec![3, 4]);
195 }
196
197 #[test]
198 fn test_embedding_batch() {
199 let emb = Embedding::new(10, 4);
200 let indices = Variable::new(
201 Tensor::from_vec(vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0], &[2, 3]).unwrap(),
202 false,
203 );
204 let output = emb.forward(&indices);
205 assert_eq!(output.shape(), vec![2, 3, 4]);
206 }
207
208 #[test]
209 fn test_embedding_parameters() {
210 let emb = Embedding::new(100, 64);
211 assert_eq!(emb.parameters().len(), 1);
212 assert_eq!(emb.num_parameters(), 100 * 64);
213 }
214
215 #[test]
216 fn test_embedding_with_padding() {
217 let emb = Embedding::with_options(10, 4, Some(0));
218 let indices = Variable::new(Tensor::from_vec(vec![0.0], &[1]).unwrap(), false);
220 let output = emb.forward(&indices);
221 let output_vec = output.data().to_vec();
222 assert!(output_vec.iter().all(|&x| x == 0.0));
223 }
224}