forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PaddlePaddle#27 from Superjomn/fea/make-compute-mo…
…re-friendly make compute more use-friendly
- Loading branch information
Showing
4 changed files
with
66 additions
and
48 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
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 |
---|---|---|
@@ -1,59 +1,68 @@ | ||
#include "cinn/lang/compute.h" | ||
|
||
#include "cinn/common/common.h" | ||
#include "cinn/poly/dim.h" | ||
#include "cinn/poly/domain.h" | ||
#include "cinn/poly/element.h" | ||
|
||
namespace cinn { | ||
namespace lang { | ||
|
||
using ir::Expr; | ||
|
||
template <> | ||
ir::Tensor Compute<compute_handle_1_t>(const std::vector<int> &dims, compute_handle_1_t handle) { | ||
CHECK_EQ(dims.size(), 1); | ||
Var i(common::axis_name(0), Int(32)); | ||
auto expr = handle(i); | ||
|
||
std::vector<Expr> shape; | ||
for (int v : dims) shape.emplace_back(v); | ||
ir::Tensor Compute(const std::vector<int> &dims, std::function<Expr(Var)> fn) { | ||
return Compute(dims, [fn](const std::vector<Var> &axis) -> Expr { | ||
CHECK_EQ(axis.size(), 1); | ||
return fn(axis[0]); | ||
}); | ||
} | ||
|
||
ir::Tensor tensor(shape, {i}, expr.type(), expr); | ||
return std::move(tensor); | ||
ir::Tensor Compute(const std::vector<int> &dims, std::function<Expr(Var, Var)> fn) { | ||
return Compute(dims, [fn](const std::vector<Var> &axis) -> Expr { | ||
CHECK_EQ(axis.size(), 2); | ||
return fn(axis[0], axis[1]); | ||
}); | ||
} | ||
|
||
template <> | ||
ir::Tensor Compute<compute_handle_2_t>(const std::vector<int> &dims, compute_handle_2_t handle) { | ||
CHECK_EQ(dims.size(), 2); | ||
poly::Dim dim("i", 0, dims[0] - 1); | ||
Var i(common::axis_name(0), Int(32)); | ||
Var j(common::axis_name(1), Int(32)); | ||
auto expr = handle(i, j); | ||
ir::Tensor Compute(const std::vector<int> &dims, std::function<Expr(Var, Var, Var)> fn) { | ||
return Compute(dims, [fn](const std::vector<Var> &axis) -> Expr { | ||
CHECK_EQ(axis.size(), 3); | ||
return fn(axis[0], axis[1], axis[2]); | ||
}); | ||
} | ||
|
||
std::vector<Expr> shape; | ||
for (int v : dims) shape.emplace_back(v); | ||
ir::Tensor Compute(const std::vector<int> &dims, std::function<Expr(Var, Var, Var, Var)> fn) { | ||
return Compute(dims, [fn](const std::vector<Var> &axis) -> Expr { | ||
CHECK_EQ(axis.size(), 4); | ||
return fn(axis[0], axis[1], axis[2], axis[3]); | ||
}); | ||
} | ||
|
||
ir::Tensor tensor(shape, {i, j}, expr.type(), expr); | ||
CHECK(tensor.get()); | ||
return std::move(tensor); | ||
ir::Tensor Compute(const std::vector<int> &dims, std::function<Expr(Var, Var, Var, Var, Var)> fn) { | ||
return Compute(dims, [fn](const std::vector<Var> &axis) -> Expr { | ||
CHECK_EQ(axis.size(), 5); | ||
return fn(axis[0], axis[1], axis[2], axis[3], axis[4]); | ||
}); | ||
} | ||
|
||
template <> | ||
ir::Tensor Compute<compute_handle_3_t>(const std::vector<int> &dims, compute_handle_3_t handle) { | ||
CHECK_EQ(dims.size(), 3); | ||
Var i(common::axis_name(0), Int(32)); | ||
Var j(common::axis_name(1), Int(32)); | ||
Var k(common::axis_name(2), Int(32)); | ||
auto expr = handle(i, j, k); | ||
ir::Tensor Compute(const std::vector<int> &dims, std::function<Expr(const std::vector<Var> &)> fn) { | ||
auto axis = detail::GenDefaultAxis(dims.size()); | ||
Expr expr = fn(axis); | ||
|
||
std::vector<Expr> shape; | ||
for (int v : dims) shape.emplace_back(v); | ||
|
||
ir::Tensor tensor(shape, {i, j}, expr.type(), expr); | ||
return std::move(tensor); | ||
ir::Tensor tensor(shape, axis, expr.type(), expr); | ||
return tensor; | ||
} | ||
|
||
} // namespace lang | ||
namespace detail { | ||
std::vector<Var> GenDefaultAxis(int naxis) { | ||
std::vector<Var> axis; | ||
for (int i = 0; i < naxis; i++) { | ||
axis.emplace_back(common::axis_name(i)); | ||
} | ||
return axis; | ||
} | ||
} // namespace detail | ||
|
||
namespace ir {} // namespace ir | ||
} // namespace lang | ||
} // namespace cinn |
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