-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
625edf2
commit a425a0a
Showing
10 changed files
with
139 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// Created by fss on 23-12-14. | ||
// | ||
#include "activation.hpp" | ||
#include "activation_sse.hpp" | ||
namespace kuiper_infer { | ||
namespace activation { | ||
StatusCode ActivationForward(ActivationType type, | ||
const std::vector<std::shared_ptr<Tensor<float>>>& inputs, | ||
std::vector<std::shared_ptr<Tensor<float>>>& outputs) { | ||
const std::string& activation_type = ActivationTypeToString(type); | ||
if (inputs.empty()) { | ||
LOG(ERROR) << "The input tensor array in the " + activation_type + " layer is empty"; | ||
return StatusCode::kInferInputsEmpty; | ||
} | ||
|
||
if (outputs.empty()) { | ||
LOG(ERROR) << "The output tensor array in the " + activation_type + " layer is empty"; | ||
return StatusCode::kInferOutputsEmpty; | ||
} | ||
|
||
if (inputs.size() != outputs.size()) { | ||
LOG(ERROR) << "The input and output tensor array size of the " + activation_type + | ||
" layer do not match"; | ||
return StatusCode::kInferInOutShapeMismatch; | ||
} | ||
|
||
const uint32_t batch_size = inputs.size(); | ||
ActivationFunc activation_function = ApplySSEActivation(type); | ||
#pragma omp parallel for num_threads(batch_size) | ||
for (uint32_t i = 0; i < batch_size; ++i) { | ||
const std::shared_ptr<Tensor<float>>& input = inputs.at(i); | ||
CHECK(input != nullptr && !input->empty()) | ||
<< "The input tensor array in the " + activation_type + " layer has an empty tensor " << i | ||
<< " th"; | ||
|
||
std::shared_ptr<Tensor<float>> output = outputs.at(i); | ||
if (output == nullptr || output->empty()) { | ||
output = std::make_shared<Tensor<float>>(input->shapes()); | ||
outputs.at(i) = output; | ||
} | ||
CHECK(output != nullptr && output->shapes() == input->shapes()) | ||
<< "The input and output tensor shapes of the " + activation_type + " layer do not match " | ||
<< i << " th"; | ||
activation_function(input, output); | ||
} | ||
return StatusCode::kSuccess; | ||
} | ||
|
||
std::string ActivationTypeToString(ActivationType type) { | ||
std::string activate_type; | ||
switch (type) { | ||
case ActivationType::kActivationRelu: { | ||
activate_type = "Relu"; | ||
break; | ||
} | ||
case ActivationType::kActivationSilu: { | ||
activate_type = "Silu"; | ||
break; | ||
} | ||
case ActivationType::kActivationRelu6: { | ||
activate_type = "Relu6"; | ||
break; | ||
} | ||
case ActivationType::kActivationSigmoid: { | ||
activate_type = "Sigmoid"; | ||
break; | ||
} | ||
case ActivationType::kActivationHardSigmoid: { | ||
activate_type = "HardSigmoid"; | ||
break; | ||
} | ||
case ActivationType::kActivationHardSwish: { | ||
activate_type = "HardSwish"; | ||
break; | ||
} | ||
default: { | ||
activate_type = "Unknown"; | ||
break; | ||
} | ||
} | ||
return activate_type; | ||
} | ||
|
||
} // namespace activation | ||
} // namespace kuiper_infer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// | ||
// Created by fss on 23-12-14. | ||
// | ||
|
||
#ifndef KUIPER_INFER_SOURCE_LAYER_DETAILS_ACTIVATION_HPP | ||
#define KUIPER_INFER_SOURCE_LAYER_DETAILS_ACTIVATION_HPP | ||
#include "data/tensor.hpp" | ||
#include "status_code.hpp" | ||
namespace kuiper_infer { | ||
namespace activation { | ||
using ActivationFunc = std::function<void(sftensor, sftensor)>; | ||
|
||
enum class ActivationType { | ||
kActivatetionUnknown = -1, | ||
kActivationRelu = 0, | ||
kActivationSilu = 1, | ||
kActivationSigmoid = 2, | ||
kActivationHardSwish = 3, | ||
kActivationHardSigmoid = 4, | ||
kActivationRelu6 = 5, | ||
}; | ||
|
||
std::string ActivationTypeToString(ActivationType type); | ||
|
||
StatusCode ActivationForward(ActivationType type, | ||
const std::vector<std::shared_ptr<Tensor<float>>>& inputs, | ||
std::vector<std::shared_ptr<Tensor<float>>>& outputs); | ||
} // namespace activation | ||
} // namespace kuiper_infer | ||
#endif // KUIPER_INFER_SOURCE_LAYER_DETAILS_ACTIVATION_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.