-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCpp_AI_ML_Comprehensive_Reference.cpp
More file actions
1950 lines (1567 loc) · 72.2 KB
/
Cpp_AI_ML_Comprehensive_Reference.cpp
File metadata and controls
1950 lines (1567 loc) · 72.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// C++ AI/MACHINE LEARNING - Comprehensive Reference - by Richard Rembert
// C++ for performance-critical ML/AI libraries with focus on optimization,
// SIMD, GPU computing, and high-performance numerical computing
// ═══════════════════════════════════════════════════════════════════════════════
// 1. SETUP AND PROJECT STRUCTURE
// ═══════════════════════════════════════════════════════════════════════════════
/*
C++ AI/ML PROJECT SETUP:
1. Build System (CMakeLists.txt):
cmake_minimum_required(VERSION 3.20)
project(MLFramework LANGUAGES CXX CUDA)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Find packages
find_package(OpenMP REQUIRED)
find_package(CUDA REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(BLAS REQUIRED blas)
pkg_check_modules(LAPACK REQUIRED lapack)
# Compiler optimizations
if(CMAKE_BUILD_TYPE STREQUAL "Release")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -march=native -mtune=native")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffast-math -funroll-loops")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto -DNDEBUG")
endif()
# SIMD support
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-mavx2" COMPILER_SUPPORTS_AVX2)
check_cxx_compiler_flag("-mfma" COMPILER_SUPPORTS_FMA)
if(COMPILER_SUPPORTS_AVX2)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mavx2")
endif()
2. Directory Structure:
MLFramework/
├── include/
│ ├── ml/
│ │ ├── core/
│ │ │ ├── tensor.hpp
│ │ │ ├── matrix.hpp
│ │ │ └── memory.hpp
│ │ ├── algorithms/
│ │ │ ├── linear.hpp
│ │ │ ├── neural.hpp
│ │ │ └── clustering.hpp
│ │ ├── optimizers/
│ │ │ ├── sgd.hpp
│ │ │ ├── adam.hpp
│ │ │ └── optimizer_base.hpp
│ │ ├── utils/
│ │ │ ├── simd.hpp
│ │ │ ├── parallel.hpp
│ │ │ └── profiler.hpp
│ │ └── gpu/
│ │ ├── cuda_ops.cuh
│ │ └── cuda_kernels.cuh
│ └── external/
├── src/
│ ├── core/
│ ├── algorithms/
│ ├── optimizers/
│ ├── utils/
│ └── gpu/
├── tests/
├── benchmarks/
├── examples/
└── docs/
3. Essential Dependencies:
- Eigen3 (Linear algebra)
- OpenBLAS/Intel MKL (Optimized BLAS)
- CUDA/cuBLAS (GPU computing)
- OpenMP (CPU parallelization)
- Google Benchmark (Performance testing)
- Google Test (Unit testing)
*/
#include <iostream>
#include <vector>
#include <memory>
#include <chrono>
#include <random>
#include <algorithm>
#include <execution>
#include <immintrin.h>
#include <omp.h>
#include <cmath>
#include <cassert>
#include <type_traits>
#include <concepts>
// ═══════════════════════════════════════════════════════════════════════════════
// 2. CORE DATA STRUCTURES AND MEMORY MANAGEMENT
// ═══════════════════════════════════════════════════════════════════════════════
namespace ml {
// Modern C++20 concepts for type safety
template<typename T>
concept Arithmetic = std::is_arithmetic_v<T>;
template<typename T>
concept FloatingPoint = std::floating_point<T>;
// High-performance memory allocator with alignment
template<typename T, size_t Alignment = 32>
class AlignedAllocator {
public:
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
template<typename U>
struct rebind {
using other = AlignedAllocator<U, Alignment>;
};
AlignedAllocator() noexcept = default;
template<typename U>
AlignedAllocator(const AlignedAllocator<U, Alignment>&) noexcept {}
pointer allocate(size_type n) {
if (n == 0) return nullptr;
void* ptr = nullptr;
size_t size = n * sizeof(T);
#ifdef _WIN32
ptr = _aligned_malloc(size, Alignment);
#else
if (posix_memalign(&ptr, Alignment, size) != 0) {
ptr = nullptr;
}
#endif
if (!ptr) {
throw std::bad_alloc();
}
return static_cast<pointer>(ptr);
}
void deallocate(pointer p, size_type) noexcept {
if (p) {
#ifdef _WIN32
_aligned_free(p);
#else
free(p);
#endif
}
}
template<typename U>
bool operator==(const AlignedAllocator<U, Alignment>&) const noexcept {
return true;
}
};
// High-performance tensor class with SIMD optimization
template<Arithmetic T, size_t Dims = 2>
class Tensor {
private:
std::vector<T, AlignedAllocator<T, 32>> data_;
std::array<size_t, Dims> shape_;
std::array<size_t, Dims> strides_;
size_t size_;
void compute_strides() {
if constexpr (Dims > 0) {
strides_[Dims - 1] = 1;
for (int i = Dims - 2; i >= 0; --i) {
strides_[i] = strides_[i + 1] * shape_[i + 1];
}
}
}
public:
using value_type = T;
using allocator_type = AlignedAllocator<T, 32>;
// Constructor with shape
template<typename... Args>
explicit Tensor(Args... dims) requires (sizeof...(Args) == Dims) {
static_assert(sizeof...(Args) == Dims, "Number of dimensions must match template parameter");
shape_ = {static_cast<size_t>(dims)...};
compute_strides();
size_ = std::accumulate(shape_.begin(), shape_.end(), 1ULL, std::multiplies<size_t>());
data_.resize(size_);
}
// Copy constructor
Tensor(const Tensor& other) = default;
// Move constructor
Tensor(Tensor&& other) noexcept = default;
// Assignment operators
Tensor& operator=(const Tensor& other) = default;
Tensor& operator=(Tensor&& other) noexcept = default;
// Element access
template<typename... Indices>
T& operator()(Indices... indices) requires (sizeof...(Indices) == Dims) {
static_assert(sizeof...(Indices) == Dims, "Number of indices must match tensor dimensions");
std::array<size_t, Dims> idx_array = {static_cast<size_t>(indices)...};
size_t linear_idx = 0;
for (size_t i = 0; i < Dims; ++i) {
assert(idx_array[i] < shape_[i]);
linear_idx += idx_array[i] * strides_[i];
}
return data_[linear_idx];
}
template<typename... Indices>
const T& operator()(Indices... indices) const requires (sizeof...(Indices) == Dims) {
static_assert(sizeof...(Indices) == Dims, "Number of indices must match tensor dimensions");
std::array<size_t, Dims> idx_array = {static_cast<size_t>(indices)...};
size_t linear_idx = 0;
for (size_t i = 0; i < Dims; ++i) {
assert(idx_array[i] < shape_[i]);
linear_idx += idx_array[i] * strides_[i];
}
return data_[linear_idx];
}
// Raw data access
T* data() noexcept { return data_.data(); }
const T* data() const noexcept { return data_.data(); }
// Shape and size
const std::array<size_t, Dims>& shape() const noexcept { return shape_; }
size_t size() const noexcept { return size_; }
constexpr size_t dimensions() const noexcept { return Dims; }
// Fill with value
void fill(T value) {
std::fill(std::execution::par_unseq, data_.begin(), data_.end(), value);
}
// Random initialization
void random_normal(T mean = T(0), T stddev = T(1)) {
static thread_local std::random_device rd;
static thread_local std::mt19937 gen(rd());
std::normal_distribution<T> dist(mean, stddev);
std::generate(std::execution::par_unseq, data_.begin(), data_.end(),
[&]() { return dist(gen); });
}
void random_uniform(T min_val = T(0), T max_val = T(1)) {
static thread_local std::random_device rd;
static thread_local std::mt19937 gen(rd());
std::uniform_real_distribution<T> dist(min_val, max_val);
std::generate(std::execution::par_unseq, data_.begin(), data_.end(),
[&]() { return dist(gen); });
}
// Mathematical operations with SIMD
Tensor& operator+=(const Tensor& other) {
assert(shape_ == other.shape_);
if constexpr (std::is_same_v<T, float>) {
simd_add_float(data_.data(), other.data_.data(), data_.data(), size_);
} else {
std::transform(std::execution::par_unseq,
data_.begin(), data_.end(),
other.data_.begin(), data_.begin(),
std::plus<T>());
}
return *this;
}
Tensor& operator*=(T scalar) {
if constexpr (std::is_same_v<T, float>) {
simd_scale_float(data_.data(), scalar, data_.data(), size_);
} else {
std::transform(std::execution::par_unseq,
data_.begin(), data_.end(), data_.begin(),
[scalar](T x) { return x * scalar; });
}
return *this;
}
private:
// SIMD operations for float
void simd_add_float(const float* a, const float* b, float* result, size_t n) {
const size_t simd_width = 8; // AVX2 processes 8 floats at once
const size_t simd_end = (n / simd_width) * simd_width;
for (size_t i = 0; i < simd_end; i += simd_width) {
__m256 va = _mm256_load_ps(&a[i]);
__m256 vb = _mm256_load_ps(&b[i]);
__m256 vresult = _mm256_add_ps(va, vb);
_mm256_store_ps(&result[i], vresult);
}
// Handle remaining elements
for (size_t i = simd_end; i < n; ++i) {
result[i] = a[i] + b[i];
}
}
void simd_scale_float(const float* input, float scalar, float* output, size_t n) {
const size_t simd_width = 8;
const size_t simd_end = (n / simd_width) * simd_width;
__m256 vscalar = _mm256_set1_ps(scalar);
for (size_t i = 0; i < simd_end; i += simd_width) {
__m256 vinput = _mm256_load_ps(&input[i]);
__m256 vresult = _mm256_mul_ps(vinput, vscalar);
_mm256_store_ps(&output[i], vresult);
}
// Handle remaining elements
for (size_t i = simd_end; i < n; ++i) {
output[i] = input[i] * scalar;
}
}
};
// Specialized Matrix class (2D Tensor) with optimized operations
template<Arithmetic T>
class Matrix : public Tensor<T, 2> {
public:
using BaseType = Tensor<T, 2>;
using value_type = T;
Matrix(size_t rows, size_t cols) : BaseType(rows, cols) {}
size_t rows() const noexcept { return this->shape()[0]; }
size_t cols() const noexcept { return this->shape()[1]; }
// Matrix multiplication with BLAS integration
Matrix multiply(const Matrix& other) const {
assert(cols() == other.rows());
Matrix result(rows(), other.cols());
if constexpr (std::is_same_v<T, float>) {
cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
static_cast<int>(rows()), static_cast<int>(other.cols()),
static_cast<int>(cols()),
1.0f, this->data(), static_cast<int>(cols()),
other.data(), static_cast<int>(other.cols()),
0.0f, result.data(), static_cast<int>(result.cols()));
} else if constexpr (std::is_same_v<T, double>) {
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
static_cast<int>(rows()), static_cast<int>(other.cols()),
static_cast<int>(cols()),
1.0, this->data(), static_cast<int>(cols()),
other.data(), static_cast<int>(other.cols()),
0.0, result.data(), static_cast<int>(result.cols()));
} else {
// Fallback for other types
parallel_matrix_multiply(*this, other, result);
}
return result;
}
// Transpose operation
Matrix transpose() const {
Matrix result(cols(), rows());
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < rows(); ++i) {
for (size_t j = 0; j < cols(); ++j) {
result(j, i) = (*this)(i, j);
}
}
return result;
}
// Element-wise operations
Matrix hadamard(const Matrix& other) const {
assert(this->shape() == other.shape());
Matrix result = *this;
#pragma omp parallel for
for (size_t i = 0; i < this->size(); ++i) {
result.data()[i] *= other.data()[i];
}
return result;
}
private:
// Fallback parallel matrix multiplication
void parallel_matrix_multiply(const Matrix& a, const Matrix& b, Matrix& c) const {
const size_t M = a.rows();
const size_t N = b.cols();
const size_t K = a.cols();
#pragma omp parallel for collapse(2)
for (size_t i = 0; i < M; ++i) {
for (size_t j = 0; j < N; ++j) {
T sum = T(0);
for (size_t k = 0; k < K; ++k) {
sum += a(i, k) * b(k, j);
}
c(i, j) = sum;
}
}
}
};
} // namespace ml
// ═══════════════════════════════════════════════════════════════════════════════
// 3. SIMD OPTIMIZED OPERATIONS
// ═══════════════════════════════════════════════════════════════════════════════
namespace ml::simd {
// SIMD utility functions for different instruction sets
class SIMDOperations {
public:
// Check CPU capabilities at runtime
static bool has_avx2() {
#ifdef __AVX2__
return true;
#else
return false;
#endif
}
static bool has_fma() {
#ifdef __FMA__
return true;
#else
return false;
#endif
}
// Vectorized dot product for float arrays
static float dot_product_avx2(const float* a, const float* b, size_t n) {
const size_t simd_width = 8;
const size_t simd_end = (n / simd_width) * simd_width;
__m256 sum_vec = _mm256_setzero_ps();
for (size_t i = 0; i < simd_end; i += simd_width) {
__m256 va = _mm256_loadu_ps(&a[i]);
__m256 vb = _mm256_loadu_ps(&b[i]);
#ifdef __FMA__
sum_vec = _mm256_fmadd_ps(va, vb, sum_vec);
#else
__m256 prod = _mm256_mul_ps(va, vb);
sum_vec = _mm256_add_ps(sum_vec, prod);
#endif
}
// Horizontal sum of the vector
__m128 high = _mm256_extractf128_ps(sum_vec, 1);
__m128 low = _mm256_castps256_ps128(sum_vec);
__m128 sum128 = _mm_add_ps(high, low);
sum128 = _mm_hadd_ps(sum128, sum128);
sum128 = _mm_hadd_ps(sum128, sum128);
float result = _mm_cvtss_f32(sum128);
// Handle remaining elements
for (size_t i = simd_end; i < n; ++i) {
result += a[i] * b[i];
}
return result;
}
// Vectorized matrix-vector multiplication
static void matvec_avx2(const float* matrix, const float* vector,
float* result, size_t rows, size_t cols) {
#pragma omp parallel for
for (size_t i = 0; i < rows; ++i) {
result[i] = dot_product_avx2(&matrix[i * cols], vector, cols);
}
}
// Vectorized ReLU activation
static void relu_avx2(const float* input, float* output, size_t n) {
const size_t simd_width = 8;
const size_t simd_end = (n / simd_width) * simd_width;
__m256 zero = _mm256_setzero_ps();
for (size_t i = 0; i < simd_end; i += simd_width) {
__m256 x = _mm256_loadu_ps(&input[i]);
__m256 result = _mm256_max_ps(x, zero);
_mm256_storeu_ps(&output[i], result);
}
// Handle remaining elements
for (size_t i = simd_end; i < n; ++i) {
output[i] = std::max(input[i], 0.0f);
}
}
// Vectorized sigmoid activation
static void sigmoid_avx2(const float* input, float* output, size_t n) {
const size_t simd_width = 8;
const size_t simd_end = (n / simd_width) * simd_width;
__m256 one = _mm256_set1_ps(1.0f);
for (size_t i = 0; i < simd_end; i += simd_width) {
__m256 x = _mm256_loadu_ps(&input[i]);
// Approximate exp using polynomial (for demonstration)
// In production, use more accurate methods or libraries
__m256 exp_x = fast_exp_avx2(x);
__m256 result = _mm256_div_ps(one, _mm256_add_ps(one, exp_x));
_mm256_storeu_ps(&output[i], result);
}
// Handle remaining elements
for (size_t i = simd_end; i < n; ++i) {
output[i] = 1.0f / (1.0f + std::exp(-input[i]));
}
}
private:
// Fast exponential approximation using AVX2
static __m256 fast_exp_avx2(__m256 x) {
// Polynomial approximation for exp(-x)
// This is a simplified version - use proper implementations in production
__m256 one = _mm256_set1_ps(1.0f);
__m256 two = _mm256_set1_ps(2.0f);
__m256 half = _mm256_set1_ps(0.5f);
// Negate x for exp(-x)
x = _mm256_sub_ps(_mm256_setzero_ps(), x);
// Taylor series approximation: 1 + x + x²/2 + x³/6 + ...
__m256 x2 = _mm256_mul_ps(x, x);
__m256 x3 = _mm256_mul_ps(x2, x);
__m256 term1 = x;
__m256 term2 = _mm256_mul_ps(x2, half);
__m256 term3 = _mm256_mul_ps(x3, _mm256_set1_ps(1.0f/6.0f));
return _mm256_add_ps(_mm256_add_ps(_mm256_add_ps(one, term1), term2), term3);
}
};
} // namespace ml::simd
// ═══════════════════════════════════════════════════════════════════════════════
// 4. NEURAL NETWORK IMPLEMENTATIONS
// ═══════════════════════════════════════════════════════════════════════════════
namespace ml::neural {
// Activation functions with vectorized implementations
class ActivationFunctions {
public:
template<FloatingPoint T>
static void relu(const T* input, T* output, size_t size) {
if constexpr (std::is_same_v<T, float>) {
simd::SIMDOperations::relu_avx2(input, output, size);
} else {
#pragma omp parallel for
for (size_t i = 0; i < size; ++i) {
output[i] = std::max(input[i], T(0));
}
}
}
template<FloatingPoint T>
static void relu_derivative(const T* input, T* output, size_t size) {
#pragma omp parallel for
for (size_t i = 0; i < size; ++i) {
output[i] = input[i] > T(0) ? T(1) : T(0);
}
}
template<FloatingPoint T>
static void sigmoid(const T* input, T* output, size_t size) {
if constexpr (std::is_same_v<T, float>) {
simd::SIMDOperations::sigmoid_avx2(input, output, size);
} else {
#pragma omp parallel for
for (size_t i = 0; i < size; ++i) {
output[i] = T(1) / (T(1) + std::exp(-input[i]));
}
}
}
template<FloatingPoint T>
static void tanh_activation(const T* input, T* output, size_t size) {
#pragma omp parallel for
for (size_t i = 0; i < size; ++i) {
output[i] = std::tanh(input[i]);
}
}
template<FloatingPoint T>
static void softmax(const T* input, T* output, size_t size) {
// Find maximum for numerical stability
T max_val = *std::max_element(input, input + size);
// Compute exp(x - max) and sum
T sum = T(0);
#pragma omp parallel for reduction(+:sum)
for (size_t i = 0; i < size; ++i) {
output[i] = std::exp(input[i] - max_val);
sum += output[i];
}
// Normalize
T inv_sum = T(1) / sum;
#pragma omp parallel for
for (size_t i = 0; i < size; ++i) {
output[i] *= inv_sum;
}
}
};
// Dense layer implementation with optimized forward/backward passes
template<FloatingPoint T>
class DenseLayer {
private:
Matrix<T> weights_;
Tensor<T, 1> biases_;
size_t input_size_;
size_t output_size_;
// Cached values for backward pass
mutable Matrix<T> last_input_;
mutable Matrix<T> last_output_;
public:
DenseLayer(size_t input_size, size_t output_size)
: weights_(output_size, input_size)
, biases_(output_size)
, input_size_(input_size)
, output_size_(output_size)
, last_input_(1, input_size)
, last_output_(1, output_size) {
// Xavier initialization
T std_dev = std::sqrt(T(2) / T(input_size + output_size));
weights_.random_normal(T(0), std_dev);
biases_.fill(T(0));
}
// Forward pass
Matrix<T> forward(const Matrix<T>& input) const {
assert(input.cols() == input_size_);
last_input_ = input; // Cache for backward pass
// Compute: output = input * weights^T + bias
Matrix<T> weights_t = weights_.transpose();
Matrix<T> output = input.multiply(weights_t);
// Add bias to each row
#pragma omp parallel for
for (size_t i = 0; i < output.rows(); ++i) {
for (size_t j = 0; j < output.cols(); ++j) {
output(i, j) += biases_(j);
}
}
last_output_ = output;
return output;
}
// Backward pass
std::pair<Matrix<T>, Matrix<T>> backward(const Matrix<T>& grad_output) const {
assert(grad_output.cols() == output_size_);
// Gradient w.r.t. weights: grad_weights = grad_output^T * input
Matrix<T> grad_output_t = grad_output.transpose();
Matrix<T> grad_weights = grad_output_t.multiply(last_input_);
// Gradient w.r.t. input: grad_input = grad_output * weights
Matrix<T> grad_input = grad_output.multiply(weights_);
return {grad_weights, grad_input};
}
// Parameter accessors
Matrix<T>& weights() { return weights_; }
const Matrix<T>& weights() const { return weights_; }
Tensor<T, 1>& biases() { return biases_; }
const Tensor<T, 1>& biases() const { return biases_; }
size_t input_size() const { return input_size_; }
size_t output_size() const { return output_size_; }
};
// Multi-layer perceptron with configurable architecture
template<FloatingPoint T>
class MLP {
private:
std::vector<std::unique_ptr<DenseLayer<T>>> layers_;
std::vector<std::function<void(const T*, T*, size_t)>> activations_;
std::vector<Matrix<T>> cached_outputs_;
public:
MLP() = default;
void add_layer(size_t input_size, size_t output_size,
std::function<void(const T*, T*, size_t)> activation = nullptr) {
layers_.emplace_back(std::make_unique<DenseLayer<T>>(input_size, output_size));
activations_.push_back(activation);
}
Matrix<T> forward(const Matrix<T>& input) {
cached_outputs_.clear();
cached_outputs_.reserve(layers_.size());
Matrix<T> current_output = input;
for (size_t i = 0; i < layers_.size(); ++i) {
current_output = layers_[i]->forward(current_output);
// Apply activation function if specified
if (activations_[i]) {
Matrix<T> activated_output = current_output;
activations_[i](current_output.data(), activated_output.data(),
current_output.size());
current_output = std::move(activated_output);
}
cached_outputs_.push_back(current_output);
}
return current_output;
}
// Backward pass with gradient computation
void backward(const Matrix<T>& loss_gradient) {
Matrix<T> current_gradient = loss_gradient;
// Backpropagate through layers in reverse order
for (int i = static_cast<int>(layers_.size()) - 1; i >= 0; --i) {
auto [weight_grad, input_grad] = layers_[i]->backward(current_gradient);
// Update current gradient for next layer
current_gradient = input_grad;
// Store gradients for optimizer (implementation depends on optimizer design)
// This would typically be handled by an optimizer class
}
}
// Get layers for parameter access
const std::vector<std::unique_ptr<DenseLayer<T>>>& layers() const { return layers_; }
std::vector<std::unique_ptr<DenseLayer<T>>>& layers() { return layers_; }
};
} // namespace ml::neural
// ═══════════════════════════════════════════════════════════════════════════════
// 5. OPTIMIZATION ALGORITHMS
// ═══════════════════════════════════════════════════════════════════════════════
namespace ml::optimizers {
// Base optimizer interface
template<FloatingPoint T>
class OptimizerBase {
public:
virtual ~OptimizerBase() = default;
virtual void update(Matrix<T>& weights, const Matrix<T>& gradients) = 0;
virtual void update(Tensor<T, 1>& biases, const Tensor<T, 1>& gradients) = 0;
virtual void reset() {}
};
// Stochastic Gradient Descent optimizer
template<FloatingPoint T>
class SGDOptimizer : public OptimizerBase<T> {
private:
T learning_rate_;
T momentum_;
T weight_decay_;
// Momentum buffers
std::unordered_map<void*, Matrix<T>> weight_momentum_;
std::unordered_map<void*, Tensor<T, 1>> bias_momentum_;
public:
SGDOptimizer(T learning_rate = T(0.01), T momentum = T(0.9), T weight_decay = T(0))
: learning_rate_(learning_rate), momentum_(momentum), weight_decay_(weight_decay) {}
void update(Matrix<T>& weights, const Matrix<T>& gradients) override {
void* weights_ptr = static_cast<void*>(&weights);
// Initialize momentum buffer if needed
if (weight_momentum_.find(weights_ptr) == weight_momentum_.end()) {
weight_momentum_[weights_ptr] = Matrix<T>(weights.rows(), weights.cols());
weight_momentum_[weights_ptr].fill(T(0));
}
Matrix<T>& momentum_buffer = weight_momentum_[weights_ptr];
// Apply weight decay if specified
Matrix<T> effective_gradients = gradients;
if (weight_decay_ > T(0)) {
#pragma omp parallel for
for (size_t i = 0; i < weights.size(); ++i) {
effective_gradients.data()[i] += weight_decay_ * weights.data()[i];
}
}
// Update momentum buffer: v = momentum * v + learning_rate * grad
#pragma omp parallel for
for (size_t i = 0; i < momentum_buffer.size(); ++i) {
momentum_buffer.data()[i] = momentum_ * momentum_buffer.data()[i] +
learning_rate_ * effective_gradients.data()[i];
}
// Update weights: w = w - v
#pragma omp parallel for
for (size_t i = 0; i < weights.size(); ++i) {
weights.data()[i] -= momentum_buffer.data()[i];
}
}
void update(Tensor<T, 1>& biases, const Tensor<T, 1>& gradients) override {
void* biases_ptr = static_cast<void*>(&biases);
// Initialize momentum buffer if needed
if (bias_momentum_.find(biases_ptr) == bias_momentum_.end()) {
bias_momentum_[biases_ptr] = Tensor<T, 1>(biases.size());
bias_momentum_[biases_ptr].fill(T(0));
}
Tensor<T, 1>& momentum_buffer = bias_momentum_[biases_ptr];
// Update momentum and biases
#pragma omp parallel for
for (size_t i = 0; i < biases.size(); ++i) {
momentum_buffer.data()[i] = momentum_ * momentum_buffer.data()[i] +
learning_rate_ * gradients.data()[i];
biases.data()[i] -= momentum_buffer.data()[i];
}
}
void reset() override {
weight_momentum_.clear();
bias_momentum_.clear();
}
};
// Adam optimizer with bias correction
template<FloatingPoint T>
class AdamOptimizer : public OptimizerBase<T> {
private:
T learning_rate_;
T beta1_;
T beta2_;
T epsilon_;
T weight_decay_;
size_t t_; // time step
// Adam state buffers
std::unordered_map<void*, Matrix<T>> weight_m_; // first moment
std::unordered_map<void*, Matrix<T>> weight_v_; // second moment
std::unordered_map<void*, Tensor<T, 1>> bias_m_;
std::unordered_map<void*, Tensor<T, 1>> bias_v_;
public:
AdamOptimizer(T learning_rate = T(0.001), T beta1 = T(0.9), T beta2 = T(0.999),
T epsilon = T(1e-8), T weight_decay = T(0))
: learning_rate_(learning_rate), beta1_(beta1), beta2_(beta2),
epsilon_(epsilon), weight_decay_(weight_decay), t_(0) {}
void update(Matrix<T>& weights, const Matrix<T>& gradients) override {
++t_;
void* weights_ptr = static_cast<void*>(&weights);
// Initialize buffers if needed
if (weight_m_.find(weights_ptr) == weight_m_.end()) {
weight_m_[weights_ptr] = Matrix<T>(weights.rows(), weights.cols());
weight_v_[weights_ptr] = Matrix<T>(weights.rows(), weights.cols());
weight_m_[weights_ptr].fill(T(0));
weight_v_[weights_ptr].fill(T(0));
}
Matrix<T>& m = weight_m_[weights_ptr];
Matrix<T>& v = weight_v_[weights_ptr];
// Apply weight decay if specified
Matrix<T> effective_gradients = gradients;
if (weight_decay_ > T(0)) {
#pragma omp parallel for
for (size_t i = 0; i < weights.size(); ++i) {
effective_gradients.data()[i] += weight_decay_ * weights.data()[i];
}
}
// Update biased first moment estimate: m = beta1 * m + (1 - beta1) * grad
// Update biased second moment estimate: v = beta2 * v + (1 - beta2) * grad²
#pragma omp parallel for
for (size_t i = 0; i < weights.size(); ++i) {
T grad = effective_gradients.data()[i];
m.data()[i] = beta1_ * m.data()[i] + (T(1) - beta1_) * grad;
v.data()[i] = beta2_ * v.data()[i] + (T(1) - beta2_) * grad * grad;
}
// Bias correction
T bias_correction1 = T(1) - std::pow(beta1_, static_cast<T>(t_));
T bias_correction2 = T(1) - std::pow(beta2_, static_cast<T>(t_));
// Update parameters
#pragma omp parallel for
for (size_t i = 0; i < weights.size(); ++i) {
T m_hat = m.data()[i] / bias_correction1;
T v_hat = v.data()[i] / bias_correction2;
weights.data()[i] -= learning_rate_ * m_hat / (std::sqrt(v_hat) + epsilon_);
}
}
void update(Tensor<T, 1>& biases, const Tensor<T, 1>& gradients) override {
void* biases_ptr = static_cast<void*>(&biases);
// Initialize buffers if needed
if (bias_m_.find(biases_ptr) == bias_m_.end()) {
bias_m_[biases_ptr] = Tensor<T, 1>(biases.size());
bias_v_[biases_ptr] = Tensor<T, 1>(biases.size());
bias_m_[biases_ptr].fill(T(0));
bias_v_[biases_ptr].fill(T(0));
}
Tensor<T, 1>& m = bias_m_[biases_ptr];
Tensor<T, 1>& v = bias_v_[biases_ptr];
// Update moments and biases (similar to weights)
#pragma omp parallel for
for (size_t i = 0; i < biases.size(); ++i) {
T grad = gradients.data()[i];
m.data()[i] = beta1_ * m.data()[i] + (T(1) - beta1_) * grad;
v.data()[i] = beta2_ * v.data()[i] + (T(1) - beta2_) * grad * grad;
}
T bias_correction1 = T(1) - std::pow(beta1_, static_cast<T>(t_));
T bias_correction2 = T(1) - std::pow(beta2_, static_cast<T>(t_));
#pragma omp parallel for
for (size_t i = 0; i < biases.size(); ++i) {
T m_hat = m.data()[i] / bias_correction1;
T v_hat = v.data()[i] / bias_correction2;
biases.data()[i] -= learning_rate_ * m_hat / (std::sqrt(v_hat) + epsilon_);
}
}
void reset() override {
weight_m_.clear();
weight_v_.clear();
bias_m_.clear();
bias_v_.clear();
t_ = 0;
}
};
} // namespace ml::optimizers
// ═══════════════════════════════════════════════════════════════════════════════
// 6. LOSS FUNCTIONS
// ═══════════════════════════════════════════════════════════════════════════════
namespace ml::loss {
template<FloatingPoint T>
class LossFunctions {
public:
// Mean Squared Error
static T mse_loss(const Matrix<T>& predictions, const Matrix<T>& targets) {
assert(predictions.shape() == targets.shape());
T sum = T(0);
#pragma omp parallel for reduction(+:sum)
for (size_t i = 0; i < predictions.size(); ++i) {
T diff = predictions.data()[i] - targets.data()[i];
sum += diff * diff;
}
return sum / static_cast<T>(predictions.size());
}