Instructions to use FardoX/Phi-3-mini-4k-instruct-CAN with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- llama.cpp
How to use FardoX/Phi-3-mini-4k-instruct-CAN with llama.cpp:
Install (macOS, Linux)
curl -LsSf https://llama.app/install.sh | sh # Start a local OpenAI-compatible server with a web UI: llama serve -hf FardoX/Phi-3-mini-4k-instruct-CAN # Run inference directly in the terminal: llama cli -hf FardoX/Phi-3-mini-4k-instruct-CAN
Install from WinGet (Windows)
winget install llama.cpp # Start a local OpenAI-compatible server with a web UI: llama serve -hf FardoX/Phi-3-mini-4k-instruct-CAN # Run inference directly in the terminal: llama cli -hf FardoX/Phi-3-mini-4k-instruct-CAN
Build from source code
git clone https://github.com/ggml-org/llama.cpp.git cd llama.cpp cmake -B build cmake --build build -j --target llama-app # Start a local OpenAI-compatible server with a web UI: ./build/bin/llama serve -hf FardoX/Phi-3-mini-4k-instruct-CAN # Run inference directly in the terminal: ./build/bin/llama cli -hf FardoX/Phi-3-mini-4k-instruct-CAN
Use Docker
docker model run hf.co/FardoX/Phi-3-mini-4k-instruct-CAN
- Llama new
- LM Studio
- Jan
- vLLM
How to use FardoX/Phi-3-mini-4k-instruct-CAN with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "FardoX/Phi-3-mini-4k-instruct-CAN" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "FardoX/Phi-3-mini-4k-instruct-CAN", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/FardoX/Phi-3-mini-4k-instruct-CAN
- Ollama
How to use FardoX/Phi-3-mini-4k-instruct-CAN with Ollama:
ollama run hf.co/FardoX/Phi-3-mini-4k-instruct-CAN
- Unsloth Desktop
- Docker Model Runner
How to use FardoX/Phi-3-mini-4k-instruct-CAN with Docker Model Runner:
docker model run hf.co/FardoX/Phi-3-mini-4k-instruct-CAN
- Lemonade
How to use FardoX/Phi-3-mini-4k-instruct-CAN with Lemonade:
Pull the model
# Download Lemonade from https://lemonade-server.ai/ lemonade pull FardoX/Phi-3-mini-4k-instruct-CAN
Run and chat with the model
lemonade run user.Phi-3-mini-4k-instruct-CAN-{{QUANT_TAG}}List all available models
lemonade list
- Atomic Chat
Download can3_phi3.patch from FardoX/Phi-3-mini-4k-instruct-CAN: direct link, hf CLI and curl.
- Browser
- Download file 20.4 kB
-
https://huggingface.co/FardoX/Phi-3-mini-4k-instruct-CAN/resolve/main/can3_phi3.patch
- Command line
-
hf download hf://FardoX/Phi-3-mini-4k-instruct-CAN/can3_phi3.patch
-
curl -L -o can3_phi3.patch https://huggingface.co/FardoX/Phi-3-mini-4k-instruct-CAN/resolve/main/can3_phi3.patch
20.4 kB
| --- a/ggml/include/ggml.h | |
| +++ b/ggml/include/ggml.h | |
| GGML_TYPE_Q1_0 = 41, | |
| GGML_TYPE_Q2_0 = 42, | |
| GGML_TYPE_F8_E4M3 = 43, | |
| - GGML_TYPE_COUNT = 44, | |
| + GGML_TYPE_CAN128 = 44, // E3 CAN: 4 nibbles por grupo de 8, livros por tensor (ggml_can_registrar), 130 B/512 | |
| + GGML_TYPE_CAN96 = 45, // E3 CAN: 3 nibbles por grupo de 8, 98 B/512 | |
| + GGML_TYPE_COUNT = 46, | |
| }; | |
| // [TAG_GGML_PREC] | |
| GGML_API size_t ggml_row_size (enum ggml_type type, int64_t ne); // size in bytes for all elements in a row | |
| GGML_API const char * ggml_type_name(enum ggml_type type); | |
| + // E3 CAN: livros int8 [M][16][8] e fator por tensor, registrados pelo carregador; consulta por nome (NULL se nao ha) | |
| + GGML_API void ggml_can_registrar(const char * nome, const int8_t * livros, int M, float escala); | |
| + GGML_API const float * ggml_can_livros(const char * nome, int * M, const float ** livros_t); | |
| GGML_API const char * ggml_op_name (enum ggml_op op); | |
| GGML_API const char * ggml_op_symbol(enum ggml_op op); | |
| --- a/ggml/src/ggml.c | |
| +++ b/ggml/src/ggml.c | |
| .to_float = (ggml_to_float_t) dequantize_row_f8_e4m3, | |
| .from_float_ref = (ggml_from_float_t) quantize_row_f8_e4m3_ref, | |
| }, | |
| + [GGML_TYPE_CAN128] = { | |
| + .type_name = "can128", | |
| + .blck_size = 512, | |
| + .type_size = 130, | |
| + .is_quantized = true, | |
| + .to_float = NULL, | |
| + .from_float_ref = NULL, | |
| + }, | |
| + [GGML_TYPE_CAN96] = { | |
| + .type_name = "can96", | |
| + .blck_size = 512, | |
| + .type_size = 98, | |
| + .is_quantized = true, | |
| + .to_float = NULL, | |
| + .from_float_ref = NULL, | |
| + }, | |
| [GGML_TYPE_Q2_K] = { | |
| .type_name = "q2_K", | |
| .blck_size = QK_K, | |
| return ggml_type_size(type)*ne/ggml_blck_size(type); | |
| } | |
| +// ---------------------------------------------------------------- E3 CAN: registro dos livros por nome de tensor | |
| +#define CAN_MAXREG 4096 | |
| +struct can_reg { char nome[GGML_MAX_NAME]; int M; float B[4*16*8]; float Bt[4*8*16]; int usado; }; | |
| +static struct can_reg * can_tab = NULL; | |
| +static uint32_t can_hash(const char * s) { uint32_t h = 2166136261u; while (*s) { h = (h ^ (uint8_t) *s++) * 16777619u; } return h; } | |
| +void ggml_can_registrar(const char * nome, const int8_t * livros, int M, float escala) { | |
| + GGML_ASSERT(M >= 1 && M <= 4); | |
| + if (!can_tab) { can_tab = (struct can_reg *) calloc(CAN_MAXREG, sizeof(struct can_reg)); } | |
| + uint32_t h = can_hash(nome) % CAN_MAXREG; | |
| + for (int i = 0; i < CAN_MAXREG; i++, h = (h + 1) % CAN_MAXREG) { | |
| + struct can_reg * r = &can_tab[h]; | |
| + if (!r->usado || strcmp(r->nome, nome) == 0) { | |
| + r->usado = 1; r->M = M; | |
| + snprintf(r->nome, sizeof(r->nome), "%s", nome); | |
| + for (int m = 0; m < M; m++) for (int j = 0; j < 16; j++) for (int e = 0; e < 8; e++) { | |
| + const float v = escala * (float) livros[(m*16 + j)*8 + e]; | |
| + r->B[(m*16 + j)*8 + e] = v; | |
| + r->Bt[(m*8 + e)*16 + j] = v; | |
| + } | |
| + return; | |
| + } | |
| + } | |
| + GGML_ABORT("ggml_can_registrar: tabela cheia"); | |
| +} | |
| +const float * ggml_can_livros(const char * nome, int * M, const float ** livros_t) { | |
| + if (!can_tab) { return NULL; } | |
| + uint32_t h = can_hash(nome) % CAN_MAXREG; | |
| + for (int i = 0; i < CAN_MAXREG; i++, h = (h + 1) % CAN_MAXREG) { | |
| + struct can_reg * r = &can_tab[h]; | |
| + if (!r->usado) { return NULL; } | |
| + if (strcmp(r->nome, nome) == 0) { if (M) { *M = r->M; } if (livros_t) { *livros_t = r->Bt; } return r->B; } | |
| + } | |
| + return NULL; | |
| +} | |
| + | |
| const char * ggml_type_name(enum ggml_type type) { | |
| assert(type >= 0); | |
| assert(type < GGML_TYPE_COUNT); | |
| --- a/ggml/src/ggml-cpu/ggml-cpu.c | |
| +++ b/ggml/src/ggml-cpu/ggml-cpu.c | |
| .vec_dot_type = GGML_TYPE_Q8_K, | |
| .nrows = 1, | |
| }, | |
| + [GGML_TYPE_CAN128] = { | |
| + .from_float = NULL, | |
| + .vec_dot = NULL, | |
| + .vec_dot_type = GGML_TYPE_F32, | |
| + .nrows = 1, | |
| + }, | |
| + [GGML_TYPE_CAN96] = { | |
| + .from_float = NULL, | |
| + .vec_dot = NULL, | |
| + .vec_dot_type = GGML_TYPE_F32, | |
| + .nrows = 1, | |
| + }, | |
| [GGML_TYPE_I32] = { | |
| .from_float = (ggml_from_float_t) ggml_cpu_fp32_to_i32, | |
| }, | |
| return; | |
| } | |
| + if (src0->type == GGML_TYPE_CAN128 || src0->type == GGML_TYPE_CAN96) { | |
| + ggml_compute_forward_mul_mat_can(params, dst); | |
| + return; | |
| + } | |
| + | |
| // If tiled is supported, it will execute the full op here and we return | |
| if (ggml_compute_forward_mul_mat_tiled(params, dst)) { | |
| return; | |
| // Workspace for tiled (see tiled.h) | |
| cur = GGML_PAD(cur, 64); | |
| cur += ggml_tiled_wdata_size(n_tasks, node); | |
| + if (node->src[0]->type == GGML_TYPE_CAN128 || node->src[0]->type == GGML_TYPE_CAN96) { | |
| + cur = ggml_can_wsize(node->src[0], node->src[1], n_tasks); | |
| + } | |
| } break; | |
| case GGML_OP_MUL_MAT_ID: | |
| { | |
| --- a/ggml/src/ggml-cpu/ops.cpp | |
| +++ b/ggml/src/ggml-cpu/ops.cpp | |
| { | |
| ggml_compute_forward_get_rows_q(params, dst); | |
| } break; | |
| + case GGML_TYPE_CAN128: | |
| + case GGML_TYPE_CAN96: | |
| + { | |
| + ggml_compute_forward_get_rows_can(params, dst); | |
| + } break; | |
| case GGML_TYPE_F16: | |
| { | |
| ggml_compute_forward_get_rows_f16(params, dst); | |
| } | |
| } | |
| } | |
| +} | |
| +#if defined(__AVX2__) | |
| +#include <immintrin.h> | |
| +#endif | |
| + | |
| +// ==================================================================== E3: tipo CAN (codigo aditivo de nibbles), CPU | |
| +// Layout do tensor [N linhas x K colunas], N % 32 == 0, K % 512 == 0: ladrilhos de 32 linhas; em cada ladrilho, por | |
| +// superbloco b de 512 colunas: NP*32 bytes de indices (byte p*32 + r: nibble baixo = tabela 2p, alto = 2p+1; tabela | |
| +// t = g*M + m, grupo g de 8 colunas, livro m; NP = 32*M) e 32 d em fp16. Livros float por nome do tensor | |
| +// (ggml_can_livros). y[r] = sum_b d[r,b] * Delta_b * sum_t T8[b][t][idx(r,b,t)], T8 = tabelas <x_g, C_m[j]> em int8 com | |
| +// passo Delta_b por superbloco (o kernel do E2, igual bit a bit a emulacao inteira). Lote > 1: as mesmas tabelas por coluna. | |
| + | |
| +static inline void can_parte(int64_t n, int tid, int T, int64_t & a, int64_t & b) { a = n * tid / T; b = n * (tid + 1) / T; } | |
| + | |
| +static const float * can_livros_de(const ggml_tensor * t, int M_esperado, const float ** Bt) { | |
| + int M = 0; | |
| + const float * B = ggml_can_livros(t->name, &M, Bt); | |
| + if (!B) { GGML_ABORT("CAN: livros nao registrados para o tensor '%s'", t->name); } | |
| + GGML_ASSERT(M == M_esperado); | |
| + return B; | |
| +} | |
| + | |
| +size_t ggml_can_wsize(const struct ggml_tensor * src0, const struct ggml_tensor * src1, int n_tasks) { | |
| + const int M = src0->type == GGML_TYPE_CAN128 ? 4 : 3; | |
| + const int64_t nsb = src0->ne[0] / 512, NT = 64 * M, ncol = ggml_nrows(src1); | |
| + return (size_t) (ncol * nsb * NT * 16 + ncol * nsb * sizeof(float) + (int64_t) n_tasks * NT * 16 * sizeof(float) + 256); | |
| +} | |
| + | |
| +// tabelas de uma coluna x num superbloco b: Tf (rascunho, NT*16 floats), T8 [NT][16] int8, devolve Delta | |
| +static float can_prep(const float * x, const float * Bt, int M, int b, float * Tf, int8_t * T8) { | |
| + const int NT = 64 * M; | |
| + float amax = 0.f; | |
| +#if defined(__AVX2__) && defined(__FMA__) | |
| + __m256 vmax = _mm256_setzero_ps(); | |
| + const __m256 absm = _mm256_castsi256_ps(_mm256_set1_epi32(0x7fffffff)); | |
| + int k = 0; | |
| + for (int g = 0; g < 64; g++) { | |
| + const float * xg = x + 512 * b + 8 * g; | |
| + __m256 xe[8]; | |
| + for (int e = 0; e < 8; e++) { xe[e] = _mm256_broadcast_ss(xg + e); } | |
| + for (int m = 0; m < M; m++) { | |
| + __m256 a0 = _mm256_setzero_ps(), a1 = _mm256_setzero_ps(); | |
| + for (int e = 0; e < 8; e++) { | |
| + a0 = _mm256_fmadd_ps(xe[e], _mm256_loadu_ps(Bt + (m * 8 + e) * 16), a0); | |
| + a1 = _mm256_fmadd_ps(xe[e], _mm256_loadu_ps(Bt + (m * 8 + e) * 16 + 8), a1); | |
| + } | |
| + _mm256_storeu_ps(Tf + 16 * k, a0); | |
| + _mm256_storeu_ps(Tf + 16 * k + 8, a1); | |
| + vmax = _mm256_max_ps(vmax, _mm256_max_ps(_mm256_and_ps(a0, absm), _mm256_and_ps(a1, absm))); | |
| + k++; | |
| + } | |
| + } | |
| + __m128 r = _mm_max_ps(_mm256_castps256_ps128(vmax), _mm256_extractf128_ps(vmax, 1)); | |
| + r = _mm_max_ps(r, _mm_movehl_ps(r, r)); | |
| + r = _mm_max_ss(r, _mm_movehdup_ps(r)); | |
| + amax = _mm_cvtss_f32(r); | |
| + if (amax == 0.f) { amax = 1.f; } | |
| + const __m256 vinv = _mm256_set1_ps(127.f / amax); | |
| + for (int j = 0; j < NT; j++) { | |
| + const __m256i i0 = _mm256_cvtps_epi32(_mm256_mul_ps(_mm256_loadu_ps(Tf + 16 * j), vinv)); | |
| + const __m256i i1 = _mm256_cvtps_epi32(_mm256_mul_ps(_mm256_loadu_ps(Tf + 16 * j + 8), vinv)); | |
| + const __m256i p = _mm256_permute4x64_epi64(_mm256_packs_epi32(i0, i1), 0xD8); | |
| + _mm_storeu_si128((__m128i *) (T8 + 16 * j), _mm_packs_epi16(_mm256_castsi256_si128(p), _mm256_extracti128_si256(p, 1))); | |
| + } | |
| +#else | |
| + int k = 0; | |
| + for (int g = 0; g < 64; g++) { | |
| + const float * xg = x + 512 * b + 8 * g; | |
| + for (int m = 0; m < M; m++, k++) { | |
| + for (int j = 0; j < 16; j++) { | |
| + float a = 0.f; | |
| + for (int e = 0; e < 8; e++) { a = fmaf(xg[e], Bt[(m * 8 + e) * 16 + j], a); } | |
| + Tf[16 * k + j] = a; amax = std::max(amax, fabsf(a)); | |
| + } | |
| + } | |
| + } | |
| + if (amax == 0.f) { amax = 1.f; } | |
| + const float inv = 127.f / amax; | |
| + for (int j = 0; j < NT * 16; j++) { T8[j] = (int8_t) nearbyintf(Tf[j] * inv); } | |
| +#endif | |
| + return amax / 127.f; | |
| +} | |
| + | |
| +// um ladrilho de 32 linhas contra as tabelas de uma coluna (T8 [nsb][2NP][16], dl [nsb]) -> y[32] | |
| +template <int NP> | |
| +static void can_scan_tile(const uint8_t * base, const int8_t * T8c, const float * dl, int nsb, float * y) { | |
| + const size_t sbb = NP * 32 + 64; | |
| +#if defined(__AVX2__) && defined(__F16C__) && defined(__FMA__) | |
| + const __m256i m0f = _mm256_set1_epi8(0x0F), ones = _mm256_set1_epi8(1); | |
| + __m256 f0 = _mm256_setzero_ps(), f1 = f0, f2 = f0, f3 = f0; | |
| + for (int b = 0; b < nsb; b++) { | |
| + const uint8_t * ib = base + b * sbb; | |
| + const int8_t * T = T8c + (size_t) b * 2 * NP * 16; | |
| + __m256i a0 = _mm256_setzero_si256(), a1 = a0; | |
| +#pragma GCC unroll 4 | |
| + for (int p = 0; p < NP; p++) { | |
| + const __m256i v = _mm256_loadu_si256((const __m256i *) (ib + 32 * p)); | |
| + const __m256i lo = _mm256_and_si256(v, m0f); | |
| + const __m256i hi = _mm256_and_si256(_mm256_srli_epi16(v, 4), m0f); | |
| + const __m256i r0 = _mm256_shuffle_epi8(_mm256_broadcastsi128_si256(_mm_loadu_si128((const __m128i *) (T + 32 * p))), lo); | |
| + const __m256i r1 = _mm256_shuffle_epi8(_mm256_broadcastsi128_si256(_mm_loadu_si128((const __m128i *) (T + 32 * p + 16))), hi); | |
| + a0 = _mm256_add_epi16(a0, _mm256_maddubs_epi16(ones, _mm256_unpacklo_epi8(r0, r1))); | |
| + a1 = _mm256_add_epi16(a1, _mm256_maddubs_epi16(ones, _mm256_unpackhi_epi8(r0, r1))); | |
| + } | |
| + const uint16_t * dp = (const uint16_t *) (ib + 32 * NP); | |
| + const __m256 dv = _mm256_set1_ps(dl[b]); | |
| + const __m256 d0 = _mm256_mul_ps(_mm256_cvtph_ps(_mm_loadu_si128((const __m128i *) (dp))), dv); | |
| + const __m256 d1 = _mm256_mul_ps(_mm256_cvtph_ps(_mm_loadu_si128((const __m128i *) (dp + 8))), dv); | |
| + const __m256 d2 = _mm256_mul_ps(_mm256_cvtph_ps(_mm_loadu_si128((const __m128i *) (dp + 16))), dv); | |
| + const __m256 d3 = _mm256_mul_ps(_mm256_cvtph_ps(_mm_loadu_si128((const __m128i *) (dp + 24))), dv); | |
| + f0 = _mm256_fmadd_ps(d0, _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm256_castsi256_si128(a0))), f0); // linhas 0-7 | |
| + f2 = _mm256_fmadd_ps(d2, _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm256_extracti128_si256(a0, 1))), f2); // 16-23 | |
| + f1 = _mm256_fmadd_ps(d1, _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm256_castsi256_si128(a1))), f1); // 8-15 | |
| + f3 = _mm256_fmadd_ps(d3, _mm256_cvtepi32_ps(_mm256_cvtepi16_epi32(_mm256_extracti128_si256(a1, 1))), f3); // 24-31 | |
| + } | |
| + _mm256_storeu_ps(y, f0); _mm256_storeu_ps(y + 8, f1); _mm256_storeu_ps(y + 16, f2); _mm256_storeu_ps(y + 24, f3); | |
| +#else | |
| + float acc[32] = {0}; | |
| + for (int b = 0; b < nsb; b++) { | |
| + const uint8_t * ib = base + b * sbb; | |
| + const int8_t * T = T8c + (size_t) b * 2 * NP * 16; | |
| + const uint16_t * dp = (const uint16_t *) (ib + 32 * NP); | |
| + for (int r = 0; r < 32; r++) { | |
| + int s = 0; | |
| + for (int p = 0; p < NP; p++) { const uint8_t v = ib[32 * p + r]; s += T[32 * p + (v & 15)] + T[32 * p + 16 + (v >> 4)]; } | |
| + acc[r] = fmaf(GGML_CPU_FP16_TO_FP32(dp[r]) * dl[b], (float) s, acc[r]); | |
| + } | |
| + } | |
| + for (int r = 0; r < 32; r++) { y[r] = acc[r]; } | |
| +#endif | |
| +} | |
| + | |
| +void ggml_compute_forward_mul_mat_can(const struct ggml_compute_params * params, struct ggml_tensor * dst) { | |
| + const ggml_tensor * src0 = dst->src[0]; | |
| + const ggml_tensor * src1 = dst->src[1]; | |
| + GGML_TENSOR_BINARY_OP_LOCALS | |
| + GGML_ASSERT(src1->type == GGML_TYPE_F32 && nb10 == sizeof(float) && nb0 == sizeof(float)); | |
| + GGML_ASSERT(ne02 == 1 && ne03 == 1 && ne00 % 512 == 0 && ne01 % 32 == 0); | |
| + const int M = src0->type == GGML_TYPE_CAN128 ? 4 : 3; | |
| + const int NP = 32 * M, NT = 64 * M; | |
| + const int64_t K = ne00, N = ne01, nsb = K / 512, ncol = ne11 * ne12 * ne13; | |
| + const float * Bt = nullptr; | |
| + can_livros_de(src0, M, &Bt); | |
| + const int ith = params->ith, nth = params->nth; | |
| + int8_t * T8 = (int8_t *) params->wdata; | |
| + float * dl = (float *) (T8 + ncol * nsb * NT * 16); | |
| + float * Tf = dl + ncol * nsb + (size_t) ith * NT * 16; | |
| + GGML_ASSERT(params->wsize >= ggml_can_wsize(src0, src1, nth) - 256); | |
| + auto coluna_x = [&](int64_t c) { | |
| + const int64_t i11 = c % ne11, i12 = (c / ne11) % ne12, i13 = c / (ne11 * ne12); | |
| + return (const float *) ((const char *) src1->data + i11 * nb11 + i12 * nb12 + i13 * nb13); | |
| + }; | |
| + auto coluna_y = [&](int64_t c) { | |
| + const int64_t i1 = c % ne1, i2 = (c / ne1) % ne2, i3 = c / (ne1 * ne2); | |
| + return (float *) ((char *) dst->data + i1 * nb1 + i2 * nb2 + i3 * nb3); | |
| + }; | |
| + int64_t a, b; | |
| + can_parte(ncol * nsb, ith, nth, a, b); | |
| + for (int64_t it = a; it < b; it++) { | |
| + const int64_t c = it / nsb, sb = it % nsb; | |
| + dl[it] = can_prep(coluna_x(c), Bt, M, (int) sb, Tf, T8 + it * NT * 16); | |
| + } | |
| + ggml_barrier(params->threadpool); | |
| + const size_t tile_bytes = (size_t) nsb * (NP * 32 + 64); | |
| + can_parte(N / 32, ith, nth, a, b); | |
| + for (int64_t tl = a; tl < b; tl++) { | |
| + const uint8_t * base = (const uint8_t *) src0->data + tl * tile_bytes; | |
| + for (int64_t c = 0; c < ncol; c++) { | |
| + float * y = coluna_y(c) + 32 * tl; | |
| + const int8_t * T8c = T8 + c * nsb * NT * 16; | |
| + if (M == 4) { can_scan_tile<128>(base, T8c, dl + c * nsb, (int) nsb, y); } | |
| + else { can_scan_tile<96>(base, T8c, dl + c * nsb, (int) nsb, y); } | |
| + } | |
| + } | |
| +} | |
| + | |
| +void ggml_compute_forward_get_rows_can(const struct ggml_compute_params * params, struct ggml_tensor * dst) { | |
| + const ggml_tensor * src0 = dst->src[0]; | |
| + const ggml_tensor * src1 = dst->src[1]; | |
| + GGML_TENSOR_BINARY_OP_LOCALS | |
| + GGML_ASSERT(ne02 == 1 && ne03 == 1 && ne00 % 512 == 0 && ne01 % 32 == 0 && dst->type == GGML_TYPE_F32); | |
| + const int M = src0->type == GGML_TYPE_CAN128 ? 4 : 3; | |
| + const int NP = 32 * M; | |
| + const float * B = can_livros_de(src0, M, nullptr); | |
| + const int64_t nsb = ne00 / 512, nr = ggml_nelements(src1); | |
| + const size_t sbb = NP * 32 + 64, tile_bytes = (size_t) nsb * sbb; | |
| + int64_t a, b; | |
| + can_parte(nr, params->ith, params->nth, a, b); | |
| + for (int64_t i = a; i < b; ++i) { | |
| + const int64_t i12 = i / (ne11 * ne10); | |
| + const int64_t i11 = (i - i12 * ne11 * ne10) / ne10; | |
| + const int64_t i10 = (i - i12 * ne11 * ne10 - i11 * ne10); | |
| + const int64_t r = *(int32_t *) ((char *) src1->data + i10 * nb10 + i11 * nb11 + i12 * nb12); | |
| + GGML_ASSERT(r >= 0 && r < ne01); | |
| + float * y = (float *) ((char *) dst->data + i10 * nb1 + i11 * nb2 + i12 * nb3); | |
| + const uint8_t * base = (const uint8_t *) src0->data + (r / 32) * tile_bytes; | |
| + const int rr = (int) (r % 32); | |
| + for (int64_t sb = 0; sb < nsb; sb++) { | |
| + const uint8_t * ib = base + sb * sbb; | |
| + const float d = GGML_CPU_FP16_TO_FP32(((const uint16_t *) (ib + 32 * NP))[rr]); | |
| + for (int g = 0; g < 64; g++) { | |
| + float v[8] = {0, 0, 0, 0, 0, 0, 0, 0}; | |
| + for (int m = 0; m < M; m++) { | |
| + const int t = g * M + m; | |
| + const uint8_t by = ib[32 * (t / 2) + rr]; | |
| + const int j = (t & 1) ? (by >> 4) : (by & 15); | |
| + const float * Bm = B + (m * 16 + j) * 8; | |
| + for (int e = 0; e < 8; e++) { v[e] += Bm[e]; } | |
| + } | |
| + for (int e = 0; e < 8; e++) { y[512 * sb + 8 * g + e] = d * v[e]; } | |
| + } | |
| + } | |
| + } | |
| } | |
| --- a/ggml/src/ggml-cpu/ops.h | |
| +++ b/ggml/src/ggml-cpu/ops.h | |
| #endif | |
| void ggml_compute_forward_dup(const struct ggml_compute_params * params, struct ggml_tensor * dst); | |
| +void ggml_compute_forward_mul_mat_can(const struct ggml_compute_params * params, struct ggml_tensor * dst); | |
| +void ggml_compute_forward_get_rows_can(const struct ggml_compute_params * params, struct ggml_tensor * dst); | |
| +size_t ggml_can_wsize(const struct ggml_tensor * src0, const struct ggml_tensor * src1, int n_tasks); | |
| void ggml_compute_forward_add(const struct ggml_compute_params * params, struct ggml_tensor * dst); | |
| void ggml_compute_forward_add_id(const struct ggml_compute_params * params, struct ggml_tensor * dst); | |
| void ggml_compute_forward_add1(const struct ggml_compute_params * params, struct ggml_tensor * dst); | |
| --- a/src/llama-model-loader.cpp | |
| +++ b/src/llama-model-loader.cpp | |
| get_key(llm_kv(LLM_KV_GENERAL_ARCHITECTURE), arch_name, false); | |
| llm_kv = LLM_KV(llm_arch_from_string(arch_name)); | |
| + // E3 CAN: livros por tensor nas chaves can.livros.<tensor> (int8 [M][16][8]) e can.escala.<tensor> (f32) | |
| + for (int64_t i = 0; i < gguf_get_n_kv(metadata); i++) { | |
| + const char * k = gguf_get_key(metadata, i); | |
| + if (strncmp(k, "can.livros.", 11) != 0) { continue; } | |
| + const std::string nome = k + 11; | |
| + const int64_t ie = gguf_find_key(metadata, ("can.escala." + nome).c_str()); | |
| + GGML_ASSERT(ie >= 0 && gguf_get_arr_type(metadata, i) == GGUF_TYPE_INT8); | |
| + const int n = (int) gguf_get_arr_n(metadata, i); | |
| + GGML_ASSERT(n % 128 == 0); | |
| + ggml_can_registrar(nome.c_str(), (const int8_t *) gguf_get_arr_data(metadata, i), n / 128, gguf_get_val_f32(metadata, ie)); | |
| + } | |
| + | |
| files.emplace_back(new llama_file(fname.c_str(), "rb", use_direct_io)); | |
| contexts.emplace_back(ctx); | |
| --- a/src/llama-model.cpp 2026-11-23 14:52:07.844517069 -0300 | |
| +++ b/src/llama-model.cpp 2026-11-23 14:52:07.855466553 -0300 | |
| // only build_lora_mm/build_lora_mm_id apply the transform: refuse archs and tensor kinds that can skip them | |
| switch (arch) { | |
| case LLM_ARCH_LLAMA: | |
| + case LLM_ARCH_PHI3: // CAN (Phi-3): qkv, wo, ffn_up/down and output all go through build_lora_mm; the graph coverage check still runs | |
| case LLM_ARCH_QWEN3: | |
| case LLM_ARCH_QWEN3MOE: | |
| case LLM_ARCH_QWEN35: | |