-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Model assignment planner (#86004)
Adds an assignment planner for trained model deployments. The planner uses a linear programming solver (ojalgo) and randomized rounding in order to compute an assignment plan that tries to maximize the number of allocations whilst minimizing memory used and ensuring deployments with assignments will not get fewer allocations than they did before.
- Loading branch information
1 parent
210ce86
commit 2f14148
Showing
17 changed files
with
2,312 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
f952ebb6919f5c21af0a8eec8138b0f2ba01b162 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2003-2017 Optimatika | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Empty file.
99 changes: 99 additions & 0 deletions
99
...org/elasticsearch/xpack/ml/inference/assignment/planning/AbstractPreserveAllocations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.ml.inference.assignment.planning; | ||
|
||
import org.elasticsearch.core.Tuple; | ||
import org.elasticsearch.xpack.ml.inference.assignment.planning.AssignmentPlan.Model; | ||
import org.elasticsearch.xpack.ml.inference.assignment.planning.AssignmentPlan.Node; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
abstract class AbstractPreserveAllocations { | ||
|
||
private final List<Node> nodes; | ||
private final List<Model> models; | ||
|
||
protected AbstractPreserveAllocations(List<Node> nodes, List<Model> models) { | ||
this.nodes = Objects.requireNonNull(nodes); | ||
this.models = Objects.requireNonNull(models); | ||
} | ||
|
||
List<Node> nodesPreservingAllocations() { | ||
return nodes.stream().map(n -> modifyNodePreservingAllocations(n)).toList(); | ||
} | ||
|
||
private Node modifyNodePreservingAllocations(Node n) { | ||
long bytesUsed = 0; | ||
int coresUsed = 0; | ||
for (Model m : models) { | ||
if (m.currentAllocationByNodeId().containsKey(n.id())) { | ||
bytesUsed += m.memoryBytes(); | ||
coresUsed += calculateUsedCores(n, m); | ||
} | ||
} | ||
|
||
return new Node(n.id(), n.availableMemoryBytes() - bytesUsed, n.cores() - coresUsed); | ||
} | ||
|
||
List<Model> modelsPreservingAllocations() { | ||
return models.stream().map(m -> modifyModelPreservingPreviousAssignments(m)).toList(); | ||
} | ||
|
||
Model modifyModelPreservingPreviousAssignments(Model m) { | ||
if (m.currentAllocationByNodeId().isEmpty()) { | ||
return m; | ||
} | ||
|
||
return new Model( | ||
m.id(), | ||
m.memoryBytes(), | ||
m.allocations() - calculatePreservedAllocations(m), | ||
m.threadsPerAllocation(), | ||
calculateAllocationsPerNodeToPreserve(m) | ||
); | ||
} | ||
|
||
AssignmentPlan mergePreservedAllocations(AssignmentPlan assignmentPlan) { | ||
// As the model/node objects the assignment plan are the modified ones, | ||
// they will not match the models/nodes members we have in this class. | ||
// Therefore, we build a lookup table based on the ids so we can merge the plan | ||
// with its preserved allocations. | ||
final Map<Tuple<String, String>, Integer> assignmentsByModelNodeIdPair = new HashMap<>(); | ||
for (Model m : assignmentPlan.models()) { | ||
Map<Node, Integer> assignments = assignmentPlan.assignments(m).orElse(Map.of()); | ||
for (Map.Entry<Node, Integer> nodeAssignment : assignments.entrySet()) { | ||
assignmentsByModelNodeIdPair.put(Tuple.tuple(m.id(), nodeAssignment.getKey().id()), nodeAssignment.getValue()); | ||
} | ||
} | ||
|
||
AssignmentPlan.Builder mergedPlanBuilder = AssignmentPlan.builder(nodes, models); | ||
for (Model m : models) { | ||
for (Node n : nodes) { | ||
int allocations = assignmentsByModelNodeIdPair.getOrDefault(Tuple.tuple(m.id(), n.id()), 0); | ||
if (m.currentAllocationByNodeId().containsKey(n.id())) { | ||
allocations += addPreservedAllocations(n, m); | ||
} | ||
if (allocations > 0) { | ||
mergedPlanBuilder.assignModelToNode(m, n, allocations); | ||
} | ||
} | ||
} | ||
return mergedPlanBuilder.build(); | ||
} | ||
|
||
protected abstract int calculateUsedCores(Node n, Model m); | ||
|
||
protected abstract Map<String, Integer> calculateAllocationsPerNodeToPreserve(Model m); | ||
|
||
protected abstract int calculatePreservedAllocations(Model m); | ||
|
||
protected abstract int addPreservedAllocations(Node n, Model m); | ||
} |
Oops, something went wrong.