# What does this PR do?

I have replaced the assert statements with appropriate exceptions in the directory `src/transformers/models/` with all models beginning with `a` and `b` letters.

Also, I have corrected error handling at places where except statements were handling AssertionError, even thought it was never to be raised. Here is an example:

```
try:
    if pointer.shape != array.shape:
        raise ValueError(f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched")
except AssertionError as e:    # Incorrect line
    e.args += (pointer.shape, array.shape)
    raise
```

I changed the above to:

```
try:
    if pointer.shape != array.shape:
        raise ValueError(f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched")
except ValueError as e:    # Corrected the line
    e.args += (pointer.shape, array.shape)
    raise
```

Fixes #12789 


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

@ArthurZucker @sgugger 

Replace assert statements with exceptions. # What does this PR do?

I have replaced the assert statements with appropriate exceptions in the directory `src/transformers/models/` with all models beginning with `a` and `b` letters.

Also, I have corrected error handling at places where except statements were handling AssertionError, even thought it was never to be raised. Here is an example:

```
try:
    if pointer.shape != array.shape:
        raise ValueError(f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched")
except AssertionError as e:    # Incorrect line
    e.args += (pointer.shape, array.shape)
    raise
```

I changed the above to:

```
try:
    if pointer.shape != array.shape:
        raise ValueError(f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched")
except ValueError as e:    # Corrected the line
    e.args += (pointer.shape, array.shape)
    raise
```

Fixes #12789 


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

@ArthurZucker @sgugger 

from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch


@require_torch
@require_tf
class DetermineFrameworkTest(TestCase):
    """
    Test `FeaturesManager.determine_framework`
    """

    def setUp(self):
        self.test_model = SMALL_MODEL_IDENTIFIER
        self.framework_pt = "pt"
        self.framework_tf = "tf"

    def _setup_pt_ckpt(self, save_dir):
        model_pt = AutoModel.from_pretrained(self.test_model)
        model_pt.save_pretrained(save_dir)

    def _setup_tf_ckpt(self, save_dir):
        model_tf = TFAutoModel.from_pretrained(self.test_model, from_pt=True)
        model_tf.save_pretrained(save_dir)

    def test_framework_provided(self):
        """
        Ensure the that the provided framework is returned.
        """
        mock_framework = "mock_framework"

        # Framework provided - return whatever the user provides
        result = FeaturesManager.determine_framework(self.test_model, mock_framework)
        self.assertEqual(result, mock_framework)

        # Local checkpoint and framework provided - return provided framework
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
            self._setup_pt_ckpt(local_pt_ckpt)
            result = FeaturesManager.determine_framework(local_pt_ckpt, mock_framework)
            self.assertEqual(result, mock_framework)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
            self._setup_tf_ckpt(local_tf_ckpt)
            result = FeaturesManager.determine_framework(local_tf_ckpt, mock_framework)
            self.assertEqual(result, mock_framework)

    def test_checkpoint_provided(self):
        """
        Ensure that the determined framework is the one used for the local checkpoint.

        For the functionality to execute, local checkpoints are provided but framework is not.
        """
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
            self._setup_pt_ckpt(local_pt_ckpt)
            result = FeaturesManager.determine_framework(local_pt_ckpt)
            self.assertEqual(result, self.framework_pt)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
            self._setup_tf_ckpt(local_tf_ckpt)
            result = FeaturesManager.determine_framework(local_tf_ckpt)
            self.assertEqual(result, self.framework_tf)

        # Invalid local checkpoint
        with TemporaryDirectory() as local_invalid_ckpt:
            with self.assertRaises(FileNotFoundError):
                result = FeaturesManager.determine_framework(local_invalid_ckpt)

    def test_from_environment(self):
        """
        Ensure that the determined framework is the one available in the environment.

        For the functionality to execute, framework and local checkpoints are not provided.
        """
        # Framework not provided, hub model is used (no local checkpoint directory)
        # TensorFlow not in environment -> use PyTorch
        mock_tf_available = MagicMock(return_value=False)
        with patch("transformers.onnx.features.is_tf_available", mock_tf_available):
            result = FeaturesManager.determine_framework(self.test_model)
            self.assertEqual(result, self.framework_pt)

        # PyTorch not in environment -> use TensorFlow
        mock_torch_available = MagicMock(return_value=False)
        with patch("transformers.onnx.features.is_torch_available", mock_torch_available):
            result = FeaturesManager.determine_framework(self.test_model)
            self.assertEqual(result, self.framework_tf)

        # Both in environment -> use PyTorch
        mock_tf_available = MagicMock(return_value=True)
        mock_torch_available = MagicMock(return_value=True)
        with patch("transformers.onnx.features.is_tf_available", mock_tf_available), patch(
            "transformers.onnx.features.is_torch_available", mock_torch_available
        ):
            result = FeaturesManager.determine_framework(self.test_model)
            self.assertEqual(result, self.framework_pt)

        # Both not in environment -> raise error
        mock_tf_available = MagicMock(return_value=False)
        mock_torch_available = MagicMock(return_value=False)
        with patch("transformers.onnx.features.is_tf_available", mock_tf_available), patch(
            "transformers.onnx.features.is_torch_available", mock_torch_available
        ):
            with self.assertRaises(EnvironmentError):
                result = FeaturesManager.determine_framework(self.test_model)

from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch


@require_torch
@require_tf
class DetermineFrameworkTest(TestCase):
    """
    Test `FeaturesManager.determine_framework`
    """

    def setUp(self):
        self.test_model = SMALL_MODEL_IDENTIFIER
        self.framework_pt = "pt"
        self.framework_tf = "tf"

    def _setup_pt_ckpt(self, save_dir):
        model_pt = AutoModel.from_pretrained(self.test_model)
        model_pt.save_pretrained(save_dir)

    def _setup_tf_ckpt(self, save_dir):
        model_tf = TFAutoModel.from_pretrained(self.test_model, from_pt=True)
        model_tf.save_pretrained(save_dir)

    def test_framework_provided(self):
        """
        Ensure the that the provided framework is returned.
        """
        mock_framework = "mock_framework"

        # Framework provided - return whatever the user provides
        result = FeaturesManager.determine_framework(self.test_model, mock_framework)
        self.assertEqual(result, mock_framework)

        # Local checkpoint and framework provided - return provided framework
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
            self._setup_pt_ckpt(local_pt_ckpt)
            result = FeaturesManager.determine_framework(local_pt_ckpt, mock_framework)
            self.assertEqual(result, mock_framework)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
            self._setup_tf_ckpt(local_tf_ckpt)
            result = FeaturesManager.determine_framework(local_tf_ckpt, mock_framework)
            self.assertEqual(result, mock_framework)

    def test_checkpoint_provided(self):
        """
        Ensure that the determined framework is the one used for the local checkpoint.

        For the functionality to execute, local checkpoints are provided but framework is not.
        """
        # PyTorch checkpoint
        with TemporaryDirectory() as local_pt_ckpt:
            self._setup_pt_ckpt(local_pt_ckpt)
            result = FeaturesManager.determine_framework(local_pt_ckpt)
            self.assertEqual(result, self.framework_pt)

        # TensorFlow checkpoint
        with TemporaryDirectory() as local_tf_ckpt:
            self._setup_tf_ckpt(local_tf_ckpt)
            result = FeaturesManager.determine_framework(local_tf_ckpt)
            self.assertEqual(result, self.framework_tf)

        # Invalid local checkpoint
        with TemporaryDirectory() as local_invalid_ckpt:
            with self.assertRaises(FileNotFoundError):
                result = FeaturesManager.determine_framework(local_invalid_ckpt)

    def test_from_environment(self):
        """
        Ensure that the determined framework is the one available in the environment.

        For the functionality to execute, framework and local checkpoints are not provided.
        """
        # Framework not provided, hub model is used (no local checkpoint directory)
        # TensorFlow not in environment -> use PyTorch
        mock_tf_available = MagicMock(return_value=False)
        with patch("transformers.onnx.features.is_tf_available", mock_tf_available):
            result = FeaturesManager.determine_framework(self.test_model)
            self.assertEqual(result, self.framework_pt)

        # PyTorch not in environment -> use TensorFlow
        mock_torch_available = MagicMock(return_value=False)
        with patch("transformers.onnx.features.is_torch_available", mock_torch_available):
            result = FeaturesManager.determine_framework(self.test_model)
            self.assertEqual(result, self.framework_tf)

        # Both in environment -> use PyTorch
        mock_tf_available = MagicMock(return_value=True)
        mock_torch_available = MagicMock(return_value=True)
        with patch("transformers.onnx.features.is_tf_available", mock_tf_available), patch(
            "transformers.onnx.features.is_torch_available", mock_torch_available
        ):
            result = FeaturesManager.determine_framework(self.test_model)
            self.assertEqual(result, self.framework_pt)

        # Both not in environment -> raise error
        mock_tf_available = MagicMock(return_value=False)
        mock_torch_available = MagicMock(return_value=False)
        with patch("transformers.onnx.features.is_tf_available", mock_tf_available), patch(
            "transformers.onnx.features.is_torch_available", mock_torch_available
        ):
            with self.assertRaises(EnvironmentError):
                result = FeaturesManager.determine_framework(self.test_model)
