From b550e44454152fd2a294e099a783465be47011b5 Mon Sep 17 00:00:00 2001 From: Gillian Kant <56161060+xillig@users.noreply.github.com> Date: Tue, 10 Nov 2026 17:37:58 +0100 Subject: [PATCH] fix(architectures): run Trompt on tables without numerical features Trompt read the batch size from the first numerical tensor, so a categorical-only (or embedding-only) table failed with IndexError in the first forward pass. The prompt records are now sized from the first tensor of whichever feature group is present. --- deeptab/architectures/experimental/trompt.py | 6 ++++- docs/model_zoo/experimental/trompt.md | 1 + .../test_architecture_contracts.py | 27 +++++++++++++++++++ tests/integration/test_model_zoo.py | 1 + 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/deeptab/architectures/experimental/trompt.py b/deeptab/architectures/experimental/trompt.py index 25c82312..61f4397b 100644 --- a/deeptab/architectures/experimental/trompt.py +++ b/deeptab/architectures/experimental/trompt.py @@ -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): diff --git a/docs/model_zoo/experimental/trompt.md b/docs/model_zoo/experimental/trompt.md index 440c107c..23488647 100644 --- a/docs/model_zoo/experimental/trompt.md +++ b/docs/model_zoo/experimental/trompt.md @@ -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. diff --git a/tests/architectures/test_architecture_contracts.py b/tests/architectures/test_architecture_contracts.py index 32dd0abd..a7ffa513 100644 --- a/tests/architectures/test_architecture_contracts.py +++ b/tests/architectures/test_architecture_contracts.py @@ -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): diff --git a/tests/integration/test_model_zoo.py b/tests/integration/test_model_zoo.py index baf68d3c..4f5c316c 100644 --- a/tests/integration/test_model_zoo.py +++ b/tests/integration/test_model_zoo.py @@ -674,6 +674,7 @@ def test_experimental_lss_fit_predict_evaluate(cls, regression_data): NODEClassifier, NDTFClassifier, MambaTabClassifier, + TromptClassifier, ]