Skip to content

Commit c6ebf54

Browse files
add inference tests
1 parent b62ce5f commit c6ebf54

File tree

3 files changed

+86
-12
lines changed

3 files changed

+86
-12
lines changed

tests/unitary/with_extras/aqua/test_data/deployment/aqua_create_deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ spec:
2626
BASE_MODEL: service_models/model-name/artifact
2727
MODEL_DEPLOY_ENABLE_STREAMING: 'true'
2828
MODEL_DEPLOY_PREDICT_ENDPOINT: /v1/completions
29-
PARAMS: --served-model-name odsc-llm --seed 42 --trust-remote-code
29+
PARAMS: --served-model-name odsc-llm --seed 42 --trust-remote-code --max-model-len 4096
3030
healthCheckPort: 8080
3131
image: "dsmc://image-name:1.0.0.0"
3232
modelUri: "ocid1.datasciencemodeldeployment.oc1.<region>.<MODEL_OCID>"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"choices": [
3+
{
4+
"finish_reason": "stop",
5+
"index": 0,
6+
"logprobs": null,
7+
"text": " The answer is 2"
8+
}
9+
],
10+
"created": 12119,
11+
"id": "cmpl-379653f5a2a0425f9a6de24979542efc",
12+
"model": "odsc-llm",
13+
"object": "text_completion",
14+
"usage": {
15+
"completion_tokens": 6,
16+
"prompt_tokens": 8,
17+
"total_tokens": 14
18+
}
19+
}

tests/unitary/with_extras/aqua/test_deployment.py

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@
1717
import oci
1818
import ads.aqua.deployment
1919
import ads.config
20-
from ads.aqua.deployment import AquaDeployment, AquaDeploymentDetail, AquaDeploymentApp
20+
from ads.aqua.deployment import (
21+
AquaDeployment,
22+
AquaDeploymentDetail,
23+
AquaDeploymentApp,
24+
MDInferenceResponse,
25+
ModelParams,
26+
)
2127
from ads.aqua.exception import AquaRuntimeError
2228
from ads.model.datascience_model import DataScienceModel
2329
from ads.model.deployment.model_deployment import ModelDeployment
@@ -139,6 +145,14 @@ class TestDataset:
139145
},
140146
}
141147

148+
model_params = {
149+
"model": "odsc-llm",
150+
"max_tokens": 500,
151+
"temperature": 0.8,
152+
"top_p": 0.8,
153+
"top_k": 10,
154+
}
155+
142156

143157
class TestAquaDeployment(unittest.TestCase):
144158
def setUp(self):
@@ -157,6 +171,7 @@ def setUpClass(cls):
157171

158172
@classmethod
159173
def tearDownClass(cls):
174+
cls.curr_dir = None
160175
os.environ.pop("CONDA_BUCKET_NS", None)
161176
os.environ.pop("ODSC_MODEL_COMPARTMENT_OCID", None)
162177
os.environ.pop("PROJECT_COMPARTMENT_OCID", None)
@@ -213,10 +228,13 @@ def test_get_deployment(self, mock_get_resource_name):
213228
)
214229
actual_attributes = asdict(result)
215230
assert set(expected_attributes) == set(actual_attributes), "Attributes mismatch"
216-
self.assertEqual(actual_attributes, TestDataset.aqua_deployment_detail)
231+
self.assertEqual(TestDataset.aqua_deployment_detail, actual_attributes)
217232

218-
self.assertEqual(result.log.name, "log-name")
219-
self.assertEqual(result.log_group.name, "log-group-name")
233+
self.assertEqual(
234+
"log-name",
235+
result.log.name,
236+
)
237+
self.assertEqual("log-group-name", result.log_group.name)
220238

221239
def test_get_deployment_missing_tags(self):
222240
"""Test for returning a runtime error if OCI_AQUA tag is missing."""
@@ -251,19 +269,20 @@ def test_get_deployment_config(self, mock_load_config):
251269

252270
self.app.get_config = MagicMock(return_value=config)
253271
result = self.app.get_deployment_config(TestDataset.MODEL_ID)
254-
self.assertEqual(result, config)
272+
self.assertEqual(config, result)
255273

256274
self.app.get_config = MagicMock(return_value=None)
257275
mock_load_config.return_value = config
258276
result = self.app.get_deployment_config(TestDataset.MODEL_ID)
259-
self.assertEqual(result, config)
277+
self.assertEqual(config, result)
260278

261279
@patch("ads.aqua.model.AquaModelApp.create")
262280
@patch("ads.aqua.deployment.get_container_image")
263281
@patch("ads.model.deployment.model_deployment.ModelDeployment.deploy")
264282
def test_create_deployment_for_foundation_model(
265283
self, mock_deploy, mock_get_container_image, mock_create
266284
):
285+
"""Test to create a deployment for foundational model"""
267286
aqua_model = os.path.join(
268287
self.curr_dir, "test_data/deployment/aqua_foundation_model.yaml"
269288
)
@@ -305,16 +324,18 @@ def test_create_deployment_for_foundation_model(
305324
expected_attributes = set(AquaDeployment.__annotations__.keys())
306325
actual_attributes = asdict(result)
307326
assert set(expected_attributes) == set(actual_attributes), "Attributes mismatch"
308-
expected_result = TestDataset.aqua_deployment_object
327+
expected_result = copy.deepcopy(TestDataset.aqua_deployment_object)
309328
expected_result["state"] = "CREATING"
310-
self.assertEqual(TestDataset.aqua_deployment_object, actual_attributes)
329+
self.assertEqual(expected_result, actual_attributes)
311330

312331
@patch("ads.aqua.model.AquaModelApp.create")
313332
@patch("ads.aqua.deployment.get_container_image")
314333
@patch("ads.model.deployment.model_deployment.ModelDeployment.deploy")
315-
def test_create_deployment_for_foundation_model(
334+
def test_create_deployment_for_fine_tuned_model(
316335
self, mock_deploy, mock_get_container_image, mock_create
317336
):
337+
"""Test to create a deployment for fine-tuned model"""
338+
318339
# todo: DataScienceModel.from_yaml should update model_file_description attribute, current workaround is to
319340
# load using with_model_file_description property.
320341
def yaml_to_json(input_file):
@@ -370,6 +391,40 @@ def yaml_to_json(input_file):
370391
expected_attributes = set(AquaDeployment.__annotations__.keys())
371392
actual_attributes = asdict(result)
372393
assert set(expected_attributes) == set(actual_attributes), "Attributes mismatch"
373-
expected_result = TestDataset.aqua_deployment_object
394+
expected_result = copy.deepcopy(TestDataset.aqua_deployment_object)
374395
expected_result["state"] = "CREATING"
375-
self.assertEqual(TestDataset.aqua_deployment_object, actual_attributes)
396+
self.assertEqual(expected_result, actual_attributes)
397+
398+
399+
class TestMDInferenceResponse(unittest.TestCase):
400+
def setUp(self):
401+
self.app = MDInferenceResponse()
402+
403+
@classmethod
404+
def setUpClass(cls):
405+
cls.curr_dir = os.path.dirname(os.path.abspath(__file__))
406+
407+
@classmethod
408+
def tearDownClass(cls):
409+
cls.curr_dir = None
410+
411+
@patch("requests.post")
412+
def test_get_model_deployment_response(self, mock_post):
413+
"""Test to check if model deployment response is returned correctly."""
414+
415+
endpoint = TestDataset.MODEL_DEPLOYMENT_URL + "/predict"
416+
self.app.prompt = "What is 1+1?"
417+
self.app.model_params = ModelParams(**TestDataset.model_params)
418+
419+
mock_response = MagicMock()
420+
response_json = os.path.join(
421+
self.curr_dir, "test_data/deployment/aqua_deployment_response.json"
422+
)
423+
with open(response_json, "r") as _file:
424+
mock_response.content = _file.read()
425+
mock_response.status_code = 200
426+
mock_post.return_value = mock_response
427+
428+
result = self.app.get_model_deployment_response(endpoint)
429+
430+
self.assertEqual(" The answer is 2", result["choices"][0]["text"])

0 commit comments

Comments
 (0)