CoolFace
Modelpublic

cwenzi/neuroflow-cpp

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
1likes
linear_full_test.cpp51 linesDownload Raw Back to tests
1#include <iostream>2#include "../include/neuroflow/tensor.hpp"3#include "../include/neuroflow/networks.hpp"4 5using namespace neuroflow;6 7int main() {8    std::cout << "Linear full test..." << std::endl;9    10    Linear linear(64, 64, true);  // use_bias = true11    12    std::cout << "weight shape: [" << linear.weight.shape_[0] << ", " << linear.weight.shape_[1] << "]" << std::endl;13    std::cout << "bias shape size: " << linear.bias.shape_.size() << std::endl;14    std::cout << "bias shape[0]: " << linear.bias.shape_[0] << std::endl;15    16    Tensor input({1, 64});17    float* d = input.as_fp32();18    for (size_t i = 0; i < input.numel(); ++i) d[i] = 0.1f * i;19    20    std::cout << "input shape: [" << input.shape_[0] << ", " << input.shape_[1] << "]" << std::endl;21    std::cout << "input numel: " << input.numel() << std::endl;22    23    // 手动执行 forward 的步骤24    Tensor output({input.shape_[0], linear.weight.shape_[0]});25    std::cout << "output shape: [" << output.shape_[0] << ", " << output.shape_[1] << "]" << std::endl;26    27    std::cout << "Calling gemm..." << std::endl;28    TensorOps::gemm(input, linear.weight, output, false, true);29    std::cout << "gemm done" << std::endl;30    31    float* out = output.as_fp32();32    float* b = linear.bias.as_fp32();33    34    std::cout << "Adding bias..." << std::endl;35    std::cout << "output.shape_[0]=" << output.shape_[0] << std::endl;36    std::cout << "output.shape_[1]=" << output.shape_[1] << std::endl;37    38    for (size_t i = 0; i < output.shape_[0]; ++i) {39        for (size_t j = 0; j < output.shape_[1]; ++j) {40            out[i * output.shape_[1] + j] += b[j];41        }42    }43    44    std::cout << "First 5 output values: ";45    for (size_t i = 0; i < 5; ++i) std::cout << out[i] << " ";46    std::cout << std::endl;47    48    std::cout << "Success!" << std::endl;49    return 0;50}51