mirror of
https://github.com/kvcache-ai/ktransformers.git
synced 2026-10-19 15:22:49 +08:00
e80614fc2d
* feat(sft): support distributed activation reuse policies * feat(sft): add frozen-base INT8 LoRA training * fix(sft): make INT8 expert LoRA rank-zero authoritative * fix(sft): preserve DeepSeek router LoRA routing * feat(sft): enable persistent INT8 LoRA training * perf(sft): accelerate INT8 VNNI with oneDNN BRGEMM * perf(int8): fuse oneDNN compensation into backward repack * [feat]: support BF16 expert LoRA training * [fix]: honor forwarded activation policy in SFT workers * feat(sft): add native block-FP8 routed expert LoRA * feat(sft): expose explicit expert placeholder ownership * fix(sft): publish fused adapter artifacts atomically * feat(sft): own artifact and adapter lifecycle contracts * fix(sft): harden artifact and rank-local contracts * fix(sft): auto-adapt owner before adapter restore * style(sft): keep lifecycle comments concise * fix(sft): require fused adapter manifests * test(sft): use spawn for distributed workers * fix(sft): preserve runtime checkpoint metadata * fix(sft): validate wrapped runtime configuration * fix(sft): preserve expert format provenance * fix(sft): own routed experts during device dispatch * test(sft): lock explicit quantization conflict * fix(cpu): make shared memory buffers lifetime-safe * release: prepare v0.7.0
101 lines
2.6 KiB
C++
101 lines
2.6 KiB
C++
/**
|
|
* @Description :
|
|
* @Author : chenht2022
|
|
* @Date : 2024-08-05 04:49:08
|
|
* @Version : 1.0.0
|
|
* @LastEditors : chenht2022
|
|
* @LastEditTime : 2024-08-05 09:21:29
|
|
* @Copyright (c) 2024 by KVCache.AI, All Rights Reserved.
|
|
**/
|
|
#include "shared_mem_buffer.h"
|
|
|
|
#include <errno.h>
|
|
#include <numa.h>
|
|
|
|
#include <cstdio>
|
|
|
|
size_t MemoryRequest::total_size() {
|
|
size_t total = 0;
|
|
for (size_t i = 0; i < sizes.size(); ++i) {
|
|
total += sizes[i];
|
|
}
|
|
return total;
|
|
}
|
|
|
|
void MemoryRequest::update_base_ptr(void* base) {
|
|
size_t total_offset = 0;
|
|
for (size_t i = 0; i < funcs.size(); ++i) {
|
|
funcs[i]((uint8_t*)base + total_offset);
|
|
total_offset += sizes[i];
|
|
}
|
|
}
|
|
|
|
void MemoryRequest::append_function(std::function<void(void*)> func, size_t size) {
|
|
funcs.push_back(func);
|
|
sizes.push_back(size);
|
|
}
|
|
|
|
SharedMemBuffer::SharedMemBuffer() {
|
|
buffer = nullptr;
|
|
size = 0;
|
|
}
|
|
|
|
SharedMemBuffer::~SharedMemBuffer() {
|
|
if (buffer) {
|
|
free(buffer);
|
|
}
|
|
}
|
|
|
|
void SharedMemBuffer::alloc(void* object, MemoryRequest requests) {
|
|
std::lock_guard<std::mutex> guard(lock);
|
|
size_t total_size = requests.total_size();
|
|
|
|
if (total_size > size) {
|
|
if (buffer) {
|
|
free(buffer);
|
|
}
|
|
void* newbuf = nullptr;
|
|
int rc = posix_memalign(&newbuf, 64, total_size);
|
|
if (rc != 0 || !newbuf) {
|
|
errno = rc; // posix_memalign returns error code instead of setting errno
|
|
printf("cannot aligned alloc %zu bytes (align=%d)\n", (size_t)total_size, 64);
|
|
perror("posix_memalign"); // ENOMEM/EINVAL
|
|
exit(1);
|
|
}
|
|
buffer = newbuf;
|
|
size = total_size;
|
|
for (auto& owner_requests : object_requests) {
|
|
for (auto& req : owner_requests.second) {
|
|
req.update_base_ptr(buffer);
|
|
}
|
|
}
|
|
}
|
|
requests.update_base_ptr(buffer);
|
|
object_requests[object].push_back(requests);
|
|
}
|
|
|
|
void SharedMemBuffer::dealloc(void* object) {
|
|
std::lock_guard<std::mutex> guard(lock);
|
|
object_requests.erase(object);
|
|
}
|
|
|
|
void SharedMemBufferNuma::alloc(int numa, void* object, MemoryRequest requests) {
|
|
std::lock_guard<std::mutex> guard(lock);
|
|
if (numa != numa_node_of_cpu(sched_getcpu())) {
|
|
printf("alloc %d from other numa for %lx\n", numa, reinterpret_cast<intptr_t>(object));
|
|
}
|
|
if (numa_mem.count(numa) == 0) {
|
|
numa_mem[numa] = std::unique_ptr<SharedMemBuffer>(new SharedMemBuffer());
|
|
}
|
|
// printf("numa %d alloc for %lx\n", numa,reinterpret_cast<intptr_t> (object));
|
|
numa_mem.at(numa)->alloc(object, requests);
|
|
}
|
|
|
|
void SharedMemBufferNuma::dealloc(int numa, void* object) {
|
|
std::lock_guard<std::mutex> guard(lock);
|
|
auto it = numa_mem.find(numa);
|
|
if (it != numa_mem.end()) {
|
|
it->second->dealloc(object);
|
|
}
|
|
}
|