Skip to content

Commit

Permalink
[ML] Model assignment planner (#86004)
Browse files Browse the repository at this point in the history
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
dimitris-athanasiou committed May 3, 2022
1 parent 210ce86 commit 2f14148
Show file tree
Hide file tree
Showing 17 changed files with 2,312 additions and 1 deletion.
1 change: 1 addition & 0 deletions x-pack/plugin/ml/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ dependencies {
api "org.apache.commons:commons-math3:3.6.1"
api "com.ibm.icu:icu4j:${versions.icu4j}"
api "org.apache.lucene:lucene-analysis-icu:${versions.lucene}"
implementation 'org.ojalgo:ojalgo:50.0.2'
nativeBundle("org.elasticsearch.ml:ml-cpp:${project.version}:deps@zip") {
changing = true
}
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugin/ml/licenses/ojalgo-50.0.2.jar.sha1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f952ebb6919f5c21af0a8eec8138b0f2ba01b162
21 changes: 21 additions & 0 deletions x-pack/plugin/ml/licenses/ojalgo-LICENSE.txt
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.
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);
}

0 comments on commit 2f14148

Please sign in to comment.