How could I transform weights from cputype to gputype?

nsjiow: <p>I write a network with libtorch and could run with cpu, but how could i use cuda?<br>
when i convert input to cuda device, error comes from that weights need cudatype as well. how could i use libtorch train my network from zero? that means i do not want use the weight trained by pytorch.</p>
<p>my code:<br>
Classifier.h<br>
<span class="hashtag">#include</span> &lt;torch/torch.h&gt;<br>
<span class="hashtag">#include</span> <br>
<span class="hashtag">#include</span> <br>
<span class="hashtag">#include</span> </p>
<p>using namespace std;</p>
<p>class Classifier: torch::nn::Module{<br>
public:<br>
Classifier();<br>
torch::Tensor forward(torch::Tensor x);<br>
void create_modules();<br>
void init_weights();<br>
void load_weights(const string&amp; s);<br>
private:<br>
torch::Device* _device;<br>
vector<a>torch::nn::Sequential</a> module_list;<br>
};</p>
<p>static const vector&lt;vector&lt;int64_t&gt;&gt; vec = {<br>
{1, 32, 2},<br>
{32, 64, 1},<br>
{64, 128, 2},<br>
{128, 128, 1},<br>
{128, 256, 2},<br>
{256, 256, 1},<br>
{256, 512, 2},<br>
{512, 512, 1},<br>
{512, 512, 1},<br>
{512, 512, 1},<br>
{512, 512, 1},<br>
{512, 512, 1},<br>
{512, 1024, 2},<br>
{1024, 1024, 1},<br>
{7},<br>
{1024, 10}<br>
};<br>
Classifier.cpp<br>
<span class="hashtag">#include</span> “Classifier.h”</p>
<p>torch::nn::Conv2dOptions conv_options(int64_t in_channels, int64_t out_channels, int64_t kernel_size,<br>
int64_t stride, int64_t padding=0, int64_t groups=1, bool with_bias=false)<br>
{<br>
torch::nn::Conv2dOptions conv_options = torch::nn::Conv2dOptions(in_channels, out_channels, kernel_size);<br>
conv_options.stride_ = stride;<br>
conv_options.padding_ = padding;<br>
conv_options.groups_ = groups;<br>
conv_options.with_bias_ = with_bias;<br>
return conv_options;<br>
}</p>
<p>torch::nn::BatchNormOptions bn_options(int64_t features)<br>
{<br>
torch::nn::BatchNormOptions bn_options = torch::nn::BatchNormOptions(features);<br>
bn_options.affine_ = true;<br>
bn_options.stateful_ = true;<br>
return bn_options;<br>
}</p>
<p>torch::nn::LinearOptions ln_options(int64_t in_channls, int64_t out_channles)<br>
{<br>
torch::nn::LinearOptions ln_options = torch::nn::LinearOptions(in_channls, out_channles);<br>
return ln_options;<br>
}</p>
<p>Classifier::Classifier(/*, torch::Device <em>device</em>/)<br>
{<br>
create_modules();<br>
}</p>
<p>struct Conv_bn: torch::nn::Module{<br>
torch::nn::Conv2d conv;<br>
torch::nn::BatchNorm bn;</p>
<pre><code>Conv_bn(int64_t in_channels, int64_t out_channels, int64_t stride):
    conv(conv_options(in_channels, out_channels, 3, stride, 1)),
    bn(bn_options(out_channels)) {}

torch::Tensor forward(torch::Tensor x)
{
    return torch::relu(bn(conv(x)));
}
</code></pre>
<p>};</p>
<p>struct Conv_dw: torch::nn::Module{<br>
torch::nn::Conv2d conv1, conv2;<br>
torch::nn::BatchNorm bn1, bn2;</p>
<pre><code>Conv_dw(int64_t in_channels, int64_t out_channels, int64_t stride):
    conv1(conv_options(in_channels, in_channels, 3, stride, 1, in_channels)),
    bn1(bn_options(in_channels)),
    conv2(conv_options(in_channels, out_channels, 1, 1)),
    bn2(bn_options(out_channels)) {}

torch::Tensor forward(torch::Tensor x)
{
    x = torch::relu(bn1(conv1(x)));
    return torch::relu(bn2(conv2(x)));
}
</code></pre>
<p>};</p>
<p>struct Avgpooling: torch::nn::Module{<br>
int64_t kernel_size;<br>
Avgpooling(int64_t n): kernel_size(n) {}<br>
torch::Tensor forward(torch::Tensor x)<br>
{<br>
return torch::avg_pool2d(x, kernel_size);<br>
}<br>
};</p>
<p>void Classifier::create_modules()<br>
{<br>
for(int i=0; i&lt;vec.size(); ++i)<br>
{<br>
torch::nn::Sequential module;<br>
if(i &lt; 1)<br>
{<br>
Conv_bn bn_layer(vec[i][0], vec[i][1], vec[i][2]);<br>
module-&gt;push_back(bn_layer);<br>
}<br>
else<br>
{<br>
if(vec[i].size() == 1)<br>
{<br>
Avgpooling avgpooling(vec[i][0]);<br>
module-&gt;push_back(avgpooling);<br>
}<br>
else if(vec[i].size() == 2)<br>
{<br>
torch::nn::Linear ln_layer(ln_options(vec[i][0], vec[i][1]));<br>
module-&gt;push_back(ln_layer);<br>
}<br>
else<br>
{<br>
Conv_dw dw_layer(vec[i][0], vec[i][1], vec[i][2]);<br>
module-&gt;push_back(dw_layer);<br>
}<br>
}<br>
module_list.push_back(module);<br>
}<br>
}</p>
<p>void Classifier::init_weights()<br>
{</p>
<p>}</p>
<p>torch::Tensor Classifier::forward(torch::Tensor x)<br>
{<br>
for(int i=0; i&lt;vec.size(); ++i)<br>
{<br>
if(vec[i].size() == 2)<br>
x = x.view({x.size(0), -1});<br>
torch::nn::SequentialImpl *seq_imp = dynamic_cast&lt;torch::nn::SequentialImpl *&gt;(module_list[i].ptr().get());<br>
x = seq_imp-&gt;forward(x);<br>
}<br>
return x;<br>
}</p>

yf225: <p>Could you try <code>model-&gt;to(torch::kCUDA)</code> to convert your model to CUDA?</p>

nsjiow: <p>I did not use JIT, network was written by libtorch with c++ directly</p>

yf225: <p>For libtorch models, we also need to use <code>model-&gt;to(torch::kCUDA)</code> to convert the model to CUDA. See <a href="https://github.com/pytorch/pytorch/blob/505fa83b2f4b02b55784c94ba52d4ab41d351623/test/cpp/api/modules.cpp#L295-L310" rel="nofollow noopener">https://github.com/pytorch/pytorch/blob/505fa83b2f4b02b55784c94ba52d4ab41d351623/test/cpp/api/modules.cpp#L295-L310</a>.</p>

AlconDivino: <p>I am facing a similar porblem.<br>
i tried using<br>
<code>model-&gt;to(torch::kCUDA)</code><br>
and<br>
<code>model-&gt;to(torch::Device(torch::kCUDA, 0)</code><br>
but none of them work.</p>
<p>I get the error message</p>
<pre><code class="lang-auto">Input type (CUDAFloatType) and weight type (CPUFloatType) should be the same (_convolution at ../../aten/src/ATen/native/Convolution.cpp:599)
</code></pre>
<p>any idea how to fix this?</p>