File size: 1,863 Bytes
26d5b81 | 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 | #include <iostream>
#include "../include/neuroflow/tensor.hpp"
#include "../include/neuroflow/networks.hpp"
using namespace neuroflow;
int main() {
std::cout << "Linear full test..." << std::endl;
Linear linear(64, 64, true); // use_bias = true
std::cout << "weight shape: [" << linear.weight.shape_[0] << ", " << linear.weight.shape_[1] << "]" << std::endl;
std::cout << "bias shape size: " << linear.bias.shape_.size() << std::endl;
std::cout << "bias shape[0]: " << linear.bias.shape_[0] << std::endl;
Tensor input({1, 64});
float* d = input.as_fp32();
for (size_t i = 0; i < input.numel(); ++i) d[i] = 0.1f * i;
std::cout << "input shape: [" << input.shape_[0] << ", " << input.shape_[1] << "]" << std::endl;
std::cout << "input numel: " << input.numel() << std::endl;
// 手动执行 forward 的步骤
Tensor output({input.shape_[0], linear.weight.shape_[0]});
std::cout << "output shape: [" << output.shape_[0] << ", " << output.shape_[1] << "]" << std::endl;
std::cout << "Calling gemm..." << std::endl;
TensorOps::gemm(input, linear.weight, output, false, true);
std::cout << "gemm done" << std::endl;
float* out = output.as_fp32();
float* b = linear.bias.as_fp32();
std::cout << "Adding bias..." << std::endl;
std::cout << "output.shape_[0]=" << output.shape_[0] << std::endl;
std::cout << "output.shape_[1]=" << output.shape_[1] << std::endl;
for (size_t i = 0; i < output.shape_[0]; ++i) {
for (size_t j = 0; j < output.shape_[1]; ++j) {
out[i * output.shape_[1] + j] += b[j];
}
}
std::cout << "First 5 output values: ";
for (size_t i = 0; i < 5; ++i) std::cout << out[i] << " ";
std::cout << std::endl;
std::cout << "Success!" << std::endl;
return 0;
}
|