|
| 1 | +import pytest |
| 2 | +from typing import Dict, Text, Union, Tuple |
| 3 | +import numpy as np |
| 4 | +import tensorflow as tf |
| 5 | + |
| 6 | +from rasa.utils.tensorflow.models import RasaModel |
| 7 | +from rasa.utils.tensorflow.model_data import RasaModelData |
| 8 | +from rasa.utils.tensorflow.model_data import FeatureArray |
| 9 | +from rasa.utils.tensorflow.constants import LABEL, IDS, SENTENCE |
| 10 | +from rasa.shared.nlu.constants import TEXT |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + "existing_outputs, new_batch_outputs, expected_output", |
| 15 | + [ |
| 16 | + ( |
| 17 | + {"a": np.array([1, 2]), "b": np.array([3, 1])}, |
| 18 | + {"a": np.array([5, 6]), "b": np.array([2, 4])}, |
| 19 | + {"a": np.array([1, 2, 5, 6]), "b": np.array([3, 1, 2, 4])}, |
| 20 | + ), |
| 21 | + ( |
| 22 | + {}, |
| 23 | + {"a": np.array([5, 6]), "b": np.array([2, 4])}, |
| 24 | + {"a": np.array([5, 6]), "b": np.array([2, 4])}, |
| 25 | + ), |
| 26 | + ( |
| 27 | + {"a": np.array([1, 2]), "b": {"c": np.array([3, 1])}}, |
| 28 | + {"a": np.array([5, 6]), "b": {"c": np.array([2, 4])}}, |
| 29 | + {"a": np.array([1, 2, 5, 6]), "b": {"c": np.array([3, 1, 2, 4])}}, |
| 30 | + ), |
| 31 | + ], |
| 32 | +) |
| 33 | +def test_merging_batch_outputs( |
| 34 | + existing_outputs: Dict[Text, Union[np.ndarray, Dict[Text, np.ndarray]]], |
| 35 | + new_batch_outputs: Dict[Text, Union[np.ndarray, Dict[Text, np.ndarray]]], |
| 36 | + expected_output: Dict[Text, Union[np.ndarray, Dict[Text, np.ndarray]]], |
| 37 | +): |
| 38 | + |
| 39 | + predicted_output = RasaModel._merge_batch_outputs( |
| 40 | + existing_outputs, new_batch_outputs |
| 41 | + ) |
| 42 | + |
| 43 | + def test_equal_dicts( |
| 44 | + dict1: Dict[Text, Union[np.ndarray, Dict[Text, np.ndarray]]], |
| 45 | + dict2: Dict[Text, Union[np.ndarray, Dict[Text, np.ndarray]]], |
| 46 | + ) -> None: |
| 47 | + assert dict2.keys() == dict1.keys() |
| 48 | + for key in dict1: |
| 49 | + val_1 = dict1[key] |
| 50 | + val_2 = dict2[key] |
| 51 | + assert type(val_1) == type(val_2) |
| 52 | + |
| 53 | + if isinstance(val_2, np.ndarray): |
| 54 | + assert np.array_equal(val_1, val_2) |
| 55 | + |
| 56 | + elif isinstance(val_2, dict): |
| 57 | + test_equal_dicts(val_1, val_2) |
| 58 | + |
| 59 | + test_equal_dicts(predicted_output, expected_output) |
| 60 | + |
| 61 | + |
| 62 | +@pytest.mark.parametrize( |
| 63 | + "batch_size, number_of_data_points, expected_number_of_batch_iterations", |
| 64 | + [(2, 3, 2), (1, 3, 3), (5, 3, 1),], |
| 65 | +) |
| 66 | +def test_batch_inference( |
| 67 | + batch_size: int, |
| 68 | + number_of_data_points: int, |
| 69 | + expected_number_of_batch_iterations: int, |
| 70 | +): |
| 71 | + model = RasaModel() |
| 72 | + |
| 73 | + def _batch_predict( |
| 74 | + batch_in: Tuple[np.ndarray], |
| 75 | + ) -> Dict[Text, Union[np.ndarray, Dict[Text, np.ndarray]]]: |
| 76 | + |
| 77 | + dummy_output = batch_in[0] |
| 78 | + output = { |
| 79 | + "dummy_output": dummy_output, |
| 80 | + "non_input_affected_output": tf.constant( |
| 81 | + np.array([[1, 2]]), dtype=tf.int32 |
| 82 | + ), |
| 83 | + } |
| 84 | + return output |
| 85 | + |
| 86 | + # Monkeypatch batch predict so that run_inference interface can be tested |
| 87 | + model.batch_predict = _batch_predict |
| 88 | + |
| 89 | + # Create dummy model data to pass to model |
| 90 | + model_data = RasaModelData( |
| 91 | + label_key=LABEL, |
| 92 | + label_sub_key=IDS, |
| 93 | + data={ |
| 94 | + TEXT: { |
| 95 | + SENTENCE: [ |
| 96 | + FeatureArray( |
| 97 | + np.random.rand(number_of_data_points, 2), |
| 98 | + number_of_dimensions=2, |
| 99 | + ), |
| 100 | + ] |
| 101 | + } |
| 102 | + }, |
| 103 | + ) |
| 104 | + output = model.run_inference(model_data, batch_size=batch_size) |
| 105 | + |
| 106 | + # Firstly, the number of data points in dummy_output should be equal |
| 107 | + # to the number of data points sent as input. |
| 108 | + assert output["dummy_output"].shape[0] == number_of_data_points |
| 109 | + |
| 110 | + # Secondly, the number of data points inside diagnostic_data should be |
| 111 | + # equal to the number of batches passed to the model because for every |
| 112 | + # batch passed as input, it would have created a |
| 113 | + # corresponding diagnostic data entry. |
| 114 | + assert output["non_input_affected_output"].shape == ( |
| 115 | + expected_number_of_batch_iterations, |
| 116 | + 2, |
| 117 | + ) |
0 commit comments