-
Notifications
You must be signed in to change notification settings - Fork 247
Victor/mcmc cherrypick #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
Victor/mcmc cherrypick #1619
Changes from 2 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_naive_mapping(ParallelComputationGraph &pcg, | ||
victorli2002 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
| #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 "compiler/mcmc/generic_mcmc_state.h" | ||
| #include "utils/nonnegative_int/nonnegative_range.h" | ||
| #include "utils/random_utils.h" | ||
| #include <optional> | ||
|
|
||
| namespace FlexFlow { | ||
|
|
||
| template <typename State, typename ScoringFunc> | ||
| void modify_state_for_minimization( | ||
| Generic_MCMC_state<State, float> &best_state, | ||
| Generic_MCMC_state<State, float> ¤t_state, | ||
| State candidate, | ||
| ScoringFunc scorer, | ||
| float temperature) { | ||
| float best_estimate = best_state.get_score(); | ||
| float new_estimate = scorer(candidate); | ||
| float delta = new_estimate - best_estimate; | ||
| if (delta < 0 || (randf() < exp(-delta / temperature))) { | ||
| current_state = Generic_MCMC_state<State, float>(candidate, new_estimate); | ||
| if (delta < 0) { | ||
| best_state = current_state; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // GeneratingFunc : State -> nn_int -> std::optional<State> | ||
| // ScoringFunc : State -> float | ||
|
|
||
| template <typename State, typename GeneratingFunc, typename ScoringFunc> | ||
| Generic_MCMC_state<State, float> | ||
| minimize_score(State const &starting_state, | ||
victorli2002 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| GeneratingFunc const &generator, | ||
victorli2002 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ScoringFunc const &scorer, | ||
victorli2002 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| GenericMCMCConfig const &search_config) { | ||
| using MCMCState = Generic_MCMC_state<State, float>; | ||
| MCMCState best_state = MCMCState(starting_state, scorer(starting_state)); | ||
| MCMCState current_state = best_state; | ||
| for (nonnegative_int i : nonnegative_range(search_config.num_iterations)) { | ||
| std::optional<State> candidate = generator(current_state.get_state(), i); | ||
| if (candidate != std::nullopt) { | ||
| modify_state_for_minimization(best_state, | ||
| current_state, | ||
| candidate.value(), | ||
| scorer, | ||
| search_config.temperature); | ||
| } | ||
| } | ||
| 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,28 @@ | ||
|
|
||
| #ifndef _FLEXFLOW_COMPILER_MCMC_GENERIC_MCMC_STATE_H | ||
| #define _FLEXFLOW_COMPILER_MCMC_GENERIC_MCMC_STATE_H | ||
| #include "utils/nonnegative_int/nonnegative_int.h" | ||
|
|
||
| namespace FlexFlow { | ||
|
|
||
| template <typename State, typename Score> | ||
| struct Generic_MCMC_state { | ||
| public: | ||
| Generic_MCMC_state(State const &state, Score const &score) | ||
victorli2002 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| : state(state), score(score) {} | ||
|
|
||
| State const &get_state() const { | ||
| return state; | ||
| } | ||
| Score const &get_score() const { | ||
| return score; | ||
| } | ||
|
|
||
| private: | ||
| State state; | ||
| Score score; | ||
| }; | ||
|
|
||
| } // 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,22 @@ | ||
| #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_graph_optimize(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_interval" | ||
| type = "::FlexFlow::nonnegative_int" | ||
|
|
||
| [[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
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.