ChatGLM-6B模型结构代码解析(单机版)

本文介绍ChatGLM-6B的模型结构,代码来自https://huggingface.co/THUDM/chatglm-6b/blob/main/modeling_chatglm.py。

一、激活函数

ChatGLM-6B使用的激活函数为GELU,其可以近似实现为:

GELU(�)≈0.5�(1+tanh⁡(2�(�+0.044715�3)))

@torch.jit.script
def gelu_impl(x):
    """OpenAI's gelu implementation."""
    return 0.5 * x * (1.0 + torch.tanh(0.7978845608028654 * x *
                                       (1.0 + 0.044715 * x * x)))
​
def gelu(x):
    return gelu_impl(x)

二、GLU层

虽然在实现代码中命名为GLU,但这里实现的还是MLP层:
GLU(�)=GELU(��1)�2

class GLU(torch.nn.Module):
    def __init__(sel

你可能感兴趣的:(#,LLM/经典模型,人工智能)