MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / init_tensor_uniform

Function init_tensor_uniform

smallthinker/tests/test-backend-ops.cpp:39–128  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37#include <vector>
38
39static void init_tensor_uniform(ggml_tensor * tensor, float min = -1.0f, float max = 1.0f) {
40 size_t nels = ggml_nelements(tensor);
41 std::vector<float> data(nels);
42 {
43 // parallel initialization
44 static const size_t n_threads = std::thread::hardware_concurrency();
45 // static RNG initialization (revisit if n_threads stops being constant)
46 static std::vector<std::default_random_engine> generators = []() {
47 std::random_device rd;
48 std::vector<std::default_random_engine> vec;
49 vec.reserve(n_threads);
50 //for (size_t i = 0; i < n_threads; i++) { vec.emplace_back(1234 + i); } // fixed seed
51 for (size_t i = 0; i < n_threads; i++) { vec.emplace_back(rd()); }
52 return vec;
53 }();
54
55 auto init_thread = [&](size_t ith, size_t start, size_t end) {
56 std::uniform_real_distribution<float> distribution(min, max);
57 auto & gen = generators[ith];
58 for (size_t i = start; i < end; i++) {
59 data[i] = distribution(gen);
60 }
61 };
62
63 std::vector<std::future<void>> tasks;
64 tasks.reserve(n_threads);
65 for (size_t i = 0; i < n_threads; i++) {
66 size_t start = i*nels/n_threads;
67 size_t end = (i+1)*nels/n_threads;
68 tasks.push_back(std::async(std::launch::async, init_thread, i, start, end));
69 }
70 for (auto & t : tasks) {
71 t.get();
72 }
73 }
74
75 if (tensor->type == GGML_TYPE_F32 || tensor->type == GGML_TYPE_I32) {
76 ggml_backend_tensor_set(tensor, data.data(), 0, nels * sizeof(float));
77 } else if (ggml_is_quantized(tensor->type) || tensor->type == GGML_TYPE_F16 || tensor->type == GGML_TYPE_BF16) {
78 GGML_ASSERT(nels % ggml_blck_size(tensor->type) == 0);
79
80 // dummy importance matrix
81 std::vector<float> imatrix(tensor->ne[0], 1.0f);
82 const float * im = imatrix.data();
83 if (!ggml_quantize_requires_imatrix(tensor->type)) {
84 // when the imatrix is optional, we want to test both quantization with and without imatrix
85 // use one of the random numbers to decide
86 if (data[0] > 0.5f*(min + max)) {
87 im = nullptr;
88 }
89 }
90
91 std::vector<uint8_t> dataq(ggml_row_size(tensor->type, nels));
92 {
93 // parallel quantization by block
94 size_t blck_size = ggml_blck_size(tensor->type);
95 size_t n_blocks = nels / blck_size;
96

Callers 15

initialize_tensorsMethod · 0.85
initialize_tensorsMethod · 0.85
initialize_tensorsMethod · 0.85
initialize_tensorsMethod · 0.85
initialize_tensorsMethod · 0.85
initialize_tensorsMethod · 0.85
initialize_tensorsMethod · 0.85
initialize_tensorsMethod · 0.85
initialize_tensorsMethod · 0.85
initialize_tensorsMethod · 0.85
initialize_tensorsMethod · 0.85
initialize_tensorsMethod · 0.85

Calls 13

ggml_row_sizeFunction · 0.85
reserveMethod · 0.80
ggml_nelementsFunction · 0.50
ggml_backend_tensor_setFunction · 0.50
ggml_is_quantizedFunction · 0.50
ggml_blck_sizeFunction · 0.50
ggml_quantize_chunkFunction · 0.50
ggml_nbytesFunction · 0.50
push_backMethod · 0.45
getMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected