也可能需要将自定义类从自定义 C++ 类实例移入或移出`IValue`, such as when you take or return IValues from TorchScript methods or you want to instantiate a custom class attribute in C++. For creating an IValue:
也可能需要将自定义类从自定义 C++ 类实例移入或移出`IValue`, such as when you take or return IValues from TorchScript methods or you want to instantiate a custom class attribute in C++. For creating an IValue:
现在,我们准备定义两个模型碎片。 对于构造函数,我们只需将所有 ResNet50 层分为两部分,然后将每个部分移至提供的设备中。 两个分片的`forward`函数获取输入数据的`RRef`,在本地获取数据,然后将其移至所需的设备。 将所有层应用于输入后,它将输出移至 CPU 并返回。 这是因为当调用方和被调用方中的设备数量不匹配时,RPC API 要求张量驻留在 CPU 上,以避免无效的设备错误。
现在,我们准备定义两个模型碎片。 对于构造器,我们只需将所有 ResNet50 层分为两部分,然后将每个部分移至提供的设备中。 两个分片的`forward`函数获取输入数据的`RRef`,在本地获取数据,然后将其移至所需的设备。 将所有层应用于输入后,它将输出移至 CPU 并返回。 这是因为当调用方和被调用方中的设备数量不匹配时,RPC API 要求张量驻留在 CPU 上,以避免无效的设备错误。
```py
```py
classResNetShard1(ResNetBase):
classResNetShard1(ResNetBase):
...
@@ -138,7 +138,7 @@ class ResNetShard2(ResNetBase):
...
@@ -138,7 +138,7 @@ class ResNetShard2(ResNetBase):