topogpt3 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
topogpt3/__init__.py ADDED
@@ -0,0 +1,205 @@
1
+ """TopoGPT3: complex-valued spectral language model for code.
2
+
3
+ This package bundles:
4
+
5
+ - ``topogpt3.model``: the base TopoGPT2 architecture (quaternion spectral
6
+ layers, BPE tokenizer, helpers).
7
+ - ``topogpt3.train``: the curriculum trainer with Grassmannian / Fisher /
8
+ phase diagnostics.
9
+ - ``topogpt3.inference``: a standard autoregressive sampler that loads a
10
+ trained safetensors checkpoint.
11
+ - ``topogpt3.inference_hrm``: a hierarchical recursive reasoning sampler
12
+ that reuses the same checkpoint with no extra trained parameters.
13
+ - ``topogpt3.lens_model``: the Jacobian-lens model adapter (LensModel
14
+ protocol + TopoGPT3LensModel wrapper).
15
+ - ``topogpt3.jlens``: Jacobian lens fitting, application, and the
16
+ ActivationRecorder / JacobianLens infrastructure.
17
+
18
+ Typical usage from a downstream project::
19
+
20
+ from topogpt3 import InferenceSettings, InferencePipeline
21
+
22
+ settings = InferenceSettings(
23
+ checkpoint_dir="checkpoints_topogpt3",
24
+ prompt="def fibonacci(",
25
+ max_new_tokens=200,
26
+ )
27
+ InferencePipeline(settings).execute()
28
+
29
+ Jacobian lens usage::
30
+
31
+ from topogpt3.lens_model import TopoGPT3LensModel
32
+ from topogpt3.jlens import fit, JacobianLens
33
+
34
+ model = TopoGPT3LensModel.from_checkpoint("checkpoints_topogpt3/last")
35
+ lens = fit(model, prompts, source_layers=[0, 1, 2, 3, 4])
36
+ lens_logits, model_logits, input_ids = lens.apply(model, "def hello(")
37
+ """
38
+ from __future__ import annotations
39
+
40
+ __version__ = "0.1.0"
41
+
42
+ from .model import (
43
+ BPETokenizer,
44
+ QuaternionSpectralLayer,
45
+ SpectralAutoencoder,
46
+ TopoGPT2,
47
+ TopoGPT2Config,
48
+ set_seed,
49
+ setup_logger,
50
+ )
51
+ from .train import (
52
+ CheckpointStore,
53
+ CodeCurriculumLoader,
54
+ GrassmannianTracker,
55
+ TopoGPT3Config,
56
+ TopoGPT3Trainer,
57
+ apply_gauss_patch,
58
+ )
59
+ from .inference import (
60
+ GenerationReport,
61
+ InferencePipeline,
62
+ InferenceSettings,
63
+ SamplingPolicy,
64
+ )
65
+ from .inference_hrm import (
66
+ GenerationReasoningSummary,
67
+ HRMInferencePipeline,
68
+ HRMInferenceSettings,
69
+ HierarchicalRecursiveReasoner,
70
+ RecursiveReasoningConfig,
71
+ )
72
+ from .lens_model import (
73
+ LensModel,
74
+ TinyDecoder,
75
+ TopoGPT3LensConfig,
76
+ TopoGPT3LensModel,
77
+ )
78
+ from .jlens import (
79
+ ActivationRecorder,
80
+ JacobianLens,
81
+ TopoGPT3JLensAppConfig,
82
+ TopoGPT3JLensFitConfig,
83
+ fit,
84
+ jacobian_for_prompt,
85
+ valid_position_mask,
86
+ )
87
+ # advanced training (identity preserved: additive
88
+ # adapters + external loss/reward wrappers, never architecture rewrites)
89
+ from .yarn import YaRNConfig, apply_yarn_to_rope, yarn_scale_inv_freq
90
+ from .chat import (
91
+ apply_chat_template,
92
+ parse_thinking,
93
+ parse_tool_calls,
94
+ split_reasoning_content,
95
+ )
96
+ from .lora import (
97
+ LoRA,
98
+ apply_lora,
99
+ freeze_non_lora,
100
+ load_lora,
101
+ lora_parameters,
102
+ merge_lora,
103
+ save_lora,
104
+ )
105
+ from .rewards import (
106
+ base_rewards,
107
+ distillation_loss,
108
+ dpo_loss_fn,
109
+ grpo_advantages,
110
+ grpo_loss,
111
+ k3_kl,
112
+ logits_to_log_probs,
113
+ rep_penalty,
114
+ spectral_bonus,
115
+ )
116
+ from .rollout import (
117
+ RolloutResult,
118
+ TorchRolloutEngine,
119
+ compute_per_token_logps,
120
+ create_rollout_engine,
121
+ )
122
+ from .trainer_utils_topo import (
123
+ SkipBatchSampler,
124
+ get_lr,
125
+ init_distributed_mode,
126
+ is_main_process,
127
+ setup_seed,
128
+ topo_checkpoint,
129
+ )
130
+ from .tools_agent import TOOLS, execute_tool, rollout_multiturn
131
+ from .eval_toolcall import evaluate as evaluate_toolcall
132
+
133
+ __all__ = [
134
+ "__version__",
135
+ "ActivationRecorder",
136
+ "BPETokenizer",
137
+ "CheckpointStore",
138
+ "CodeCurriculumLoader",
139
+ "fit",
140
+ "GenerationReasoningSummary",
141
+ "GenerationReport",
142
+ "GrassmannianTracker",
143
+ "HierarchicalRecursiveReasoner",
144
+ "HRMInferencePipeline",
145
+ "HRMInferenceSettings",
146
+ "InferencePipeline",
147
+ "InferenceSettings",
148
+ "jacobian_for_prompt",
149
+ "JacobianLens",
150
+ "LensModel",
151
+ "QuaternionSpectralLayer",
152
+ "RecursiveReasoningConfig",
153
+ "SamplingPolicy",
154
+ "set_seed",
155
+ "setup_logger",
156
+ "SpectralAutoencoder",
157
+ "TinyDecoder",
158
+ "TopoGPT2",
159
+ "TopoGPT2Config",
160
+ "TopoGPT3Config",
161
+ "TopoGPT3JLensAppConfig",
162
+ "TopoGPT3JLensFitConfig",
163
+ "TopoGPT3LensConfig",
164
+ "TopoGPT3LensModel",
165
+ "TopoGPT3Trainer",
166
+ "apply_gauss_patch",
167
+ "valid_position_mask",
168
+ "YaRNConfig",
169
+ "apply_yarn_to_rope",
170
+ "yarn_scale_inv_freq",
171
+ "apply_chat_template",
172
+ "parse_thinking",
173
+ "parse_tool_calls",
174
+ "split_reasoning_content",
175
+ "LoRA",
176
+ "apply_lora",
177
+ "freeze_non_lora",
178
+ "load_lora",
179
+ "lora_parameters",
180
+ "merge_lora",
181
+ "save_lora",
182
+ "base_rewards",
183
+ "distillation_loss",
184
+ "dpo_loss_fn",
185
+ "grpo_advantages",
186
+ "grpo_loss",
187
+ "k3_kl",
188
+ "logits_to_log_probs",
189
+ "rep_penalty",
190
+ "spectral_bonus",
191
+ "RolloutResult",
192
+ "TorchRolloutEngine",
193
+ "compute_per_token_logps",
194
+ "create_rollout_engine",
195
+ "SkipBatchSampler",
196
+ "get_lr",
197
+ "init_distributed_mode",
198
+ "is_main_process",
199
+ "setup_seed",
200
+ "topo_checkpoint",
201
+ "TOOLS",
202
+ "execute_tool",
203
+ "rollout_multiturn",
204
+ "evaluate_toolcall",
205
+ ]
topogpt3/__main__.py ADDED
@@ -0,0 +1,81 @@
1
+ from __future__ import annotations
2
+
3
+ import sys
4
+
5
+
6
+ def main() -> None:
7
+ """TopoGPT3 entry point. Delegates to subcommands."""
8
+ if len(sys.argv) < 2:
9
+ print(
10
+ "TopoGPT3: complex-valued spectral language model\n"
11
+ "\n"
12
+ "Usage:\n"
13
+ " python3 -m topogpt3 this help\n"
14
+ " python3 -m topogpt3 infer standard autoregressive inference\n"
15
+ " python3 -m topogpt3 infer-hrm HRM recursive reasoning inference\n"
16
+ " python3 -m topogpt3 train curriculum training\n"
17
+ " python3 -m topogpt3 api start API server\n"
18
+ " python3 -m topogpt3 lens test model loading\n"
19
+ " python3 -m topogpt3 jlens Jacobian lens demo\n"
20
+ " python3 -m topogpt3 check quick checkpoint validation\n"
21
+ " python3 -m topogpt3 train-lora SFT with native LoRA adapters\n"
22
+ " python3 -m topogpt3 train-dpo preference alignment (DPO)\n"
23
+ " python3 -m topogpt3 train-grpo RLAIF (GRPO/CISPO, no critic)\n"
24
+ " python3 -m topogpt3 train-ppo RLAIF (PPO + value head)\n"
25
+ " python3 -m topogpt3 train-distill white-box distillation\n"
26
+ " python3 -m topogpt3 train-agent agentic RL (multi-turn tools)\n"
27
+ " python3 -m topogpt3 convert merge LoRA / export stubs\n"
28
+ " python3 -m topogpt3 export-chat curriculum HF -> chat JSONL\n"
29
+ )
30
+ return
31
+ subcommand = sys.argv[1]
32
+ sys.argv = sys.argv[1:]
33
+ if subcommand in ("jlens", "lens"):
34
+ from .jlens import _demo_jlens
35
+ _demo_jlens()
36
+ elif subcommand == "check":
37
+ from .lens_model import TopoGPT3LensModel
38
+ m = TopoGPT3LensModel.from_checkpoint("checkpoints_topogpt3/last")
39
+ print(f"OK: n_layers={m.n_layers}, d_model={m.d_model}")
40
+ elif subcommand in ("api", "api_server"):
41
+ from .api_server import main as api_main
42
+ api_main()
43
+ elif subcommand in ("infer", "inference"):
44
+ from .inference import main as infer_main
45
+ infer_main()
46
+ elif subcommand in ("infer-hrm", "inference-hrm"):
47
+ from .inference_hrm import main as hrm_main
48
+ hrm_main()
49
+ elif subcommand == "train":
50
+ from .train import main as train_main
51
+ train_main()
52
+ elif subcommand == "train-lora":
53
+ from .train_lora import main as m
54
+ m()
55
+ elif subcommand == "train-dpo":
56
+ from .train_dpo import main as m
57
+ m()
58
+ elif subcommand == "train-grpo":
59
+ from .train_grpo import main as m
60
+ m()
61
+ elif subcommand == "train-ppo":
62
+ from .train_ppo import main as m
63
+ m()
64
+ elif subcommand == "train-distill":
65
+ from .train_distill import main as m
66
+ m()
67
+ elif subcommand == "train-agent":
68
+ from .train_agent import main as m
69
+ m()
70
+ elif subcommand == "convert":
71
+ from .convert import main as m
72
+ m()
73
+ elif subcommand == "export-chat":
74
+ from .export_chat import main as m
75
+ m()
76
+ else:
77
+ print(f"Unknown subcommand: {subcommand}")
78
+
79
+
80
+ if __name__ == "__main__":
81
+ main()