Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unity device mapping algorithm #1459

Merged
merged 35 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
950da68
pass existing tests
wmdi Aug 5, 2024
d36d1ea
unity algorithm builds
wmdi Aug 7, 2024
c71b773
fmt
wmdi Aug 7, 2024
678e990
fix
wmdi Aug 21, 2024
ee2429c
Merge branch 'repo-refactor' into compiler_test
lockshaw Aug 22, 2024
49a4a0c
Merge remote-tracking branch 'flexflow/repo-refactor' into compiler_test
wmdi Aug 27, 2024
8e27f2a
refactor machine mapping
wmdi Aug 27, 2024
8191bcd
Merge branch 'compiler_test' of github.com:wmdi/FlexFlow into compile…
wmdi Aug 27, 2024
9b9f529
add unit tests
wmdi Aug 29, 2024
150ca5e
fmt
wmdi Aug 29, 2024
fc388ce
add more tests
wmdi Sep 2, 2024
e628c72
fmt
wmdi Sep 2, 2024
8eff2b9
fix
wmdi Sep 4, 2024
0d409e9
Merge remote-tracking branch 'flexflow/repo-refactor' into compiler_test
wmdi Sep 11, 2024
7c03f24
refactor get_optimal_machine_mapping a bit and improve the tests
wmdi Sep 12, 2024
89ed108
remove debug codes
wmdi Sep 12, 2024
a112225
Merge remote-tracking branch 'origin/repo-refactor' into wmdi-compile…
lockshaw Sep 18, 2024
3a35951
A lot of simplifying and modularizing of unity dp code
lockshaw Sep 26, 2024
00c2bae
Get tests building again
lockshaw Sep 27, 2024
3d9c9bb
Merge remote-tracking branch 'origin/repo-refactor' into wmdi-compile…
lockshaw Sep 27, 2024
7e73162
Get all the new testcases working
lockshaw Sep 28, 2024
bdcc10e
Move over to ProblemTree/ResultTree framework for machine mapping
lockshaw Sep 29, 2024
b0475b4
Settle on ProblemTree/BinaryTreePath-indexed-MachineMappingResult for…
lockshaw Sep 30, 2024
2bbec5c
More code cleanup and PR prep
lockshaw Oct 1, 2024
85fd5b4
Get tests building again
lockshaw Oct 2, 2024
597e13c
Pass some basic tests of get_optimal_machine_mapping
lockshaw Oct 3, 2024
0c2ab05
Migrate over to use type-erased binary tree
lockshaw Oct 3, 2024
e4073bc
Move back to templated FullBinaryTree
lockshaw Oct 3, 2024
5d22c6d
Get all existing tests passing again
lockshaw Oct 4, 2024
3d08831
Fix tests and format
lockshaw Oct 5, 2024
4b180df
Move graph_optimize_state.cc to correct location
lockshaw Oct 5, 2024
dcd2e13
Further code simplification and polishing
lockshaw Oct 7, 2024
39c8f1c
Pass all tests
lockshaw Oct 8, 2024
75f7e98
Remove a bunch of unnecessary code
lockshaw Oct 8, 2024
a2b8832
Format
lockshaw Oct 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/compiler/include/compiler/graph_optimize_result.struct.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace = "FlexFlow"
name = "GraphOptimizeResult"
features = [ ]

includes = [
"compiler/machine_mapping.dtg.h",
"pcg/parallel_computation_graph/parallel_computation_graph.h"
]

[[fields]]
name = "pcg"
type = "::FlexFlow::ParallelComputationGraph"

[[fields]]
name = "machine_mapping"
type = "::FlexFlow::MachineMapping"
31 changes: 31 additions & 0 deletions lib/compiler/include/compiler/graph_optimize_state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef _FLEXFLOW_COMPILER_MCMC_STATE_H
#define _FLEXFLOW_COMPILER_MCMC_STATE_H

#include "compiler/graph_optimize_result.dtg.h"
#include "compiler/machine_mapping.h"

namespace FlexFlow {

struct GraphOptimizeState {

Check warning on line 9 in lib/compiler/include/compiler/graph_optimize_state.h

View check run for this annotation

Codecov / codecov/patch

lib/compiler/include/compiler/graph_optimize_state.h#L9

Added line #L9 was not covered by tests
GraphOptimizeState(GraphOptimizeResult const &graph_optimize_result, float runtime);

GraphOptimizeResult graph_optimize_result;
float runtime;

bool operator==(GraphOptimizeState const &other) const;
bool operator!=(GraphOptimizeState const &other) const;
bool operator<(GraphOptimizeState const &other) const;
};

} // namespace FlexFlow

namespace std {

template <>
struct hash<::FlexFlow::GraphOptimizeState> {
size_t operator()(::FlexFlow::GraphOptimizeState const &) const;
};

} // namespace std

#endif
37 changes: 0 additions & 37 deletions lib/compiler/include/compiler/graph_utils.h

This file was deleted.

17 changes: 1 addition & 16 deletions lib/compiler/include/compiler/machine_mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "compiler/machine_mapping.dtg.h"
#include "compiler/optimal_cost_state.dtg.h"
#include "cost_estimate.h"
#include "cost_estimator.h"
#include "pcg/machine_specification.dtg.h"
#include "pcg/machine_specification.h"
#include "pcg/machine_view.h"
Expand All @@ -29,10 +29,6 @@ struct OptimalCostResult {
};
FF_VISITABLE_STRUCT(OptimalCostResult, runtime, machine_mapping);

struct OptimalCostRuntimeCmp {
bool operator()(OptimalCostResult const &, OptimalCostResult const &);
};

class OptimalCostCache {
public:
OptimalCostCache() = default;
Expand All @@ -55,15 +51,4 @@ OptimalCostResult optimal_cost(

} // namespace FlexFlow

// namespace std {
//
// template <>
// struct hash<std::unordered_map<FlexFlow::Node, FlexFlow::MachineMapping>> {
// size_t operator()(
// std::unordered_map<FlexFlow::Node, FlexFlow::MachineMapping> const &g)
// const;
// };

// }; // namespace std

#endif
12 changes: 4 additions & 8 deletions lib/compiler/include/compiler/optimal_cost_state.struct.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ includes = [
"utils/graph/serial_parallel/serial_parallel_decomposition.dtg.h",
"pcg/machine_specification.dtg.h",
"pcg/machine_view.dtg.h",
"utils/graph/node/node.dtg.h",
"utils/graph/open_dataflow_graph/open_dataflow_edge.dtg.h",
"utils/graph/open_dataflow_graph/open_dataflow_value.dtg.h",
"utils/hash/unordered_map.h",
"utils/fmt/unordered_map.h",
"utils/hash/unordered_map.h",
]

[[fields]]
Expand All @@ -28,9 +28,5 @@ name = "resource"
type = "::FlexFlow::MachineSpecification"

[[fields]]
name = "given_machine_views"
type = "std::unordered_map<::FlexFlow::Node, ::FlexFlow::MachineView>"

[[fields]]
name = "frontier_machine_views"
type = "std::unordered_map<::FlexFlow::OpenDataflowEdge, ::FlexFlow::MachineView>"
name = "fixed_machine_views"
type = "std::unordered_map<::FlexFlow::OpenDataflowValue, ::FlexFlow::MachineView>"
26 changes: 26 additions & 0 deletions lib/compiler/include/compiler/optimizer_config.struct.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace = "FlexFlow"
name = "OptimizerConfig"
features = [
"eq",
"hash",
"fmt",
]

includes = [
]

[[fields]]
name = "alpha"
type = "float"

[[fields]]
name = "budget"
type = "int"

[[fields]]
name = "threshold"
type = "float"

[[fields]]
name = "max_num_ops"
type = "int"
33 changes: 6 additions & 27 deletions lib/compiler/include/compiler/unity_algorithm.h
Original file line number Diff line number Diff line change
@@ -1,39 +1,18 @@
#ifndef _FLEXFLOW_COMPILER_UNITY_ALGORITHM_H
#define _FLEXFLOW_COMPILER_UNITY_ALGORITHM_H

#include "compiler/graph_optimize_result.dtg.h"
#include "compiler/machine_mapping.h"
#include "cost_estimate.h"
#include "machine_mapping.h"
#include "cost_estimator.h"
#include "optimizer_config.dtg.h"
#include "pcg/computation_graph.h"
#include "pcg/machine_specification.dtg.h"
#include "substitutions/sub_parallel_computation_graph.h"
namespace FlexFlow {

struct Strategy {
ParallelComputationGraph pcg;
MachineMapping machine_mapping;
req<float> runtime;
friend bool operator!=(Strategy const &lhs, Strategy const &rhs) {
return (lhs.machine_mapping != rhs.machine_mapping) ||
(lhs.runtime != rhs.runtime);
}
};

FF_VISITABLE_STRUCT(Strategy, pcg, machine_mapping, runtime);

struct StrategyRuntimeCmp {
bool operator()(Strategy const &, Strategy const &);
};

struct OptimizerConfig {
float alpha;
int budget;
float threshold;
int max_num_ops;
};
namespace FlexFlow {

Strategy graph_optimize(
ComputationGraph &cg,
GraphOptimizeResult graph_optimize(
ParallelComputationGraph &pcg,
CostEstimator const &cost_estimator,
MachineSpecification const &resources,
std::function<std::unordered_set<MachineView>(
Expand Down
80 changes: 80 additions & 0 deletions lib/compiler/src/graph_optimize_state.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "compiler/graph_optimize_state.h"

namespace FlexFlow {

GraphOptimizeState::GraphOptimizeState(
GraphOptimizeResult const &graph_optimize_result, float runtime)
: graph_optimize_result(graph_optimize_result), runtime(runtime) {}

Check warning on line 7 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L5-L7

Added lines #L5 - L7 were not covered by tests

bool GraphOptimizeState::operator==(GraphOptimizeState const &other) const {
auto layers1 = topological_ordering(graph_optimize_result.pcg);
auto layers2 = topological_ordering(other.graph_optimize_result.pcg);

Check warning on line 11 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L9-L11

Added lines #L9 - L11 were not covered by tests
if (layers1.size() != layers2.size()) {
return false;
}
std::unordered_map<parallel_tensor_guid_t, parallel_tensor_guid_t> mapping;

Check warning on line 15 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L15

Added line #L15 was not covered by tests
for (size_t i = 0; i < layers1.size(); ++i) {
if (get_parallel_layer_attrs(graph_optimize_result.pcg, layers1[i]) !=
get_parallel_layer_attrs(other.graph_optimize_result.pcg, layers2[i])) {
return false;

Check warning on line 19 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L18-L19

Added lines #L18 - L19 were not covered by tests
}
auto inputs1 = get_layer_inputs(graph_optimize_result.pcg, layers1[i]);
auto inputs2 =
get_layer_inputs(other.graph_optimize_result.pcg, layers2[i]);

Check warning on line 23 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L21-L23

Added lines #L21 - L23 were not covered by tests
if (inputs1.size() != inputs2.size()) {
return false;
}
for (size_t j = 0; j < inputs1.size(); ++j) {
if (inputs1[j] != mapping.at(inputs2[j])) {
return false;
}
}
auto outputs1 = get_layer_outputs(graph_optimize_result.pcg, layers1[i]);
auto outputs2 =
get_layer_outputs(other.graph_optimize_result.pcg, layers2[i]);

Check warning on line 34 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L32-L34

Added lines #L32 - L34 were not covered by tests
if (outputs1.size() != outputs2.size()) {
return false;

Check warning on line 36 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L36

Added line #L36 was not covered by tests
}
for (size_t j = 0; j < outputs1.size(); ++j) {
mapping.emplace(outputs2[j], outputs1[j]);

Check warning on line 39 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L39

Added line #L39 was not covered by tests
}
}
return true;
}

bool GraphOptimizeState::operator!=(GraphOptimizeState const &other) const {
return !(*this == other);

Check warning on line 46 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L45-L46

Added lines #L45 - L46 were not covered by tests
}

bool GraphOptimizeState::operator<(GraphOptimizeState const &other) const {
return runtime < other.runtime;

Check warning on line 50 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L49-L50

Added lines #L49 - L50 were not covered by tests
}

} // namespace FlexFlow

namespace std {

size_t hash<::FlexFlow::GraphOptimizeState>::operator()(

Check warning on line 57 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L57

Added line #L57 was not covered by tests
::FlexFlow::GraphOptimizeState const &state) const {
size_t seed = 0;
auto layers = topological_ordering(state.graph_optimize_result.pcg);
::FlexFlow::hash_combine(seed, layers.size());

Check warning on line 61 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L59-L61

Added lines #L59 - L61 were not covered by tests
for (auto layer : layers) {
::FlexFlow::hash_combine(
seed, get_parallel_layer_attrs(state.graph_optimize_result.pcg, layer));
auto inputs = get_layer_inputs(state.graph_optimize_result.pcg, layer);
::FlexFlow::hash_combine(seed, inputs.size());

Check warning on line 66 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L63-L66

Added lines #L63 - L66 were not covered by tests
for (auto input : inputs) {
for (size_t i = 0; i < layers.size(); ++i) {
if (get_source_layer(state.graph_optimize_result.pcg, input) ==
layers[i]) {
::FlexFlow::hash_combine(seed, i);

Check warning on line 71 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L70-L71

Added lines #L70 - L71 were not covered by tests
break;
}
}
}
}
return seed;

Check warning on line 77 in lib/compiler/src/graph_optimize_state.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/graph_optimize_state.cc#L77

Added line #L77 was not covered by tests
}

} // namespace std
Loading
Loading