1919
2020#include " event_flagcx.hpp"
2121#include " stream_guard_flagcx.hpp"
22+ #include " utils_flagcx.hpp"
2223
2324namespace c10d {
2425
@@ -74,8 +75,38 @@ class flagcxWork : public Work {
7475
7576class flagcxBackend : public Backend {
7677public:
78+ // TODO: check with all vendors to make sure their torch implementation support
79+ // backend options
80+ #if defined(USE_NVIDIA_ADAPTOR) || defined(USE_METAX_ADAPTOR)
81+ struct TuneObjectKey {
82+ std::string commOp;
83+ size_t nBytes;
84+
85+ bool operator <(const TuneObjectKey &other) const noexcept {
86+ if (other.commOp == commOp) {
87+ return nBytes < other.nBytes ;
88+ }
89+ return commOp < other.commOp ;
90+ }
91+ };
92+
93+ struct Options : Backend::Options {
94+ explicit Options (bool enableTuner = false );
95+
96+ static c10::intrusive_ptr<Options> create (bool enableTuner = false ) {
97+ return c10::make_intrusive<Options>(enableTuner);
98+ }
99+
100+ bool enableTuner{false };
101+ };
102+
103+ explicit flagcxBackend (
104+ const c10::intrusive_ptr<::c10d::Store> &store, int rank = -1 ,
105+ int size = -1 , c10::intrusive_ptr<Options> options = Options::create());
106+ #else
77107 explicit flagcxBackend (const c10::intrusive_ptr<::c10d::Store> &store,
78108 int rank = -1 , int size = -1 );
109+ #endif
79110
80111 ~flagcxBackend () override ;
81112
@@ -167,9 +198,18 @@ class flagcxBackend : public Backend {
167198 c10::intrusive_ptr<Work> recvAnysource (std::vector<at::Tensor> &tensors,
168199 int tag) override ;
169200
201+ #if defined(USE_NVIDIA_ADAPTOR) || defined(USE_METAX_ADAPTOR)
202+ void checkRecordingEnded ();
203+ void recordTuneObject (flagcxCommOp_t commOp, flagcxDataType_t dataType,
204+ size_t count);
205+ static c10::intrusive_ptr<Backend> createFlagcxBackend (
206+ c10d::DistributedBackendOptions backendOptions,
207+ c10::intrusive_ptr<Options> extraOptions = Options::create());
208+ #else
170209 static c10::intrusive_ptr<Backend>
171210 createFlagcxBackend (const c10::intrusive_ptr<::c10d::Store> &store, int rank,
172211 int size, const std::chrono::duration<float > &timeout);
212+ #endif
173213
174214 static void flagcxBackendConstructor () __attribute__((constructor)) {
175215 std::string devName = " cuda" ;
@@ -193,8 +233,14 @@ class flagcxBackend : public Backend {
193233 py::object module = py::module::import (" torch.distributed" );
194234 py::object registerBackend =
195235 module .attr (" Backend" ).attr (" register_backend" );
236+ #if defined(USE_NVIDIA_ADAPTOR) || defined(USE_METAX_ADAPTOR)
237+ registerBackend (" flagcx" , py::cpp_function (createFlagcxBackend),
238+ py::arg (" extended_api" ) = true ,
239+ py::arg (" devices" ) = py::make_tuple (devName));
240+ #else
196241 registerBackend (" flagcx" , py::cpp_function (createFlagcxBackend),
197242 py::arg (" devices" ) = py::make_tuple (devName));
243+ #endif
198244 }
199245
200246protected:
@@ -214,6 +260,15 @@ class flagcxBackend : public Backend {
214260 std::unordered_map<int , flagcxStream_t> flagcxStreams_;
215261 std::unordered_map<int , std::unique_ptr<flagcxEvent>> flagcxEvents_;
216262 flagcxHandlerGroup_t handler_ = nullptr ;
263+ #if defined(USE_NVIDIA_ADAPTOR) || defined(USE_METAX_ADAPTOR)
264+ const c10::intrusive_ptr<Options> options_;
265+ std::set<TuneObjectKey> tuneObjectSet_;
266+ // whether we finished recording tuning objects
267+ // a tuning object is a (commOp, nBytes) pair
268+ // we record the tuning objects that will occur in this communicator so that
269+ // flagcxTuner knows which communicator it is tuning
270+ bool recordingEnded = false ;
271+ #endif
217272#ifdef USE_ASCEND_ADAPTOR
218273 aclrtStream acl_stream;
219274#endif
0 commit comments