| 68 | |
| 69 | template <typename T> |
| 70 | void run_multinomial(Handle* handle) { |
| 71 | using ctype = typename DTypeTrait<T>::ctype; |
| 72 | auto opr = handle->create_operator<MultinomialRNG>(); |
| 73 | |
| 74 | size_t num_groups = 2; |
| 75 | size_t num_samples = 10000; |
| 76 | size_t len_probs = 4; |
| 77 | bool replacement = true; |
| 78 | |
| 79 | TensorLayout ly_out{TensorShape{num_groups, num_samples}, dtype::Int32()}; |
| 80 | TensorLayout ly_probs{TensorShape{num_groups, len_probs}, T()}; |
| 81 | SyncedTensor<dt_int32> out(handle, ly_out); |
| 82 | SyncedTensor<ctype> probs(handle, ly_probs); |
| 83 | |
| 84 | auto probs_ptr = probs.ptr_mutable_host(); |
| 85 | probs_ptr[0] = 0.1; |
| 86 | probs_ptr[1] = 0.2; |
| 87 | probs_ptr[2] = 0.3; |
| 88 | probs_ptr[3] = 0.4; |
| 89 | probs_ptr[4] = 0.0; |
| 90 | probs_ptr[5] = 0.7; |
| 91 | probs_ptr[6] = 0.2; |
| 92 | probs_ptr[7] = 0.1; |
| 93 | |
| 94 | Tensor<dt_byte> workspace( |
| 95 | handle, {TensorShape{opr->get_workspace_in_bytes(ly_probs, ly_out)}, |
| 96 | dtype::Byte()}); |
| 97 | |
| 98 | opr->param().num_samples = num_samples; |
| 99 | opr->param().replacement = replacement; |
| 100 | opr->exec( |
| 101 | probs.tensornd_dev(), out.tensornd_dev(), |
| 102 | {workspace.ptr(), workspace.layout().total_nr_elems()}); |
| 103 | |
| 104 | auto ptr = out.ptr_mutable_host(); |
| 105 | |
| 106 | std::vector<float> sample_probs(num_groups * len_probs, 0); |
| 107 | for (size_t i = 0; i < num_groups; ++i) { |
| 108 | for (size_t j = 0; j < num_samples; ++j) { |
| 109 | sample_probs[i * len_probs + ptr[i * num_samples + j]] += 1; |
| 110 | } |
| 111 | } |
| 112 | for (size_t i = 0; i < num_groups * len_probs; ++i) { |
| 113 | sample_probs[i] /= num_samples; |
| 114 | } |
| 115 | |
| 116 | for (size_t i = 0; i < num_groups * len_probs; ++i) { |
| 117 | ASSERT_LE(std::abs(sample_probs[i] - probs_ptr[i]), 1e-2); |
| 118 | } |
| 119 | |
| 120 | std::vector<float> float_data_group0; |
| 121 | std::vector<float> float_data_group1; |
| 122 | for (size_t i = 0; i < num_samples; ++i) { |
| 123 | float_data_group0.push_back(static_cast<float>(ptr[i])); |
| 124 | } |
| 125 | for (size_t i = num_samples; i < 2 * num_samples; ++i) { |
| 126 | float_data_group1.push_back(static_cast<float>(ptr[i])); |
| 127 | } |
nothing calls this directly
no test coverage detected