torch model trained with GPU be used in a CPU-only environment

7,273 views
Skip to first unread message

Xuwang Yin

unread,
Jun 10, 2015, 11:55:16ā€ÆAM6/10/15
to tor...@googlegroups.com
I have a torch model trained with GPU, now i want to use this model in a CPU-only environment.
Is it possible, if so, how can i do that? thanks!

soumith

unread,
Jun 10, 2015, 11:57:08ā€ÆAM6/10/15
to torch7 on behalf of Xuwang Yin
model = model:float()

That's it, you should be all set.

On Wed, Jun 10, 2015 at 11:55 AM, Xuwang Yin via torch7 <torch7+APn2wQcrd_8OQt5Q0FoODsOgr...@googlegroups.com> wrote:
I have a torch model trained with GPU, now i want to use this model in a CPU-only environment.
Is it possible, if so, how can i do that? thanks!

--
You received this message because you are subscribed to the Google Groups "torch7" group.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+un...@googlegroups.com.
To post to this group, send email to tor...@googlegroups.com.
Visit this group at http://groups.google.com/group/torch7.
For more options, visit https://groups.google.com/d/optout.

Yossi Biton

unread,
Jun 13, 2015, 5:18:16ā€ÆAM6/13/15
to tor...@googlegroups.com
will work unless you use some packages with cuda support only (ccn2 or inn for example).


On Wednesday, June 10, 2015 at 6:57:08 PM UTC+3, smth chntla wrote:
model = model:float()

That's it, you should be all set.
Message has been deleted

Xuwang Yin

unread,
Jun 13, 2015, 9:40:42ā€ÆAM6/13/15
to tor...@googlegroups.com
I think there might be some issues here.
My experiments showed that to load a GPU-trained nn.Sequential model,Ā 
you have to first "require 'cunn'", otherwise it won't load ("requre 'nn'" only): "torch/install/share/lua/5.1/torch/File.lua:249: unknown Torch class <torch.CudaTensor>".Ā 
In the training code i found that when you trained a nn.Sequential model on GPU, you have to first "convert" the model to GPU:Ā 
-- CUDA?
if opt.type == 'cuda' then
Ā  Ā model:cuda()
Ā  Ā criterion:cuda()
end
I think that after the model being "converted", you have to require cunn to load the model.Ā 
So, even you want to use the model on CPU, you have to get a NVIDIA card and have CUDA installed, which,Ā 
will makes running the model on CPU pointless.

Any suggestions?


åœØ 2015幓6꜈10ę—„ę˜ŸęœŸäø‰ UTC+8äø‹åˆ11:57:08ļ¼Œsmth chntla写道ļ¼š
model = model:float()

That's it, you should be all set.

Yossi Biton

unread,
Jun 13, 2015, 11:00:52ā€ÆAM6/13/15
to tor...@googlegroups.com
convert your model to float/double before saving it :
model:float()

now you don't have to require 'cunn' when loading it.

Xuwang Yin

unread,
Jun 13, 2015, 11:35:03ā€ÆPM6/13/15
to torch7 on behalf of Yossi Biton
Yes, it works, and if you didn't save the model as float during the training process, you can use 'cunn' to load the model, convert it to float and then save it. Nice work

On Sat, Jun 13, 2015 at 11:00 PM, Yossi Biton via torch7 <torch7+APn2wQezX724pTV9rC5uq5YnV...@googlegroups.com> wrote:
convert your model to float/double before saving it :
model:float()

now you don't have to require 'cunn' when loading it.


On Saturday, June 13, 2015 at 4:40:42 PM UTC+3, Xuwang Yin wrote:
I think there might be some issues here.
My experiments showed that to load a GPU-trained nn.Sequential model,Ā 
you have to first "require 'cunn'", otherwise it won't load ("requre 'nn'" only): "torch/install/share/lua/5.1/torch/File.lua:249: unknown Torch class <torch.CudaTensor>".Ā 
In the training code i found that when you trained a nn.Sequential model on GPU, you have to first "convert" the model to GPU:Ā 
-- CUDA?
if opt.type == 'cuda' then
Ā  Ā model:cuda()
Ā  Ā criterion:cuda()
end
I think that after the model being "converted", you have to require cunn to load the model.Ā 
So, even you want to use the model on CPU, you have to get a NVIDIA card and have CUDA installed, which,Ā 
will makes running the model on CPU pointless.

Any suggestions?


åœØ 2015幓6꜈10ę—„ę˜ŸęœŸäø‰ UTC+8äø‹åˆ11:57:08ļ¼Œsmth chntla写道ļ¼š
model = model:float()

That's it, you should be all set.

On Wed, Jun 10, 2015 at 11:55 AM, Xuwang Yin via torch7 <torch7+APn2wQcrd_8OQt5Q0FoODsOgr...@googlegroups.com> wrote:
I have a torch model trained with GPU, now i want to use this model in a CPU-only environment.
Is it possible, if so, how can i do that? thanks!

--
You received this message because you are subscribed to the Google Groups "torch7" group.
To unsubscribe from this group and stop receiving emails from it, send an email to torch7+un...@googlegroups.com.
To post to this group, send email to tor...@googlegroups.com.
Visit this group at http://groups.google.com/group/torch7.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "torch7" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/torch7/ugBCwaoXw_s/unsubscribe.
To unsubscribe from this group and all its topics, send an email to torch7+un...@googlegroups.com.

ignorant

unread,
Oct 21, 2015, 3:18:37ā€ÆPM10/21/15
to torch7
Hi,

Is there a way to make this work for nets with inn module as well? For example, Alexnet fromĀ https://github.com/BVLC/caffe/tree/master/models/bvlc_reference_caffenet

I want to deploy the trained net for prediction on a cheaper AWS instance.

Thanks,


On Saturday, June 13, 2015 at 11:35:03 PM UTC-4, Xuwang Yin wrote:
Yes, it works, and if you didn't save the model as float during the training process, you can use 'cunn' to load the model, convert it to float and then save it. Nice work

Francisco Vitor Suzano Massa

unread,
Oct 21, 2015, 5:18:56ā€ÆPM10/21/15
to torch7
For the moment, no, because Alexnet
1. uses SpatialCrossResponseNormalization, which is not present in nn yet
2. uses groups=2 for convolution

There is an ongoing PR to add SpatialCrossResponseNormalization to nn https://github.com/torch/nn/pull/413 , but even after it gets merged nn doesn't support groups in convolution for the moment. This could be overcome by some network surgery though. I'll probably add support to groups in SpatialConvolution sometime after CVPR though (i.e., in ~3 weeks)

ignorant

unread,
Oct 21, 2015, 6:26:52ā€ÆPM10/21/15
to torch7
Thanks for the work!Ā 
I thought of taking care of it in loadcaffe next but saw responses inĀ https://github.com/szagoruyko/loadcaffe/issues/8 . So I guess I will wait for now.. or maybe use caffe in cpu mode for prediction till there is a resolution.

Francisco Vitor Suzano Massa

unread,
Oct 21, 2015, 6:49:07ā€ÆPM10/21/15
to torch7
Just for information, if you want to use groups in torch in CPU, I've hacked a simple function that takes care of that
https://github.com/fmassa/torch-nn/blob/master/SpatialConvolutionGroups.lua

But beware that I haven't tested it thoughtly, there might be bugs

vasg

unread,
Sep 9, 2016, 4:41:59ā€ÆAM9/9/16
to torch7
I have trained the model on a GPU, I tried loading it in GPU environment, and received unknown Torch class <torch.CudaTensor> error. This error didn't occur when I added "require 'cunn'" before loading the model.
Does anyone know the reason for this behaviour?

Thanks!

Vislab

unread,
Sep 9, 2016, 4:55:08ā€ÆAM9/9/16
to torch7
You have to require 'cunn' when loading models that have CudaTensors in them.

Venu Sharma

unread,
Dec 4, 2016, 3:09:19ā€ÆAM12/4/16
to torch7
Hi Ā Francisco Vitor Suzano Massa,

Ā  Ā  Do you have a PR for groups implementation in SpatialConvolution for nn? Ā 

Jon Pi

unread,
Apr 5, 2017, 5:38:47ā€ÆAM4/5/17
to torch7
Hey
Did anyone get aĀ 

attempt to call method 'double' (a nil value)

or

attempt to call method 'float' (a nil value)


when trying:
model = torch.load(modelName)
model
= model.float() Ā  Ā # model = model.double()

Thanks!

Jon Pi

unread,
Apr 5, 2017, 8:53:35ā€ÆAM4/5/17
to torch7
oops! I meant when trying:
modelĀ =Ā torch.load(modelName)
modelĀ 
=Ā model:float()Ā Ā  Ā # model = model:double()

Mika S

unread,
Apr 5, 2017, 6:37:39ā€ÆPM4/5/17
to torch7
Can you paste result of :
torch.type(model)

Wang Jie

unread,
Jun 11, 2017, 9:26:44ā€ÆAM6/11/17
to torch7
hello,i don't know how to use model = model:float(),please help.


åœØ 2015幓6꜈10ę—„ę˜ŸęœŸäø‰ UTC+8äø‹åˆ11:57:08ļ¼Œsmth chntla写道ļ¼š
model = model:float()

That's it, you should be all set.

Spandan Madan

unread,
Aug 6, 2018, 6:07:57ā€ÆPM8/6/18
to torch7
Hi Jon,

Did you end up finding a fix for this? I'm having the same issue.

Spandan Madan

unread,
Aug 6, 2018, 6:10:43ā€ÆPM8/6/18
to torch7
Here's what I try:

model = torch.load(ModelName)
model = model:float()

Here's the error-

[string "model_lineback = model_lineback:float()"]:1: attempt to call method 'float' (a nil value)
stack traceback:
Ā  Ā  Ā  Ā  [string "model_lineback = model_lineback:float()"]:1: in main chunk
Ā  Ā  Ā  Ā  [C]: in function 'xpcall'
Ā  Ā  Ā  Ā  /home/t-spmada/torch/install/share/lua/5.1/trepl/init.lua:679: in function 'repl'
Ā  Ā  Ā  Ā  ...mada/torch/install/lib/luarocks/rocks/trepl/scm-1/bin/th:204: in main chunk
Ā  Ā  Ā  Ā  [C]: at 0x00405d50
Reply all
Reply to author
Forward
0 new messages