Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for BEiT #230

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
Empty file.
Empty file.
51 changes: 51 additions & 0 deletions tests/jax/models/beit/base/test_beit_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# SPDX-FileCopyrightText: (c) 2025 Tenstorrent AI ULC
#
# SPDX-License-Identifier: Apache-2.0


from typing import Callable

import pytest
from infra import RunMode
from utils import compile_fail, record_model_test_properties

from ..tester import FlaxBeitForImageClassificationTester

MODEL_PATH = "microsoft/beit-base-patch16-224"
MODEL_NAME = "beit-base"


# ----- Fixtures -----


@pytest.fixture
def inference_tester() -> FlaxBeitForImageClassificationTester:
return FlaxBeitForImageClassificationTester(MODEL_PATH)


@pytest.fixture
def training_tester() -> FlaxBeitForImageClassificationTester:
return FlaxBeitForImageClassificationTester(MODEL_PATH, RunMode.TRAINING)


# ----- Tests -----


@pytest.mark.xfail(reason=compile_fail("failed to legalize operation 'ttir.gather'"))
def test_flax_beit_base_inference(
inference_tester: FlaxBeitForImageClassificationTester,
record_tt_xla_property: Callable,
):
record_model_test_properties(record_tt_xla_property, MODEL_NAME)

inference_tester.test()


@pytest.mark.skip(reason="Support for training not implemented")
def test_flax_beit_base_training(
training_tester: FlaxBeitForImageClassificationTester,
record_tt_xla_property: Callable,
):
record_model_test_properties(record_tt_xla_property, MODEL_NAME)

training_tester.test()
Empty file.
50 changes: 50 additions & 0 deletions tests/jax/models/beit/large/test_beit_large.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# SPDX-FileCopyrightText: (c) 2025 Tenstorrent AI ULC
#
# SPDX-License-Identifier: Apache-2.0

from typing import Callable

import pytest
from infra import RunMode
from utils import compile_fail, record_model_test_properties

from ..tester import FlaxBeitForImageClassificationTester

MODEL_PATH = "microsoft/beit-large-patch16-224"
MODEL_NAME = "beit-large"


# ----- Fixtures -----


@pytest.fixture
def inference_tester() -> FlaxBeitForImageClassificationTester:
return FlaxBeitForImageClassificationTester(MODEL_PATH)


@pytest.fixture
def training_tester() -> FlaxBeitForImageClassificationTester:
return FlaxBeitForImageClassificationTester(MODEL_PATH, RunMode.TRAINING)


# ----- Tests -----


@pytest.mark.xfail(reason=compile_fail("failed to legalize operation 'ttir.gather'"))
def test_flax_beit_large_inference(
inference_tester: FlaxBeitForImageClassificationTester,
record_tt_xla_property: Callable,
):
record_model_test_properties(record_tt_xla_property, MODEL_NAME)

inference_tester.test()


@pytest.mark.skip(reason="Support for training not implemented")
def test_flax_beit_large_training(
training_tester: FlaxBeitForImageClassificationTester,
record_tt_xla_property: Callable,
):
record_model_test_properties(record_tt_xla_property, MODEL_NAME)

training_tester.test()
44 changes: 44 additions & 0 deletions tests/jax/models/beit/tester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-FileCopyrightText: (c) 2025 Tenstorrent AI ULC
#
# SPDX-License-Identifier: Apache-2.0

from typing import Dict, Sequence

import jax
from flax import linen as nn
from infra import ComparisonConfig, ModelTester, RunMode
from transformers import BeitImageProcessor, FlaxBeitForImageClassification


class FlaxBeitForImageClassificationTester(ModelTester):
"""Tester for Beit family of models on image classification tasks."""

def __init__(
self,
model_name: str,
comparison_config: ComparisonConfig = ComparisonConfig(),
run_mode: RunMode = RunMode.INFERENCE,
) -> None:
self._model_name = model_name
super().__init__(comparison_config, run_mode)

# @override
def _get_model(self) -> nn.Module:
return FlaxBeitForImageClassification.from_pretrained(self._model_name)

# @override
def _get_input_activations(self) -> Sequence[jax.Array]:
image = jax.random.uniform(jax.random.PRNGKey(42), (1, 3, 224, 224))
preprocessor = BeitImageProcessor.from_pretrained(
self._model_name, do_rescale=False
)
inputs = preprocessor(image, return_tensors="np")
return inputs

# @override
def _get_forward_method_kwargs(self) -> Dict[str, jax.Array]:
assert hasattr(self._model, "params")
return {
"params": self._model.params,
**self._get_input_activations(),
}
Loading