Skip to content

Commit 643d5bf

Browse files
committed
Add comments for public functions and classes
ghstack-source-id: a0191f931e5aa7268f93c1fd8a69d0b6c98b5bc3 Pull Request resolved: #222
1 parent 8cea05b commit 643d5bf

File tree

7 files changed

+98
-7
lines changed

7 files changed

+98
-7
lines changed

multipy/runtime/deploy.h

+59-6
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ struct TORCH_API InterpreterSession {
5757
// InterpreterSession* I)' instead. We will have no backwards compatibility
5858
// guarentees for this function.
5959
ReplicatedObj createMovable(Obj obj);
60+
61+
// Converts a `ReplicatedObj` to an `Obj` on this InterpreterSession.
6062
Obj fromMovable(const ReplicatedObj& obj);
6163

6264
protected:
@@ -73,6 +75,9 @@ struct TORCH_API InterpreterSession {
7375
std::function<void()> deconstruction_callback_ = nullptr;
7476
};
7577

78+
// An `Interpreter` represents an invidual subinterpreter created by `torch::deploy`.
79+
// It allows for the creation of `InterpreterSession` objects which allow users to interact with
80+
// python objects.
7681
class TORCH_API Interpreter {
7782
private:
7883
void* handle_;
@@ -84,17 +89,22 @@ class TORCH_API Interpreter {
8489
multipy::optional<EmbeddedFile> torchPluginFile_;
8590

8691
public:
92+
// Create an Interpreter which is managed by `manager` and using the enviroment `env`
8793
Interpreter(InterpreterManager* manager, std::shared_ptr<Environment> env);
94+
95+
// Create an Interpreter manager using enviroment `env` which is not tied to an Interpreter Manager.
8896
explicit Interpreter(std::shared_ptr<Environment> env)
8997
: Interpreter(nullptr, env) {}
9098

99+
// Get a new `InterpreterSession` from this Interpreter.
91100
InterpreterSession acquireSession() const {
92101
if (manager_) {
93102
return InterpreterSession(pImpl_->acquireSession(), manager_);
94103
} else {
95104
return InterpreterSession(pImpl_->acquireSession());
96105
}
97106
}
107+
98108
~Interpreter();
99109
Interpreter(Interpreter&& rhs) noexcept
100110
: handle_(rhs.handle_),
@@ -113,17 +123,26 @@ class TORCH_API Interpreter {
113123

114124
struct Package;
115125

126+
// The default LoadBalancer for torch::deploy which handles allocating and freeing subinterpreters.
116127
struct TORCH_API LoadBalancer {
128+
129+
// create a Loadbalancer which handles `n` interpreters.
117130
explicit LoadBalancer(size_t n)
118131
: uses_(new uint64_t[8 * n]), allocated_(n), n_(n) {
119132
// 8*... to avoid false sharing of atomics on the same cache line
120133
memset(uses_.get(), 0, 8 * n_ * sizeof(uint64_t));
121134
}
135+
136+
// change the amount of subinterpreters which is handled by the load balancer.
122137
void setResourceLimit(size_t n) {
123138
MULTIPY_INTERNAL_ASSERT(n <= allocated_);
124139
n_ = n;
125140
}
141+
142+
// allocate an subinterpreter, and return its ID which is used to free it.
126143
int acquire();
144+
145+
// free the subinterpreter with ID `where`. This ID is returned by `LoadBalancer::acquire()`
127146
void free(int where);
128147

129148
private:
@@ -134,13 +153,18 @@ struct TORCH_API LoadBalancer {
134153
size_t n_;
135154
};
136155

156+
// An `InterpreterManager` handles the interaction of multiple subinterpreters such as allocating
157+
// subinterpreters, or load balancing the subinterpreters.
137158
struct TORCH_API InterpreterManager {
159+
160+
// constructor for `InterpreterManager` which takes the number of interpreters
161+
// (usually correlates to number of cores on your cpu), and a pointer to an `Enviroment`.
138162
explicit InterpreterManager(
139163
size_t nInterp = 2,
140164
std::shared_ptr<Environment> env = std::make_shared<NoopEnvironment>());
141165

142-
// get a free model, guarenteed that no other user of acquireOne has the same
143-
// model. It _is_ possible that other users will be using the interpreter.
166+
// get a free InterpreterSession, guarenteed that no other user of acquireOne has the same
167+
// InterpreterSession. It _is_ possible that other users will be using the interpreter.
144168
InterpreterSession acquireOne() {
145169
int where = resources_.acquire();
146170
InterpreterSession I = instances_[where].acquireSession();
@@ -154,11 +178,18 @@ struct TORCH_API InterpreterManager {
154178
at::ArrayRef<Interpreter> allInstances() {
155179
return instances_;
156180
}
181+
182+
// debugging tool to control the size of the loadBalancer
183+
// and change the number of interpreters on the fly
157184
void debugLimitInterpreters(size_t N) {
158185
AT_ASSERT(N <= instances_.size());
159186
resources_.setResourceLimit(N);
160187
}
188+
189+
// loads a package from a file with name `uri`
161190
Package loadPackage(const std::string& uri);
191+
192+
// loads a package from a `PyTorchStreamReader` or any class other which uses `ReadAdapterInterface`
162193
Package loadPackage(
163194
std::shared_ptr<caffe2::serialize::ReadAdapterInterface> reader);
164195

@@ -171,10 +202,12 @@ struct TORCH_API InterpreterManager {
171202
registeredModuleSource_[std::move(name)] = std::move(src);
172203
}
173204

174-
// Util function for debugging.
205+
// Util function for debugging which outputs the number of registered modules.
175206
size_t countRegisteredModuleSources() {
176207
return registeredModuleSource_.size();
177208
}
209+
210+
// Converts `obj` from on `InterpreterSession` I into a `ReplicatedObj`.
178211
ReplicatedObj createMovable(Obj obj, InterpreterSession* I);
179212
InterpreterManager(const InterpreterManager&) = delete;
180213
InterpreterManager& operator=(const InterpreterManager&) = delete;
@@ -189,6 +222,7 @@ struct TORCH_API InterpreterManager {
189222
std::unordered_map<std::string, std::string> registeredModuleSource_;
190223
};
191224

225+
// Underlying implementation for ReplicatedObj.
192226
struct TORCH_API ReplicatedObjImpl {
193227
ReplicatedObjImpl(
194228
size_t object_id,
@@ -204,32 +238,44 @@ struct TORCH_API ReplicatedObjImpl {
204238
InterpreterManager* manager_;
205239
};
206240

241+
// A python object which is Replicated from an `Obj` such that it is able to move around to different `InterpreterSessions`
242+
// by using `InterpreterSession::fromMovable(ReplicatedObj)`
207243
struct TORCH_API ReplicatedObj {
244+
245+
// default constructor for `ReplicatedObj`
208246
ReplicatedObj() : pImpl_(nullptr) {}
247+
248+
// Create an `InterpreterSession` using `onThisInterpreter`. If `onThisInterpreter` is
249+
// a `nullptr', then the associated `InterpreterManager` allocates it.
209250
InterpreterSession acquireSession(
210251
const Interpreter* onThisInterpreter = nullptr) const;
211252
at::IValue operator()(at::ArrayRef<at::IValue> args) const {
212253
auto I = acquireSession();
213254
return I.self(args).toIValue();
214255
}
215256

257+
// invokes `callKwargs` using the underlying python object, and returns the output as an `IValue`.
216258
[[nodiscard]] at::IValue callKwargs(
217259
std::vector<at::IValue> args,
218260
std::unordered_map<std::string, c10::IValue> kwargs) const {
219261
auto I = acquireSession();
220262
return I.self.callKwargs(std::move(args), std::move(kwargs)).toIValue();
221263
}
222264

265+
// invokes `callKwargs` using the underlying python object, and returns the output as an `IValue`.
223266
[[nodiscard]] at::IValue callKwargs(
224267
std::unordered_map<std::string, c10::IValue> kwargs) const {
225268
auto I = acquireSession();
226269
return I.self.callKwargs(std::move(kwargs)).toIValue();
227270
}
228271

272+
// invokes `hasattr` using the underlying python object, and returns the output as an `IValue`.
229273
[[nodiscard]] bool hasattr(const char* name) const {
230274
auto I = acquireSession();
231275
return I.self.hasattr(name);
232276
}
277+
278+
233279
void unload(const Interpreter* onThisInterpreter = nullptr);
234280
Obj toObj(InterpreterSession* I);
235281

@@ -242,21 +288,24 @@ struct TORCH_API ReplicatedObj {
242288
friend struct InterpreterManager;
243289
};
244290

291+
// PythonMethodWrapper is a more specific instance of a
292+
// ReplicatedObj which represents a python method, and
293+
// is therefore callable and has argument names accessible.
245294
class PythonMethodWrapper : public torch::IMethod {
246-
// PythonMethodWrapper is a more specific instance of a
247-
// ReplicatedObj which represents a python method, and
248-
// is therefore callable and has argument names accessible.
249295
public:
250296
// TODO(whc) make bound method pickleable, then directly construct from that
297+
251298
PythonMethodWrapper(
252299
torch::deploy::ReplicatedObj model,
253300
std::string methodName)
254301
: model_(std::move(model)), methodName_(std::move(methodName)) {}
255302

303+
// return the name of the python method.
256304
const std::string& name() const override {
257305
return methodName_;
258306
}
259307

308+
// overrides the `()` operater to call the underlying python method.
260309
c10::IValue operator()(
261310
std::vector<c10::IValue> args,
262311
const IValueMap& kwargs = IValueMap()) const override {
@@ -274,6 +323,7 @@ class PythonMethodWrapper : public torch::IMethod {
274323
std::string methodName_;
275324
};
276325

326+
// An object to encapsulate a `torch::package` which can act as part (or entire) enviroment for subinterpreters.
277327
struct TORCH_API Package {
278328
// shorthand for getting the object as a pickle resource in the package
279329
ReplicatedObj loadPickle(const std::string& module, const std::string& file) {
@@ -308,12 +358,15 @@ struct TORCH_API Package {
308358
}
309359
#endif
310360

361+
// allocate an `InterpreterSession` and load the appropriate torch.package with it.
311362
InterpreterSession acquireSession() {
312363
auto I = manager_->acquireOne();
313364
I.self =
314365
I.impl_->createOrGetPackageImporterFromContainerFile(containerFile_);
315366
return I;
316367
}
368+
369+
// Converts an `Obj` from `InterpreterSession` `I` into a `ReplicatedObj`.
317370
ReplicatedObj createMovable(Obj obj, InterpreterSession* I) {
318371
return manager_->createMovable(obj, I);
319372
}

multipy/runtime/elf_file.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ struct Section {
3535
}
3636
};
3737

38+
// TODO: consolidate other ELF file related functions in loader.cpp to this file
39+
3840
/*
3941
* This class provie utilities to handle ELF file. Only support 64bit ELF file.
4042
*/
41-
// TODO: consolidate other ELF file related functions in loader.cpp to this file
4243
class ElfFile {
4344
public:
4445
explicit ElfFile(const char* filename);

multipy/runtime/interpreter/interpreter_impl.h

+16
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ namespace deploy {
1919
struct InterpreterSessionImpl;
2020
struct Obj;
2121

22+
// Representation a Pickled Object
2223
struct PickledObject {
2324
std::string data_;
2425
std::vector<at::Storage> storages_;
@@ -28,6 +29,7 @@ struct PickledObject {
2829
std::shared_ptr<caffe2::serialize::PyTorchStreamReader> containerFile_;
2930
};
3031

32+
// The underlying implementation of `Obj` which holds the underlying `py::object`.
3133
struct InterpreterObj {
3234
friend struct Obj;
3335
friend struct ReplicatedObjImpl;
@@ -72,21 +74,34 @@ struct Obj {
7274
: isDefault_(false), baseObj_(baseObj) {}
7375
Obj() : isDefault_(true), baseObj_(nullptr) {}
7476

77+
// convert underlying `py::object` into an `IValue`.
7578
at::IValue toIValue() const;
79+
80+
// overwrite `()` to use `call` of the underlying `py::object` with `Obj`s as args.
81+
// The output is represented as an `Obj`.
7682
Obj operator()(at::ArrayRef<Obj> args);
83+
84+
// overwrite `()` to use `call` of the underlying `py::object` with `IValue`s as args.
85+
// The output is represented as an `Obj`.
7786
Obj operator()(at::ArrayRef<at::IValue> args);
87+
88+
// calls callKwargs from the underlying `py::object`
7889
Obj callKwargs(
7990
std::vector<at::IValue> args,
8091
std::unordered_map<std::string, c10::IValue> kwargs);
92+
// calls callKwargs from the underlying `py::object`. The output is represented as an `Obj`.
8193
Obj callKwargs(std::unordered_map<std::string, c10::IValue> kwargs);
94+
// calls hasattr from the underlying `py::object`. The output is represented as an `Obj`.
8295
bool hasattr(const char* attr);
96+
// calls attr from the underlying `py::object`. The output is represented as an `Obj`.
8397
Obj attr(const char* attr);
8498

8599
private:
86100
bool isDefault_;
87101
std::shared_ptr<InterpreterObj> baseObj_;
88102
};
89103

104+
// The underlying implementation of `InterpreterSession`
90105
struct InterpreterSessionImpl {
91106
friend struct Package;
92107
friend struct ReplicatedObj;
@@ -132,6 +147,7 @@ struct InterpreterSessionImpl {
132147
}
133148
};
134149

150+
// The underlying implementation of `Interpreter`
135151
struct InterpreterImpl {
136152
virtual InterpreterSessionImpl* acquireSession() = 0;
137153
virtual void setFindModule(

multipy/runtime/interpreter/plugin_registry.h

+15
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,36 @@ namespace py = pybind11;
1111

1212
namespace multipy {
1313

14+
// A `Converter` is used in order to convert `PyObject`s/`py::object` into
15+
// an `IValue` or some other representation usch as storage.
1416
class Converter {
1517
public:
1618
virtual ~Converter() = default;
1719

20+
// convert a `py::handle` to an `IValue`
1821
virtual multipy::optional<at::IValue> toTypeInferredIValue(
1922
py::handle input) = 0;
23+
24+
// convert an `IValue` into a `py::object`
2025
virtual multipy::optional<py::object> toPyObject(at::IValue ivalue) = 0;
26+
27+
// convert an `PyObject` into a `Storage`
2128
virtual multipy::optional<at::Storage> createStorage(PyObject* obj) = 0;
29+
30+
// create a `PyObject` from `storage`
2231
virtual multipy::optional<PyObject*> createPyObject(
2332
const at::Storage& storage) = 0;
33+
34+
// return the `THPDtype` of `scalarType`
2435
virtual multipy::optional<THPDtype*> getTHPDtype(
2536
at::ScalarType scalarType) = 0;
2637
};
2738

39+
// register a converter to be used by torch::deploy / multipy.
40+
// The order of the registration of the converters is dictated by the order of compilation.
2841
void registerConverter(Converter*);
42+
// deregister a converter from torch::deploy / multipy
43+
// The order of the deregistration of the converters is dictated by the order of compilation.
2944
void deregisterConverter(Converter*);
3045

3146
at::IValue toTypeInferredIValue(py::handle input);

multipy/runtime/mem_file.h

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ struct MemFile {
5151
[[nodiscard]] const char* data() const {
5252
return (const char*)mem_;
5353
}
54+
55+
// return the file descriptor of the underlying file.
5456
int valid() {
5557
return fcntl(fd_, F_GETFD) != -1 || errno != EBADF;
5658
}
@@ -62,6 +64,8 @@ struct MemFile {
6264
close(fd_);
6365
}
6466
}
67+
68+
// return the size of the underlying file defined by the `MemFile`
6569
size_t size() {
6670
return n_bytes_;
6771
}

multipy/runtime/noop_environment.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace torch {
1212
namespace deploy {
1313

14+
// An empty Enviroment
1415
class NoopEnvironment : public Environment {
1516
public:
1617
void configureInterpreter(Interpreter* /* interp */) override {}

multipy/runtime/path_environment.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace torch {
1313
namespace deploy {
1414

15+
// An Enviroment which is defined by a specific path to python code (ie. condas sitepackages)
1516
class PathEnvironment : public Environment {
1617
public:
1718
explicit PathEnvironment(std::string path) : path_(std::move(path)) {}

0 commit comments

Comments
 (0)