char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
b7feacf7f
char/aliceai-t5-moe
1#pragma once 2 3#include <atomic> 4#include <cassert> 5#include <cerrno> 6#include <cstdarg> 7#include <cstddef> 8#include <cstdint> 9#include <cstdio> 10#include <cstdlib> 11#include <ctime> 12 13#define unlikely (x ) __builtin_expect(!!(x), 0) 14#define likely (x ) __builtin_expect(!!(x), 1) 15 16#ifndef UNUSED 17# define UNUSED (x ) (void) (x) 18#endif 19 20/** Checks is a value is a power of two. Does not handle zero. */ 21#define IS_POT (v ) (((v) & ((v) - 1)) == 0) 22 23/** Checks is a value is a power of two. Zero handled. */ 24#define IS_POT_NONZERO (v ) ((v) != 0 && IS_POT(v)) 25 26/** Align a value to a power of two */ 27#define ALIGN_POT (x ,pot_align ) (((x) + (pot_align) - 1) & ~((pot_align) - 1)) 28 29#define p_atomic_read (_v ) __atomic_load_n((_v), __ATOMIC_ACQUIRE) 30 31static inline bool util_is_power_of_two_nonzero64 (uint64_t v ) { 32return IS_POT_NONZERO (v ); 33} 34 35static inline uint64_t align64 (uint64_t value ,uint64_t alignment ) { 36assert (util_is_power_of_two_nonzero64 (alignment )); 37return ALIGN_POT (value ,alignment ); 38} 39 40struct list_head { 41list_head * prev ; 42list_head * next ; 43}; 44 45struct util_sparse_array { 46size_t elem_size ; 47unsigned node_size_log2 ; 48 49uintptr_t root ; 50}; 51 52void * util_sparse_array_get (util_sparse_array * arr ,uint64_t idx ); 53void util_sparse_array_init (util_sparse_array * arr ,size_t elem_size ,size_t node_size ); 54 55inline void os_time_sleep (int64_t usecs ) { 56timespec time ; 57time .tv_sec = usecs /1000000 ; 58time .tv_nsec = (usecs %1000000 )* 1000 ; 59while (clock_nanosleep (CLOCK_MONOTONIC ,0 ,& time ,& time )== EINTR ) 60 ; 61} 62 63struct timer_data { 64long long start ; 65long long total ; 66long long count ; 67}; 68 69static inline void start_timer (timer_data * timer ) { 70timespec ts ; 71clock_gettime (CLOCK_MONOTONIC ,& ts ); 72timer -> start = (long long )ts .tv_sec * 1000000000LL + ts .tv_nsec ; 73} 74 75// returns the duration in ns 76static inline long long stop_timer (timer_data * timer ) { 77timespec ts ; 78clock_gettime (CLOCK_MONOTONIC ,& ts ); 79long long timer_end = (long long )ts .tv_sec * 1000000000LL + ts .tv_nsec ; 80 81long long duration = (timer_end - timer -> start ); 82timer -> total += duration ; 83timer -> count += 1 ; 84 85return duration ; 86}