-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
communication.cc
148 lines (128 loc) · 4.89 KB
/
communication.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Copyright 2023 The JAX Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include <cstdint>
#include <memory>
#include <utility>
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/Visitors.h"
#include "mlir/Support/LogicalResult.h"
#include "jaxlib/mosaic/dialect/tpu/tpu_dialect.h"
#include "xla/layout.h"
namespace mlir::tpu {
namespace {
struct CommsAnalysisState {
bool has_communication = false;
bool has_custom_barrier = false;
explicit operator bool() { return has_communication && has_custom_barrier; }
};
void analyzeCrossChipCommunication(mlir::Operation *op,
CommsAnalysisState *state) {
if (auto dma = dyn_cast<tpu::EnqueueDMAOp>(op)) {
state->has_communication |= dma.getDeviceId() != nullptr;
} else if (auto signal = dyn_cast<tpu::SemaphoreSignalOp>(op)) {
state->has_communication |= signal.getDeviceId() != nullptr;
} else if (auto barrier = dyn_cast<tpu::GetBarrierSemaphoreOp>(op)) {
state->has_custom_barrier = true;
}
for (Region ®ion : op->getRegions()) {
for (Block &block : region.getBlocks()) {
for (Operation &op : block.getOperations()) {
analyzeCrossChipCommunication(&op, state);
if (*state) {
return;
}
}
}
}
}
} // namespace
std::pair<bool, bool> mightCommunicateBetweenChips(mlir::Operation *op) {
CommsAnalysisState state;
analyzeCrossChipCommunication(op, &state);
return std::make_pair(state.has_communication, state.has_custom_barrier);
}
#define GEN_PASS_DECL_LOGICALTOPHYSICALDEVICEIDPASS
#define GEN_PASS_DEF_LOGICALTOPHYSICALDEVICEIDPASS
#include "jaxlib/mosaic/dialect/tpu/tpu_passes.h.inc"
namespace {
template <typename Op>
void logicalToPhysicalDeviceIds(Op op, Value device_assignment) {
auto device_id = op.getDeviceIdMutable();
if (device_id.empty()) {
return;
}
CHECK_EQ(device_id.size(), 1);
mlir::OpBuilder builder(op);
auto logical_id = builder.create<arith::IndexCastOp>(
op.getLoc(), builder.getIndexType(), op.getDeviceId());
auto physical_id = builder.create<memref::LoadOp>(
op.getLoc(), device_assignment, ValueRange{logical_id});
device_id.assign(physical_id);
}
} // namespace
struct LogicalToPhysicalDeviceIdPass
: public impl::LogicalToPhysicalDeviceIdPassBase<
LogicalToPhysicalDeviceIdPass> {
explicit LogicalToPhysicalDeviceIdPass(int64_t total_devices_) {
total_devices = total_devices_;
}
void runOnOperation() override {
if (total_devices <= 0) {
signalPassFailure();
return;
}
func::FuncOp func = getOperation();
if (func.getName() == "main") {
auto device_assignment_type = MemRefType::get(
{total_devices}, IntegerType::get(func.getContext(), 32),
TiledLayoutAttr::get(func.getContext(), {xla::Tile({128})}, {1}),
MemorySpaceAttr::get(func.getContext(), MemorySpace::smem));
func.insertArgument(func.getNumArguments(), device_assignment_type,
nullptr, UnknownLoc::get(func.getContext()));
auto device_assignment_arg = func.getArgument(func.getNumArguments() - 1);
func.walk([device_assignment_arg](Operation *some_op) {
if (auto op = dyn_cast<tpu::EnqueueDMAOp>(some_op)) {
logicalToPhysicalDeviceIds(op, device_assignment_arg);
} else if (auto op = dyn_cast<tpu::SemaphoreSignalOp>(some_op)) {
logicalToPhysicalDeviceIds(op, device_assignment_arg);
}
});
} else {
auto result = func.walk([](Operation *some_op) {
auto fail = [some_op]() {
some_op->emitOpError(
"Communication ops are only allowed in the main function.");
return WalkResult::interrupt();
};
if (auto op = dyn_cast<tpu::EnqueueDMAOp>(some_op)) {
return fail();
}
if (auto op = dyn_cast<tpu::SemaphoreSignalOp>(some_op)) {
return fail();
}
return WalkResult::advance();
});
if (result.wasInterrupted()) {
signalPassFailure();
}
}
}
};
std::unique_ptr<OperationPass<func::FuncOp>>
createLogicalToPhysicalDeviceIdPass(int64_t total_devices) {
return std::make_unique<LogicalToPhysicalDeviceIdPass>(total_devices);
}
} // namespace mlir::tpu