38 omp_set_num_threads(1);
79typedef void *(*ck_strict_mtmd_clip_init_fn)(
const char *, int, int, int, int, int);
91 const char *gguf_path = getenv(
"CK_STRICT_GGUF_PATH");
92 const char *shim_path = getenv(
"CK_STRICT_MTMD_SHIM_SO");
93 if (!gguf_path || !gguf_path[0] || !shim_path || !shim_path[0]) {
96 if (!planar || !out || channels != 3 || height <= 0 || width <= 0) {
100 void *shim = dlopen(shim_path, RTLD_LAZY | RTLD_LOCAL);
114 if (!init_fn || !free_fn || !embd_nbytes_fn || !encode_fn) {
119 const size_t pixel_count = (size_t) height * (
size_t) width;
120 float *interleaved = (
float *) malloc(pixel_count * (
size_t) channels *
sizeof(float));
125 for (
size_t idx = 0; idx < pixel_count; ++idx) {
126 interleaved[idx * 3 + 0] = planar[idx];
127 interleaved[idx * 3 + 1] = planar[pixel_count + idx];
128 interleaved[idx * 3 + 2] = planar[2 * pixel_count + idx];
132 void *handle = init_fn(gguf_path, 0, 0, 0, 0, 0);
134 const size_t needed_bytes = embd_nbytes_fn(handle, width, height);
135 if (needed_bytes > 0 && needed_bytes <= out_elems *
sizeof(
float)) {
136 ok = encode_fn(handle, 1, interleaved, height, width, out) ? 1 : 0;
155 const char *val = getenv(name);
156 if (!val || !val[0]) {
162 long n = strtol(val, &
end, 10);
163 if (errno != 0 ||
end == val || n <= 0 || n > (1L << 20)) {
172 int physical_cores = 0;
173 int logical_cores = (int)sysconf(_SC_NPROCESSORS_ONLN);
174 if (logical_cores <= 0) {
178 int cpu_cores_hint = 0;
179 int siblings_hint = 0;
182 FILE *f = fopen(
"/proc/cpuinfo",
"r");
185 int physical_id = -1;
194 const int seen_cap = (int)(
sizeof(seen) /
sizeof(seen[0]));
197 #define CK_ADD_PAIR(pid, cid) \
199 if ((pid) >= 0 && (cid) >= 0) { \
201 for (int ii = 0; ii < seen_count; ++ii) { \
202 if (seen[ii].physical_id == (pid) && \
203 seen[ii].core_id == (cid)) { \
208 if (!exists && seen_count < seen_cap) { \
209 seen[seen_count].physical_id = (pid); \
210 seen[seen_count].core_id = (cid); \
216 while (fgets(line,
sizeof(line), f)) {
220 if (line[0] ==
'\n' || line[0] ==
'\0') {
227 if (sscanf(line,
"physical id : %d", &val) == 1) {
231 if (sscanf(line,
"core id : %d", &val) == 1) {
235 if (sscanf(line,
"cpu cores : %d", &val) == 1) {
236 if (val > cpu_cores_hint) cpu_cores_hint = val;
239 if (sscanf(line,
"siblings : %d", &val) == 1) {
240 if (val > siblings_hint) siblings_hint = val;
251 physical_cores = seen_count;
256 if (physical_cores <= 1 && logical_cores > 1) {
257 int threads_per_core = 0;
258 if (siblings_hint > 0 && cpu_cores_hint > 0 && siblings_hint >= cpu_cores_hint) {
259 threads_per_core = siblings_hint / cpu_cores_hint;
261 if (threads_per_core > 1) {
262 int inferred_physical = logical_cores / threads_per_core;
263 if (inferred_physical > 1) {
264 return inferred_physical;
267 if (cpu_cores_hint > 1 && cpu_cores_hint <= logical_cores) {
268 return cpu_cores_hint;
270 return logical_cores;
273 if (physical_cores > 1) {
274 return physical_cores;
277 return logical_cores;
283 if (num_threads <= 0) {
288 if (env_threads <= 0) {
299 omp_set_num_threads(num_threads);
303 mkl_set_num_threads(num_threads);
306 fprintf(stderr,
"[CK] Set %d threads (auto=%d)\n",
Persistent pthread thread pool for CK-Engine inference.
ck_threadpool_t * ck_threadpool_global(void)
void ck_threadpool_global_destroy(void)
static int ck_parse_env_int(const char *name)
void ck_threadpool_init(void)
void ck_threadpool_shutdown(void)
void ck_set_num_threads(int num_threads)
static size_t ck_strict_next_gemm_a_size
static size_t ck_strict_next_gemm_a_cap
void ck_strict_store_next_gemm_a(const float *data, size_t elems)
static float * ck_strict_next_gemm_a
static int ck_strict_parity
const float * ck_strict_consume_next_gemm_a(size_t elems)
int ck_get_physical_cores(void)
void ck_set_strict_parity(int enabled)
void(* ck_strict_mtmd_clip_free_fn)(void *)
int ck_strict_mtmd_clip_encode_planar_f32(const float *planar, int channels, int height, int width, float *out, size_t out_elems)
static int ck_strict_next_gemm_a_valid
void *(* ck_strict_mtmd_clip_init_fn)(const char *, int, int, int, int, int)
#define CK_ADD_PAIR(pid, cid)
static int g_threads_initialized
ck_threadpool_t * ck_get_threadpool(void)
int ck_get_num_threads(void)
int ck_strict_parity_enabled(void)
size_t(* ck_strict_mtmd_clip_embd_nbytes_by_img_fn)(void *, int, int)
int(* ck_strict_mtmd_clip_encode_float_image_fn)(void *, int, float *, int, int, float *)