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

New MachineView representation #1458

Merged
merged 40 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
15dd787
containers helper functions
Aug 6, 2024
1f43d69
Additional support for unordered_multiset
Aug 6, 2024
0f2cb56
format fix
Aug 6, 2024
1eeeba8
Unordered Machine Mapping and adjacent changes
Aug 7, 2024
53dca87
repo-refactor merge
Aug 7, 2024
d3f1657
formatting
Aug 7, 2024
67a94a2
Minor fixes
Aug 9, 2024
5a5d276
Update to StridedRectangle interface
Aug 9, 2024
b302903
Minor updates
Aug 9, 2024
775cb90
added get_allowed_machine_views
Aug 10, 2024
c0c1c00
formatting
Aug 10, 2024
fdd556e
minor fix
Aug 10, 2024
075f4de
Added StartInvariantMachineView
Aug 10, 2024
878954f
formatting
Aug 10, 2024
340f441
Containers fix
Aug 13, 2024
1e8fa90
Implemented tensor to machine view injection
Aug 13, 2024
6e6adaa
small refactor
Aug 13, 2024
4b8600d
formatting
Aug 13, 2024
94e2dc2
Merge branch 'repo-refactor' into unordered-machine-view
lockshaw Aug 22, 2024
c9532d1
Cleaning Up
Aug 28, 2024
7d078a1
Formatting fix
Aug 28, 2024
40ab5ef
new machine-view interface
Sep 9, 2024
f7a50d4
repo-refactor merge
Sep 13, 2024
3cbc6be
update to allowed machine views
Sep 13, 2024
7eff4f5
PR review fixes
Sep 13, 2024
21ca265
update to machine view and getting allowed machine view to match new …
Sep 15, 2024
0810bce
merge with `repo-refactor`
Oct 4, 2024
0385e01
formatting
Oct 4, 2024
b438b49
minor fix
Oct 4, 2024
52b7a26
PR fixes
Oct 5, 2024
3a5fbf4
PR fixes
Oct 5, 2024
c1e1c8c
machineview interface change
Oct 5, 2024
5cc2a2f
Minor PR fixes
Oct 9, 2024
e1cd5a2
.cc machine view fixes + added StartInvariantMachineView
Oct 9, 2024
1744555
Merge remote-tracking branch 'origin/repo-refactor' into pietro-machi…
lockshaw Oct 9, 2024
93f9bb4
minor PR fixes
Oct 9, 2024
3c3518a
minor fixes
Oct 9, 2024
9fc8712
Post-merge fixes
lockshaw Oct 9, 2024
aa2bb4b
Merge remote-tracking branch 'refs/remotes/Marsella8/unordered-machin…
lockshaw Oct 9, 2024
20f6a75
Format
lockshaw Oct 9, 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
29 changes: 29 additions & 0 deletions lib/compiler/include/compiler/allowed_machine_views.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef _FLEXFLOW_COMPILER_ALLOWED_MACHINE_VIEWS_H
#define _FLEXFLOW_COMPILER_ALLOWED_MACHINE_VIEWS_H

#include "compiler/tensor_to_machine_view_injection.dtg.h"
#include "op-attrs/parallel_tensor_shape.dtg.h"
#include "pcg/machine_specification.h"
#include "pcg/machine_view.h"
#include "pcg/start_invariant_machine_view.dtg.h"

namespace FlexFlow {

std::unordered_set<MachineView>
get_allowed_machine_views(MachineSpecification const &machine_spec,
ParallelTensorShape const &shape,
DeviceType device_type = DeviceType::GPU);

std::unordered_set<StartInvariantMachineView>
get_allowed_start_invariant_machine_views(
MachineSpecification const &machine_spec,
ParallelTensorShape const &shape,
DeviceType device_type = DeviceType::GPU);

std::unordered_set<TensorToMachineViewInjection>
get_all_tensor_to_machine_view_injections(MachineView const &mv,
ParallelTensorShape const &shape);

} // namespace FlexFlow

#endif
13 changes: 0 additions & 13 deletions lib/compiler/include/compiler/machine_mapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include "compiler/machine_mapping.dtg.h"
#include "compiler/optimal_cost_state.dtg.h"
#include "cost_estimate.h"
#include "pcg/machine_specification.dtg.h"
#include "pcg/machine_specification.h"
#include "pcg/machine_view.h"
#include "pcg/parallel_computation_graph/parallel_computation_graph.h"
#include "substitutions/sub_parallel_computation_graph.h"
Expand Down Expand Up @@ -55,15 +53,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace = "FlexFlow"
name = "TensorToMachineViewInjection"
features = [
"eq",
"hash",
"fmt",
]

includes = [
"pcg/machine_view_dim_idx.dtg.h",
"op-attrs/parallel_tensor_dim_idx.dtg.h",
"utils/bidict/bidict.h",
"utils/hash/unordered_map.h"
]

[[fields]]
name = "raw_bidict"
type = "::FlexFlow::bidict<::FlexFlow::machine_view_dim_idx, ::FlexFlow::parallel_tensor_dim_idx>"
191 changes: 191 additions & 0 deletions lib/compiler/src/compiler/allowed_machine_views.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#include "compiler/allowed_machine_views.h"
#include "op-attrs/parallel_tensor_dims.h"
#include "op-attrs/parallel_tensor_shape.h"
#include "pcg/machine_specification.h"
#include "pcg/machine_view.h"
#include "pcg/start_invariant_machine_view.h"
#include "utils/containers/all_of.h"
#include "utils/containers/cartesian_product.h"
#include "utils/containers/extend.h"
#include "utils/containers/filter.h"
#include "utils/containers/permutations.h"
#include "utils/containers/product.h"
#include "utils/containers/range.h"
#include "utils/containers/replicate.h"
#include "utils/containers/sorted.h"
#include "utils/containers/transform.h"
#include "utils/containers/unordered_set_of.h"
#include "utils/containers/without_order.h"
#include "utils/containers/zip.h"
#include "utils/graph/serial_parallel/serial_parallel_decomposition.h"
#include "utils/overload.h"

namespace FlexFlow {

static std::unordered_multiset<int>
get_unordered_tensor_parallel_degrees(ParallelTensorShape const &shape) {
std::unordered_multiset<int> degrees =
without_order(ff_ordered_shard_degrees(shape));
degrees.insert(get_sum_degree(shape));
degrees.insert(get_discard_copy_degree(shape));

Check warning on line 30 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L26-L30

Added lines #L26 - L30 were not covered by tests
// filtering non-parallel dims
degrees = filter(degrees, [](int degree) { return degree != 1; });
return degrees;

Check warning on line 33 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L32-L33

Added lines #L32 - L33 were not covered by tests
}

bool is_valid_machine_view(MachineView const &mv,

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

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L36

Added line #L36 was not covered by tests
MachineSpecification const &machine_spec) {

int num_devices = get_num_devices(machine_spec, get_device_type(mv));
return (num_devices > get_raw_id(get_last_device_id(mv)));

Check warning on line 40 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L39-L40

Added lines #L39 - L40 were not covered by tests
}

bool is_valid_machine_view(MachineView const &mv,

Check warning on line 43 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L43

Added line #L43 was not covered by tests
ParallelTensorShape const &shape) {

std::vector<int> mv_degrees =
transform(get_num_devices_per_dim(mv),
[](num_points_t degree) { return degree.unwrapped; });
std::unordered_multiset<int> unordered_tensor_degrees =
get_unordered_tensor_parallel_degrees(shape);

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

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L46-L50

Added lines #L46 - L50 were not covered by tests

return without_order(mv_degrees) == unordered_tensor_degrees;

Check warning on line 52 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L52

Added line #L52 was not covered by tests
}

static std::unordered_set<MachineView>
get_candidate_machine_views(MachineSpecification const &machine_spec,

Check warning on line 56 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L56

Added line #L56 was not covered by tests
ParallelTensorShape const &shape,
DeviceType const &device_type) {

auto candidate_strides =
[](std::vector<int> const &tensor_dims,

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

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L60-L61

Added lines #L60 - L61 were not covered by tests
int total_devices) -> std::unordered_multiset<std::vector<stride_t>> {
int min_num_devices_with_full_stride_volume =
product(transform(tensor_dims, [](int degree) { return degree - 1; }));
int max_stride_upper_bound =
std::ceil(total_devices / min_num_devices_with_full_stride_volume);
std::vector<stride_t> single_stride_range =
transform(range(1, max_stride_upper_bound + 1),
[](int stride) { return stride_t(stride); });
std::unordered_multiset<std::vector<stride_t>> strides =
cartesian_product(replicate(tensor_dims.size(), single_stride_range));
return strides;
};

Check warning on line 73 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L63-L73

Added lines #L63 - L73 were not covered by tests

auto get_strided_rectangle = [](std::vector<stride_t> const &strides,

Check warning on line 75 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L75

Added line #L75 was not covered by tests
std::vector<int> const &num_points_per_dim) {
std::vector<StridedRectangleSide> sides =
transform(zip(num_points_per_dim, strides), [&](auto const &p) {
return StridedRectangleSide(num_points_t(p.first),
stride_t(p.second));
});
return StridedRectangle{sides};
};

Check warning on line 83 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L77-L83

Added lines #L77 - L83 were not covered by tests

std::unordered_multiset<int> tensor_dims =
get_unordered_tensor_parallel_degrees(shape);
int total_devices = get_num_devices(machine_spec, device_type);

Check warning on line 87 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L85-L87

Added lines #L85 - L87 were not covered by tests

std::unordered_set<MachineView> machine_views;
for (std::vector<stride_t> const &strides :
candidate_strides(sorted(tensor_dims), total_devices)) {
StridedRectangle rect = get_strided_rectangle(strides, sorted(tensor_dims));
auto start_inv_mv = StartInvariantMachineView{rect};

Check warning on line 93 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L89-L93

Added lines #L89 - L93 were not covered by tests
for (int start_id : range(total_devices)) {
device_id_t start_device = device_id_from_index(start_id, device_type);
machine_views.insert(
machine_view_from_start_invariant(start_inv_mv, start_device));

Check warning on line 97 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L95-L97

Added lines #L95 - L97 were not covered by tests
}
}
return machine_views;

Check warning on line 100 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L100

Added line #L100 was not covered by tests
}

std::unordered_set<MachineView>
get_allowed_machine_views(MachineSpecification const &machine_spec,

Check warning on line 104 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L104

Added line #L104 was not covered by tests
ParallelTensorShape const &shape,
DeviceType device_type) {

std::unordered_set<MachineView> views =
get_candidate_machine_views(machine_spec, shape, device_type);
return filter(views, [&](MachineView const &view) {
return is_valid_machine_view(view, shape) &&
is_valid_machine_view(view, machine_spec);
});

Check warning on line 113 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L108-L113

Added lines #L108 - L113 were not covered by tests
}

std::unordered_set<StartInvariantMachineView>
get_allowed_start_invariant_machine_views(

Check warning on line 117 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L117

Added line #L117 was not covered by tests
MachineSpecification const &machine_spec,
ParallelTensorShape const &shape,
DeviceType device_type) {
return transform(get_allowed_machine_views(machine_spec, shape, device_type),
start_invariant_from_machine_view);

Check warning on line 122 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L121-L122

Added lines #L121 - L122 were not covered by tests
}

ParallelDim get_parallel_dim_at_idx(ParallelTensorShape const &shape,

Check warning on line 125 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L125

Added line #L125 was not covered by tests
parallel_tensor_dim_idx idx) {
return idx.visit<ParallelDim>(
overload{[&](ff_dim_t shard_dim) {
return ParallelDim{shape.dims.shard_dims.at(shard_dim)};

Check warning on line 129 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L127-L129

Added lines #L127 - L129 were not covered by tests
},
[&](ReplicaType replica_type) {
ReplicaParallelDimSet replicas = shape.dims.replica_dims;
int degree = (ReplicaType::SUM == replica_type
? replicas.sum_degree.value

Check warning on line 134 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L131-L134

Added lines #L131 - L134 were not covered by tests
: replicas.discard_copy_degree.value);
return ParallelDim{ReplicaParallelDim{degree, replica_type}};
}});

Check warning on line 137 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L136-L137

Added lines #L136 - L137 were not covered by tests
}

std::unordered_set<parallel_tensor_dim_idx>
get_parallel_tensor_indices(ParallelTensorShape const &shape) {
std::unordered_set<parallel_tensor_dim_idx> indices;
extend(indices, transform(range(num_shard_dims(shape)), [](int idx) {
return parallel_tensor_dim_idx(ff_dim_t(idx));

Check warning on line 144 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L141-L144

Added lines #L141 - L144 were not covered by tests
}));
indices.insert(parallel_tensor_dim_idx(ReplicaType::SUM));
indices.insert(parallel_tensor_dim_idx(ReplicaType::DISCARD_COPY));
return indices;

Check warning on line 148 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L146-L148

Added lines #L146 - L148 were not covered by tests
}

std::unordered_set<machine_view_dim_idx>
get_machine_view_indices(MachineView const &mv) {
return transform(unordered_set_of(range(num_dims(mv))),
[](int idx) { return machine_view_dim_idx(idx); });

Check warning on line 154 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L152-L154

Added lines #L152 - L154 were not covered by tests
}

bool is_valid_injection(TensorToMachineViewInjection const &injection,

Check warning on line 157 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L157

Added line #L157 was not covered by tests
MachineView const &mv,
ParallelTensorShape const &shape) {
return all_of(injection.raw_bidict, [&](auto const pair) {
int mv_degree = get_side_at_idx(mv, pair.first).num_points.unwrapped;
int tensor_degree = get_degree(get_parallel_dim_at_idx(shape, pair.second));
return (tensor_degree == mv_degree);
});

Check warning on line 164 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L160-L164

Added lines #L160 - L164 were not covered by tests
}

std::unordered_set<TensorToMachineViewInjection>
get_all_tensor_to_machine_view_injections(

Check warning on line 168 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L168

Added line #L168 was not covered by tests
MachineView const &mv, ParallelTensorShape const &shape) {
assert(is_valid_machine_view(mv, shape));
std::unordered_set<machine_view_dim_idx> mv_indices =
get_machine_view_indices(mv);
std::unordered_set<parallel_tensor_dim_idx> shape_indices =
get_parallel_tensor_indices(shape);
shape_indices = filter(shape_indices, [&](auto const idx) {
return get_degree(get_parallel_dim_at_idx(shape, idx)) != 1;
});

Check warning on line 177 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L170-L177

Added lines #L170 - L177 were not covered by tests

std::unordered_set<TensorToMachineViewInjection> injections;
for (std::vector<parallel_tensor_dim_idx> const &p :
permutations(shape_indices)) {
TensorToMachineViewInjection injection =
TensorToMachineViewInjection(bidict(zip(sorted(mv_indices), p)));

Check warning on line 183 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L179-L183

Added lines #L179 - L183 were not covered by tests
if (is_valid_injection(injection, mv, shape)) {
injections.insert(injection);

Check warning on line 185 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L185

Added line #L185 was not covered by tests
}
}
return injections;

Check warning on line 188 in lib/compiler/src/compiler/allowed_machine_views.cc

View check run for this annotation

Codecov / codecov/patch

lib/compiler/src/compiler/allowed_machine_views.cc#L188

Added line #L188 was not covered by tests
}

} // namespace FlexFlow
Loading
Loading