| 692 | } |
| 693 | |
| 694 | static std::pair<int, int> test_regression(ggml_backend_sched_t backend_sched, ggml_backend_t backend) { |
| 695 | int ntest = 0; |
| 696 | int npass = 0; |
| 697 | |
| 698 | // Test for simple regression with f(x) = a*x + b |
| 699 | |
| 700 | constexpr int64_t ndata_regression = 201; |
| 701 | constexpr float a_true = 1.2f; |
| 702 | constexpr float b_true = 3.4f; |
| 703 | |
| 704 | std::mt19937 gen(12345); |
| 705 | std::normal_distribution<float> nd{0.0f, 0.1f}; |
| 706 | |
| 707 | ggml_opt_dataset_t dataset = ggml_opt_dataset_init( |
| 708 | GGML_TYPE_F32, GGML_TYPE_F32, 1, 1, ndata_regression, ndata_regression); |
| 709 | |
| 710 | float * data = ggml_get_data_f32(ggml_opt_dataset_data( dataset)); |
| 711 | float * labels = ggml_get_data_f32(ggml_opt_dataset_labels(dataset)); |
| 712 | |
| 713 | constexpr float x_min = -100.0f; |
| 714 | constexpr float x_max = 100.0f; |
| 715 | |
| 716 | for (int64_t idata = 0; idata < ndata_regression; ++idata) { |
| 717 | const float x = x_min + (x_max - x_min) * idata/(ndata_regression-1); |
| 718 | const float y = a_true*x + b_true + nd(gen); |
| 719 | |
| 720 | data[idata] = x; |
| 721 | labels[idata] = y; |
| 722 | } |
| 723 | |
| 724 | struct ggml_context * ctx_static; |
| 725 | struct ggml_context * ctx_compute; |
| 726 | { |
| 727 | struct ggml_init_params params = { |
| 728 | /*.mem_size =*/ 3*ggml_tensor_overhead(), |
| 729 | /*.mem_buffer =*/ nullptr, |
| 730 | /*.no_alloc =*/ true, |
| 731 | }; |
| 732 | ctx_static = ggml_init(params); |
| 733 | } |
| 734 | { |
| 735 | struct ggml_init_params params = { |
| 736 | /*.mem_size =*/ GGML_DEFAULT_GRAPH_SIZE*ggml_tensor_overhead() + 3*ggml_graph_overhead(), |
| 737 | /*.mem_buffer =*/ nullptr, |
| 738 | /*.no_alloc =*/ true, |
| 739 | }; |
| 740 | ctx_compute = ggml_init(params); |
| 741 | } |
| 742 | |
| 743 | // The first dimension is the dimension of the datapoints, the second dimension is the number of datapoints. |
| 744 | struct ggml_tensor * x = ggml_new_tensor_2d(ctx_static, GGML_TYPE_F32, 1, ndata_regression); |
| 745 | ggml_set_name(x, "x"); |
| 746 | |
| 747 | struct ggml_tensor * a = ggml_new_tensor_1d(ctx_static, GGML_TYPE_F32, 1); |
| 748 | ggml_set_name(a, "a"); |
| 749 | ggml_set_param(a); |
| 750 | |
| 751 | struct ggml_tensor * b = ggml_new_tensor_1d(ctx_static, GGML_TYPE_F32, 1); |
no test coverage detected