cnngpt 0.0.1
CNNGPT - CNNGPT
License: MIT
Requires Python: <4.0,>=3.10
Keywords: artificial intelligence, deep learning, optimizers, Prompt Engineering

[![Multi-Modality](agorabanner.png)](https://discord.com/servers/agora-999382051935506503)

# CNN-Based Language Model

[![Join our Discord](https://img.shields.io/badge/Discord-Join%20our%20server-5865F2?style=for-the-badge&logo=discord&logoColor=white)](https://discord.gg/agora-999382051935506503) [![Subscribe on YouTube](https://img.shields.io/badge/YouTube-Subscribe-red?style=for-the-badge&logo=youtube&logoColor=white)](https://www.youtube.com/@kyegomez3242) [![Connect on LinkedIn](https://img.shields.io/badge/LinkedIn-Connect-blue?style=for-the-badge&logo=linkedin&logoColor=white)](https://www.linkedin.com/in/kye-g-38759a207/) [![Follow on X.com](https://img.shields.io/badge/X.com-Follow-1DA1F2?style=for-the-badge&logo=x&logoColor=white)](https://x.com/kyegomezb)



## Detailed Explanation of Each Step

### Initialization Parameters

- **`vocab_size`**: The size of the vocabulary (number of unique tokens).
- **`embedding_dim`**: The dimension of the embeddings.
- **`num_layers`**: The number of convolutional layers.
- **`kernel_size`**: The size of the convolutional kernels.
- **`hidden_dim`**: The dimension of the hidden representations (should match `embedding_dim` for residual connections).
- **`max_seq_len`**: The maximum sequence length the model can handle.

### Embedding and Positional Encoding

- **Embeddings**: Convert token IDs to dense vectors.
- **Positional Encoding**: Adds a learnable positional embedding to each token embedding.

### Convolutional Blocks

- **Causal Convolution**: Uses padding on the left to ensure that the convolution at time `t` does not depend on future time steps.
- **Dilation**: Expands the receptive field exponentially, allowing the model to capture long-term dependencies.
- **GLU Activation**: Introduces a gating mechanism that can control the flow of information.
  - The output of the convolution is split into two halves along the channel dimension.
  - One half is passed through a sigmoid function to act as a gate for the other half.
- **Layer Normalization**: Normalizes the outputs to improve training stability.
- **Residual Connections**: Adds the input to the output to facilitate training deeper networks.

### Output Layer

- **Projection**: Maps the final hidden states to the vocabulary space to produce logits for each token.

## Handling Tensor Sizes

Throughout the network, we carefully manage tensor shapes to maintain consistency:

- After embedding and positional encoding: `[batch_size, seq_len, embedding_dim]`
- Before convolution: Transposed to `[batch_size, embedding_dim, seq_len]`
- After convolution and GLU: `[batch_size, hidden_dim, seq_len]`
- After layer normalization and residual connection: Same shape as input to convolution for residual addition.
- Before output layer: Transposed back to `[batch_size, seq_len, embedding_dim]`
- Output logits: `[batch_size, seq_len, vocab_size]`

## Important Notes

- **Causality**: By appropriately padding and slicing the convolution outputs, we ensure that the model does not use future information when predicting the current time step.
- **Residual Connections**: The `embedding_dim` and `hidden_dim` must be equal to correctly add the residual connection.
- **Layer Normalization**: Applied over the feature dimension; we transpose the tensor to `[batch_size, seq_len, hidden_dim]` before applying `LayerNorm`.
- **GLU Activation Function**: The gating mechanism enhances the model's capacity to model complex patterns.
- **Flexibility**: The model can handle sequences shorter than `max_seq_len`; positional encodings are sliced accordingly.

## Conclusion

We have successfully translated the detailed algorithm into a PyTorch implementation, carefully following each step and ensuring that the code aligns with the design principles outlined earlier. This CNN-based language model leverages causal and dilated convolutions, gated activations, residual connections, and layer normalization to effectively model textual data for generation tasks.

By understanding each component and its role in the model, we can appreciate how this architecture captures both local and global dependencies in language, offering a powerful alternative to traditional models in natural language processing.