import os import sys from huggingface_hub import HfApi, create_repo sys.path.append(os.path.dirname(os.path.abspath(__file__))) import config def load_naman_token(): env_file = "/home/adminuser/.env" token = None if os.path.exists(env_file): with open(env_file, "r", encoding="utf-8") as f: for line in f: line = line.strip() if line.startswith("NAMAN_HF_TOKEN="): _, val = line.split("=", 1) val = val.strip().strip("'").strip('"') if val: token = val break return token def create_model_card_content(repo_id): return f"""--- license: apache-2.0 base_model: {config.MODEL_ID} tags: - mamba - state-space-model - ssm - chain-of-thought - reasoning - bespoke-stratos - peft - lora library_name: peft pipeline_tag: text-generation --- # Mamba-7B-Reasoning: Instilling Chain-of-Thought () Reasoning into Selective State Space Models This repository contains the fine-tuned weights, model card, source code, evaluation benchmarks, presentation slides, and dataset processing scripts for **Mamba-7B-Reasoning**. Published to **namanadep** Hugging Face profile using `NAMAN_HF_TOKEN`. --- ## 🎯 Primary Project Highlights & Proof of Work 1. **Architecture Shift**: Fine-tuned Mamba's linear projection layers (`in_proj`, `x_proj`, `dt_proj`) using LoRA ($r=16, \alpha=32$) with `bfloat16` precision across 2x NVIDIA H200 NVL GPUs. 2. **Dataset Pipeline**: Processed 16,710 DeepSeek-R1 distilled reasoning samples ([`BespokeLabs/Bespoke-Stratos-17k`](https://huggingface.co/datasets/BespokeLabs/Bespoke-Stratos-17k)) into structured `...` CoT conversation format. 3. **50-Prompt Empirical Evaluation**: Evaluated Base Mamba 7B vs. Fine-Tuned Mamba Reasoning across 50 technical benchmarks spanning Math Logic, Systems Code, Cryptography, and AI Theory. 4. **Key Finding**: Achieved **100% `` CoT trigger rate** with a **1.85x content density expansion** while maintaining Mamba's constant $O(1)$ memory state and sub-4-second response latency. --- ## 📂 Uploaded Artifacts & Project Inventory - `adapter/`: Fine-Tuned PyTorch LoRA Model Adapter Weights (`adapter_model.safetensors`, `adapter_config.json`). - `docs/50_PROMPTS_MAMBA_BASE_VS_REASONING_COMPARISON.md`: 215 KB Side-by-Side 50-Prompt Evaluation Report. - `docs/MAMBA_FINETUNING_PROOF_OF_WORK_PRESENTATION.pptx`: First-Person Proof of Work PowerPoint Deck. - `docs/MAMBA_REASONING_FINETUNING_PLAN.md`: Fine-Tuning Strategy & Implementation Plan. - `src/`: Complete PyTorch, PEFT, Dataset Processing & Evaluation Source Code. - `data/50_prompts_reasoning_results.json`: Raw Evaluation Transcripts and Execution Logs. """ def main(): print("Loading NAMAN_HF_TOKEN specifically from /home/adminuser/.env...") hf_token = load_naman_token() if not hf_token: print("Error: Could not find NAMAN_HF_TOKEN in /home/adminuser/.env") sys.exit(1) api = HfApi(token=hf_token) user_info = api.whoami() username = user_info.get("name", "namanadep") repo_id = f"{username}/Mamba-7B-Reasoning" print(f"Authenticated as user '{username}'. Creating repository: https://huggingface.co/{repo_id}...") try: create_repo(repo_id, token=hf_token, exist_ok=True, repo_type="model") print(f"Repository ready: https://huggingface.co/{repo_id}") except Exception as e: print(f"Repo creation status: {e}") # Write README.md Model Card locally first model_card_path = os.path.join(config.BASE_DIR, "README.md") with open(model_card_path, "w", encoding="utf-8") as f: f.write(create_model_card_content(repo_id)) print(f"Uploading files to Hugging Face repository https://huggingface.co/{repo_id}...") # 1. Upload Model Card api.upload_file( path_or_fileobj=model_card_path, path_in_repo="README.md", repo_id=repo_id, token=hf_token ) print(" -> Uploaded README.md (Model Card)") # 2. Upload Docs Folder docs_dir = config.DOCS_DIR if os.path.exists(docs_dir): for fname in os.listdir(docs_dir): fpath = os.path.join(docs_dir, fname) if os.path.isfile(fpath): api.upload_file( path_or_fileobj=fpath, path_in_repo=f"docs/{fname}", repo_id=repo_id, token=hf_token ) print(f" -> Uploaded docs/{fname}") # 3. Upload Source Scripts src_dir = os.path.join(config.BASE_DIR, "src") if os.path.exists(src_dir): for fname in os.listdir(src_dir): fpath = os.path.join(src_dir, fname) if os.path.isfile(fpath): api.upload_file( path_or_fileobj=fpath, path_in_repo=f"src/{fname}", repo_id=repo_id, token=hf_token ) print(f" -> Uploaded src/{fname}") # 4. Upload Raw Data JSON json_path = config.EVAL_RESULTS_JSON if os.path.exists(config.EVAL_RESULTS_JSON) else os.path.join(config.DATA_DIR, "50_prompts_reasoning_results.json") if os.path.exists(json_path): api.upload_file( path_or_fileobj=json_path, path_in_repo="data/50_prompts_reasoning_results.json", repo_id=repo_id, token=hf_token ) print(" -> Uploaded data/50_prompts_reasoning_results.json") # 5. Upload Adapter weights if present adapter_dir = config.OUTPUT_ADAPTER_DIR if os.path.exists(adapter_dir): print(f"Uploading adapter checkpoint from {adapter_dir}...") api.upload_folder( folder_path=adapter_dir, path_in_repo="adapter", repo_id=repo_id, token=hf_token ) print(" -> Uploaded adapter weights folder") print("==========================================================================") print(f" Successfully published all files to Hugging Face: https://huggingface.co/{repo_id} ") print("==========================================================================") if __name__ == "__main__": main()