Skip to content

Commit d4acd99

Browse files
author
Jessica Lin
authored
Merge branch 'master' into tutorials_refresh
2 parents 232ebe1 + 8ac4017 commit d4acd99

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

advanced_source/torch_script_custom_classes.rst

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,13 @@ Now let's take a look at how we will make this class visible to TorchScript, a p
7878
// In general, you cannot register a non-specialized template
7979
// class. For non-templated classes, you can just pass the
8080
// class name directly as the template parameter.
81-
// - The single parameter to ``torch::class_()`` is a
82-
// string indicating the name of the class. This is the name
83-
// the class will appear as in both Python and TorchScript.
84-
// For example, our MyStackClass class would appear as ``torch.classes.MyStackClass``.
81+
// - The arguments passed to the constructor make up the "qualified name"
82+
// of the class. In this case, the registered class will appear in
83+
// Python and C++ as `torch.classes.my_classes.MyStackClass`. We call
84+
// the first argument the "namespace" and the second argument the
85+
// actual class name.
8586
static auto testStack =
86-
torch::class_<MyStackClass<std::string>>("MyStackClass")
87+
torch::class_<MyStackClass<std::string>>("my_classes", "MyStackClass")
8788
// The following line registers the contructor of our MyStackClass
8889
// class that takes a single `std::vector<std::string>` argument,
8990
// i.e. it exposes the C++ method `MyStackClass(std::vector<T> init)`.
@@ -217,7 +218,7 @@ demonstrates that:
217218
#
218219
# This instantiation will invoke the MyStackClass(std::vector<T> init) constructor
219220
# we registered earlier
220-
s = torch.classes.MyStackClass(["foo", "bar"])
221+
s = torch.classes.my_classes.MyStackClass(["foo", "bar"])
221222
222223
# We can call methods in Python
223224
s.push("pushed")
@@ -233,16 +234,16 @@ demonstrates that:
233234
# For now, we need to assign the class's type to a local in order to
234235
# annotate the type on the TorchScript function. This may change
235236
# in the future.
236-
MyStackClass = torch.classes.MyStackClass
237+
MyStackClass = torch.classes.my_classes.MyStackClass
237238
238239
@torch.jit.script
239240
def do_stacks(s : MyStackClass): # We can pass a custom class instance to TorchScript
240-
s2 = torch.classes.MyStackClass(["hi", "mom"]) # We can instantiate the class
241+
s2 = torch.classes.my_classes.MyStackClass(["hi", "mom"]) # We can instantiate the class
241242
s2.merge(s) # We can call a method on the class
242243
return s2.clone(), s2.top() # We can also return instances of the class
243244
# from TorchScript function/methods
244245
245-
stack, top = do_stacks(torch.classes.MyStackClass(["wow"]))
246+
stack, top = do_stacks(torch.classes.my_classes.MyStackClass(["wow"]))
246247
assert top == "wow"
247248
for expected in ["wow", "mom", "hi"]:
248249
assert stack.pop() == expected
@@ -265,7 +266,7 @@ instantiates and calls a method on our MyStackClass class:
265266
super().__init__()
266267
267268
def forward(self, s : str) -> str:
268-
stack = torch.classes.MyStackClass(["hi", "mom"])
269+
stack = torch.classes.my_classes.MyStackClass(["hi", "mom"])
269270
return stack.pop() + s
270271
271272
scripted_foo = torch.jit.script(Foo())
@@ -435,7 +436,7 @@ an attribute, you'll get the following error:
435436
class Foo(torch.nn.Module):
436437
def __init__(self):
437438
super().__init__()
438-
self.stack = torch.classes.MyStackClass(["just", "testing"])
439+
self.stack = torch.classes.my_classes.MyStackClass(["just", "testing"])
439440
440441
def forward(self, s : str) -> str:
441442
return self.stack.pop() + s
@@ -447,7 +448,7 @@ an attribute, you'll get the following error:
447448
.. code-block:: shell
448449
449450
$ python export_attr.py
450-
RuntimeError: Cannot serialize custom bound C++ class __torch__.torch.classes.MyStackClass. Please define serialization methods via def_pickle for this class. (pushIValueImpl at ../torch/csrc/jit/pickler.cpp:128)
451+
RuntimeError: Cannot serialize custom bound C++ class __torch__.torch.classes.my_classes.MyStackClass. Please define serialization methods via def_pickle for this class. (pushIValueImpl at ../torch/csrc/jit/pickler.cpp:128)
451452
452453
This is because TorchScript cannot automatically figure out what information
453454
save from your C++ class. You must specify that manually. The way to do that
@@ -466,7 +467,7 @@ Here is an example of how we can update the registration code for our
466467
.. code-block:: cpp
467468
468469
static auto testStack =
469-
torch::class_<MyStackClass<std::string>>("MyStackClass")
470+
torch::class_<MyStackClass<std::string>>("my_classes", "MyStackClass")
470471
.def(torch::init<std::vector<std::string>>())
471472
.def("top", [](const c10::intrusive_ptr<MyStackClass<std::string>>& self) {
472473
return self->stack_.back();
@@ -528,7 +529,7 @@ now run successfully:
528529
class Foo(torch.nn.Module):
529530
def __init__(self):
530531
super().__init__()
531-
self.stack = torch.classes.MyStackClass(["just", "testing"])
532+
self.stack = torch.classes.my_classes.MyStackClass(["just", "testing"])
532533
533534
def forward(self, s : str) -> str:
534535
return self.stack.pop() + s
@@ -562,7 +563,7 @@ example of how to do that:
562563
static auto instance_registry = torch::RegisterOperators().op(
563564
torch::RegisterOperators::options()
564565
.schema(
565-
"foo::manipulate_instance(__torch__.torch.classes.MyStackClass x) -> __torch__.torch.classes.MyStackClass Y")
566+
"foo::manipulate_instance(__torch__.torch.classes.my_classes.MyStackClass x) -> __torch__.torch.classes.my_classes.MyStackClass Y")
566567
.catchAllKernel<decltype(manipulate_instance), &manipulate_instance>());
567568
568569
Refer to the `custom op tutorial <https://pytorch.org/tutorials/advanced/torch_script_custom_ops.html>`_
@@ -575,7 +576,7 @@ Once this is done, you can use the op like the following example:
575576
class TryCustomOp(torch.nn.Module):
576577
def __init__(self):
577578
super(TryCustomOp, self).__init__()
578-
self.f = torch.classes.MyStackClass(["foo", "bar"])
579+
self.f = torch.classes.my_classes.MyStackClass(["foo", "bar"])
579580
580581
def forward(self):
581582
return torch.ops.foo.manipulate_instance(self.f)

intermediate_source/rpc_tutorial.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ Getting Started with Distributed RPC Framework
33
**Author**: `Shen Li <https://mrshenli.github.io/>`_
44

55

6-
.. warning::
7-
The `torch.distributed.rpc <https://pytorch.org/docs/master/rpc.html>`__ package
8-
is experimental and subject to change. It also requires PyTorch 1.4.0+ to run as this is the first version to support RPC.
9-
10-
116
This tutorial uses two simple examples to demonstrate how to build distributed
127
training with the `torch.distributed.rpc <https://pytorch.org/docs/master/rpc.html>`__
138
package which is first introduced as an experimental feature in PyTorch v1.4.

intermediate_source/torchvision_tutorial.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ should return:
3232
- ``boxes (FloatTensor[N, 4])``: the coordinates of the ``N``
3333
bounding boxes in ``[x0, y0, x1, y1]`` format, ranging from ``0``
3434
to ``W`` and ``0`` to ``H``
35-
- ``labels (Int64Tensor[N])``: the label for each bounding box
35+
- ``labels (Int64Tensor[N])``: the label for each bounding box. ``0`` represents always the background class.
3636
- ``image_id (Int64Tensor[1])``: an image identifier. It should be
3737
unique between all the images in the dataset, and is used during
3838
evaluation
@@ -56,6 +56,8 @@ If your model returns the above methods, they will make it work for both
5656
training and evaluation, and will use the evaluation scripts from
5757
``pycocotools``.
5858

59+
One note on the ``labels``. The model considers class ``0`` as background. If your dataset does not contain the background class, you should not have ``0`` in your ``labels``. For example, assuming you have just two classes, *cat* and *dog*, you can define ``1`` (not ``0``) to represent *cats* and ``2`` to represent *dogs*. So, for instance, if one of the images has booth classes, your ``labels`` tensor should look like ``[1,2]``.
60+
5961
Additionally, if you want to use aspect ratio grouping during training
6062
(so that each batch only contains images with similar aspect ratio),
6163
then it is recommended to also implement a ``get_height_and_width``

0 commit comments

Comments
 (0)