blob: 211d8c44d51894b64d3acecd0a25751d22780de3 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
* Copyright (c) 2016 by Contributors
* \file caffe_common.h
* \brief Common functions for caffeOp and caffeLoss symbols
* \author Haoran Wang
*/
#ifndef PLUGIN_CAFFE_CAFFE_COMMON_H_
#define PLUGIN_CAFFE_CAFFE_COMMON_H_
#include <mxnet/operator.h>
#include <dmlc/type_traits.h>
#include <caffe/proto/caffe.pb.h>
#include <vector>
#include <iostream>
#include <exception>
#include <caffe/layer.hpp>
#include <caffe/blob.hpp>
#include <caffe/layer_factory.hpp>
namespace mxnet {
namespace op {
namespace caffe {
/**
* \brief The class sets caffe's mode before doing forward/backward
* \tparam xpu The device that the op will be executed on.
*/
class CaffeMode {
public:
template<typename xpu> static void SetMode();
};
// Initialization funciton called by caffeOp & caffeLoss
template<typename Dtype>
void InitCaffeBlobs(std::vector< ::caffe::Blob<Dtype>*>* v, int n_num) {
for (index_t i=0; i < n_num; ++i)
v->push_back(new ::caffe::Blob<Dtype>());
}
template<typename Dtype>
void DelCaffeBlobs(std::vector< ::caffe::Blob<Dtype>*>* v, int n_num) {
for (index_t i=0; i < n_num; ++i)
delete v->at(i);
}
struct NULLDeleter {template<typename T> void operator()(T*){}};
template <typename Dtype>
void Deleter(::caffe::Layer<Dtype> *ptr) {
}
template <typename Dtype>
class LayerRegistry {
public:
static ::caffe::Layer<Dtype> * CreateLayer(const ::caffe::LayerParameter& param) {
::caffe::shared_ptr< ::caffe::Layer<Dtype> > ptr =
::caffe::LayerRegistry<Dtype>::CreateLayer(param);
// avoid caffe::layer destructor, which deletes the weights layer owns
new ::caffe::shared_ptr< ::caffe::Layer<Dtype> >(ptr);
return ptr.get();
}
};
} // namespace caffe
} // namespace op
} // namespace mxnet
/*! \brief override type_name for caffe::LayerParameter */
namespace dmlc {
DMLC_DECLARE_TYPE_NAME(::caffe::LayerParameter, "caffe-layer-parameter");
}
#endif // PLUGIN_CAFFE_CAFFE_COMMON_H_