/* Copyright (c) 2018 Anakin Authors, Inc. All Rights Reserved.

   Licensed 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.
*/
#include "framework/operators/box_clip.h"

namespace anakin {

namespace ops {

template<typename Ttype, Precision Ptype>
Status BoxClipHelper<Ttype, Ptype>::InitParam() {
    DLOG(WARNING) << "Parsing BoxClip op parameter.";
    auto is_ori_box=GET_PARAMETER_WITH_DEFAULT(bool,is_ori_box,true);
    BoxClipParam<Ttype> param_box_clip(is_ori_box);
    _param_box_clip = param_box_clip;
    return Status::OK();
}

template<typename Ttype, Precision Ptype>
Status BoxClipHelper<Ttype, Ptype>::Init(OpContext<Ttype>& ctx,
                                        const std::vector<Tensor4dPtr<Ttype> >& ins,
                                        std::vector<Tensor4dPtr<Ttype> >& outs) {
    SABER_CHECK(_funcs_concat.init(ins, outs, _param_box_clip, SPECIFY, SABER_IMPL, ctx));
    return Status::OK();
}

template<typename Ttype, Precision Ptype>
Status BoxClipHelper<Ttype, Ptype>::InferShape(const std::vector<Tensor4dPtr<Ttype>>& ins,
        std::vector<Tensor4dPtr<Ttype>>& outs) {
    SABER_CHECK(_funcs_concat.compute_output_shape(ins, outs, _param_box_clip));
    return Status::OK();
}


#define INSTANCE_CONCAT(Ttype, Ptype) \
template<> \
void BoxClip<Ttype, Ptype>::operator()(OpContext<Ttype>& ctx, \
        const std::vector<Tensor4dPtr<Ttype> >& ins, \
                std::vector<Tensor4dPtr<Ttype> >& outs) { \
    auto* impl = static_cast<BoxClipHelper<Ttype, Ptype>*>(this->_helper); \
    auto& param = \
        static_cast<BoxClipHelper<Ttype, Ptype>*>(this->_helper)->_param_box_clip; \
    impl->_funcs_concat(ins, outs, param, ctx); \
}

#ifdef USE_CUDA
INSTANCE_CONCAT(NV, Precision::FP32);
template class BoxClipHelper<NV, Precision::FP32>;
ANAKIN_REGISTER_OP_HELPER(BoxClip, BoxClipHelper, NV, Precision::FP32);
#endif

#ifdef AMD_GPU
INSTANCE_CONCAT(AMD, Precision::FP32);
template class BoxClipHelper<AMD, Precision::FP32>;
ANAKIN_REGISTER_OP_HELPER(BoxClip, BoxClipHelper, AMD, Precision::FP32);
#endif

#ifdef USE_ARM_PLACE
INSTANCE_CONCAT(ARM, Precision::FP32);
template class BoxClipHelper<ARM, Precision::FP32>;
ANAKIN_REGISTER_OP_HELPER(BoxClip, BoxClipHelper, ARM, Precision::FP32);
#endif

#if defined USE_X86_PLACE || defined BUILD_LITE
INSTANCE_CONCAT(X86, Precision::FP32);
template class BoxClipHelper<X86, Precision::FP32>;
ANAKIN_REGISTER_OP_HELPER(BoxClip, BoxClipHelper, X86, Precision::FP32);
#endif

//! register op
ANAKIN_REGISTER_OP(BoxClip)
.Doc("BoxClip operator")
#ifdef USE_CUDA
.__alias__<NV, Precision::FP32>("box_clip")
#endif
#ifdef USE_ARM_PLACE
.__alias__<ARM, Precision::FP32>("box_clip")
#endif
#if defined USE_X86_PLACE || defined BUILD_LITE
.__alias__<X86, Precision::FP32>("box_clip")
#endif
#ifdef AMD_GPU
.__alias__<AMD, Precision::FP32>("box_clip")
#endif
.num_in(2)
.num_out(1);

} /* namespace ops */

} /* namespace anakin */


