Repository: pytorch
Issue: [caffe2]train on GPU and test on cpu failed
Labels: caffe2

I am learning caffe2. I want to train on a Machine with GPU, and I want to test the trained model on an ARM  cpu without GPU.
I tried the cifar10 tuorial on
[https://github.com/caffe2/tutorials/blob/master/CIFAR10_Part1.ipynb](url)
to speed up the training , I use GPU mode while training
```
with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)):
	data, label = AddInput(
		train_model, batch_size=training_net_batch_size,
		db=training_lmdb_path,
		db_type='lmdb')
	# Add model definition, save return value to 'softmax' variable
	softmax = Add_Original_CIFAR10_Model(train_model, data, num_classes, image_height, image_width, image_channels)
	# Add training operators using the softmax output from the model
	AddTrainingOperators(train_model, softmax, label)
	# Add periodic checkpoint outputs to the model
	AddCheckpoints(train_model, checkpoint_iters, db_type="lmdb")

with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)):
	data, label = AddInput(
		val_model, batch_size=validation_images,
		db=validation_lmdb_path,
		db_type='lmdb')
	# Add model definition, save return value to 'softmax' variable
	softmax = Add_Original_CIFAR10_Model(val_model, data, num_classes, image_height, image_width, image_channels)
	# Add accuracy operator
	AddAccuracy(val_model, softmax, label)

with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)):
	Add_Original_CIFAR10_Model(deploy_model, "data", num_classes, image_height, image_width, image_channels)

```
the trained model is saved in
```
/home/ice/caffe2_notebooks/tutorial_files/tutorial_cifar10/2018-07-18_16-11-41/
```
the directory's contents is like this:
```
cifar10_checkpoint_01000.lmdb  cifar10_checkpoint_02000.lmdb  cifar10_init_net.pb  cifar10_predict_net.pb
```
Then I followed this
[https://github.com/caffe2/tutorials/blob/master/CIFAR10_Part2.ipynb](url)
to test the saved model trained just before.
But I got error
```
Success, you may continue!
WARNING: Logging before InitGoogleLogging() is written to STDERR
W0718 19:45:00.070950 20855 init.h:99] Caffe2 GlobalInit should be run before any other API calls.
W0718 19:45:00.071094 20855 init.h:99] Caffe2 GlobalInit should be run before any other API calls.
I0718 19:45:00.194257 20855 operator.cc:169] Engine CUDNN is not available for operator MaxPool.
I0718 19:45:00.194357 20855 operator.cc:169] Engine CUDNN is not available for operator AveragePool.
I0718 19:45:00.194423 20855 operator.cc:169] Engine CUDNN is not available for operator AveragePool.
Original python traceback for operator `4` in network `test_model` in exception above (most recent call last):
Traceback (most recent call last):
  File "train_gpu_pred_cpu.py", line 147, in <module>
    workspace.RunNet(test_model.net)
  File "/usr/local/pytorch/lib64/python3.6/site-packages/caffe2/python/workspace.py", line 217, in RunNet
    StringifyNetName(name), num_iter, allow_fail,
  File "/usr/local/pytorch/lib64/python3.6/site-packages/caffe2/python/workspace.py", line 178, in CallWithExceptionIntercept
    return func(*args, **kwargs)
RuntimeError: [enforce fail at blob.h:84] IsType<T>(). wrong type for the Blob instance. Blob contains caffe2::Tensor<caffe2::CPUContext> while caller expects caffe2::Tensor<caffe2::CUDAContext> .
Offending Blob name: data.
Error from operator: 
input: "data" input: "conv1_w" input: "conv1_b" output: "conv1" name: "" type: "Conv" arg { name: "kernel" i: 5 } arg { name: "order" s: "NCHW" } arg { name: "stride" i: 1 } arg { name: "pad" i: 2 } arg { name: "exhaustive_search" i: 0 } device_option { device_type: 1 cuda_gpu_id: 0 } engine: "CUDNN"

```
I have tried:
```
INIT_NET = "/home/ice/caffe2_notebooks/tutorial_files/tutorial_cifar10/2018-07-18_16-11-41/cifar10_init_net.pb" # GPU trained
PREDICT_NET = "/home/ice/caffe2_notebooks/tutorial_files/tutorial_cifar10/2018-07-18_16-11-41/cifar10_predict_net.pb" # GPU trained

device_opts = core.DeviceOption(caffe2_pb2.CPU, 0)
arg_scope = {"order": "NCHW"}
test_model = model_helper.ModelHelper(name="test_model", arg_scope=arg_scope, init_params=False)

# Add the data input layer to the model, pointing at the TEST_LMDB
data,_ = AddInputLayer(test_model,1,TEST_LMDB,'lmdb')
# In [5]:
# Populate the model helper obj with the init net stuff, which provides the 
#    weight initializations for the model
init_net_proto = caffe2_pb2.NetDef()
with open(INIT_NET, "rb") as f:
    init_net_proto.ParseFromString(f.read())
    init_net_proto.device_option.CopyFrom(device_opts)
with core.DeviceScope(core.DeviceOption(caffe2_pb2.CPU, 0)):
    test_model.param_init_net = test_model.param_init_net.AppendNet(core.Net(init_net_proto))

# Populate the model helper obj with the predict net stuff, which defines 
#    the structure of the model
predict_net_proto = caffe2_pb2.NetDef()
with open(PREDICT_NET, "rb") as f:
    predict_net_proto.ParseFromString(f.read())
    predict_net_proto.device_option.CopyFrom(device_opts)
with core.DeviceScope(core.DeviceOption(caffe2_pb2.CPU, 0)):
    test_model.net = test_model.net.AppendNet(core.Net(predict_net_proto))

# Add an accuracy feature to the model for convenient reporting during testing
accuracy = brew.accuracy(test_model, ['softmax', 'label' ], 'accuracy')
# In [6]:
# Run the param init net to put the trained model info into the workspace
workspace.RunNetOnce(test_model.param_init_net)
workspace.CreateNet(test_model.net, overwrite=True)

```
But the error still exists.So, what's the right way to load a model trained in GPU mode, and test on CPU mode? Thanks.