Brunobkr/llama.cpp_AlgMor24_github
ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.
03.1k
1/*!2 Copyright 2013 Lovell Fuller and others.3 SPDX-License-Identifier: Apache-2.04*/5 6#include <iostream>7#include <numeric>8#include <string>9#include <vector>10 11#include <napi.h>12#include <vips/vips8>13 14#include "./common.h"15#include "./stats.h"16 17class StatsWorker : public Napi::AsyncWorker {18 public:19 StatsWorker(Napi::Function callback, StatsBaton *baton, Napi::Function debuglog) :20 Napi::AsyncWorker(callback), baton(baton), debuglog(Napi::Persistent(debuglog)) {}21 ~StatsWorker() {}22 23 const int STAT_MIN_INDEX = 0;24 const int STAT_MAX_INDEX = 1;25 const int STAT_SUM_INDEX = 2;26 const int STAT_SQ_SUM_INDEX = 3;27 const int STAT_MEAN_INDEX = 4;28 const int STAT_STDEV_INDEX = 5;29 const int STAT_MINX_INDEX = 6;30 const int STAT_MINY_INDEX = 7;31 const int STAT_MAXX_INDEX = 8;32 const int STAT_MAXY_INDEX = 9;33 34 void Execute() {35 // Decrement queued task counter36 sharp::counterQueue--;37 38 vips::VImage image;39 sharp::ImageType imageType = sharp::ImageType::UNKNOWN;40 try {41 std::tie(image, imageType) = OpenInput(baton->input);42 } catch (std::runtime_error const &err) {43 (baton->err).append(err.what());44 }45 if (imageType != sharp::ImageType::UNKNOWN) {46 try {47 vips::VImage stats = image.stats();48 int const bands = image.bands();49 for (int b = 1; b <= bands; b++) {50 ChannelStats cStats(51 static_cast<int>(stats.getpoint(STAT_MIN_INDEX, b).front()),52 static_cast<int>(stats.getpoint(STAT_MAX_INDEX, b).front()),53 stats.getpoint(STAT_SUM_INDEX, b).front(),54 stats.getpoint(STAT_SQ_SUM_INDEX, b).front(),55 stats.getpoint(STAT_MEAN_INDEX, b).front(),56 stats.getpoint(STAT_STDEV_INDEX, b).front(),57 static_cast<int>(stats.getpoint(STAT_MINX_INDEX, b).front()),58 static_cast<int>(stats.getpoint(STAT_MINY_INDEX, b).front()),59 static_cast<int>(stats.getpoint(STAT_MAXX_INDEX, b).front()),60 static_cast<int>(stats.getpoint(STAT_MAXY_INDEX, b).front()));61 baton->channelStats.push_back(cStats);62 }63 // Image is not opaque when alpha layer is present and contains a non-mamixa value64 if (image.has_alpha()) {65 double const minAlpha = static_cast<double>(stats.getpoint(STAT_MIN_INDEX, bands).front());66 if (minAlpha != vips_interpretation_max_alpha(image.interpretation())) {67 baton->isOpaque = false;68 }69 }70 // Convert to greyscale71 vips::VImage greyscale = image.colourspace(VIPS_INTERPRETATION_B_W)[0];72 // Estimate entropy via histogram of greyscale value frequency73 baton->entropy = std::abs(greyscale.hist_find().hist_entropy());74 // Estimate sharpness via standard deviation of greyscale laplacian75 if (image.width() > 1 || image.height() > 1) {76 VImage laplacian = VImage::new_matrixv(3, 3,77 0.0, 1.0, 0.0,78 1.0, -4.0, 1.0,79 0.0, 1.0, 0.0);80 laplacian.set("scale", 9.0);81 baton->sharpness = greyscale.conv(laplacian).deviate();82 }83 // Most dominant sRGB colour via 4096-bin 3D histogram84 vips::VImage hist = sharp::RemoveAlpha(image)85 .colourspace(VIPS_INTERPRETATION_sRGB)86 .hist_find_ndim(VImage::option()->set("bins", 16));87 std::complex<double> maxpos = hist.maxpos();88 int const dx = static_cast<int>(std::real(maxpos));89 int const dy = static_cast<int>(std::imag(maxpos));90 std::vector<double> pel = hist(dx, dy);91 int const dz = std::distance(pel.begin(), std::find(pel.begin(), pel.end(), hist.max()));92 baton->dominantRed = dx * 16 + 8;93 baton->dominantGreen = dy * 16 + 8;94 baton->dominantBlue = dz * 16 + 8;95 } catch (std::runtime_error const &err) {96 (baton->err).append(err.what());97 }98 }99 100 // Clean up101 vips_error_clear();102 vips_thread_shutdown();103 }104 105 void OnOK() {106 Napi::Env env = Env();107 Napi::HandleScope scope(env);108 109 // Handle warnings110 std::string warning = sharp::VipsWarningPop();111 while (!warning.empty()) {112 debuglog.SHARP_CALLBACK_FN_NAME(Receiver().Value(), { Napi::String::New(env, warning) });113 warning = sharp::VipsWarningPop();114 }115 if (baton->err.empty()) {116 // Stats Object117 Napi::Object info = Napi::Object::New(env);118 Napi::Array channels = Napi::Array::New(env);119 120 std::vector<ChannelStats>::iterator it;121 int i = 0;122 for (it = baton->channelStats.begin(); it < baton->channelStats.end(); it++, i++) {123 Napi::Object channelStat = Napi::Object::New(env);124 channelStat.Set("min", it->min);125 channelStat.Set("max", it->max);126 channelStat.Set("sum", it->sum);127 channelStat.Set("squaresSum", it->squaresSum);128 channelStat.Set("mean", it->mean);129 channelStat.Set("stdev", it->stdev);130 channelStat.Set("minX", it->minX);131 channelStat.Set("minY", it->minY);132 channelStat.Set("maxX", it->maxX);133 channelStat.Set("maxY", it->maxY);134 channels.Set(i, channelStat);135 }136 137 info.Set("channels", channels);138 info.Set("isOpaque", baton->isOpaque);139 info.Set("entropy", baton->entropy);140 info.Set("sharpness", baton->sharpness);141 Napi::Object dominant = Napi::Object::New(env);142 dominant.Set("r", baton->dominantRed);143 dominant.Set("g", baton->dominantGreen);144 dominant.Set("b", baton->dominantBlue);145 info.Set("dominant", dominant);146 Callback().SHARP_CALLBACK_FN_NAME(Receiver().Value(), { env.Null(), info });147 } else {148 Callback().SHARP_CALLBACK_FN_NAME(Receiver().Value(),149 { Napi::Error::New(env, sharp::TrimEnd(baton->err)).Value() });150 }151 152 delete baton->input;153 delete baton;154 }155 156 private:157 StatsBaton* baton;158 Napi::FunctionReference debuglog;159};160 161/*162 stats(options, callback)163*/164Napi::Value stats(const Napi::CallbackInfo& info) {165 // V8 objects are converted to non-V8 types held in the baton struct166 StatsBaton *baton = new StatsBaton;167 Napi::Object options = info[size_t(0)].As<Napi::Object>();168 169 // Input170 baton->input = sharp::CreateInputDescriptor(options.Get("input").As<Napi::Object>());171 baton->input->access = VIPS_ACCESS_RANDOM;172 173 // Function to notify of libvips warnings174 Napi::Function debuglog = options.Get("debuglog").As<Napi::Function>();175 176 // Join queue for worker thread177 Napi::Function callback = info[size_t(1)].As<Napi::Function>();178 StatsWorker *worker = new StatsWorker(callback, baton, debuglog);179 worker->Receiver().Set("options", options);180 worker->Queue();181 182 // Increment queued task counter183 sharp::counterQueue++;184 185 return info.Env().Undefined();186}187 