now crashing
This commit is contained in:
parent
b484674707
commit
7ac56bdc62
1 changed files with 33 additions and 16 deletions
49
ggml.cpp
49
ggml.cpp
|
@ -9426,23 +9426,37 @@ void ggml_tensor_checksum(const char * name,const struct ggml_tensor * tensor);
|
||||||
void ggml_tensor_hash(const char * name,const struct ggml_tensor * tensor, int decimalPlace);
|
void ggml_tensor_hash(const char * name,const struct ggml_tensor * tensor, int decimalPlace);
|
||||||
#include "ggml-backend-impl.h"
|
#include "ggml-backend-impl.h"
|
||||||
// helper function to convert the tensor buffer to a float array
|
// helper function to convert the tensor buffer to a float array
|
||||||
float* ggml_tensor_to_float(const ggml_tensor& tensor, size_t* out_size) {
|
float* ggml_tensor_to_float(const ggml_tensor* tensor) {
|
||||||
//if (tensor->type != GGML_TYPE_FLOAT) {
|
//if (tensor->type != GGML_TYPE_FLOAT) {
|
||||||
//throw std::runtime_error("Only support for floating-point tensors");
|
//throw std::runtime_error("Only support for floating-point tensors");
|
||||||
//}
|
//}
|
||||||
const size_t num_elements = tensor->n_dims > 0 ? std::accumulate(tensor->nb, tensor->nb + tensor->n_dims, 1) : 0;
|
//if (out_size) {
|
||||||
float* buffer = new float[num_elements];
|
// *out_size = num_elements;
|
||||||
if (out_size) {
|
// }
|
||||||
*out_size = num_elements;
|
|
||||||
}
|
if(tensor->type == GGML_TYPE_F32)
|
||||||
memcpy(buffer, ggml_get_data_f32(tensor), ggml_nbytes(tensor));
|
{
|
||||||
|
const size_t num_elements = tensor->n_dims > 0 ? std::accumulate(tensor->nb, tensor->nb + tensor->n_dims, 1) : 0;
|
||||||
|
float* buffer = new float[num_elements];
|
||||||
|
|
||||||
|
memcpy(buffer, ggml_get_data_f32(tensor), ggml_nbytes(tensor));
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const size_t num_elements = ggml_nbytes(tensor)/sizeof(float);
|
||||||
|
float* buffer = new float[num_elements];
|
||||||
|
|
||||||
|
memcpy(buffer, (float*)ggml_get_data(tensor), ggml_nbytes(tensor));
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
//memcpy(vec, ggml_get_data_f32(embeddings), ggml_nbytes(embeddings));
|
//memcpy(vec, ggml_get_data_f32(embeddings), ggml_nbytes(embeddings));
|
||||||
return buffer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// function to create a hash table of the N most common values of a given tensor
|
// function to create a hash table of the N most common values of a given tensor
|
||||||
std::vector<double> find_n_most_common_values(const ggml_tensor& tensor, int decimal_place, size_t top_n) {
|
std::vector<double> find_n_most_common_values(const ggml_tensor* tensor, int decimal_place, size_t top_n) {
|
||||||
float* buffer = ggml_tensor_to_float(tensor, nullptr);
|
float* buffer = ggml_tensor_to_float(tensor);
|
||||||
auto values = std::unordered_map<double, int>(); // hash table to store the count of each value
|
auto values = std::unordered_map<double, int>(); // hash table to store the count of each value
|
||||||
|
|
||||||
if (decimal_place <= 0 || top_n <= 0) {
|
if (decimal_place <= 0 || top_n <= 0) {
|
||||||
|
@ -9450,16 +9464,19 @@ std::vector<double> find_n_most_common_values(const ggml_tensor& tensor, int dec
|
||||||
}
|
}
|
||||||
|
|
||||||
// find N most common values by counting the frequency of each value with truncated decimal places
|
// find N most common values by counting the frequency of each value with truncated decimal places
|
||||||
for (size_t i = 0; i < buffer->size(); ++i) {
|
auto size = ggml_nbytes(tensor)/sizeof(float);
|
||||||
|
for (size_t i = 0; i < size; ++i) {
|
||||||
const double value = std::pow(10, static_cast<double>(decimal_place));
|
const double value = std::pow(10, static_cast<double>(decimal_place));
|
||||||
buffer[i] *= value; // multiply by value to truncate decimal places
|
buffer[i] *= value; // multiply by value to truncate decimal places
|
||||||
int count = values.find(buffer[i])->second + 1;
|
if (values.find(buffer[i]) != values.end()){
|
||||||
if (count > top_n) {
|
int count = values.find(buffer[i])->second + 1;
|
||||||
|
if (count > top_n) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (decimal_place <= 0 || count >= top_n) {
|
if (decimal_place <= 0 || count >= top_n) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort the values in descending order of frequency
|
// sort the values in descending order of frequency
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue