forked from logicalclocks/hopsworks-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_resources.py
928 lines (787 loc) · 35.4 KB
/
test_resources.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
#
# Copyright 2024 Hopsworks AB
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import copy
import pytest
from hsml import resources
from hsml.constants import RESOURCES
from mock import call
SERVING_RESOURCE_LIMITS = {"cores": 2, "memory": 516, "gpus": 2}
class TestResources:
# Resources
def test_from_response_json_cpus(self, backend_fixtures):
# Arrange
json = backend_fixtures["resources"]["get_only_cores"]["response"]
# Act
r = resources.Resources.from_response_json(json)
# Assert
assert r.cores == json["cores"]
assert r.memory is None
assert r.gpus is None
def test_from_response_json_memory(self, backend_fixtures):
# Arrange
json = backend_fixtures["resources"]["get_only_memory"]["response"]
# Act
r = resources.Resources.from_response_json(json)
# Assert
assert r.cores is None
assert r.memory is json["memory"]
assert r.gpus is None
def test_from_response_json_gpus(self, backend_fixtures):
# Arrange
json = backend_fixtures["resources"]["get_only_gpus"]["response"]
# Act
r = resources.Resources.from_response_json(json)
# Assert
assert r.cores is None
assert r.memory is None
assert r.gpus == json["gpus"]
def test_from_response_json_cores_and_memory(self, backend_fixtures):
# Arrange
json = backend_fixtures["resources"]["get_cores_and_memory"]["response"]
# Act
r = resources.Resources.from_response_json(json)
# Assert
assert r.cores == json["cores"]
assert r.memory == json["memory"]
assert r.gpus is None
def test_from_response_json_cores_and_gpus(self, backend_fixtures):
# Arrange
json = backend_fixtures["resources"]["get_cores_and_gpus"]["response"]
# Act
r = resources.Resources.from_response_json(json)
# Assert
assert r.cores == json["cores"]
assert r.memory is None
assert r.gpus == json["gpus"]
def test_from_response_json_memory_and_gpus(self, backend_fixtures):
# Arrange
json = backend_fixtures["resources"]["get_memory_and_gpus"]["response"]
# Act
r = resources.Resources.from_response_json(json)
# Assert
assert r.cores is None
assert r.memory == json["memory"]
assert r.gpus == json["gpus"]
def test_from_response_json_cores_memory_and_gpus(self, backend_fixtures):
# Arrange
json = backend_fixtures["resources"]["get_cores_memory_and_gpus"]["response"]
# Act
r = resources.Resources.from_response_json(json)
# Assert
assert r.cores == json["cores"]
assert r.memory == json["memory"]
assert r.gpus == json["gpus"]
# ComponentResources
# - from response json
def test_from_response_json_component_resources(self, mocker):
# Arrange
res = {"something": "here"}
json_decamelized = {"key": "value"}
mock_humps_decamelize = mocker.patch(
"humps.decamelize", return_value=json_decamelized
)
mock_from_json = mocker.patch(
"hsml.resources.ComponentResources.from_json",
return_value="from_json_result",
)
# Act
result = resources.ComponentResources.from_response_json(res)
# Assert
assert result == "from_json_result"
mock_humps_decamelize.assert_called_once_with(res)
mock_from_json.assert_called_once_with(json_decamelized)
# - constructor
def test_constructor_component_resources_default(self, mocker, backend_fixtures):
# Arrange
json = backend_fixtures["resources"][
"get_component_resources_num_instances_requests_and_limits"
]["response"]
mock_default_resource_limits = mocker.patch(
"hsml.resources.ComponentResources._get_default_resource_limits",
return_value=(0, 1, 2),
)
mock_fill_missing_resources = mocker.patch(
"hsml.resources.ComponentResources._fill_missing_resources"
)
mock_validate_resources = mocker.patch(
"hsml.resources.ComponentResources._validate_resources"
)
mock_resources_init = mocker.patch(
"hsml.resources.Resources.__init__", return_value=None
)
# Act
pr = resources.PredictorResources(num_instances=json["num_instances"])
# Assert
assert pr.num_instances == json["num_instances"]
assert mock_default_resource_limits.call_count == 2
assert mock_fill_missing_resources.call_count == 2
assert (
mock_fill_missing_resources.call_args_list[0][0][1] == RESOURCES.MIN_CORES
)
assert (
mock_fill_missing_resources.call_args_list[0][0][2] == RESOURCES.MIN_MEMORY
)
assert mock_fill_missing_resources.call_args_list[0][0][3] == RESOURCES.MIN_GPUS
assert mock_fill_missing_resources.call_args_list[1][0][1] == 0
assert mock_fill_missing_resources.call_args_list[1][0][2] == 1
assert mock_fill_missing_resources.call_args_list[1][0][3] == 2
mock_validate_resources.assert_called_once_with(pr._requests, pr._limits)
expected_calls = [
call(RESOURCES.MIN_CORES, RESOURCES.MIN_MEMORY, RESOURCES.MIN_GPUS),
call(0, 1, 2),
]
mock_resources_init.assert_has_calls(expected_calls)
def test_constructor_component_resources(self, mocker, backend_fixtures):
# Arrange
json = backend_fixtures["resources"][
"get_component_resources_num_instances_requests_and_limits"
]["response"]
mock_util_get_obj_from_json = mocker.patch(
"hopsworks_common.util.get_obj_from_json",
side_effect=[json["requests"], json["limits"]],
)
mock_default_resource_limits = mocker.patch(
"hsml.resources.ComponentResources._get_default_resource_limits",
return_value=(0, 1, 2),
)
mock_fill_missing_resources = mocker.patch(
"hsml.resources.ComponentResources._fill_missing_resources"
)
mock_validate_resources = mocker.patch(
"hsml.resources.ComponentResources._validate_resources"
)
# Act
pr = resources.PredictorResources(
num_instances=json["num_instances"],
requests=json["requests"],
limits=json["limits"],
)
# Assert
assert pr.num_instances == json["num_instances"]
assert pr.requests == json["requests"]
assert pr.limits == json["limits"]
mock_default_resource_limits.assert_called_once()
assert mock_fill_missing_resources.call_count == 2
assert (
mock_fill_missing_resources.call_args_list[0][0][1] == RESOURCES.MIN_CORES
)
assert (
mock_fill_missing_resources.call_args_list[0][0][2] == RESOURCES.MIN_MEMORY
)
assert mock_fill_missing_resources.call_args_list[0][0][3] == RESOURCES.MIN_GPUS
assert mock_fill_missing_resources.call_args_list[1][0][1] == 0
assert mock_fill_missing_resources.call_args_list[1][0][2] == 1
assert mock_fill_missing_resources.call_args_list[1][0][3] == 2
mock_validate_resources.assert_called_once_with(pr._requests, pr._limits)
assert mock_util_get_obj_from_json.call_count == 2
expected_calls = [
call(json["requests"], resources.Resources),
call(json["limits"], resources.Resources),
]
mock_util_get_obj_from_json.assert_has_calls(expected_calls)
# - extract fields from json
def test_extract_fields_from_json_component_resources_with_key(
self, backend_fixtures
):
# Arrange
json = backend_fixtures["resources"][
"get_component_resources_requested_instances_and_predictor_resources"
]["response"]
copy_json = copy.deepcopy(json)
resources.ComponentResources.RESOURCES_CONFIG_KEY = "predictor_resources"
resources.ComponentResources.NUM_INSTANCES_KEY = "requested_instances"
# Act
kwargs = resources.ComponentResources.extract_fields_from_json(copy_json)
# Assert
assert kwargs["num_instances"] == json["requested_instances"]
assert isinstance(kwargs["requests"], resources.Resources)
assert (
kwargs["requests"].cores == json["predictor_resources"]["requests"]["cores"]
)
assert (
kwargs["requests"].memory
== json["predictor_resources"]["requests"]["memory"]
)
assert (
kwargs["requests"].gpus == json["predictor_resources"]["requests"]["gpus"]
)
assert isinstance(kwargs["limits"], resources.Resources)
assert kwargs["limits"].cores == json["predictor_resources"]["limits"]["cores"]
assert (
kwargs["limits"].memory == json["predictor_resources"]["limits"]["memory"]
)
assert kwargs["limits"].gpus == json["predictor_resources"]["limits"]["gpus"]
def test_extract_fields_from_json_component_resources(
self, mocker, backend_fixtures
):
# Arrange
json = backend_fixtures["resources"][
"get_component_resources_requested_instances_and_predictor_resources_alternative"
]["response"]
copy_json = copy.deepcopy(json)
resources.ComponentResources.RESOURCES_CONFIG_KEY = "predictor_resources"
resources.ComponentResources.NUM_INSTANCES_KEY = "requested_instances"
# Act
kwargs = resources.ComponentResources.extract_fields_from_json(copy_json)
# Assert
assert kwargs["num_instances"] == json["num_instances"]
assert isinstance(kwargs["requests"], resources.Resources)
assert kwargs["requests"].cores == json["resources"]["requests"]["cores"]
assert kwargs["requests"].memory == json["resources"]["requests"]["memory"]
assert kwargs["requests"].gpus == json["resources"]["requests"]["gpus"]
assert isinstance(kwargs["limits"], resources.Resources)
assert kwargs["limits"].cores == json["resources"]["limits"]["cores"]
assert kwargs["limits"].memory == json["resources"]["limits"]["memory"]
assert kwargs["limits"].gpus == json["resources"]["limits"]["gpus"]
def test_extract_fields_from_json_component_resources_flatten(
self, backend_fixtures
):
# Arrange
json = backend_fixtures["resources"][
"get_component_resources_num_instances_requests_and_limits"
]["response"]
copy_json = copy.deepcopy(json)
resources.ComponentResources.RESOURCES_CONFIG_KEY = "predictor_resources"
resources.ComponentResources.NUM_INSTANCES_KEY = "requested_instances"
# Act
kwargs = resources.ComponentResources.extract_fields_from_json(copy_json)
# Assert
assert kwargs["num_instances"] == json["num_instances"]
assert isinstance(kwargs["requests"], resources.Resources)
assert kwargs["requests"].cores == json["requests"]["cores"]
assert kwargs["requests"].memory == json["requests"]["memory"]
assert kwargs["requests"].gpus == json["requests"]["gpus"]
assert isinstance(kwargs["limits"], resources.Resources)
assert kwargs["limits"].cores == json["limits"]["cores"]
assert kwargs["limits"].memory == json["limits"]["memory"]
assert kwargs["limits"].gpus == json["limits"]["gpus"]
# - fill missing dependencies
def test_fill_missing_dependencies_none(self, mocker):
# Arrange
class MockResources:
cores = None
memory = None
gpus = None
mock_resource = MockResources()
# Act
resources.ComponentResources._fill_missing_resources(mock_resource, 10, 11, 12)
# Assert
assert mock_resource.cores == 10
assert mock_resource.memory == 11
assert mock_resource.gpus == 12
def test_fill_missing_dependencies_all(self, mocker):
# Arrange
class MockResources:
cores = 1
memory = 2
gpus = 3
mock_resource = MockResources()
# Act
resources.ComponentResources._fill_missing_resources(mock_resource, 10, 11, 12)
# Assert
assert mock_resource.cores == 1
assert mock_resource.memory == 2
assert mock_resource.gpus == 3
def test_fill_missing_dependencies_some(self, mocker):
# Arrange
class MockResources:
cores = 1
memory = None
gpus = None
mock_resource = MockResources()
# Act
resources.ComponentResources._fill_missing_resources(mock_resource, 10, 11, 12)
# Assert
assert mock_resource.cores == 1
assert mock_resource.memory == 11
assert mock_resource.gpus == 12
# - get default resource limits
def test_get_default_resource_limits_no_hard_limit_and_lower_than_default(
self, mocker
):
# Arrange
no_limit_res = {"cores": -1, "memory": -1, "gpus": -1}
mock_comp_res = mocker.MagicMock()
mock_comp_res._requests = resources.Resources(cores=0.2, memory=516, gpus=0)
mock_comp_res._default_resource_limits = (
resources.ComponentResources._get_default_resource_limits
)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=no_limit_res, # no upper limit
)
# Act
cores, memory, gpus = mock_comp_res._default_resource_limits(mock_comp_res)
# Assert
assert cores == RESOURCES.MAX_CORES
assert memory == RESOURCES.MAX_MEMORY
assert gpus == RESOURCES.MAX_GPUS
mock_get_serving_res_limits.assert_called_once()
def test_get_default_resource_limits_no_hard_limit_and_higher_than_default(
self, mocker
):
# Arrange
no_limit_res = {"cores": -1, "memory": -1, "gpus": -1}
mock_comp_res = mocker.MagicMock()
mock_comp_res._requests = resources.Resources(cores=4, memory=2048, gpus=2)
mock_comp_res._default_resource_limits = (
resources.ComponentResources._get_default_resource_limits
)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=no_limit_res, # no upper limit
)
# Act
cores, memory, gpus = mock_comp_res._default_resource_limits(mock_comp_res)
# Assert
assert cores == mock_comp_res._requests.cores
assert memory == mock_comp_res._requests.memory
assert gpus == mock_comp_res._requests.gpus
mock_get_serving_res_limits.assert_called_once()
def test_get_default_resource_limits_with_higher_hard_limit_and_lower_than_default(
self, mocker
):
# Arrange
hard_limit_res = {"cores": 3, "memory": 3072, "gpus": 3}
mock_comp_res = mocker.MagicMock()
mock_comp_res._requests = resources.Resources(cores=1, memory=516, gpus=0)
mock_comp_res._default_resource_limits = (
resources.ComponentResources._get_default_resource_limits
)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=hard_limit_res, # upper limit
)
# Act
cores, memory, gpus = mock_comp_res._default_resource_limits(mock_comp_res)
# Assert
assert cores == RESOURCES.MAX_CORES
assert memory == RESOURCES.MAX_MEMORY
assert gpus == RESOURCES.MAX_GPUS
mock_get_serving_res_limits.assert_called_once()
def test_get_default_resource_limits_with_higher_hard_limit_and_higher_than_default(
self, mocker
):
# Arrange
hard_limit_res = {"cores": 3, "memory": 3072, "gpus": 3}
mock_comp_res = mocker.MagicMock()
mock_comp_res._requests = resources.Resources(cores=3, memory=2048, gpus=1)
mock_comp_res._default_resource_limits = (
resources.ComponentResources._get_default_resource_limits
)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=hard_limit_res, # upper limit
)
# Act
cores, memory, gpus = mock_comp_res._default_resource_limits(mock_comp_res)
# Assert
assert cores == hard_limit_res["cores"]
assert memory == hard_limit_res["memory"]
assert gpus == hard_limit_res["gpus"]
mock_get_serving_res_limits.assert_called_once()
def test_get_default_resource_limits_with_lower_hard_limit_and_lower_than_default(
self, mocker
):
# Arrange
RESOURCES.MAX_GPUS = 1 # override default
hard_limit_res = {"cores": 1, "memory": 516, "gpus": 0}
mock_comp_res = mocker.MagicMock()
mock_comp_res._requests = resources.Resources(cores=0.5, memory=256, gpus=0.5)
mock_comp_res._default_resource_limits = (
resources.ComponentResources._get_default_resource_limits
)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=hard_limit_res, # upper limit
)
# Act
cores, memory, gpus = mock_comp_res._default_resource_limits(mock_comp_res)
# Assert
assert cores == hard_limit_res["cores"]
assert memory == hard_limit_res["memory"]
assert gpus == hard_limit_res["gpus"]
mock_get_serving_res_limits.assert_called_once()
def test_get_default_resource_limits_with_lower_hard_limit_and_higher_than_default(
self, mocker
):
# Arrange
RESOURCES.MAX_GPUS = 1 # override default
hard_limit_res = {"cores": 1, "memory": 516, "gpus": 0}
mock_comp_res = mocker.MagicMock()
mock_comp_res._requests = resources.Resources(cores=4, memory=4080, gpus=4)
mock_comp_res._default_resource_limits = (
resources.ComponentResources._get_default_resource_limits
)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=hard_limit_res, # upper limit
)
# Act
cores, memory, gpus = mock_comp_res._default_resource_limits(mock_comp_res)
# Assert
assert cores == hard_limit_res["cores"]
assert memory == hard_limit_res["memory"]
assert gpus == hard_limit_res["gpus"]
mock_get_serving_res_limits.assert_called_once()
# - validate resources
def test_validate_resources_no_hard_limits_valid_resources(self, mocker):
# Arrange
no_limit_res = {"cores": -1, "memory": -1, "gpus": -1}
requests = resources.Resources(cores=1, memory=1024, gpus=0)
limits = resources.Resources(cores=2, memory=2048, gpus=1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=no_limit_res, # upper limit
)
# Act
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
def test_validate_resources_no_hard_limit_invalid_cores_request(self, mocker):
# Arrange
no_limit_res = {"cores": -1, "memory": -1, "gpus": -1}
requests = resources.Resources(cores=0, memory=1024, gpus=0)
limits = resources.Resources(cores=2, memory=2048, gpus=1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=no_limit_res, # upper limit
)
# Act
with pytest.raises(ValueError) as e_info:
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
assert "Requested number of cores must be greater than 0 cores." in str(
e_info.value
)
def test_validate_resources_no_hard_limit_invalid_memory_request(self, mocker):
# Arrange
no_limit_res = {"cores": -1, "memory": -1, "gpus": -1}
requests = resources.Resources(cores=1, memory=0, gpus=0)
limits = resources.Resources(cores=2, memory=2048, gpus=1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=no_limit_res, # upper limit
)
# Act
with pytest.raises(ValueError) as e_info:
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
assert "Requested memory resources must be greater than 0 MB." in str(
e_info.value
)
def test_validate_resources_no_hard_limit_invalid_gpus_request(self, mocker):
# Arrange
no_limit_res = {"cores": -1, "memory": -1, "gpus": -1}
requests = resources.Resources(
cores=1, memory=1024, gpus=-1
) # 0 gpus is accepted
limits = resources.Resources(cores=2, memory=2048, gpus=1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=no_limit_res, # upper limit
)
# Act
with pytest.raises(ValueError) as e_info:
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
assert (
"Requested number of gpus must be greater than or equal to 0 gpus."
in str(e_info.value)
)
def test_validate_resources_no_hard_limit_cores_request_out_of_range(self, mocker):
# Arrange
no_limit_res = {"cores": -1, "memory": -1, "gpus": -1}
requests = resources.Resources(cores=2, memory=1024, gpus=0)
limits = resources.Resources(cores=1, memory=2048, gpus=1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=no_limit_res, # upper limit
)
# Act
with pytest.raises(ValueError) as e_info:
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
assert (
f"Requested number of cores cannot exceed the limit of {str(limits.cores)} cores."
in str(e_info.value)
)
def test_validate_resources_no_hard_limit_invalid_memory_request_out_of_range(
self, mocker
):
# Arrange
no_limit_res = {"cores": -1, "memory": -1, "gpus": -1}
requests = resources.Resources(cores=1, memory=2048, gpus=0)
limits = resources.Resources(cores=2, memory=1024, gpus=1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=no_limit_res, # upper limit
)
# Act
with pytest.raises(ValueError) as e_info:
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
assert (
f"Requested memory resources cannot exceed the limit of {str(limits.memory)} MB."
in str(e_info.value)
)
def test_validate_resources_no_hard_limit_invalid_gpus_request_out_of_range(
self, mocker
):
# Arrange
no_limit_res = {"cores": -1, "memory": -1, "gpus": -1}
requests = resources.Resources(cores=1, memory=1024, gpus=2)
limits = resources.Resources(cores=2, memory=2048, gpus=1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=no_limit_res, # upper limit
)
# Act
with pytest.raises(ValueError) as e_info:
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
assert (
f"Requested number of gpus cannot exceed the limit of {str(limits.gpus)} gpus."
in str(e_info.value)
)
def test_validate_resources_with_hard_limit_valid_resources(self, mocker):
# Arrange
hard_limit_res = {"cores": 3, "memory": 3072, "gpus": 3}
requests = resources.Resources(cores=1, memory=1024, gpus=0)
limits = resources.Resources(cores=2, memory=2048, gpus=1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=hard_limit_res, # upper limit
)
# Act
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
def test_validate_resources_with_hard_limit_invalid_cores_limit(self, mocker):
# Arrange
hard_limit_res = {"cores": 3, "memory": 3072, "gpus": 3}
requests = resources.Resources(cores=2, memory=1024, gpus=0)
limits = resources.Resources(cores=0, memory=2048, gpus=1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=hard_limit_res, # upper limit
)
# Act
with pytest.raises(ValueError) as e_info:
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
assert "Limit number of cores must be greater than 0 cores." in str(
e_info.value
)
def test_validate_resources_with_hard_limit_invalid_memory_limit(self, mocker):
# Arrange
hard_limit_res = {"cores": 3, "memory": 3072, "gpus": 3}
requests = resources.Resources(cores=2, memory=1024, gpus=0)
limits = resources.Resources(cores=1, memory=0, gpus=1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=hard_limit_res, # upper limit
)
# Act
with pytest.raises(ValueError) as e_info:
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
assert "Limit memory resources must be greater than 0 MB." in str(e_info.value)
def test_validate_resources_with_hard_limit_invalid_gpus_limit(self, mocker):
# Arrange
hard_limit_res = {"cores": 3, "memory": 3072, "gpus": 3}
requests = resources.Resources(cores=2, memory=1024, gpus=0)
limits = resources.Resources(cores=1, memory=2048, gpus=-1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=hard_limit_res, # upper limit
)
# Act
with pytest.raises(ValueError) as e_info:
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
assert "Limit number of gpus must be greater than or equal to 0 gpus." in str(
e_info.value
)
def test_validate_resources_with_hard_limit_invalid_cores_request(self, mocker):
# Arrange
hard_limit_res = {"cores": 3, "memory": 3072, "gpus": 3}
requests = resources.Resources(cores=2, memory=1024, gpus=0)
limits = resources.Resources(cores=4, memory=2048, gpus=1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=hard_limit_res, # upper limit
)
# Act
with pytest.raises(ValueError) as e_info:
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
assert (
f"Limit number of cores cannot exceed the maximum of {hard_limit_res['cores']} cores."
in str(e_info.value)
)
def test_validate_resources_with_hard_limit_invalid_memory_request(self, mocker):
# Arrange
hard_limit_res = {"cores": 3, "memory": 3072, "gpus": 3}
requests = resources.Resources(cores=2, memory=1024, gpus=0)
limits = resources.Resources(cores=3, memory=4076, gpus=1)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=hard_limit_res, # upper limit
)
# Act
with pytest.raises(ValueError) as e_info:
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
assert (
f"Limit memory resources cannot exceed the maximum of {hard_limit_res['memory']} MB."
in str(e_info.value)
)
def test_validate_resources_with_hard_limit_invalid_gpus_request(self, mocker):
# Arrange
hard_limit_res = {"cores": 3, "memory": 3072, "gpus": 3}
requests = resources.Resources(cores=2, memory=1024, gpus=0)
limits = resources.Resources(cores=3, memory=2048, gpus=4)
mock_get_serving_res_limits = mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=hard_limit_res, # upper limit
)
# Act
with pytest.raises(ValueError) as e_info:
resources.ComponentResources._validate_resources(requests, limits)
# Assert
mock_get_serving_res_limits.assert_called_once()
assert (
f"Limit number of gpus cannot exceed the maximum of {hard_limit_res['gpus']} gpus."
in str(e_info.value)
)
# PredictorResources
def test_from_response_json_predictor_resources(self, mocker, backend_fixtures):
mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=SERVING_RESOURCE_LIMITS,
)
json = backend_fixtures["resources"][
"get_component_resources_num_instances_requests_and_limits"
]["response"]
# Act
r = resources.PredictorResources.from_response_json(json)
# Assert
assert r.num_instances == json["num_instances"]
assert r.requests.cores == json["requests"]["cores"]
assert r.requests.memory == json["requests"]["memory"]
assert r.requests.gpus == json["requests"]["gpus"]
assert r.limits.cores == json["limits"]["cores"]
assert r.limits.memory == json["limits"]["memory"]
assert r.limits.gpus == json["limits"]["gpus"]
def test_from_response_json_predictor_resources_specific_keys(
self, mocker, backend_fixtures
):
mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=SERVING_RESOURCE_LIMITS,
)
json = backend_fixtures["resources"][
"get_component_resources_requested_instances_and_predictor_resources"
]["response"]
# Act
r = resources.PredictorResources.from_response_json(json)
# Assert
assert r.num_instances == json["requested_instances"]
assert r.requests.cores == json["predictor_resources"]["requests"]["cores"]
assert r.requests.memory == json["predictor_resources"]["requests"]["memory"]
assert r.requests.gpus == json["predictor_resources"]["requests"]["gpus"]
assert r.limits.cores == json["predictor_resources"]["limits"]["cores"]
assert r.limits.memory == json["predictor_resources"]["limits"]["memory"]
assert r.limits.gpus == json["predictor_resources"]["limits"]["gpus"]
# TransformerResources
def test_from_response_json_transformer_resources(self, mocker, backend_fixtures):
mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=SERVING_RESOURCE_LIMITS,
)
json = backend_fixtures["resources"][
"get_component_resources_num_instances_requests_and_limits"
]["response"]
# Act
r = resources.TransformerResources.from_response_json(json)
# Assert
assert r.num_instances == json["num_instances"]
assert r.requests.cores == json["requests"]["cores"]
assert r.requests.memory == json["requests"]["memory"]
assert r.requests.gpus == json["requests"]["gpus"]
assert r.limits.cores == json["limits"]["cores"]
assert r.limits.memory == json["limits"]["memory"]
assert r.limits.gpus == json["limits"]["gpus"]
def test_from_response_json_transformer_resources_specific_keys(
self, mocker, backend_fixtures
):
mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=SERVING_RESOURCE_LIMITS,
)
json = backend_fixtures["resources"][
"get_component_resources_requested_instances_and_transformer_resources"
]["response"]
# Act
r = resources.TransformerResources.from_response_json(json)
# Assert
assert r.num_instances == json["requested_transformer_instances"]
assert r.requests.cores == json["transformer_resources"]["requests"]["cores"]
assert r.requests.memory == json["transformer_resources"]["requests"]["memory"]
assert r.requests.gpus == json["transformer_resources"]["requests"]["gpus"]
assert r.limits.cores == json["transformer_resources"]["limits"]["cores"]
assert r.limits.memory == json["transformer_resources"]["limits"]["memory"]
assert r.limits.gpus == json["transformer_resources"]["limits"]["gpus"]
def test_from_response_json_transformer_resources_default_limits(
self, mocker, backend_fixtures
):
mocker.patch(
"hopsworks_common.client.get_serving_resource_limits",
return_value=SERVING_RESOURCE_LIMITS,
)
mocker.patch(
"hsml.resources.ComponentResources._get_default_resource_limits",
return_value=(
SERVING_RESOURCE_LIMITS["cores"],
SERVING_RESOURCE_LIMITS["memory"],
SERVING_RESOURCE_LIMITS["gpus"],
),
)
json = backend_fixtures["resources"][
"get_component_resources_num_instances_and_requests"
]["response"]
# Act
r = resources.TransformerResources.from_response_json(json)
# Assert
assert r.num_instances == json["num_instances"]
assert r.requests.cores == json["requests"]["cores"]
assert r.requests.memory == json["requests"]["memory"]
assert r.requests.gpus == json["requests"]["gpus"]
assert r.limits.cores == SERVING_RESOURCE_LIMITS["cores"]
assert r.limits.memory == SERVING_RESOURCE_LIMITS["memory"]
assert r.limits.gpus == SERVING_RESOURCE_LIMITS["gpus"]