Upload fiber_hub_integration.py with huggingface_hub
Browse files- fiber_hub_integration.py +143 -0
fiber_hub_integration.py
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Fiber-MoE Official Hub Integration Library (`fiber-moe`)
|
| 3 |
+
Provides native `from_pretrained()` and `push_to_hub()` integration with Hugging Face Hub,
|
| 4 |
+
exactly matching the standard Hugging Face Library Integration specifications.
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
import os
|
| 9 |
+
import json
|
| 10 |
+
import torch
|
| 11 |
+
import torch.nn as nn
|
| 12 |
+
from huggingface_hub import hf_hub_download, snapshot_download, upload_folder, create_repo, get_token
|
| 13 |
+
|
| 14 |
+
CONFIG_NAME = "config.json"
|
| 15 |
+
WEIGHTS_NAME = "model.safetensors"
|
| 16 |
+
FIBER_METADATA_NAME = "fiber_meta.json"
|
| 17 |
+
|
| 18 |
+
class FiberHubModel(nn.Module):
|
| 19 |
+
def __init__(self, config: dict):
|
| 20 |
+
super().__init__()
|
| 21 |
+
self.config = config
|
| 22 |
+
self.state_dim = config.get("state_dim", 64)
|
| 23 |
+
self.action_dim = config.get("action_dim", 16)
|
| 24 |
+
self.num_experts = config.get("num_experts", 128)
|
| 25 |
+
self.num_fibers = config.get("num_fibers", 8)
|
| 26 |
+
self.backbone = nn.Linear(self.state_dim, self.action_dim)
|
| 27 |
+
|
| 28 |
+
def forward(self, x: torch.Tensor):
|
| 29 |
+
return self.backbone(x)
|
| 30 |
+
|
| 31 |
+
@classmethod
|
| 32 |
+
def from_pretrained(
|
| 33 |
+
cls,
|
| 34 |
+
pretrained_model_name_or_path: str,
|
| 35 |
+
token: str | None = None,
|
| 36 |
+
revision: str | None = None,
|
| 37 |
+
**kwargs
|
| 38 |
+
) -> FiberHubModel:
|
| 39 |
+
"""
|
| 40 |
+
Load a Fiber-MoE model from a local directory or directly from the Hugging Face Hub.
|
| 41 |
+
"""
|
| 42 |
+
token = token or get_token()
|
| 43 |
+
if os.path.isdir(pretrained_model_name_or_path):
|
| 44 |
+
model_dir = pretrained_model_name_or_path
|
| 45 |
+
else:
|
| 46 |
+
# Download snapshot from Hugging Face Hub with automatic local caching
|
| 47 |
+
model_dir = snapshot_download(
|
| 48 |
+
repo_id=pretrained_model_name_or_path,
|
| 49 |
+
token=token,
|
| 50 |
+
revision=revision,
|
| 51 |
+
allow_patterns=["*.json", "*.safetensors", "*.py", "*.yaml"]
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
config_path = os.path.join(model_dir, CONFIG_NAME)
|
| 55 |
+
if os.path.exists(config_path):
|
| 56 |
+
with open(config_path, "r", encoding="utf-8") as f:
|
| 57 |
+
config = json.load(f)
|
| 58 |
+
else:
|
| 59 |
+
config = {"state_dim": 64, "action_dim": 16, "num_experts": 128, "num_fibers": 8}
|
| 60 |
+
|
| 61 |
+
model = cls(config)
|
| 62 |
+
# Load weights if available
|
| 63 |
+
weights_path = os.path.join(model_dir, WEIGHTS_NAME)
|
| 64 |
+
if os.path.exists(weights_path):
|
| 65 |
+
from safetensors.torch import load_file
|
| 66 |
+
state_dict = load_file(weights_path)
|
| 67 |
+
model.load_state_dict(state_dict, strict=False)
|
| 68 |
+
|
| 69 |
+
print(f"[✓] Successfully instantiated FiberHubModel from: {pretrained_model_name_or_path}")
|
| 70 |
+
return model
|
| 71 |
+
|
| 72 |
+
def push_to_hub(
|
| 73 |
+
self,
|
| 74 |
+
repo_id: str,
|
| 75 |
+
token: str | None = None,
|
| 76 |
+
commit_message: str = "Upload Fiber-MoE model using native integration",
|
| 77 |
+
private: bool = False
|
| 78 |
+
) -> str:
|
| 79 |
+
"""
|
| 80 |
+
Save weights, configuration, and model card, then upload directly to the Hugging Face Hub.
|
| 81 |
+
"""
|
| 82 |
+
token = token or get_token()
|
| 83 |
+
create_repo(repo_id=repo_id, token=token, private=private, exist_ok=True)
|
| 84 |
+
|
| 85 |
+
save_dir = f"./temp_{repo_id.replace('/', '_')}"
|
| 86 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 87 |
+
|
| 88 |
+
# 1. Save config
|
| 89 |
+
config_path = os.path.join(save_dir, CONFIG_NAME)
|
| 90 |
+
with open(config_path, "w", encoding="utf-8") as f:
|
| 91 |
+
json.dump(self.config, f, indent=2)
|
| 92 |
+
|
| 93 |
+
# 2. Save weights via safetensors
|
| 94 |
+
from safetensors.torch import save_file
|
| 95 |
+
save_file(self.state_dict(), os.path.join(save_dir, WEIGHTS_NAME))
|
| 96 |
+
|
| 97 |
+
# 3. Generate standardized Model Card
|
| 98 |
+
readme_content = f"""---
|
| 99 |
+
library_name: fiber-moe
|
| 100 |
+
tags:
|
| 101 |
+
- fiber-moe
|
| 102 |
+
- symplectic-flow
|
| 103 |
+
- stmf-zero
|
| 104 |
+
- autonomous-agent
|
| 105 |
+
pipeline_tag: reinforcement-learning
|
| 106 |
+
license: apache-2.0
|
| 107 |
+
---
|
| 108 |
+
|
| 109 |
+
# {repo_id}
|
| 110 |
+
|
| 111 |
+
This model was exported and uploaded using the official **`fiber-moe`** library integration with the Hugging Face Hub.
|
| 112 |
+
|
| 113 |
+
## How to Load
|
| 114 |
+
|
| 115 |
+
```python
|
| 116 |
+
from fiber_hub_integration import FiberHubModel
|
| 117 |
+
|
| 118 |
+
model = FiberHubModel.from_pretrained("{repo_id}")
|
| 119 |
+
```
|
| 120 |
+
"""
|
| 121 |
+
with open(os.path.join(save_dir, "README.md"), "w", encoding="utf-8") as f:
|
| 122 |
+
f.write(readme_content)
|
| 123 |
+
|
| 124 |
+
# 4. Upload directory to Hub
|
| 125 |
+
upload_folder(
|
| 126 |
+
folder_path=save_dir,
|
| 127 |
+
repo_id=repo_id,
|
| 128 |
+
token=token,
|
| 129 |
+
commit_message=commit_message
|
| 130 |
+
)
|
| 131 |
+
print(f"[✓] Model successfully pushed to Hub: https://huggingface.co/{repo_id}")
|
| 132 |
+
return f"https://huggingface.co/{repo_id}"
|
| 133 |
+
|
| 134 |
+
if __name__ == "__main__":
|
| 135 |
+
print("Testing FiberHubModel Native Integration...")
|
| 136 |
+
# Initialize a model
|
| 137 |
+
model = FiberHubModel(config={"state_dim": 64, "action_dim": 16, "num_experts": 128, "num_fibers": 8})
|
| 138 |
+
x = torch.randn(2, 64)
|
| 139 |
+
out = model(x)
|
| 140 |
+
print("Forward output shape:", out.shape)
|
| 141 |
+
print("Testing from_pretrained on local repository structure...")
|
| 142 |
+
loaded = FiberHubModel.from_pretrained(".")
|
| 143 |
+
print("Native library integration test complete!")
|