1pub mod accumulator;
59pub mod complex;
61pub mod dispatch;
63pub mod f16;
65pub mod quantized;
67
68pub mod builtins;
76
77pub mod buffer;
81pub mod tensor;
83pub mod tensor_snap;
85pub mod scratchpad;
87pub mod aligned_pool;
89mod kernel_bridge;
91pub use kernel_bridge::kernel;
92pub mod paged_kv;
94pub mod binned_alloc;
96pub mod frame_arena;
98pub mod object_slab;
100pub mod gc;
102pub mod sparse;
104pub mod sparse_solvers;
106pub mod tensor_tiled;
108pub mod tensor_simd;
110pub mod tensor_pool;
112pub mod det_map;
114pub mod linalg;
116pub mod value;
118pub mod error;
120pub mod lib_registry;
122pub mod json;
124pub mod datetime;
126pub mod window;
128pub mod stats;
130pub mod distributions;
132pub mod hypothesis;
134pub mod ml;
136pub mod fft;
138pub mod stationarity;
140pub mod ode;
142pub mod sparse_eigen;
144pub mod interpolate;
146pub mod optimize;
148pub mod clustering;
150pub mod tensor_dtype;
152pub mod timeseries;
154pub mod integrate;
156pub mod differentiate;
158pub mod profile;
161
162pub use buffer::Buffer;
167pub use tensor::Tensor;
169pub use scratchpad::Scratchpad;
171pub use aligned_pool::{AlignedPool, AlignedByteSlice};
173pub use paged_kv::{KvBlock, PagedKvCache};
175pub use gc::{GcRef, GcHeap};
177pub use binned_alloc::BinnedAllocator;
179pub use frame_arena::{FrameArena, ArenaStore};
181pub use object_slab::{ObjectSlab, SlabRef};
183pub use sparse::{SparseCsr, SparseCoo};
185pub use tensor_tiled::TiledMatmul;
187pub use det_map::{DetMap, murmurhash3, murmurhash3_finalize, value_hash, values_equal_static};
189pub use value::{Value, Bf16, FnValue};
191pub use error::RuntimeError;
193pub use tensor_dtype::{DType, TypedStorage};
195
196#[cfg(test)]
201mod tests {
202 use super::*;
203 use std::rc::Rc;
204 use cjc_repro::Rng;
205
206 #[test]
209 fn test_buffer_alloc_get_set() {
210 let mut buf = Buffer::alloc(5, 0.0f64);
211 assert_eq!(buf.len(), 5);
212 assert_eq!(buf.get(0), Some(0.0));
213 assert_eq!(buf.get(4), Some(0.0));
214 assert_eq!(buf.get(5), None);
215
216 buf.set(2, 42.0).unwrap();
217 assert_eq!(buf.get(2), Some(42.0));
218
219 assert!(buf.set(10, 1.0).is_err());
220 }
221
222 #[test]
223 fn test_buffer_from_vec() {
224 let buf = Buffer::from_vec(vec![1, 2, 3, 4, 5]);
225 assert_eq!(buf.len(), 5);
226 assert_eq!(buf.get(0), Some(1));
227 assert_eq!(buf.get(4), Some(5));
228 assert_eq!(buf.as_slice(), vec![1, 2, 3, 4, 5]);
229 }
230
231 #[test]
232 fn test_buffer_cow_behavior() {
233 let buf_a = Buffer::from_vec(vec![10, 20, 30]);
234 let mut buf_b = buf_a.clone();
235
236 assert_eq!(buf_a.refcount(), 2);
237 assert_eq!(buf_b.refcount(), 2);
238
239 buf_b.set(0, 99).unwrap();
240
241 assert_eq!(buf_a.refcount(), 1);
242 assert_eq!(buf_b.refcount(), 1);
243 assert_eq!(buf_a.get(0), Some(10));
244 assert_eq!(buf_b.get(0), Some(99));
245 }
246
247 #[test]
248 fn test_buffer_clone_buffer_forces_deep_copy() {
249 let buf_a = Buffer::from_vec(vec![1, 2, 3]);
250 let buf_b = buf_a.clone_buffer();
251
252 assert_eq!(buf_a.refcount(), 1);
253 assert_eq!(buf_b.refcount(), 1);
254 assert_eq!(buf_a.as_slice(), buf_b.as_slice());
255 }
256
257 #[test]
260 fn test_tensor_creation_and_indexing() {
261 let t = Tensor::zeros(&[2, 3]);
262 assert_eq!(t.shape(), &[2, 3]);
263 assert_eq!(t.ndim(), 2);
264 assert_eq!(t.len(), 6);
265 assert_eq!(t.get(&[0, 0]).unwrap(), 0.0);
266 assert_eq!(t.get(&[1, 2]).unwrap(), 0.0);
267
268 assert!(t.get(&[2, 0]).is_err());
269 assert!(t.get(&[0]).is_err());
270 }
271
272 #[test]
273 fn test_tensor_from_vec_and_set() {
274 let mut t = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3]).unwrap();
275 assert_eq!(t.get(&[0, 0]).unwrap(), 1.0);
276 assert_eq!(t.get(&[0, 2]).unwrap(), 3.0);
277 assert_eq!(t.get(&[1, 0]).unwrap(), 4.0);
278 assert_eq!(t.get(&[1, 2]).unwrap(), 6.0);
279
280 t.set(&[1, 1], 99.0).unwrap();
281 assert_eq!(t.get(&[1, 1]).unwrap(), 99.0);
282 }
283
284 #[test]
285 fn test_tensor_elementwise_ops() {
286 let a = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2]).unwrap();
287 let b = Tensor::from_vec(vec![5.0, 6.0, 7.0, 8.0], &[2, 2]).unwrap();
288
289 let sum = a.add(&b).unwrap();
290 assert_eq!(sum.to_vec(), vec![6.0, 8.0, 10.0, 12.0]);
291
292 let diff = a.sub(&b).unwrap();
293 assert_eq!(diff.to_vec(), vec![-4.0, -4.0, -4.0, -4.0]);
294
295 let prod = a.mul_elem(&b).unwrap();
296 assert_eq!(prod.to_vec(), vec![5.0, 12.0, 21.0, 32.0]);
297
298 let quot = b.div_elem(&a).unwrap();
299 assert_eq!(quot.to_vec(), vec![5.0, 3.0, 7.0 / 3.0, 2.0]);
300 }
301
302 #[test]
303 fn test_tensor_matmul_correctness() {
304 let a = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2]).unwrap();
305 let b = Tensor::from_vec(vec![5.0, 6.0, 7.0, 8.0], &[2, 2]).unwrap();
306
307 let c = a.matmul(&b).unwrap();
308 assert_eq!(c.shape(), &[2, 2]);
309 assert_eq!(c.get(&[0, 0]).unwrap(), 19.0);
310 assert_eq!(c.get(&[0, 1]).unwrap(), 22.0);
311 assert_eq!(c.get(&[1, 0]).unwrap(), 43.0);
312 assert_eq!(c.get(&[1, 1]).unwrap(), 50.0);
313 }
314
315 #[test]
316 fn test_tensor_matmul_nonsquare() {
317 let a = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3]).unwrap();
318 let b = Tensor::from_vec(vec![7.0, 8.0, 9.0, 10.0, 11.0, 12.0], &[3, 2]).unwrap();
319
320 let c = a.matmul(&b).unwrap();
321 assert_eq!(c.shape(), &[2, 2]);
322 assert_eq!(c.get(&[0, 0]).unwrap(), 58.0);
323 assert_eq!(c.get(&[0, 1]).unwrap(), 64.0);
324 assert_eq!(c.get(&[1, 0]).unwrap(), 139.0);
325 assert_eq!(c.get(&[1, 1]).unwrap(), 154.0);
326 }
327
328 #[test]
329 fn test_tensor_reshape_shares_buffer() {
330 let t = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3]).unwrap();
331 let r = t.reshape(&[3, 2]).unwrap();
332
333 assert_eq!(r.shape(), &[3, 2]);
334 assert_eq!(r.get(&[0, 0]).unwrap(), 1.0);
335 assert_eq!(r.get(&[2, 1]).unwrap(), 6.0);
336
337 assert_eq!(t.buffer.refcount(), 2);
338
339 assert!(t.reshape(&[4, 2]).is_err());
340 }
341
342 #[test]
343 fn test_tensor_sum_and_mean() {
344 let t = Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[4]).unwrap();
345 assert!((t.sum() - 10.0).abs() < 1e-12);
346 assert!((t.mean() - 2.5).abs() < 1e-12);
347 }
348
349 #[test]
352 fn test_gc_alloc_and_read() {
353 let mut heap = GcHeap::new(100);
354 let r1 = heap.alloc(42i64);
355 let r2 = heap.alloc("hello".to_string());
356
357 assert_eq!(heap.live_count(), 2);
358 assert_eq!(*heap.get::<i64>(r1).unwrap(), 42);
359 assert_eq!(heap.get::<String>(r2).unwrap().as_str(), "hello");
360
361 assert!(heap.get::<f64>(r1).is_none());
362 }
363
364 #[test]
365 fn test_gc_collect_is_noop_rc_backed() {
366 let mut heap = GcHeap::new(100);
369 let r1 = heap.alloc(1i64);
370 let r2 = heap.alloc(2i64);
371 let r3 = heap.alloc(3i64);
372
373 assert_eq!(heap.live_count(), 3);
374
375 heap.collect(&[r1, r2]);
377
378 assert_eq!(heap.live_count(), 3, "RC keeps all objects alive");
379 assert_eq!(*heap.get::<i64>(r1).unwrap(), 1);
380 assert_eq!(*heap.get::<i64>(r2).unwrap(), 2);
381 assert_eq!(*heap.get::<i64>(r3).unwrap(), 3);
382 }
383
384 #[test]
385 fn test_gc_explicit_free_and_slot_reuse() {
386 let mut heap = GcHeap::new(100);
388 let r1 = heap.alloc(1i64);
389 let r2 = heap.alloc(2i64);
390 let r3 = heap.alloc(3i64);
391
392 heap.free(r1);
394 heap.free(r2);
395 heap.free(r3);
396 assert_eq!(heap.live_count(), 0);
397 assert_eq!(heap.free_list().len(), 3);
398
399 let r4 = heap.alloc(99i64);
401 assert!(r4.index < 3, "should reuse a freed slot");
402 assert_eq!(*heap.get::<i64>(r4).unwrap(), 99);
403 }
404
405 #[test]
408 fn test_stable_summation_via_tensor() {
409 let n = 100_000;
410 let data: Vec<f64> = (0..n).map(|_| 0.00001).collect();
411 let t = Tensor::from_vec(data, &[n]).unwrap();
412 let result = t.sum();
413 let expected = 0.00001 * n as f64;
414 assert!(
415 (result - expected).abs() < 1e-10,
416 "Kahan sum drift: expected {expected}, got {result}"
417 );
418 }
419
420 #[test]
423 fn test_tensor_randn_deterministic() {
424 let mut rng1 = Rng::seeded(42);
425 let mut rng2 = Rng::seeded(42);
426
427 let t1 = Tensor::randn(&[3, 4], &mut rng1);
428 let t2 = Tensor::randn(&[3, 4], &mut rng2);
429
430 assert_eq!(t1.to_vec(), t2.to_vec());
431 }
432
433 #[test]
436 fn test_value_display() {
437 assert_eq!(format!("{}", Value::Int(42)), "42");
438 assert_eq!(format!("{}", Value::Bool(true)), "true");
439 assert_eq!(format!("{}", Value::Void), "void");
440 assert_eq!(format!("{}", Value::String(Rc::new("hi".into()))), "hi");
441 }
442
443 #[test]
444 fn test_cow_string_clone_shares() {
445 let s = Value::String(Rc::new("hello".into()));
446 let s2 = s.clone();
447 if let (Value::String(a), Value::String(b)) = (&s, &s2) {
448 assert!(Rc::ptr_eq(a, b));
449 } else {
450 panic!("expected String values");
451 }
452 }
453
454 #[test]
455 fn test_cow_string_display() {
456 let s = Value::String(Rc::new("world".into()));
457 assert_eq!(format!("{}", s), "world");
458 }
459
460 #[test]
463 fn test_byteslice_value_display_utf8() {
464 let bs = Value::ByteSlice(Rc::new(b"hello".to_vec()));
465 assert_eq!(format!("{}", bs), r#"b"hello""#);
466 }
467
468 #[test]
469 fn test_byteslice_value_display_hex() {
470 let bs = Value::ByteSlice(Rc::new(vec![0xff, 0x00, 0x41]));
471 assert_eq!(format!("{}", bs), r#"b"\xff\x00A""#);
472 }
473
474 #[test]
475 fn test_strview_value_display() {
476 let sv = Value::StrView(Rc::new(b"world".to_vec()));
477 assert_eq!(format!("{}", sv), "world");
478 }
479
480 #[test]
481 fn test_u8_value_display() {
482 assert_eq!(format!("{}", Value::U8(65)), "65");
483 }
484
485 #[test]
486 fn test_byteslice_hash_deterministic() {
487 let a = Value::ByteSlice(Rc::new(b"hello".to_vec()));
488 let b = Value::ByteSlice(Rc::new(b"hello".to_vec()));
489 assert_eq!(value_hash(&a), value_hash(&b));
490 }
491
492 #[test]
493 fn test_byteslice_hash_different_content() {
494 let a = Value::ByteSlice(Rc::new(b"hello".to_vec()));
495 let b = Value::ByteSlice(Rc::new(b"world".to_vec()));
496 assert_ne!(value_hash(&a), value_hash(&b));
497 }
498
499 #[test]
500 fn test_byteslice_equality() {
501 let a = Value::ByteSlice(Rc::new(b"abc".to_vec()));
502 let b = Value::ByteSlice(Rc::new(b"abc".to_vec()));
503 let c = Value::ByteSlice(Rc::new(b"def".to_vec()));
504 assert!(values_equal_static(&a, &b));
505 assert!(!values_equal_static(&a, &c));
506 }
507
508 #[test]
509 fn test_strview_equality() {
510 let a = Value::StrView(Rc::new(b"test".to_vec()));
511 let b = Value::StrView(Rc::new(b"test".to_vec()));
512 assert!(values_equal_static(&a, &b));
513 }
514
515 #[test]
516 fn test_u8_hash_and_equality() {
517 let a = Value::U8(42);
518 let b = Value::U8(42);
519 let c = Value::U8(99);
520 assert_eq!(value_hash(&a), value_hash(&b));
521 assert_ne!(value_hash(&a), value_hash(&c));
522 assert!(values_equal_static(&a, &b));
523 assert!(!values_equal_static(&a, &c));
524 }
525
526 #[test]
527 fn test_byteslice_clone_shares_rc() {
528 let bs = Value::ByteSlice(Rc::new(b"data".to_vec()));
529 let bs2 = bs.clone();
530 if let (Value::ByteSlice(a), Value::ByteSlice(b)) = (&bs, &bs2) {
531 assert!(Rc::ptr_eq(a, b));
532 } else {
533 panic!("expected ByteSlice values");
534 }
535 }
536
537 #[test]
538 fn test_byteslice_in_detmap() {
539 let mut map = DetMap::new();
540 let key = Value::ByteSlice(Rc::new(b"token".to_vec()));
541 map.insert(key.clone(), Value::Int(1));
542
543 let lookup = Value::ByteSlice(Rc::new(b"token".to_vec()));
544 assert!(map.contains_key(&lookup));
545 match map.get(&lookup) {
546 Some(Value::Int(1)) => {},
547 _ => panic!("expected Int(1)"),
548 }
549 }
550
551 #[test]
552 fn test_murmurhash3_byteslice_stability() {
553 let h1 = murmurhash3(b"hello");
554 let h2 = murmurhash3(b"hello");
555 assert_eq!(h1, h2);
556
557 let h3 = murmurhash3(b"");
558 let h4 = murmurhash3(b"");
559 assert_eq!(h3, h4);
560
561 assert_ne!(murmurhash3(b"hello"), murmurhash3(b"world"));
562 }
563}