FundamentalAnalysisGPT is a custom-built, Pre-LayerNorm Generative Pretrained Transformer (GPT) designed for the financial domain. This project demonstrates a complete end-to-end LLM pipeline consisting of two phases:
- Generative Pretraining: The base model is trained from scratch on the SEC EDGAR corpus (10-K and 10-Q filings) to learn the linguistic structure, vocabulary, and statistical distribution of corporate financial disclosures.
- Parameter-Efficient Fine-Tuning (PEFT): The pre-trained base model is adapted using Low-Rank Adaptation (LoRA) on the FinQA dataset. By freezing 99.86% of the network and injecting trainable rank-8 adapters into the attention projection matrices, the model is taught to shift from "document continuation" to "instruction-following" for financial question answering.
- GPU Requirement: This code is optimized for an NVIDIA A100 (or equivalent Ampere/Hopper architecture like L4 or H100).
- Precision & Compilers: The training loop utilizes
torch.bfloat16automatic mixed precision, FlashAttention, andtorch.compile. - Note: Attempting to run this code on older GPUs (like the free Colab T4) will result in a
CUDA error: no kernel image is available for executioncrash due to the lack of hardware support forbfloat16tensor cores.
The project is designed to be run in a Google Colab environment (A100 runtime). Colab pre-installs most requirements (like PyTorch and CUDA bindings), but you must strictly downgrade NumPy to avoid C++ ABI conflicts with the HuggingFace datasets library.
Run the following command in your environment before executing the code:
pip install -q "numpy<2" "datasets<3" tiktoken matplotlib
If running locally, ensure you have PyTorch 2.0+ installed with CUDA 12.1+ support.
Milestone_3.ipynb: The main executable Jupyter Notebook containing the full pipeline.architecture.py: The consolidated Python module containing the custom Tokenizer, Dataset streaming classes, Transformer model, and optimized training loops.results/: Contains the generated loss curve plots and textual outputs.lora_adapted_model.pth/milestone_3_model.pth: The saved model weights (generated during runtime).
To reproduce the workflow, open code/Milestone_3.ipynb in Google Colab (with an A100 GPU attached) and execute the cells sequentially.
To Run Pretraining (Base Model):
- In the Configuration cell, set
TRAIN_MODEL = True. - Run the notebook. The script will stream the SEC EDGAR dataset, train for 2,000 steps, and automatically evaluate validation loss every 20 steps.
To Run LoRA Adaptation:
- The notebook automatically freezes the base model and injects the
LinearWithLoRAwrappers. - The FinQA dataset is processed using the
gpt2tiktoken encoding to maintain vocabulary consistency. - The LoRA training loop executes for 500 update steps and prints the training/validation loss.
All reported metrics and visualizations are generated automatically by the notebook:
- Loss Curves: Upon completing the pretraining loop, the notebook uses
matplotlibto plot the Training vs. Validation loss and saves it directly to the working directory asloss_curve.png. - Perplexity & Loss Tables: The final Training Loss, Validation Loss, and calculated Perplexity are printed to the standard output console immediately following the training loops.
- Model weights are saved to the root directory as
milestone_3_model.pthandlora_adapted_model.pth. - Plots are saved to the root directory as
loss_curve.png(and should be moved to theresults/folder for submission review).
The final cells in Milestone_3.ipynb contain the inference loops used in the video demo.
To reproduce the base vs. adapted comparison:
- Ensure the notebook has loaded the
lora_adapted_model.pthweights (which the script does automatically after fine-tuning). - Run the final cell labeled "Inference Evaluation".
- The script will process 4 distinct financial prompts using multinomial sampling (
temperature=0.8,top_k=40). - The generated text will be printed to the console, demonstrating both the model's mastery of SEC structural style and the limitations of its pretraining prior on instruction-following tasks.