Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion deeptab/architectures/experimental/trompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,11 @@ def forward(self, *data):
Tensor
The output predictions of the model.
"""
O = self.init_rec.unsqueeze(0).repeat(data[0][0].shape[0], 1, 1) # noqa: E741
# The prompt records are repeated once per row. Any feature group may be
# empty (e.g. a categorical-only table has no numerical tensors), so the
# batch size comes from the first tensor of whichever group is present.
batch_size = next(tensor.shape[0] for group in data if group for tensor in group)
O = self.init_rec.unsqueeze(0).repeat(batch_size, 1, 1) # noqa: E741
outputs = []

for i in range(self.n_cycles):
Expand Down
1 change: 1 addition & 0 deletions docs/model_zoo/experimental/trompt.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ param_grid = {
## Nuances and Limitations

- Trompt returns a prediction for each cycle. DeepTab's loss handling treats those cycle predictions like an ensemble.
- Any feature group may be absent. A table with only categorical columns, only numerical columns, or only external embeddings sizes the prompt records from whichever group is present. Previously, a categorical-only table crashed in the first forward pass because the batch size was read from the (empty) numerical group.
- Increasing `P` increases the number of prompt records and the prompt-column importance map size.
- Increasing `n_cycles` increases iterative refinement cost and adds more cycle predictions to the loss.
- The current implementation is prompt-inspired but not a standard Transformer with attention heads.
Expand Down
27 changes: 27 additions & 0 deletions tests/architectures/test_architecture_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,33 @@ def test_tabtransformer_categorical_only_forward_and_backward(num_classes, with_
assert all(torch.isfinite(gradient).all() for gradient in gradients)


@pytest.mark.parametrize("num_classes", [1, 3])
@pytest.mark.parametrize("feature_groups", ["categorical", "categorical+embeddings", "embeddings"])
def test_trompt_forward_without_numerical_features(num_classes, feature_groups):
from deeptab.architectures.experimental.trompt import Trompt
from deeptab.configs import TromptConfig

categorical_info = (
{"city": {"dimension": 1, "categories": 2, "preprocessing": "int"}} if "categorical" in feature_groups else {}
)
embedding_info = {"text": {"dimension": 3}} if "embeddings" in feature_groups else {}
model = Trompt(
feature_information=({}, categorical_info, embedding_info),
num_classes=num_classes,
config=TromptConfig(d_model=8, P=4, n_cycles=2),
)
categories = [torch.tensor([[0], [1], [0], [1]])] if categorical_info else []
embeddings = [torch.ones(4, 3)] if embedding_info else []
predictions = model([], categories, embeddings)

expected_shape = (4, 2, num_classes) if num_classes > 1 else (4, 2)
assert predictions.shape == expected_shape
assert torch.isfinite(predictions).all()
predictions.sum().backward()
assert model.init_rec.grad is not None
assert torch.isfinite(model.init_rec.grad).all()


@pytest.mark.smoke
@pytest.mark.parametrize("model_class", model_classes)
def test_model_inherits_base_model(model_class):
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test_model_zoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ def test_experimental_lss_fit_predict_evaluate(cls, regression_data):
NODEClassifier,
NDTFClassifier,
MambaTabClassifier,
TromptClassifier,
]


Expand Down
Loading