Kolkir/mlcpp

[Need Pytorch C++ API Usage Support]

vinayak618 opened this issue · 1 comments

Hi.
Thank you so much for the C++ frontend example.
I'm in process of converting the python module to C++ for performance improvements.
Any clue how can i write the below code in C++.

`
class CBN(nn.Module):

 def __init__(self, num_features):
    super(CBN, self).__init__()
    self.bn = BatchNorm2d(num_features)

def forward(self, x):
    bn = self.bn(x)
    return bn

def __call__(self, x):
    return self.forward(x)`

`class BCN(nn.Module):

def __init__(self):
    super(BCN, self).__init__()
    self.layer = nn.Sequential()
    self.layer.add_module('bn',CBN(in_channels))
    self.layer.add_module('conv',nn.Conv2d(1,1,1, stride=1,
                                               padding=1, bias=True))
    self.layer.add_module('ReLU', nn.ReLU(inplace=True))

def forward(self, x):
    return self.layer(x.contiguous())

def __call__(self, x):
    return self.forward(x)`

@Kolkir
Any help would be great.
Thank you

Hello, there are a lot of samples of how to implement PyTorch modules in this repository.
You can start with such code:

class CBN public torch::nn::Module {
 public:
  CBN(int num_features):
    bn(torch::nn::BatchNormOptions(num_features)) {
    register_module("bn", bn);
  }

  torch::Tensor forward(torch::Tensor x) {
    return bn->forward(x);
  }

 private:
  torch::nn::BatchNorm bn{nullptr};
};

if you can't find an answer in the PyToch documentation and samples you can just look in the source code, how python functions are implemented.
Also please use PyTorch forum for such questions.