实用代码小技巧

即查即用

Environment

查看系统架构

1
uname -m

或者

1
lscpu

设置环境变量

在 ~/.bashrc

1
export CUDACXX=/usr/local/cuda/bin/nvcc

命令行

1
source ~/.bashrc

ssh permission

Permissions 0664 for ‘/home/wdl/.ssh/id_wdl’ are too open.

1
chmod 600 /home/wdl/.ssh/id_wdl

externally-managed-environment

error: externally-managed-environment

  1. 直接捂嘴
1
sudo mv /usr/lib/python3.12/EXTERNALLY-MANAGED /usr/lib/python3.12/EXTERNALLY-MANAGED.bak
  1. 创建虚拟环境
1
2
python -m venv ENV_DIR
source ENV_DIR/bin/activate

ENV_DIR 指定存放环境的目录

退出环境:

1
deactivate

清华源

1
pip3 install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple

Files

scp

1
scp -r xxx:path yyy:path

rsync

1
rsync -avzP src dst

image-20250619182739859

-z:在传输过程中对数据进行压缩

-P:进度条与断点续传

统计文件夹下的所有文件大小

1
du -sh .

校验是否损坏

1
md5sum test.txt

压缩与解压

1
2
tar -czvf archive.tar file1 file2 directory
tar -zxvf archive.tar

Markdown

强制换页

1
<div STYLE="page-break-after: always;"></div>

空格

img

图片居中显示

1
2
3
4
<center class="half">
    <img src="图片链接1" width="200"/>
    <img src="图片链接2" width="200"/>
</center>

Git

git diff

仅输出不同文件名

1
git diff --name-only .. ..

git submodule

1
git submodule update --init --recursive

本地彻底回退

git log 找到希望回退到的 commit 的哈希值

1
git reset --hard <commit-hash>

避免输入passphrase

linux

1
2
3
eval $(ssh-agent -s)
ssh-add ~/.ssh/id_rsa
ssh-add -l	# 验证是否成功

windows:打开PowerShell

1
2
Start-Service ssh-agent
ssh-add C:\Users\韦东良\.ssh\id_rsa

Host key has changed

ssh -v 报错:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ED25519 key sent by the remote host is ...
Please contact your system administrator.
Add correct host key in C:\\Users\\.../.ssh/known_hosts to get rid of this message.
Offending ECDSA key in C:\\Users\\.../.ssh/known_hosts:58
Host key for 192.168.1.79 has changed and you have requested strict checking.
Host key verification failed.

原因:远程主机的主机密钥(Host Key)发生了变化,而本地的 known_hosts 文件中记录的旧密钥与当前服务器的密钥不匹配,导致了 SSH 客户端拒绝连接

解决方案:更新本地的 known_hosts 文件,找到并删除与 192.168.1.79 相关的行,重新连接

Debug

VScode python

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "sync test",
            "type": "debugpy",
            "request": "launch",
            "python": "/disk2/wdl/miniconda3/envs/infinigen/bin/python",
            "program": "flex_llama3.py", 
            "args": [
                "--model", "/disk2/wdl/llama-3.2-3b-instruct",
                "--path", "/disk2/wdl/FlexGen/llama_weights",
                "--offload-dir", "/disk2/wdl/FlexGen/offload_dir", 
                "--prompt-len", "7",
                "--gen-len", "10",
                "--gpu-batch-size", "1",
                "--num-gpu-batches", "2",
                "--prefill-batch-size", "512",
                "--percent", "100", "0", "0", "0", "100", "0", 
                "--attn-sparsity", "0.1",
                "--compress-weight",
            ],
            "console": "integratedTerminal",
            "cwd": "/disk2/wdl/FlexGen/flexgen",
        },
    ]
}

VScode C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "qkv fuse",
            "type": "cppdbg",
            "request": "launch",
            "program": "/home/wdl/powerinfer-refactor/build_qkv/bin/llama-cli",
            "args": [
                "-t", "4",
                "-no-cnv",
                "--temp", "0.6", 
                "--top-k", "20", 
                "--top-p", "0.95", 
                "--no-warmup",
                "-n", "256",
                "--samplers", "'temperature;top_k;top_p'",
                "-m", "/home/wdl/our-20b-q4_0.gguf",
                "-p", "Once upon a time ",
                // "-f", "/home/wdl/context_1000.txt",
            ],
            "stopAtEntry": true,
            "cwd": "/home/wdl/powerinfer-refactor/build_qkv/bin/",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

Segmentation fault (core dump)

程序发生 Segmentation fault (core dump) 之后:

1
sudo coredumpctl

如果没有出现信息,则需要:

1
2
3
sudo apt-get install systemd-coredump
sudo systemctl restart systemd-sysctl.service
echo "|/usr/lib/systemd/systemd-coredump %P %u %g %s %t %c %h" | sudo tee /proc/sys/kernel/core_pattern

这时候重新运行出错程序,如果还是不行,可能是因为为了防止程序错误地产生巨大的核心转储文件占满硬盘,默认情况下将core dump的大小限制为0,需要:

1
2
ulimit -c 				# 查看core文件大小限制
ulimit -c unlimited

sudo coredumpctl有输出之后:

1
coredumpctl gdb

默认会查看最近的一个core dump。gdb内用bt可以查看调用堆栈,用fr N可以去往第N层堆栈

llama.cpp 算子

image-20250617134214788

VSCode

IntelliSense 卡顿问题

先删除 .cache/vscode-cpptools/ipch

还卡就没别的办法了,只能设置里 disable

image-20250620181402961

Coding

C代码使用C++代码

例:现在需要调用在某个 .c 文件中,调用一个由 .hpp 与 .cpp 文件定义的函数

步骤:

  1. 新建一个 .h 文件,将 .hpp 文件改造为 .h 文件
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
#ifdef __cplusplus
extern "C" {
#endif

void TraceEventStart(const char *name);
void TraceEventEnd();

#ifdef __cplusplus
} // extern "C"
#endif
  1. 在 .cpp 文件中 include 这个 .h 文件,同样地,也要用 extern “C” 包裹起来
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#ifdef __cplusplus
extern "C" {
#endif

void TraceEventStart(const char *name) {
    TRACE_EVENT_BEGIN(event_category, perfetto::StaticString{name});
}

void TraceEventEnd() {
    TRACE_EVENT_END(event_category);
}

#ifdef __cplusplus
} // extern "C"
#endif

​ 3. 在 .c 文件里 include .h文件

文件读写

C++

流方法:fstream 对象在销毁时会自动调用 close()

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <fstream>
#include <string>

// read
std::ofstream log(get_log_filename(layer_id, head_id), std::ios::binary | std::ios::app);
int value = 100;
log << value << " ";
log.write(reinterpret_cast<const char*>(&value), sizeof(value)); // binary
log.close();

// write
std::ifstream log(filename, std::ios::binary);
log >> value;
log.read(reinterpret_cast<char*>(&value), sizeof(value));

// 使用 std::fstream 必须手动指定读写模式
std::fstream iofile("data.bin", std::ios::in | std::ios::out | std::ios::binary);

常见的打开模式:

标识符含义说明
std::ios::in读模式为读取而打开文件。ifstream 的默认模式。
std::ios::out写模式为写入而打开文件。ofstream 的默认模式。
std::ios::binary二进制模式以二进制方式处理文件,而非文本模式。读写速度快且无损
std::ios::app追加模式(append) 写入操作将在文件末尾进行。
std::ios::trunc截断模式(Truncate) 如果文件已存在,打开时会清空其所有内容。ofstream 默认行为。
std::ios::ate打开后定位到末尾(At End) 文件打开后,立即将位置指针移动到文件末尾。可以写入或移动到任何位置。

stringstream 操纵字符串:

1
2
3
4
5
6
7
8
#include <sstream>
#include <string>

std::string get_log_filename(size_t layer_id, size_t head_id) {
    std::stringstream ss;
    ss << "prefill_cache" << "/cache_L" << layer_id << "_H" << head_id << ".log";
    return ss.str();
}

C

fread 方法:

“w” 写, “r” 读, “a” 追加, “b” 二进制

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <cstdio>

FILE *f = fopen(input->name, "w");
if (f) {
    const char* text = "hello";
    fwrite(text, sizeof(char), 5, f); // 写入数据
    fflush(f);                        // 强制将缓冲区内容写入文件
    fclose(f);                        // 必须手动关闭
}

FILE *f = fopen("data.bin", "rb");
if (f == NULL) {
    perror("Error opening file");
    return -1;
}

int value;
size_t items_read = fread(&value, sizeof(int), 1, f);

if (items_read == 1) {
	printf("Read value: %d\n", value);
}

fclose(f);

fget 方法:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
FILE *f = fopen("log.txt", "r"); // "r" = read (文本读)
if (f) {
    char line[256]; // 定义一个行缓冲区
    
    while (fgets(line, sizeof(line), f) != NULL) {
        printf("%s", line);
    }
    
    fclose(f); 
}

fprintf 方法:读取格式化文本

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
FILE *f = fopen("config.txt", "r"); 
if (f == NULL) {
    perror("Error opening file");
    return -1;
}

int layer_id, head_id;
fscanf(f, "%d %d", &layer_id, &head_id);
printf("Layer: %d, Head: %d\n", layer_id, head_id);

FILE *f = fopen("config.txt", "w"); 

fprintf(f, "Log Entry:\n");
fprintf(f, "Processed Layer %d, Head %d.\n", layer_id, head_id);

fclose(f); 

同时进行读和写,应该使用以下三种带 + 的模式之一:

模式含义文件不存在时文件已存在时
“r+”读写更新打开失败 (返回 NULL)不清空内容,指针在文件开头
“w+”写读更新创建新文件清空内容 (截断为0),指针在文件开头
“a+”追加读写创建新文件不清空内容,初始读指针在开头,写指针在末尾

Python

with 语句块结束时,无论是否发生异常,Python 都会自动关闭文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
with open("log.txt", "w", encoding="utf-8") as f:
    f.write("\n")

with open("log.txt", "r", encoding="utf-8") as f:
    # 方式一:一次性读取所有内容
    content = f.read()
    print(content)

    # 方式二:逐行读取
    for line in f:
        print(line.strip()) # strip() 去除行尾换行符
        
# 写二进制文件
data = b'\xDE\xAD\xBE\xEF'
with open("data.bin", "wb") as f:
    f.write(data)

# 读二进制文件
with open("data.bin", "rb") as f:
    read_data = f.read()
    print(read_data)
Licensed under CC BY-NC-SA 4.0
本文总访问量(loading...)
使用 Hugo 构建
主题 StackJimmy 设计
本站总访问量(loading...)
本站访客数(loading...)人次