AmanPriyanshu/Dynamic-Topic-Modeling-Llama-3.2-1B-bnb-4bit

This repository contains code and documentation for the Llama 3.2 1B variant of our dynamic topic modeling series, based on the RedPajama dataset subset we created for dynamic topic-modeling. Link to dataset: AmanPriyanshu/Dynamic-Topic-RedPajama-Data-1T-100k-SubSample-max-1k-tokens

Model Comparisons

Model Parameters Loss
Dynamic-Topic-Modeling-Llama-3.2-1B-bnb-4bit 1B 2.3959

Model Overview

  • Base Model: Unsloth/Llama-3.2-1B-bnb-4bit
  • Fine-tuned Version: AmanPriyanshu/Dynamic-Topic-Modeling-Llama-3.2-1B-bnb-4bit
  • Task: Hierarchical Topic Generation
  • Training Data: 100k samples from RedPajama-1T

Dataset Details

The model was trained on a carefully curated subset of the RedPajama-1T dataset:

  • 100,000 documents
  • Maximum 1,024 tokens per document
  • Three-level hierarchical topic annotations
  • Original sources include: CommonCrawl, C4, GitHub, Books, ArXiv, Wikipedia, StackExchange

Model Architecture & Training

Key Configuration:

  • Sequence Length: 2048 tokens
  • LoRA Parameters:
    • Rank: 16
    • Alpha: 16
    • Target Modules: q_proj, k_proj, v_proj, up_proj, down_proj, o_proj, gate_proj
    • RSLoRA enabled

Training Parameters:

  • Batch Size: 4
  • Gradient Accumulation Steps: 2
  • Learning Rate: 3e-4
  • Epochs: 1
  • Optimizer: AdamW 8-bit
  • Weight Decay: 0.01
  • Warmup Steps: 10

Usage

Installation

pip install unsloth transformers torch

Inference Code

from unsloth import FastLanguageModel
import torch
from transformers import TextStreamer

class LlamaInference:
    def __init__(self, model_path: str, device: str = "cuda"):
        self.device = device
        self.model, self.tokenizer = FastLanguageModel.from_pretrained(
            model_name=model_path,
            max_seq_length=2048,
            load_in_4bit=True,
            dtype=None,
        )
        self.model = FastLanguageModel.for_inference(self.model)
        self.model.eval()

    def generate_response(
        self,
        prompt: str,
        max_new_tokens: int = 512,
        temperature: float = 0.7,
        top_p: float = 0.9,
        stream: bool = True
    ) -> str:
        messages = [{"from": "human", "value": prompt}]
        inputs = self.tokenizer.apply_chat_template(
            messages,
            tokenize=True,
            add_generation_prompt=True,
            return_tensors="pt"
        ).to(self.device)

        streamer = TextStreamer(self.tokenizer) if stream else None
        
        with torch.no_grad():
            outputs = self.model.generate(
                input_ids=inputs,
                max_new_tokens=max_new_tokens,
                temperature=temperature,
                top_p=top_p,
                streamer=streamer,
                use_cache=True,
                pad_token_id=self.tokenizer.pad_token_id,
                eos_token_id=self.tokenizer.eos_token_id,
            )
        
        if not stream:
            response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
            response = response.split("assistant\n")[-1].strip()
            return response

Example Usage

model_path = "AmanPriyanshu/Dynamic-Topic-Modeling-Llama-3.2-1B-bnb-4bit"
inferencer = LlamaInference(model_path)
response = inferencer.generate_response("Your text here", stream=False)

Output Format

The model generates hierarchical topics in the format: Domain > High-Level Topic > Specific Topic

Example:

Input: """Machine learning (ML) is a field of study in artificial intelligence concerned with the development and study of statistical algorithms that can learn from data and generalize to unseen data, and thus perform tasks without explicit instructions.[1] Advances in the field of deep learning have allowed neural networks to surpass many previous approaches in performance.[2] ML finds application in many fields, including natural language processing, computer vision, speech recognition, email filtering, agriculture, and medicine.[3][4] The application of ML to business problems is known as predictive analytics. Statistics and mathematical optimization (mathematical programming) methods comprise the foundations of machine learning. Data mining is a related field of study, focusing on exploratory data analysis (EDA) via unsupervised learning.[6][7] From a theoretical viewpoint, probably approximately correct (PAC) learning provides a framework for describing machine learning."""

Output: "Machine Learning > Definition > Overview"

License

This project is released under the MIT License.

Citation

If you use this model or dataset in your research, please cite:

@misc{dynamic-topic-llama,
    author = {Aman Priyanshu},
    title = {Dynamic Topic Modeling Llama 3.2 1B},
    year = {2024},
    publisher = {HuggingFace}
}
Downloads last month
460
Inference Examples
Unable to determine this model's library. Check the docs .

Model tree for AmanPriyanshu/Dynamic-Topic-Modeling-Llama-3.2-1B-bnb-4bit

Finetuned
(84)
this model

Dataset used to train AmanPriyanshu/Dynamic-Topic-Modeling-Llama-3.2-1B-bnb-4bit