diff --git a/nntrainer/layers/acti_func.h b/nntrainer/layers/acti_func.h index f449d5064..3a8dc0fe0 100644 --- a/nntrainer/layers/acti_func.h +++ b/nntrainer/layers/acti_func.h @@ -37,8 +37,8 @@ class ActiFunc { */ template ActiFunc(ActivationType at = ActivationType::ACT_NONE, - bool in_place_ = true) : - in_place(in_place_) { + bool is_inplace_ = true) : + is_inplace(is_inplace_) { setActiFunc(at); } @@ -72,19 +72,19 @@ class ActiFunc { this->setActivation(leakyRelu, leakyReluPrime); break; case ActivationType::ACT_SWISH: - in_place = false; + is_inplace = false; this->setActivation(swish, swishPrime); break; case ActivationType::ACT_GELU: - in_place = false; + is_inplace = false; this->setActivation(gelu, geluPrime); break; case ActivationType::ACT_TANH_GELU: - in_place = false; + is_inplace = false; this->setActivation(tanhGelu, tanhGeluPrime); break; case ActivationType::ACT_SIGMOID_GELU: - in_place = false; + is_inplace = false; this->setActivation(sigmoidGelu, sigmoidGeluPrime); break; case ActivationType::ACT_ELU: @@ -149,7 +149,7 @@ class ActiFunc { /** * @copydoc Layer::supportInPlace() */ - bool supportInPlace() const { return in_place; } + bool supportInPlace() const { return is_inplace; } /** * @brief Calculate softmax for Tensor Type @@ -649,7 +649,7 @@ class ActiFunc { &activation_fn, std::function const &activation_prime_fn) { - if (in_place) + if (is_inplace) return ML_ERROR_INVALID_PARAMETER; _act_fn = activation_fn; @@ -672,7 +672,7 @@ class ActiFunc { &activation_fn, std::function const &activation_prime_fn) { - if (!in_place) { + if (!is_inplace) { _act_prime_fn = [activation_prime_fn]( funcParam const &t_in, funcParam &t_out, funcParam &outgoing_derivative, @@ -715,7 +715,7 @@ class ActiFunc { _act_fn = [activation_fn](Tensor const &x, Tensor &hidden) -> Tensor & { return x.apply(activation_fn, hidden); }; - if (!in_place) { + if (!is_inplace) { _act_prime_fn = [activation_prime_fn](Tensor const &t_in, Tensor &t_out, Tensor &outgoing_derivative, @@ -765,7 +765,7 @@ class ActiFunc { throw std::runtime_error( "Error setting activation layer to work in-place"); - in_place = val; + is_inplace = val; } private: @@ -780,7 +780,7 @@ class ActiFunc { ActivationType activation_type; /**< type of the activation represented by this */ - bool in_place; /**< if this class should operate in_place */ + bool is_inplace; /**< if this class should operate is_inplace */ }; } // namespace nntrainer diff --git a/nntrainer/layers/input_layer.cpp b/nntrainer/layers/input_layer.cpp index c84fb450c..ad55284bb 100644 --- a/nntrainer/layers/input_layer.cpp +++ b/nntrainer/layers/input_layer.cpp @@ -75,17 +75,6 @@ void InputLayer::finalize(InitLayerContext &context) { } context.setOutputDimensions(output_dims); - - is_inplace = true; - - /** - * @note Input Layer assuems that the FP32 IN Tensor always. Therefore, if the - * activation data type is not fp32, then it does not support in-place - * operation. - */ - if (context.getActivationDataType() != ml::train::TensorDim::DataType::FP32) { - is_inplace = false; - } } } /* namespace nntrainer */ diff --git a/nntrainer/layers/input_layer.h b/nntrainer/layers/input_layer.h index e9183e23d..cfab71333 100644 --- a/nntrainer/layers/input_layer.h +++ b/nntrainer/layers/input_layer.h @@ -105,7 +105,7 @@ class InputLayer : public Layer { private: std::tuple input_props; - bool is_inplace; + bool is_inplace = true; }; } // namespace nntrainer diff --git a/nntrainer/layers/layer_context.cpp b/nntrainer/layers/layer_context.cpp index 06fbbaffe..65accddff 100644 --- a/nntrainer/layers/layer_context.cpp +++ b/nntrainer/layers/layer_context.cpp @@ -41,11 +41,11 @@ static void suffixSpec(VarGradSpecV2 &spec, unsigned int idx) { InitLayerContext::InitLayerContext( const std::vector &dim, const std::vector &req_out_connected, - bool in_place_, const std::string &n, const std::string &prefix_, + bool is_inplace_, const std::string &n, const std::string &prefix_, const float max_norm, std::array tensor_type_, const float loss_scale_, ml::train::ExecutionMode mode_) : input_dim(dim), - in_place(in_place_), + is_inplace(is_inplace_), clip_by_global_norm(max_norm), output_specs(), req_out_is_connected(req_out_connected), @@ -126,13 +126,13 @@ const std::vector &InitLayerContext::getOutSpecs() const { } RunLayerContext::RunLayerContext(const std::string &name, bool trainable, - float l, bool in_place_, float loss_scale_, + float l, bool is_inplace_, float loss_scale_, bool restore_, const std::vector &w, const std::vector &in, const std::vector &out, const std::vector &t) : loss(l), - in_place(in_place_), + is_inplace(is_inplace_), loss_scale(loss_scale_), restoreData(restore_), weights(w), diff --git a/nntrainer/layers/layer_context.h b/nntrainer/layers/layer_context.h index 8e256027c..0ce3b5bc1 100644 --- a/nntrainer/layers/layer_context.h +++ b/nntrainer/layers/layer_context.h @@ -46,7 +46,7 @@ class InitLayerContext { * @param dim Input dimensions for the layer * @param req_out_connected bool vector to tell if requested output is * trainable or not - * @param in_place_ true if the context is inplacable + * @param is_inplace_ true if the context is inplacable * @param name name * @param prefix_ prefix * @param max_norm max norm @@ -57,7 +57,7 @@ class InitLayerContext { */ InitLayerContext( const std::vector &dim, - const std::vector &req_out_connected, bool in_place_, + const std::vector &req_out_connected, bool is_inplace_, const std::string &n = "", const std::string &prefix_ = "", const float max_norm = 0.0, std::array tensor_type_ = {"NCHW", "FP32", "FP32"}, @@ -383,7 +383,7 @@ class InitLayerContext { * * @return true if in-place, else false */ - bool getInPlace() const { return in_place; } + bool getInPlace() const { return is_inplace; } /** * @brief get Initial value of Loss_Scale. This is set to RunLayerContext @@ -403,7 +403,7 @@ class InitLayerContext { private: std::vector input_dim; /**< Input dimensions for the layer */ - bool in_place; /**< if the layer is expected to run in-place */ + bool is_inplace; /**< if the layer is expected to run in-place */ float clip_by_global_norm; /**< max norm value for clip by norm */ std::vector output_specs; /**< Specification for the output */ @@ -440,14 +440,15 @@ class RunLayerContext { * */ RunLayerContext() : - loss(0.0), in_place(false), loss_scale(1.0), restoreData(false) {} + loss(0.0), is_inplace(false), loss_scale(1.0), restoreData(false) {} /** * @brief Construct a new Run Layer Context object * */ - RunLayerContext(const std::string &name, bool in_place_) : RunLayerContext() { - in_place = in_place_; + RunLayerContext(const std::string &name, bool is_inplace_) : + RunLayerContext() { + is_inplace = is_inplace_; std::get(props).set(name); } @@ -455,9 +456,10 @@ class RunLayerContext { * @brief Construct a new Run Layer Context object * */ - RunLayerContext(const std::string &name, bool in_place_, float loss_scale_) : + RunLayerContext(const std::string &name, bool is_inplace_, + float loss_scale_) : RunLayerContext() { - in_place = in_place_; + is_inplace = is_inplace_; std::get(props).set(name); loss_scale = loss_scale_; } @@ -468,7 +470,7 @@ class RunLayerContext { * @param name name of the layer * @param trainable if the layer is trainable * @param l loss of the layer - * @param in_place_ execution in-place of the layer + * @param is_inplace_ execution in-place of the layer * @param loss_scale loss_scale of the layer * @param w weights of the layer * @param in inputs of the layer @@ -476,7 +478,7 @@ class RunLayerContext { * @param t extra tensors of the layer */ RunLayerContext(const std::string &name, bool trainable, float l, - bool in_place_, float loss_scale_, bool restoreData_, + bool is_inplace_, float loss_scale_, bool restoreData_, const std::vector &w, const std::vector &in, const std::vector &out, @@ -889,7 +891,7 @@ class RunLayerContext { * * @return true if in-place, else false */ - bool getInPlace() const { return in_place; } + bool getInPlace() const { return is_inplace; } /** * @brief get layer weights @@ -946,7 +948,7 @@ class RunLayerContext { private: std::tuple props; /**< props of the layer */ float loss; /**< loss of the layer */ - bool in_place; /**< if the layer is expected to run in-place */ + bool is_inplace; /**< if the layer is expected to run in-place */ float loss_scale; /**< loss_scale of the layer */ bool restoreData; /**< reset output for mixed precsion */ diff --git a/nntrainer/layers/loss/loss_layer.cpp b/nntrainer/layers/loss/loss_layer.cpp index 27e696dae..23dafd767 100644 --- a/nntrainer/layers/loss/loss_layer.cpp +++ b/nntrainer/layers/loss/loss_layer.cpp @@ -25,10 +25,6 @@ void LossLayer::finalize(InitLayerContext &context) { nntrainer::TensorDataTypeInfo>::from_string("FP32")); context.setOutputDimensions(output_dim); - - is_inplace = true; - if (context.getActivationDataType() != ml::train::TensorDim::DataType::FP32) - is_inplace = false; } void LossLayer::updateLoss(RunLayerContext &context, const Tensor &l) { diff --git a/nntrainer/layers/loss/loss_layer.h b/nntrainer/layers/loss/loss_layer.h index 17fee0c33..b643d5013 100644 --- a/nntrainer/layers/loss/loss_layer.h +++ b/nntrainer/layers/loss/loss_layer.h @@ -72,7 +72,7 @@ class LossLayer : public Layer { Tensor l; /**< loss tensor to store intermediate value to calculate loss value */ - bool is_inplace; + bool is_inplace = true; }; } // namespace nntrainer