Skip to content
Open

MCMC #1601

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
0d639ac
initial implmentation of meomry algorithm
wmdi Oct 13, 2024
da857a5
fmt
wmdi Oct 16, 2024
ef8c5c2
pass existing tests
wmdi Oct 16, 2024
982f1f5
initialize memory algorithm
wmdi Oct 31, 2024
01c6a6b
Merge remote-tracking branch 'flexflow/repo-refactor' into memory-alg
wmdi Oct 31, 2024
964c885
fix tests & format
wmdi Oct 31, 2024
0c0e7b0
minimum tests for memory algorithm
wmdi Nov 7, 2024
7778377
renaming
wmdi Dec 18, 2024
0315160
fmt
wmdi Dec 18, 2024
855a7d5
fix
wmdi Dec 30, 2024
2b4e127
rename single machine mapping
wmdi Jan 9, 2025
f72fb6f
Merge branch 'master' into memory-alg
lockshaw Jan 9, 2025
50bae93
format
wmdi Jan 9, 2025
3297d3f
Merge branch 'memory-alg' of github.com:wmdi/FlexFlow into memory-alg
wmdi Jan 9, 2025
d96b678
top-level loop for compiler
wmdi Jan 15, 2025
cd9b031
Merge branch 'master' into memory-alg
lockshaw Jan 15, 2025
1dcaa42
Merge branch 'master' into memory-alg
lockshaw Jan 20, 2025
c16bcf6
fixes
wmdi Jan 21, 2025
2e93e74
Merge branch 'memory-alg' of github.com:wmdi/FlexFlow into memory-alg
wmdi Jan 21, 2025
62389ad
upd
wmdi Jan 22, 2025
6d2fe50
fixes
wmdi Jan 29, 2025
45a931c
fix
wmdi Jan 30, 2025
efc7a9a
Merge remote-tracking branch 'flexflow/master' into memory-alg
wmdi Jan 30, 2025
4f97602
Merge remote-tracking branch 'flexflow/master' into memory-alg
wmdi Feb 12, 2025
14234b4
fix some errors introduced in merge
wmdi Feb 12, 2025
30e51fc
upd
wmdi Feb 20, 2025
ddbace1
Merge remote-tracking branch 'origin/master' into memory-alg
wmdi Feb 25, 2025
eb58e91
add test case for get mm problem tree
wmdi Feb 25, 2025
40c3494
Fix is_valid_machine_mapping_problem_tree, add hacky printing for pro…
lockshaw Feb 26, 2025
962934d
upd
wmdi Feb 27, 2025
550127a
update (#3)
Marsella8 Feb 28, 2025
948d247
fix get_optimal_machine_mapping
wmdi Mar 2, 2025
612bff5
implement divisible_by constarint type in substitutions
wmdi Mar 2, 2025
1c1933f
MCMC algorithm draft
Mar 12, 2025
d70a44a
Merge remote-tracking branch 'wmdi/memory-alg' into MCMC
Mar 12, 2025
fe6d950
removing substitution part of MCMC for now
Mar 12, 2025
be740cc
Adding randomness to MCMC to make it true MCMC, adding secondary non-…
Apr 1, 2025
355fe3f
Adding substitutions to MCMC (not quite working yet)
Apr 3, 2025
e398ba0
Added generic MCMC
Apr 4, 2025
2f186c3
Refactor MCMC to fit the generic
Apr 5, 2025
651ba94
Slight refactoring
May 18, 2025
13a5ee4
Merge remote-tracking branch 'origin/master' into MCMC
May 18, 2025
e3cf79a
Updating MCMC to work with substitutions
May 18, 2025
1af4a6b
Fixing formatting
May 18, 2025
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
3 changes: 3 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source_up_if_exists

use flake
8 changes: 8 additions & 0 deletions .vimrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
" example search path configuration
set path=lib/runtime/**,lib/**

" set build target
" let g:target = "pcg"

" set test target
" let g:test_target = "utils-test"
18 changes: 18 additions & 0 deletions lib/compiler/include/compiler/algorithm_config.variant.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace = "FlexFlow"
name = "AlgorithmConfig"
features = [
"eq",
"hash",
"fmt",
]

includes = [
"compiler/data_parallelism/data_parallelism_config.dtg.h",
"compiler/unity_algorithm/unity_search_config.dtg.h",
]

[[values]]
type = "::FlexFlow::DataParallelismConfig"

[[values]]
type = "::FlexFlow::UnitySearchConfig"
32 changes: 6 additions & 26 deletions lib/compiler/include/compiler/compiler.h
Original file line number Diff line number Diff line change
@@ -1,42 +1,22 @@
#ifndef _FLEXFLOW_COMPILER_COMPILER_H
#define _FLEXFLOW_COMPILER_COMPILER_H

#include "pcg/cost_values.h"
#include "pcg/machine_view.h"
#include "pcg/parallel_computation_graph/parallel_computation_graph.h"
#include "pcg/tensor_mapping.h"
#include "compiler/algorithm_config.dtg.h"
#include "compiler/cost_estimator/cost_estimator.h"
#include "compiler/search_result.dtg.h"
#include "pcg/machine_specification.dtg.h"

namespace FlexFlow {

enum class SearchAlgorithm {
DATA_PARALLEL,
};

using SearchAlgorithmConfig = std::variant<>;
using SearchSolution = std::variant<>;

struct SearchResult {
ParallelComputationGraph pcg;
TensorMapping tensor_mapping;
SearchSolution solution;
CostValues cost_values;
UNITY,
};

SearchResult optimize(ComputationGraph const &,
MachineSpecification const &,
CostEstimator const &,
SearchAlgorithm,
optional<AlgorithmConfig> const &);

// struct SearchSolution {
// LabelledMultiDiGraph<PCGOperatorAttrs, ParallelTensorShape> optimized_pcg;
// std::unordered_map<Node, MachineView> device_assignments;
// /* std::unordered_map<tensor_guid_t,
// std::unordered_set<parallel_tensor_guid_t>> tensor_mappings; */
// };
//
// SearchSolution run_data_parallelize(ComputationGraph const &,
// MachineSpecification const &);
AlgorithmConfig const &);

} // namespace FlexFlow

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace = "FlexFlow"
name = "DataParallelismConfig"
features = [
"eq",
"hash",
"fmt",
]

includes = [
]

[[fields]]
name = "degree"
type = "int"
16 changes: 0 additions & 16 deletions lib/compiler/include/compiler/graph_optimize_result.struct.toml

This file was deleted.

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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#define _FLEXFLOW_COMPILER_MACHINE_MAPPING_H

#include "compiler/machine_mapping/machine_mapping.dtg.h"
#include "compiler/machine_mapping/machine_mapping_result.h"
#include "compiler/series_parallel/pcg/pcg_binary_sp_decomposition.dtg.h"
#include "pcg/device_id_t.dtg.h"
#include "pcg/machine_specification.dtg.h"
#include "pcg/operator_task_space.dtg.h"
Expand All @@ -14,6 +16,13 @@ MachineMapping combine_disjoint_mappings(MachineMapping const &,

bool nodes_are_disjoint(MachineMapping const &m1, MachineMapping const &m2);

parallel_layer_guid_t
get_layer_from_path(PCGBinarySPDecomposition const &sp_decomposition,
BinaryTreePath const &path);

std::optional<MachineMapping> get_machine_mapping_from_machine_mapping_result(
PCGBinarySPDecomposition const &, MachineMappingResult const &);

} // namespace FlexFlow

#endif
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,
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace FlexFlow {

bool is_valid_machine_mapping_problem_tree(
MachineMappingProblemTree const &problem_tree);

MachineMappingProblemTree
get_machine_mapping_problem_tree(ParallelComputationGraph const &pcg,
PCGBinarySPDecomposition const &sp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "compiler/machine_mapping/machine_mapping_problem_tree/machine_mapping_problem_tree.dtg.h"
#include "compiler/machine_mapping/machine_mapping_problem_tree/mm_problem_tree_parallel_split.dtg.h"
#include "compiler/machine_mapping/machine_mapping_problem_tree/mm_problem_tree_series_split.dtg.h"
#include "compiler/machine_mapping/machine_mapping_problem_tree/unmapped_op_cost_estimate_key.dtg.h"
#include "utils/full_binary_tree/binary_tree_path.dtg.h"
#include "utils/graph/series_parallel/binary_sp_decomposition_tree/generic_binary_sp_decomposition_tree/generic_binary_sp_decomposition_tree_implementation.dtg.h"
#include "utils/graph/series_parallel/sp_decomposition_tree_node_type.dtg.h"
Expand All @@ -27,6 +28,9 @@ std::optional<MachineMappingProblemTree>
mm_problem_tree_get_subtree_at_path(MachineMappingProblemTree const &,
BinaryTreePath const &);

std::string as_dot(MachineMappingProblemTree const &);
void debug_print_dot(MachineMappingProblemTree const &);

} // namespace FlexFlow

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ includes = [
"op-attrs/parallel_tensor_shape.dtg.h",
"<vector>",
"pcg/machine_view.dtg.h",
"pcg/operator_task_space.dtg.h",
]

src_includes = [
Expand All @@ -34,3 +35,6 @@ type = "std::vector<::FlexFlow::ParallelTensorShape>"
name = "output_shapes"
type = "std::vector<::FlexFlow::ParallelTensorShape>"

[[fields]]
name = "op_task_space"
type = "::FlexFlow::OperatorTaskSpace"
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ FeasibleMachineMappingResult require_feasible(MachineMappingResult const &);
make_singleton_machine_mapping_result(float runtime,
MachineView const &machine_view);

[[nodiscard]] float get_runtime_cost(MachineMappingResult const &mm_result);

} // namespace FlexFlow

#endif
57 changes: 57 additions & 0 deletions lib/compiler/include/compiler/mcmc/generic_mcmc_algorithm.h
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> &current_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,
GeneratingFunc const &generator,
ScoringFunc const &scorer,
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 lib/compiler/include/compiler/mcmc/generic_mcmc_config.struct.toml
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"
27 changes: 27 additions & 0 deletions lib/compiler/include/compiler/mcmc/generic_mcmc_state.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#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)
: 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
22 changes: 22 additions & 0 deletions lib/compiler/include/compiler/mcmc/mcmc_over_mapped_pcg.h
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/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,
CostEstimator const &cost_estimator,
MachineSpecification const &resources,
MCMCOverMappedPCGConfig const &search_config);

} // namespace FlexFlow

#endif
Loading
Loading