-
Notifications
You must be signed in to change notification settings - Fork 247
Implement MCMC mapping/optimization algorithm #1619
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
Open
victorli2002
wants to merge
5
commits into
flexflow:master
Choose a base branch
from
victorli2002:victor/mcmc_cherrypick
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
lib/compiler/include/compiler/machine_mapping/allowed_machine_views.h
This file contains hidden or 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 @@ | ||
| #ifndef _FLEXFLOW_COMPILER_ALLOWED_MACHINE_VIEWS_H | ||
| #define _FLEXFLOW_COMPILER_ALLOWED_MACHINE_VIEWS_H | ||
|
|
||
| #include "pcg/machine_specification.dtg.h" | ||
| #include "pcg/machine_view.dtg.h" | ||
| #include "pcg/operator_task_space.dtg.h" | ||
|
|
||
| namespace FlexFlow { | ||
|
|
||
| bool is_valid_machine_view(MachineView const &mv, | ||
| OperatorTaskSpace const &task, | ||
| MachineSpecification const &ms); | ||
|
|
||
| std::unordered_set<MachineView> | ||
| get_allowed_machine_views(MachineSpecification const &machine_spec, | ||
| OperatorTaskSpace const &task, | ||
| DeviceType device_type); | ||
|
|
||
| } // namespace FlexFlow | ||
|
|
||
| #endif |
32 changes: 32 additions & 0 deletions
32
...compiler/include/compiler/machine_mapping/apply_substitution_and_update_machine_mapping.h
This file contains hidden or 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,32 @@ | ||
| #ifndef _FLEXFLOW_LIB_SUBSTITUTIONS_INCLUDE_SUBSTITUTIONS_APPLY_SUBSTITUTION_APPLY_SUBSTITUTION_AND_UPDATE_MACHINE_MAPPING_H | ||
| #define _FLEXFLOW_LIB_SUBSTITUTIONS_INCLUDE_SUBSTITUTIONS_APPLY_SUBSTITUTION_APPLY_SUBSTITUTION_AND_UPDATE_MACHINE_MAPPING_H | ||
|
|
||
| #include "compiler/search_result.dtg.h" | ||
| #include "substitutions/pcg_pattern_match.dtg.h" | ||
| #include "substitutions/sub_parallel_computation_graph.dtg.h" | ||
| #include "substitutions/substitution.dtg.h" | ||
|
|
||
| namespace FlexFlow { | ||
| /** | ||
| * @brief Applies \p substitution to \p mapped_pcg at the location specified by | ||
| * \p match, returning the resulting SearchResult (mapped pcg) | ||
| * | ||
| * @param mapped_pcg | ||
| * @param substitution | ||
| * @param match The location at which to apply substitution. This location in | ||
| * sub_pcg should match substitution's PCGPattern. Likely created by running | ||
| * FlexFlow::find_pattern_matches(PCGPattern const &, | ||
| * SubParallelComputationGraph const &). | ||
| * @return SearchResult A mapped pcg similar to mapped_pcg, but with | ||
| * the subgraph of the pcg specified by match replaced with the result of the | ||
| * output expression of substitution and the machine mapping updated to account | ||
| * for the new output | ||
| */ | ||
| SearchResult apply_substitution_and_update_machine_mapping( | ||
| SearchResult const &mapped_pcg, | ||
| Substitution const &sub, | ||
| PCGPatternMatch const &match); | ||
|
|
||
| } // namespace FlexFlow | ||
|
|
||
| #endif |
19 changes: 19 additions & 0 deletions
19
lib/compiler/include/compiler/machine_mapping/machine_mapping_mutation_set.h
This file contains hidden or 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,19 @@ | ||
| #ifndef _FLEXFLOW_LIB_COMPILER_INCLUDE_COMPILER_MCMC_MACHINE_MAPPING_MUTATION_SET_H | ||
| #define _FLEXFLOW_LIB_COMPILER_INCLUDE_COMPILER_MCMC_MACHINE_MAPPING_MUTATION_SET_H | ||
|
|
||
| #include "compiler/machine_mapping/machine_mapping.h" | ||
| #include "compiler/search_result.dtg.h" | ||
|
|
||
| namespace FlexFlow { | ||
| std::optional<MachineMapping> | ||
| get_random_mapping(ParallelComputationGraph &pcg, | ||
| MachineSpecification const &resources, | ||
| DeviceType const &device_type); | ||
|
|
||
| std::optional<MachineMapping> | ||
| get_random_mutation(SearchResult mapped_pcg, | ||
| MachineSpecification const &resource, | ||
| DeviceType const &device_type); | ||
| } // namespace FlexFlow | ||
|
|
||
| #endif | ||
41 changes: 41 additions & 0 deletions
41
lib/compiler/include/compiler/mcmc/generic_mcmc_algorithm.h
This file contains hidden or 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,41 @@ | ||
| #ifndef _FLEXFLOW_COMPILER_MCMC_GENERIC_MCMC_ALGORITHM_H | ||
| #define _FLEXFLOW_COMPILER_MCMC_GENERIC_MCMC_ALGORITHM_H | ||
|
|
||
| #include "compiler/mcmc/generic_mcmc_config.dtg.h" | ||
| #include "utils/containers/transform.h" | ||
| #include "utils/nonnegative_int/nonnegative_range.h" | ||
| #include "utils/optional.h" | ||
| #include "utils/random_utils.h" | ||
|
|
||
| namespace FlexFlow { | ||
|
|
||
| // SamplingFn : State -> std::optional<State> | ||
| // CostFn : State -> float | ||
|
|
||
| template <typename State, typename SamplingFn, typename CostFn> | ||
| State run_mcmc(State const &starting_state, | ||
| SamplingFn const &sampler, | ||
| CostFn const &cost, | ||
| GenericMCMCConfig const &search_config) { | ||
| State best_state = starting_state; | ||
| State current_state = best_state; | ||
| for (nonnegative_int i : nonnegative_range(search_config.num_iterations)) { | ||
| std::optional<State> maybe_new_state = | ||
| transform(sampler(current_state), [&](State const &s) { | ||
| float delta = cost(s) - cost(best_state); | ||
| if (randf() < exp(-delta / search_config.temperature)) { | ||
| if (delta < 0) { | ||
| best_state = s; | ||
| } | ||
| return s; | ||
| } | ||
| return current_state; | ||
| }); | ||
| current_state = or_else(maybe_new_state, [&]() { return current_state; }); | ||
| } | ||
| return best_state; | ||
| } | ||
|
|
||
| } // namespace FlexFlow | ||
|
|
||
| #endif |
19 changes: 19 additions & 0 deletions
19
lib/compiler/include/compiler/mcmc/generic_mcmc_config.struct.toml
This file contains hidden or 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,19 @@ | ||
| namespace = "FlexFlow" | ||
| name = "GenericMCMCConfig" | ||
| features = [ | ||
| "eq", | ||
| "hash", | ||
| "fmt", | ||
| ] | ||
|
|
||
| includes = [ | ||
| "utils/nonnegative_int/nonnegative_int.h" | ||
| ] | ||
|
|
||
| [[fields]] | ||
| name = "temperature" | ||
| type = "float" | ||
|
|
||
| [[fields]] | ||
| name = "num_iterations" | ||
| type = "::FlexFlow::nonnegative_int" |
This file contains hidden or 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,23 @@ | ||
| #ifndef _FLEXFLOW_COMPILER_MCMC_OVER_MAPPED_PCG_H | ||
| #define _FLEXFLOW_COMPILER_MCMC_OVER_MAPPED_PCG_H | ||
|
|
||
| #include "compiler/cost_estimator/runtime_only_cost_estimator.h" | ||
| #include "compiler/mcmc/mcmc_over_mapped_pcg_config.dtg.h" | ||
| #include "compiler/search_result.dtg.h" | ||
| #include "pcg/computation_graph.h" | ||
| #include "pcg/machine_specification.dtg.h" | ||
| #include "pcg/parallel_computation_graph/parallel_computation_graph.dtg.h" | ||
| #include "substitutions/sub_parallel_computation_graph.h" | ||
| #include "substitutions/substitution.h" | ||
|
|
||
| namespace FlexFlow { | ||
|
|
||
| SearchResult | ||
| mcmc_over_mapped_pcg(ParallelComputationGraph &pcg, | ||
| RuntimeOnlyCostEstimator const &cost_estimator, | ||
| MachineSpecification const &resources, | ||
| MCMCOverMappedPCGConfig const &search_config); | ||
|
|
||
| } // namespace FlexFlow | ||
|
|
||
| #endif |
28 changes: 28 additions & 0 deletions
28
lib/compiler/include/compiler/mcmc/mcmc_over_mapped_pcg_config.struct.toml
This file contains hidden or 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,28 @@ | ||
| namespace = "FlexFlow" | ||
| name = "MCMCOverMappedPCGConfig" | ||
| features = [ | ||
| "eq", | ||
| "hash", | ||
| "fmt", | ||
| ] | ||
|
|
||
| includes = [ | ||
| "pcg/device_type.dtg.h", | ||
| "utils/nonnegative_int/nonnegative_int.h" | ||
| ] | ||
|
|
||
| [[fields]] | ||
| name = "temperature" | ||
| type = "float" | ||
|
|
||
| [[fields]] | ||
| name = "num_iterations" | ||
| type = "::FlexFlow::nonnegative_int" | ||
|
|
||
| [[fields]] | ||
| name = "substitution_frequency" | ||
| type = "float" | ||
|
|
||
| [[fields]] | ||
| name = "device_type" | ||
| type = "::FlexFlow::DeviceType" |
This file contains hidden or 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,13 @@ | ||
| #ifndef _FLEXFLOW_LIB_COMPILER_INCLUDE_COMPILER_GRAPH_OPTIMIZE_RESULT_H | ||
| #define _FLEXFLOW_LIB_COMPILER_INCLUDE_COMPILER_GRAPH_OPTIMIZE_RESULT_H | ||
|
|
||
| #include "compiler/search_result.dtg.h" | ||
|
|
||
| namespace FlexFlow { | ||
|
|
||
| std::string format_as(SearchResult const &); | ||
| std::ostream &operator<<(std::ostream &, SearchResult const &); | ||
|
|
||
| } // namespace FlexFlow | ||
|
|
||
| #endif |
This file contains hidden or 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,17 @@ | ||
| namespace = "FlexFlow" | ||
| name = "SearchResult" | ||
| features = [ | ||
| ] | ||
|
|
||
| includes = [ | ||
| "pcg/parallel_computation_graph/parallel_computation_graph.h", | ||
| "compiler/machine_mapping/machine_mapping.h", | ||
| ] | ||
|
|
||
| [[fields]] | ||
| name = "pcg" | ||
| type = "::FlexFlow::ParallelComputationGraph" | ||
|
|
||
| [[fields]] | ||
| name = "machine_mapping" | ||
| type = "::FlexFlow::MachineMapping" |
This file contains hidden or 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
78 changes: 78 additions & 0 deletions
78
lib/compiler/src/compiler/machine_mapping/apply_substitution_and_update_machine_mapping.cc
This file contains hidden or 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,78 @@ | ||
| #include "compiler/machine_mapping/apply_substitution_and_update_machine_mapping.h" | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. somehow break this up and remove some dup with the base apply_substitution try to add tests |
||
| #include "pcg/parallel_computation_graph/parallel_computation_graph_edge.h" | ||
| #include "pcg/parallel_computation_graph/parallel_tensor_guid_t.h" | ||
| #include "substitutions/apply_substitution/apply_substitution.h" | ||
| #include "substitutions/apply_substitution/evaluate_substitution_output.h" | ||
| #include "substitutions/apply_substitution/output_expr_to_result_sub_pcg_mapping.h" | ||
| #include "substitutions/open_parallel_tensor_guid_t.h" | ||
| #include "substitutions/pcg_pattern_match.h" | ||
| #include "substitutions/sub_parallel_computation_graph.h" | ||
| #include "substitutions/sub_parallel_computation_graph_data.dtg.h" | ||
| #include "substitutions/sub_parallel_computation_graph_edge.h" | ||
| #include "utils/containers/filter.h" | ||
| #include "utils/containers/is_subseteq_of.h" | ||
| #include "utils/containers/keys.h" | ||
| #include "utils/containers/merge_maps.h" | ||
| #include "utils/containers/restrict_keys.h" | ||
| #include "utils/containers/set_minus.h" | ||
| #include "utils/containers/values.h" | ||
| #include "utils/containers/vector_of.h" | ||
| #include "utils/random_utils.h" | ||
| #include <libassert/assert.hpp> | ||
|
|
||
| namespace FlexFlow { | ||
|
|
||
| SearchResult apply_substitution_and_update_machine_mapping( | ||
| SearchResult const &mapped_pcg, | ||
| Substitution const &sub, | ||
| PCGPatternMatch const &match) { | ||
| SubParallelComputationGraph spcg = sub_pcg_from_full_pcg(mapped_pcg.pcg); | ||
|
|
||
| std::pair<SubParallelComputationGraph, OutputExprToResultSubPCGMapping> | ||
| substitution_output_result = | ||
| evaluate_substitution_output(spcg, sub, match); | ||
|
|
||
| SubParallelComputationGraph post_substitution_graph = | ||
| apply_substitution_from_output_result( | ||
| substitution_output_result, spcg, sub, match); | ||
|
|
||
| std::unordered_map<parallel_layer_guid_t, ParallelLayerAttrs> post_node_data = | ||
| get_sub_pcg_data(post_substitution_graph).node_data; | ||
|
|
||
| std::unordered_set<parallel_layer_guid_t> | ||
| substitution_output_parallel_layers = | ||
| get_parallel_layers(substitution_output_result.first); | ||
|
|
||
| std::unordered_map<parallel_layer_guid_t, MachineView> machine_views = | ||
| mapped_pcg.machine_mapping.machine_views; | ||
|
|
||
| std::unordered_set<parallel_layer_guid_t> matched_nodes = | ||
| unordered_set_of(values(match.node_assignment)); | ||
|
|
||
| std::vector<MachineView> substituted_machine_views = vector_of( | ||
| transform(matched_nodes, [&](parallel_layer_guid_t const &node) { | ||
| return machine_views.at(node); | ||
| })); | ||
|
|
||
| for (parallel_layer_guid_t layer : substitution_output_parallel_layers) { | ||
| machine_views.insert_or_assign(layer, | ||
| select_random(substituted_machine_views)); | ||
| } | ||
|
|
||
| ASSERT(is_subseteq_of(keys(post_node_data), keys(machine_views))); | ||
|
|
||
| std::unordered_map<parallel_layer_guid_t, MachineView> | ||
| post_node_machine_views = | ||
| filter(machine_views, | ||
| [&](std::pair<parallel_layer_guid_t, MachineView> const &p) { | ||
| return post_node_data.count(p.first); | ||
| }); | ||
|
|
||
| ASSERT(keys(post_node_data) == keys(post_node_machine_views)); | ||
|
|
||
| return SearchResult{ | ||
| pcg_from_sub_pcg_by_dropping_inputs(post_substitution_graph), | ||
| MachineMapping{post_node_machine_views}}; | ||
| } | ||
|
|
||
| } // namespace FlexFlow | ||
52 changes: 52 additions & 0 deletions
52
lib/compiler/src/compiler/machine_mapping/machine_mapping_mutation_set.cc
This file contains hidden or 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,52 @@ | ||
| #include "compiler/machine_mapping/machine_mapping_mutation_set.h" | ||
| #include "compiler/allowed_machine_views.h" | ||
| #include "pcg/machine_view.h" | ||
| #include "pcg/operator_task_space.h" | ||
| #include "utils/containers/vector_of.h" | ||
| #include "utils/nonnegative_int/nonnegative_range.h" | ||
| #include "utils/random_utils.h" | ||
|
|
||
| namespace FlexFlow { | ||
|
|
||
| std::optional<MachineMapping> | ||
| get_random_mapping(ParallelComputationGraph &pcg, | ||
| MachineSpecification const &resources, | ||
| DeviceType const &device_type) { | ||
| std::vector<parallel_layer_guid_t> layers = topological_ordering(pcg); | ||
| std::unordered_map<parallel_layer_guid_t, MachineView> machine_views; | ||
| for (parallel_layer_guid_t layer : layers) { | ||
| OperatorTaskSpace task = get_operator_task_space(pcg, layer); | ||
| std::unordered_set<MachineView> allowed_machine_views = | ||
| get_allowed_machine_views(resources, task, DeviceType::GPU); | ||
| if (allowed_machine_views.empty()) { | ||
| return std::nullopt; | ||
| } | ||
| machine_views.insert( | ||
| {layer, select_random(vector_of(allowed_machine_views))}); | ||
| } | ||
| return MachineMapping{machine_views}; | ||
| } | ||
|
|
||
| std::optional<MachineMapping> | ||
| get_random_mutation(SearchResult mapped_pcg, | ||
| MachineSpecification const &resources, | ||
| DeviceType const &device_type) { | ||
| ParallelComputationGraph pcg = mapped_pcg.pcg; | ||
| std::vector<parallel_layer_guid_t> layers = topological_ordering(pcg); | ||
| if (layers.size() == 0) { | ||
| return std::nullopt; | ||
| } | ||
| parallel_layer_guid_t random_layer = select_random(layers); | ||
|
|
||
| MachineMapping machine_mapping = mapped_pcg.machine_mapping; | ||
| MachineView machine_view = machine_mapping.machine_views.at(random_layer); | ||
| OperatorTaskSpace task = get_operator_task_space(pcg, random_layer); | ||
|
|
||
| std::vector<MachineView> allowed_machine_views = | ||
| vector_of(get_allowed_machine_views(resources, task, device_type)); | ||
| MachineView random_new_machine_view = select_random(allowed_machine_views); | ||
|
|
||
| machine_mapping.machine_views.at(random_layer) = random_new_machine_view; | ||
| return machine_mapping; | ||
| } | ||
| } // namespace FlexFlow |
This file contains hidden or 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,15 @@ | ||
| #include "compiler/mcmc/generic_mcmc_algorithm.h" | ||
victorli2002 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #include "utils/archetypes/value_type.h" | ||
|
|
||
| namespace FlexFlow { | ||
|
|
||
| using State = value_type<0>; | ||
| using SamplingFn = std::function<std::optional<State>(State)>; | ||
| using CostFn = std::function<float(State)>; | ||
|
|
||
| template State run_mcmc(State const &starting_state, | ||
| SamplingFn const &sampler, | ||
| CostFn const &cost, | ||
| GenericMCMCConfig const &search_config); | ||
|
|
||
| } // namespace FlexFlow | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.