Text Classification
Transformers
decision-model
latent-reasoning
recurrent-depth
adaptive-compute
multilingual
Instructions to use d2v1shx/dazo with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use d2v1shx/dazo with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="d2v1shx/dazo")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("d2v1shx/dazo", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Publish Dazo from GitHub dc39eab
Browse files- dazo/modeling_dazo.py +38 -2
- modeling_dazo.py +38 -2
dazo/modeling_dazo.py
CHANGED
|
@@ -9,7 +9,7 @@ from __future__ import annotations
|
|
| 9 |
from typing import Optional
|
| 10 |
|
| 11 |
import torch
|
| 12 |
-
from transformers import AutoModel, PreTrainedModel
|
| 13 |
|
| 14 |
from .configuration_dazo import DazoConfig
|
| 15 |
from .core import DazoCore
|
|
@@ -21,7 +21,13 @@ class DazoForDecision(PreTrainedModel):
|
|
| 21 |
|
| 22 |
def __init__(self, config: DazoConfig):
|
| 23 |
super().__init__(config)
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
hidden = int(self.backbone.config.hidden_size)
|
| 26 |
self.core = DazoCore(
|
| 27 |
context_dim=hidden,
|
|
@@ -40,6 +46,36 @@ class DazoForDecision(PreTrainedModel):
|
|
| 40 |
if config.freeze_backbone:
|
| 41 |
self.freeze_backbone()
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def freeze_backbone(self):
|
| 44 |
for p in self.backbone.parameters():
|
| 45 |
p.requires_grad = False
|
|
|
|
| 9 |
from typing import Optional
|
| 10 |
|
| 11 |
import torch
|
| 12 |
+
from transformers import AutoConfig, AutoModel, PreTrainedModel
|
| 13 |
|
| 14 |
from .configuration_dazo import DazoConfig
|
| 15 |
from .core import DazoCore
|
|
|
|
| 21 |
|
| 22 |
def __init__(self, config: DazoConfig):
|
| 23 |
super().__init__(config)
|
| 24 |
+
|
| 25 |
+
# IMPORTANT: never call AutoModel.from_pretrained() here. Transformers may
|
| 26 |
+
# instantiate this class under a meta-device context while loading a Dazo
|
| 27 |
+
# checkpoint. Nested from_pretrained() calls are invalid in that context.
|
| 28 |
+
backbone_cfg = self._resolve_backbone_config(config)
|
| 29 |
+
self.backbone = AutoModel.from_config(backbone_cfg)
|
| 30 |
+
|
| 31 |
hidden = int(self.backbone.config.hidden_size)
|
| 32 |
self.core = DazoCore(
|
| 33 |
context_dim=hidden,
|
|
|
|
| 46 |
if config.freeze_backbone:
|
| 47 |
self.freeze_backbone()
|
| 48 |
|
| 49 |
+
@staticmethod
|
| 50 |
+
def _resolve_backbone_config(config: DazoConfig):
|
| 51 |
+
saved = getattr(config, "backbone_config", None)
|
| 52 |
+
if saved:
|
| 53 |
+
saved = dict(saved)
|
| 54 |
+
model_type = saved.pop("model_type")
|
| 55 |
+
return AutoConfig.for_model(model_type, **saved)
|
| 56 |
+
|
| 57 |
+
# Backward compatibility for checkpoints produced before backbone_config
|
| 58 |
+
# was persisted. Config loading is safe in a meta context; weight loading is not.
|
| 59 |
+
backbone_cfg = AutoConfig.from_pretrained(config.backbone_name)
|
| 60 |
+
config.backbone_config = backbone_cfg.to_dict()
|
| 61 |
+
return backbone_cfg
|
| 62 |
+
|
| 63 |
+
@classmethod
|
| 64 |
+
def from_backbone_pretrained(cls, config: DazoConfig) -> "DazoForDecision":
|
| 65 |
+
"""Create a fresh Dazo model initialized from the named pretrained encoder.
|
| 66 |
+
|
| 67 |
+
Use this path for *new training*. Saved Dazo checkpoints should use the normal
|
| 68 |
+
``DazoForDecision.from_pretrained(path_or_repo)`` API so the outer checkpoint
|
| 69 |
+
loader restores both the backbone and Dazo reasoning-core weights.
|
| 70 |
+
"""
|
| 71 |
+
backbone_cfg = AutoConfig.from_pretrained(config.backbone_name)
|
| 72 |
+
config.backbone_config = backbone_cfg.to_dict()
|
| 73 |
+
model = cls(config)
|
| 74 |
+
model.backbone = AutoModel.from_pretrained(config.backbone_name, config=backbone_cfg)
|
| 75 |
+
if config.freeze_backbone:
|
| 76 |
+
model.freeze_backbone()
|
| 77 |
+
return model
|
| 78 |
+
|
| 79 |
def freeze_backbone(self):
|
| 80 |
for p in self.backbone.parameters():
|
| 81 |
p.requires_grad = False
|
modeling_dazo.py
CHANGED
|
@@ -9,7 +9,7 @@ from __future__ import annotations
|
|
| 9 |
from typing import Optional
|
| 10 |
|
| 11 |
import torch
|
| 12 |
-
from transformers import AutoModel, PreTrainedModel
|
| 13 |
|
| 14 |
from .configuration_dazo import DazoConfig
|
| 15 |
from .core import DazoCore
|
|
@@ -21,7 +21,13 @@ class DazoForDecision(PreTrainedModel):
|
|
| 21 |
|
| 22 |
def __init__(self, config: DazoConfig):
|
| 23 |
super().__init__(config)
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
hidden = int(self.backbone.config.hidden_size)
|
| 26 |
self.core = DazoCore(
|
| 27 |
context_dim=hidden,
|
|
@@ -40,6 +46,36 @@ class DazoForDecision(PreTrainedModel):
|
|
| 40 |
if config.freeze_backbone:
|
| 41 |
self.freeze_backbone()
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def freeze_backbone(self):
|
| 44 |
for p in self.backbone.parameters():
|
| 45 |
p.requires_grad = False
|
|
|
|
| 9 |
from typing import Optional
|
| 10 |
|
| 11 |
import torch
|
| 12 |
+
from transformers import AutoConfig, AutoModel, PreTrainedModel
|
| 13 |
|
| 14 |
from .configuration_dazo import DazoConfig
|
| 15 |
from .core import DazoCore
|
|
|
|
| 21 |
|
| 22 |
def __init__(self, config: DazoConfig):
|
| 23 |
super().__init__(config)
|
| 24 |
+
|
| 25 |
+
# IMPORTANT: never call AutoModel.from_pretrained() here. Transformers may
|
| 26 |
+
# instantiate this class under a meta-device context while loading a Dazo
|
| 27 |
+
# checkpoint. Nested from_pretrained() calls are invalid in that context.
|
| 28 |
+
backbone_cfg = self._resolve_backbone_config(config)
|
| 29 |
+
self.backbone = AutoModel.from_config(backbone_cfg)
|
| 30 |
+
|
| 31 |
hidden = int(self.backbone.config.hidden_size)
|
| 32 |
self.core = DazoCore(
|
| 33 |
context_dim=hidden,
|
|
|
|
| 46 |
if config.freeze_backbone:
|
| 47 |
self.freeze_backbone()
|
| 48 |
|
| 49 |
+
@staticmethod
|
| 50 |
+
def _resolve_backbone_config(config: DazoConfig):
|
| 51 |
+
saved = getattr(config, "backbone_config", None)
|
| 52 |
+
if saved:
|
| 53 |
+
saved = dict(saved)
|
| 54 |
+
model_type = saved.pop("model_type")
|
| 55 |
+
return AutoConfig.for_model(model_type, **saved)
|
| 56 |
+
|
| 57 |
+
# Backward compatibility for checkpoints produced before backbone_config
|
| 58 |
+
# was persisted. Config loading is safe in a meta context; weight loading is not.
|
| 59 |
+
backbone_cfg = AutoConfig.from_pretrained(config.backbone_name)
|
| 60 |
+
config.backbone_config = backbone_cfg.to_dict()
|
| 61 |
+
return backbone_cfg
|
| 62 |
+
|
| 63 |
+
@classmethod
|
| 64 |
+
def from_backbone_pretrained(cls, config: DazoConfig) -> "DazoForDecision":
|
| 65 |
+
"""Create a fresh Dazo model initialized from the named pretrained encoder.
|
| 66 |
+
|
| 67 |
+
Use this path for *new training*. Saved Dazo checkpoints should use the normal
|
| 68 |
+
``DazoForDecision.from_pretrained(path_or_repo)`` API so the outer checkpoint
|
| 69 |
+
loader restores both the backbone and Dazo reasoning-core weights.
|
| 70 |
+
"""
|
| 71 |
+
backbone_cfg = AutoConfig.from_pretrained(config.backbone_name)
|
| 72 |
+
config.backbone_config = backbone_cfg.to_dict()
|
| 73 |
+
model = cls(config)
|
| 74 |
+
model.backbone = AutoModel.from_pretrained(config.backbone_name, config=backbone_cfg)
|
| 75 |
+
if config.freeze_backbone:
|
| 76 |
+
model.freeze_backbone()
|
| 77 |
+
return model
|
| 78 |
+
|
| 79 |
def freeze_backbone(self):
|
| 80 |
for p in self.backbone.parameters():
|
| 81 |
p.requires_grad = False
|