cwenzi/neuroflow-cpp
1
1#include <iostream>2#include <exception>3#include "../include/neuroflow/model.hpp"4#include "../include/neuroflow/memory.hpp"5#include "../include/neuroflow/networks.hpp"6 7using namespace neuroflow;8 9int main() {10 try {11 std::cout << "Step by step forward test..." << std::endl;12 13 NeuroFlowModel::Config cfg;14 cfg.input_dim = 64;15 cfg.hidden_dim = 32;16 cfg.output_dim = 5;17 cfg.memory_slots = 8;18 cfg.memory_dim = 16;19 cfg.num_layers = 1;20 cfg.num_associations = 2;21 cfg.use_mla = false;22 23 NeuroFlowModel model(cfg);24 size_t batch = 2;25 26 Tensor x({batch, cfg.input_dim});27 for (size_t i = 0; i < x.numel(); ++i) x.as_fp32()[i] = 0.1f * i;28 29 std::cout << "1. Input projection..." << std::endl;30 Tensor h = model.input_proj_linear->forward(x);31 std::cout << " h shape: [" << h.shape_[0] << ", " << h.shape_[1] << "]" << std::endl;32 h = model.input_proj_norm->forward(h);33 h = model.input_proj_gelu->forward(h);34 std::cout << " after norm/gelu: [" << h.shape_[0] << ", " << h.shape_[1] << "]" << std::endl;35 36 std::cout << "2. SN forward..." << std::endl;37 auto sn_out = model.sn->forward(h);38 std::cout << " saliency: [" << sn_out.saliency.shape_[0] << ", " << sn_out.saliency.shape_[1] << "]" << std::endl;39 std::cout << " gates: [" << sn_out.gates.shape_[0] << ", " << sn_out.gates.shape_[1] << "]" << std::endl;40 41 std::cout << "3. ECN forward..." << std::endl;42 auto ecn_out = model.ecn->forward(h);43 std::cout << " decision: [" << ecn_out.decision.shape_[0] << ", " << ecn_out.decision.shape_[1] << "]" << std::endl;44 std::cout << " value: [" << ecn_out.value.shape_[0] << ", " << ecn_out.value.shape_[1] << "]" << std::endl;45 46 std::cout << "4. Memory encode..." << std::endl;47 Tensor mem_seed = model.memory->encode(h);48 std::cout << " mem_seed: [" << mem_seed.shape_[0] << ", " << mem_seed.shape_[1] << "]" << std::endl;49 50 std::cout << "5. DMN forward..." << std::endl;51 auto dmn_out = model.dmn->forward(mem_seed);52 std::cout << " vision shape size: " << dmn_out.vision.shape_.size() << std::endl;53 for (size_t i = 0; i < dmn_out.vision.shape_.size(); ++i) 54 std::cout << " dim " << i << ": " << dmn_out.vision.shape_[i] << std::endl;55 56 std::cout << "6. Memory retrieve..." << std::endl;57 auto mem_out = model.memory->forward(h);58 std::cout << " retrieved: [" << mem_out.retrieved.shape_[0] << ", " << mem_out.retrieved.shape_[1] << "]" << std::endl;59 60 std::cout << "7. Reshaping dmn_out.vision..." << std::endl;61 std::cout << " vision numel: " << dmn_out.vision.numel() << std::endl;62 std::cout << " trying reshape to [" << batch << ", " << dmn_out.vision.shape_[1] << "]" << std::endl;63 std::cout << " expected numel: " << (batch * dmn_out.vision.shape_[1]) << std::endl;64 65 if (dmn_out.vision.numel() != batch * dmn_out.vision.shape_[1]) {66 std::cout << " MISMATCH! vision actual shape may be different" << std::endl;67 }68 69 Tensor dmn_weighted = dmn_out.vision.reshape({batch, dmn_out.vision.shape_[1]});70 std::cout << " reshape success" << std::endl;71 72 std::cout << "All steps passed!" << std::endl;73 return 0;74 } catch (const std::exception& e) {75 std::cout << "Error: " << e.what() << std::endl;76 return 1;77 }78}79 