[Serve] Require traffic weights to sum more closely to 1. (#8476)

This commit is contained in:
Robert Nishihara
2020-05-18 11:46:34 -07:00
committed by GitHub
parent 0fadc11437
commit 14aeb30473
+7 -1
View File
@@ -458,14 +458,20 @@ class ServeMaster:
dict), "Traffic policy must be dictionary"
prob = 0
for backend, weight in traffic_policy_dictionary.items():
if weight < 0:
raise ValueError(
"Attempted to assign a weight of {} to backend '{}'. "
"Weights cannot be negative.".format(weight, backend))
prob += weight
if backend not in self.backends:
raise ValueError(
"Attempted to assign traffic to a backend '{}' that "
"is not registered.".format(backend))
# These weights will later be plugged into np.random.choice, which
# uses a tolerance of 1e-8.
assert np.isclose(
prob, 1, atol=0.02
prob, 1, atol=1e-8
), "weights must sum to 1, currently they sum to {}".format(prob)
self.traffic_policies[endpoint_name] = traffic_policy_dictionary