|
| 1 | +#include <cmath> |
| 2 | +#include <vector> |
| 3 | +#include <iostream> |
| 4 | +#include <stdexcept> |
| 5 | + |
| 6 | +#include "cppflow/cppflow.h" |
| 7 | + |
| 8 | +bool float_equal(const float f1, const float f2) { |
| 9 | + return std::abs(f1/f2-1.0f) < 1e-6; |
| 10 | +} |
| 11 | + |
| 12 | +void test1(const bool is_cpu) { |
| 13 | + std::cout << "test1 starts: is_cpu=" << is_cpu << std::endl; |
| 14 | + float target = 1.0; |
| 15 | + int64_t ndim = 2; |
| 16 | + cppflow::tensor t1; |
| 17 | + |
| 18 | + if(is_cpu) { |
| 19 | + std::vector<float> _data(ndim, target); |
| 20 | + t1 = cppflow::tensor(_data, {ndim}); |
| 21 | + } else { |
| 22 | + t1 = cppflow::fill({ndim}, target); |
| 23 | + } |
| 24 | + |
| 25 | + std::cout << "tensor::device(true) : " << t1.device(true) << std::endl; |
| 26 | + std::cout << "tensor::device(false) : " << t1.device(false) << std::endl; |
| 27 | + |
| 28 | + auto t1_tensor = t1.get_tensor(); |
| 29 | + auto raw_data = static_cast<float*>(TF_TensorData(t1_tensor.get())); |
| 30 | + float result_value = raw_data[0]; |
| 31 | + if(float_equal(result_value, target)) { |
| 32 | + std::cout << "tensor::get_tensor() test1-1: pass" << std::endl; |
| 33 | + } else { |
| 34 | + std::cout << "tensor::get_tensor() test1-1: result_value=" << result_value << ", target=" << target << std::endl; |
| 35 | + throw std::runtime_error("tensor::get_tensor() test1-1: failed"); |
| 36 | + } |
| 37 | + |
| 38 | + // IMPORTANT NOTE: CANNOT modify the returned cache |
| 39 | + float target2 = target + 10.0; |
| 40 | + raw_data[1] = target2; |
| 41 | + result_value = t1.get_data<float>()[0]; |
| 42 | + float result_value2 = t1.get_data<float>()[1]; |
| 43 | + if(float_equal(result_value, target)) { |
| 44 | + std::cout << "tensor::get_tensor() test1-2: pass" << std::endl; |
| 45 | + } else { |
| 46 | + std::cout << "tensor::get_tensor() test1-2: failed, result_value=" << result_value << ", target=" << target << std::endl; |
| 47 | + throw std::runtime_error("tensor::get_tensor() test1-2: failed"); |
| 48 | + } |
| 49 | + if(float_equal(result_value2, target2)) { |
| 50 | + std::cout << "tensor::get_tensor() test1-3: pass" << std::endl; |
| 51 | + } else { |
| 52 | + std::cout << "The failure of test1-3 is not considered as a bug." << std::endl; |
| 53 | + std::cout << "tensor::get_tensor() test1-3: failed, result_value=" << result_value2 << ", target2=" << target2 << std::endl; |
| 54 | + } |
| 55 | + |
| 56 | + auto t2 = t1 + cppflow::tensor(0.f); |
| 57 | + std::cout << "Can NOT modify the cache!" << std::endl; |
| 58 | + std::cout << "t2: " << t2 << std::endl; |
| 59 | + |
| 60 | + auto dt = cppflow::to_string(t1.dtype()); |
| 61 | + std::string expected_dtype{"TF_FLOAT"}; |
| 62 | + if(dt == expected_dtype) { |
| 63 | + std::cout << "tensor::get_tensor() test1-4: pass" << std::endl; |
| 64 | + } else { |
| 65 | + std::cout << "tensor::get_tensor() test1-4: dtype=" << dt << ", expected_dtype=" << expected_dtype << std::endl; |
| 66 | + throw std::runtime_error("tensor::get_tensor() test1-4: failed"); |
| 67 | + } |
| 68 | + |
| 69 | + auto shape_tensor = t1.shape(); |
| 70 | + auto shape = shape_tensor.get_data<int32_t>()[0]; |
| 71 | + if(shape == ndim) { |
| 72 | + std::cout << "tensor::get_tensor() test1-5: pass" << std::endl; |
| 73 | + } else { |
| 74 | + std::cout << "tensor::get_tensor() test1-5: shape_tensor.dtype()=" << cppflow::to_string(shape_tensor.dtype()) << std::endl; |
| 75 | + std::cout << "tensor::get_tensor() test1-5: shape_tensor=" << shape_tensor << std::endl; |
| 76 | + std::cout << "tensor::get_tensor() test1-5: shape()=" << shape << ", ndim=" << ndim << std::endl; |
| 77 | + throw std::runtime_error("tensor::get_tensor() test1-5: failed"); |
| 78 | + } |
| 79 | + |
| 80 | + std::cout << std::endl; |
| 81 | +} |
| 82 | + |
| 83 | +int main() { |
| 84 | + test1(true); |
| 85 | + test1(false); |
| 86 | + |
| 87 | + return 0; |
| 88 | +} |
0 commit comments