feat: scaffold FCES-native C++ project with libtorch integration
- CMakeLists.txt with libtorch, GoogleTest, GoogleBenchmark, OpenMP, pybind11 - Header files: config, controller, population, fitness, evolution, spectral, oscillation, telemetry, optimizer - Source implementations: controller (full micro-MLP forward pass, mutation, crossover), fitness (Welford's algorithm), oscillation (DFT), spectral (SVD rank), optimizer (sign-SGD stub) - Tests: controller, population, fitness, optimizer (Google Test) - Benchmarks: evolve throughput, optimizer step (Google Benchmark) - Examples: simple optimization, PyTorch/libtorch integration - Python extension: pybind11 bindings with setup.py - README with architecture diagram and build instructions
This commit is contained in:
40
src/spectral.cpp
Normal file
40
src/spectral.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "fces/spectral.hpp"
|
||||
#include <numeric>
|
||||
|
||||
namespace fces {
|
||||
|
||||
SpectralSensor::SpectralSensor(torch::nn::Module& /*model*/) {}
|
||||
|
||||
void SpectralSensor::track_layer(const std::string& name, const torch::Tensor& weight) {
|
||||
if (weight.dim() >= 2) {
|
||||
layer_ranks_[name] = compute_effective_rank(weight);
|
||||
}
|
||||
}
|
||||
|
||||
float SpectralSensor::get_global_rank() const {
|
||||
if (layer_ranks_.empty()) return 0.0f;
|
||||
float sum = 0.0f;
|
||||
for (const auto& [_, rank] : layer_ranks_) {
|
||||
sum += rank;
|
||||
}
|
||||
return sum / static_cast<float>(layer_ranks_.size());
|
||||
}
|
||||
|
||||
void SpectralSensor::reset() {
|
||||
layer_ranks_.clear();
|
||||
}
|
||||
|
||||
float SpectralSensor::compute_effective_rank(const torch::Tensor& weight) {
|
||||
// SVD-based effective rank (Shannon entropy of normalized singular values)
|
||||
auto svd = torch::linalg::svdvals(weight.to(torch::kFloat32));
|
||||
auto s = svd / svd.sum();
|
||||
auto log_s = torch::log(s + 1e-10f);
|
||||
float entropy = -(s * log_s).sum().item<float>();
|
||||
return std::exp(entropy);
|
||||
}
|
||||
|
||||
float SpectralController::compute_alpha(float global_rank, float grokking_coefficient) const {
|
||||
return global_rank * grokking_coefficient;
|
||||
}
|
||||
|
||||
} // namespace fces
|
||||
Reference in New Issue
Block a user