-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support leakyrelu and floor op
- Loading branch information
Showing
10 changed files
with
138 additions
and
1 deletion.
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
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
23 changes: 23 additions & 0 deletions
23
src/05computation/include/computation/operators/leaky_relu.h
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,23 @@ | ||
#ifndef COMPUTATION_LEAKY_RELU_H | ||
#define COMPUTATION_LEAKY_RELU_H | ||
|
||
#include "../operator.h" | ||
|
||
namespace refactor::computation { | ||
|
||
struct LeakyRelu final : public Operator { | ||
float alpha; | ||
|
||
constexpr LeakyRelu(float alpha_) noexcept | ||
: Operator(), alpha(alpha_){}; | ||
|
||
static size_t typeId() noexcept; | ||
size_t opTypeId() const noexcept final; | ||
std::string_view name() const noexcept final; | ||
kernel::CollectorBox candidateKernels(Target) const noexcept final; | ||
std::string serialize() const noexcept final; | ||
}; | ||
|
||
}// namespace refactor::computation | ||
|
||
#endif// COMPUTATION_LEAKY_RELU_H |
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,20 @@ | ||
#include "computation/operators/leaky_relu.h" | ||
|
||
namespace refactor::computation { | ||
using Op = LeakyRelu; | ||
|
||
auto Op::typeId() noexcept -> size_t { | ||
static uint8_t ID = 1; | ||
return reinterpret_cast<size_t>(&ID); | ||
} | ||
auto Op::opTypeId() const noexcept -> size_t { return typeId(); } | ||
auto Op::name() const noexcept -> std::string_view { return "LeakyRelu"; } | ||
|
||
auto Op::candidateKernels(Target target) const noexcept -> kernel::CollectorBox { | ||
return nullptr; | ||
} | ||
auto Op::serialize() const noexcept -> std::string { | ||
return fmt::format("{}({})", name(), alpha); | ||
} | ||
|
||
}// namespace refactor::computation |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include "leaky_relu.hh" | ||
#include "common.h" | ||
#include "computation/operators/leaky_relu.h" | ||
#include <execution> | ||
|
||
namespace refactor::onnx { | ||
using Op = LeakyRelu; | ||
|
||
Op::LeakyRelu(Float alpha) | ||
: Operator(), alpha(alpha) {} | ||
|
||
auto Op::build(ModelContext const &, std::string_view, Attributes attributes) -> OpBox { | ||
auto alpha = attributes.getOrInsert("alpha", {0.01f}).float_(); | ||
return OpBox(std::make_unique<Op>(alpha)); | ||
} | ||
auto Op::typeId() -> size_t { | ||
static uint8_t ID = 1; | ||
return reinterpret_cast<size_t>(&ID); | ||
} | ||
|
||
auto Op::opTypeId() const -> size_t { return typeId(); } | ||
auto Op::opTypeName() const -> std::string_view { return "onnx::LeakyRelu"; } | ||
|
||
auto Op::infer(TensorRefs inputs, InferOptions const &options) const -> InferResult { | ||
EXPECT_SIZE(1) | ||
auto dataType = inputs[0].dataType; | ||
if (!dataType.isFloat()) { | ||
return Err(InferError(ERROR_MSG("Data type not support"))); | ||
} | ||
auto ans = Tensor::share(dataType, inputs[0].shape, extractDependency(inputs)); | ||
return Ok(Tensors{std::move(ans)}); | ||
} | ||
auto Op::lower(TensorRefs) const -> computation::OpBox { | ||
using Op_ = computation::LeakyRelu; | ||
return std::make_unique<Op_>(alpha); | ||
} | ||
|
||
|
||
}// namespace refactor::onnx |
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,25 @@ | ||
#ifndef ONNX_LEAKY_RELU_HH | ||
#define ONNX_LEAKY_RELU_HH | ||
|
||
#include "frontend/operator.h" | ||
|
||
namespace refactor::onnx { | ||
using namespace frontend; | ||
|
||
struct LeakyRelu final : public Operator { | ||
Float alpha; | ||
|
||
explicit LeakyRelu(Float); | ||
|
||
static OpBox build(ModelContext const &, std::string_view, Attributes); | ||
static size_t typeId(); | ||
|
||
size_t opTypeId() const final; | ||
std::string_view opTypeName() const final; | ||
InferResult infer(TensorRefs, InferOptions const &) const final; | ||
computation::OpBox lower(TensorRefs) const final; | ||
}; | ||
|
||
}// namespace refactor::onnx | ||
|
||
#endif// ONNX_LEAKY_RELU_HH |
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