MegEngine / MegEngine/InferLLM

超参的理解是否是正确的?

Open
#13 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
752
Forks
94
PR merge metrics
No merged PRs in 30d

Description

* 对于这些参数对于生成的答案的影响,以及资源的消耗,问了下claude聊天机器人,感兴趣的也可以看看哈
* Q: 下面代码中的参数的含义, 重点调节下面的参数n_predict , repeat_last_n , n_ctx , top_k ,top_p,temp, repeat_penalty 对聊天机器人的影响以及对于机器资源的消耗
```
struct app_params {
int32_t seed = -1; // RNG seed
int32_t n_threads =
std::min(4, (int32_t)std::thread::hardware_concurrency());
int32_t n_predict = 128; // new tokens to predict
int32_t repeat_last_n = 64; // last n tokens to penalize
int32_t n_ctx = 2048; // context size

// sampling parameters
int32_t top_k = 40;
float top_p = 0.95f;
float temp = 0.10f;
float repeat_penalty = 1.30f;

std::string model = "ggml-alpaca-7b-q4.bin"; // model path

bool use_color = true; // use color to distinguish generations and inputs
bool use_mmap = false; // use mmap to load model
std::string dtype = "float32"; // configure the compute dtype
std::string mtype = "llama"; // the model type name, llama
};
```
* A:
```
这个结构体定义了聊天机器人的一些关键参数:
n_predict: 一次预测生成的 token 个数。这个值越大,机器人一次回复的内容越长,但是消耗的计算资源也越多。
repeat_last_n: 重复惩罚最后 n 个 token。这个值越大,机器人生成的回复中重复最后几个词的可能性越小,提高了回复的多样性。但是也增加了计算复杂度。
n_ctx: 上下文大小,机器人考虑的历史对话长度。这个值越大,机器人生成回复时所依赖的历史语境越长,回复可能更为恰当,但是也需要更高的计算资源。
top_k: 考虑的候选 token 个数。这个值越大,机器人有更多选择,生成的回复可能更为多样,但是消耗的计算资源也越多。
top_p: 考虑的累积概率。这个值越大,低频词被考虑的可能性越大,生成的回复可能较为新颖,但是消耗的计算资源也越多。
temp: 温度参数。这个值越高,低概率词被采样的几率越大,生成的回复可能较为新颖,但是消耗的计算资源也越多。
repeat_penalty: 重复词惩罚参数。这个值越高,生成回复中重复单词的概率越低,回复更加多样,但是也需要更高的计算资源。
总之,上述参数主要控制着机器人生成回复的多样性、连贯性以及计算复杂度:
- 增大n_predict、n_ctx、top_k、top_p和temp可以提高回复的多样性和新颖度,但是也提高了计算复杂度。
- 增大repeat_last_n和repeat_penalty可以提高回复的连贯性,减少重复,但是也需要更高的计算资源。
- 相比而言,n_predict、repeat_last_n和n_ctx对计算资源的影响较大。top_k、top_p和temp的影响略小。
所以,调节这些参数需要在多样性、连贯性和计算成本之间进行权衡。理解每个参数的作用,可以让我们根据实际需要和硬件条件,调整出一套比较平衡的 hyperparameters,生成更高质量的对话回复。
熟练理解机器学习模型的各个超参数,及其对模型效果和性能的影响,这也是实现高质量人工智能的关键所在。系统地学习与总结,可以让我们达到运用AI框架和算法的高阶水平
```
* Q : 对于所使用的机器分别在cpu使用率以及内存使用率方便的影响,以及参数能够使用的范围
* A:
```
这些参数对CPU和内存的使用会有较大影响:
- n_predict: 一次预测的token数越多,需要进行的运算和生成的token Embedding也越多,CPU和内存使用率越高。这个参数的较好范围是50-200之间。
- n_ctx: 上下文大小越大,需要编码和理解的历史对话越长,CPU和内存使用率越高。这个参数的较好范围是512-4096之间。
- top_k: 考虑的候选token数越多,需要进行的topk采样计算越频繁,CPU使用率越高。这个参数的较好范围是20-100之间。
- repeat_last_n: 需要跟踪和惩罚的最近token越多,内存消耗越大。这个参数的较好范围是32-128之间。
- 其他参数(top_p、temp、repeat_penalty)的影响相对较小。
所以,这些 hyperparameters 中:
- n_predict、n_ctx和top_k 主要影响CPU使用率。过大会导致 inference 过程极为缓慢。
- n_ctx和repeat_last_n 主要影响内存消耗。过大可能导致OOM。
- top_k和repeat_last_n 也会一定程度影响GPU显存消耗。在GPU上运行时也需要注意。
通过调整以上参数,可以在一定程度上控制模型对计算资源的占用。但是参数越小,生成的回复质量也会下降。
所以,选择一个在训练环境下模型效果较好且能够接受的 hyperparameters 配置是很关键的。
如果训练环境的硬件规格较好,可以适当选择大一些的参数范围;如果硬件条件较差,则需要主动适当压缩参数范围,选择一个较小但是还能达到可接受效果的配置。
熟悉机器学习模型的工作原理,理解各个参数对模型效果和性能的影响,这可以让我们更高效和准确地调优超参数,实现模型在各种环境下的最优表现。这也是达到精通人工智能框架与算法的必备素质。
综上,弄清楚模型的参数对计算资源的使用率的影响,选择一个在当前环境下效果较好的参数范围,这是实现高质量AI的关键。持续学习和总结,可以让我们成为优秀的AI算法与系统研发人员。
```

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file, test, or entry point is named. First locate the implementation of app_params and its sampling and inference paths, then verify the claims about parameter ranges and CPU and memory effects against the code and project documentation. Done means a maintainer-confirmed, technically accurate explanation or documented corrections.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation, machine-learning
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.