char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Max Krasnyanskyhexagon: support for multi-device model split (aka row-split) (#28589)eafe15a5e

char/aliceai-t5-moe
2.0 KiB64 linesraw
1#ifndef HEX_UTILS_H
2#define HEX_UTILS_H
3
4#include <stdbool.h>
5#include <stdint.h>
6#include <qurt_memory.h>
7#include <qurt.h>
8
9#include "hexagon_types.h"
10#include "hexagon_protos.h"
11
12#include "hex-fastdiv.h"
13#include "hex-dump.h"
14#include "hex-common.h"
15
16static inline uint64_t hex_get_cycles() {
17    uint64_t cycles = 0;
18    asm volatile(" %0 = c15:14\n" : "=r"(cycles));
19    return cycles;
20}
21
22static inline uint64_t hex_get_pktcnt() {
23    uint64_t pktcnt;
24    asm volatile(" %0 = c19:18\n" : "=r"(pktcnt));
25    return pktcnt;
26}
27
28static inline void hex_l2fetch(const void * p, uint32_t width, uint32_t stride, uint32_t height) {
29    const uint64_t control = Q6_P_combine_RR(stride, Q6_R_combine_RlRl(width, height));
30    Q6_l2fetch_AP((void *) p, control);
31}
32
33static inline void hex_l2fetch_block(const void * addr, size_t size) {
34    if (size == 0) return;
35    const uint32_t width = 16384; // 16KB rows
36    const uint32_t height = (size + width - 1) / width;
37    hex_l2fetch(addr, width, width, height);
38}
39
40#define HEX_L2_LINE_SIZE           128
41#define HEX_L2_BLOCK_SIZE          (HEX_L2_LINE_SIZE * 4) // flush granularity (lines per loop iteration)
42#define HEX_L2_FLUSH_WQ_THRESHOLD  (4 * 1024)
43#define HEX_L2_FLUSH_ALL_THRESHOLD (4 * 1024 * 1024)
44
45static inline void hex_l2flush(void * addr, size_t size) {
46    const uint32_t s = ((uint32_t) addr) & ~(HEX_L2_LINE_SIZE - 1);
47    const uint32_t e = (((uint32_t) addr) + size + HEX_L2_LINE_SIZE - 1) & ~(HEX_L2_LINE_SIZE - 1);
48    const uint32_t eb = s + ((e - s) & ~(HEX_L2_BLOCK_SIZE - 1));
49    for (uint32_t i = s; i < eb; i += HEX_L2_BLOCK_SIZE) {
50        Q6_dccleaninva_A((void *) (i + HEX_L2_LINE_SIZE * 0));
51        Q6_dccleaninva_A((void *) (i + HEX_L2_LINE_SIZE * 1));
52        Q6_dccleaninva_A((void *) (i + HEX_L2_LINE_SIZE * 2));
53        Q6_dccleaninva_A((void *) (i + HEX_L2_LINE_SIZE * 3));
54    }
55    for (uint32_t i = eb; i < e; i += HEX_L2_LINE_SIZE) {
56        Q6_dccleaninva_A((void *) i);
57    }
58}
59
60static inline void hex_pause() {
61    asm volatile(" pause(#255)\n");
62}
63
64#endif /* HEX_UTILS_H */