-
Notifications
You must be signed in to change notification settings - Fork 57
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
ad3b148
commit 886eba7
Showing
10 changed files
with
392 additions
and
13 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#include <array> | ||
#include "backends/xnnpack/Functions/XpTransposeFunc.hpp" | ||
|
||
namespace mllm::xnnpack { | ||
|
||
void XpTransposeFunction::setup(vector<Tensor *> outputs, vector<Tensor *> inputs, vector<float> args) { | ||
Chl axis0_ = (Chl)args[0]; | ||
Chl axis1_ = (Chl)args[1]; | ||
|
||
// inputs[0]->transShape(SEQUENCE, DIMENSION); | ||
if (axis0_ == SEQUENCE && axis1_ == DIMENSION) { | ||
if (inputs[0]->ctype() == BSHD) { | ||
outputs[0]->reshape(inputs[0]->batch(), inputs[0]->head(), inputs[0]->dimension(), inputs[0]->sequence()); | ||
} | ||
} else if (axis0_ == THW && axis1_ == CHANNLE) { | ||
if (inputs[0]->ctype() == BCTHW) { | ||
outputs[0]->reshape(inputs[0]->batch(), inputs[0]->time(), inputs[0]->height(), inputs[0]->width(), inputs[0]->channel()); | ||
} | ||
} else if (axis0_ == BATCH && axis1_ == SEQUENCE) { | ||
if (inputs[0]->ctype() == BSHD) { | ||
outputs[0]->reshape(inputs[0]->sequence(), inputs[0]->head(), inputs[0]->batch(), inputs[0]->dimension()); | ||
} | ||
} | ||
} | ||
|
||
void XpTransposeFunction::execute(vector<Tensor *> outputs, vector<Tensor *> inputs, vector<float> args) { | ||
auto xpb = (XnnpackBackend *)inputs[0]->backend(); | ||
tryDefineAllXpTensors(xpb, inputs); | ||
tryDefineAllXpTensors(xpb, outputs); | ||
|
||
std::array<size_t, 4> perm{3, 2, 1, 0}; | ||
|
||
Chl axis0_ = (Chl)args[0]; | ||
Chl axis1_ = (Chl)args[1]; | ||
|
||
// inputs[0]->transShape(SEQUENCE, DIMENSION); | ||
if (axis0_ == SEQUENCE && axis1_ == DIMENSION) { | ||
if (inputs[0]->ctype() == BSHD) { | ||
std::swap(perm[2], perm[3]); | ||
} else { | ||
Log::error("XpTransposeFunction NYI"); | ||
exit(-1); | ||
} | ||
} else if (axis0_ == THW && axis1_ == CHANNLE) { | ||
if (inputs[0]->ctype() == BCTHW) { | ||
Log::error("XpTransposeFunction NYI"); | ||
exit(-1); | ||
outputs[0]->reshape(inputs[0]->batch(), inputs[0]->time(), inputs[0]->height(), inputs[0]->width(), inputs[0]->channel()); | ||
} else { | ||
Log::error("XpTransposeFunction NYI"); | ||
exit(-1); | ||
} | ||
} else if (axis0_ == BATCH && axis1_ == SEQUENCE) { | ||
if (inputs[0]->ctype() == BSHD) { | ||
Log::error("XpTransposeFunction NYI"); | ||
exit(-1); | ||
outputs[0] | ||
->reshape(inputs[0]->sequence(), inputs[0]->head(), inputs[0]->batch(), inputs[0]->dimension()); | ||
} else { | ||
Log::error("XpTransposeFunction NYI"); | ||
exit(-1); | ||
} | ||
} else { | ||
Log::error("XpTransposeFunction NYI"); | ||
exit(-1); | ||
} | ||
|
||
auto status = xnn_define_static_transpose(xpb->getXnnSubgraph(), 4, perm.data(), inputs[0]->uuid(), outputs[0]->uuid(), 0); | ||
|
||
if (status != xnn_status_success) { | ||
Log::error("XpGeLU::execute Error"); | ||
exit(-1); | ||
} | ||
} | ||
|
||
} // namespace mllm::xnnpack |
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 @@ | ||
/** | ||
* @file XpTransposeFunc.hpp | ||
* @author Chenghua Wang (chenghua.wang.edu@gmail.com) | ||
* @version 0.1 | ||
* @date 2024-10-16 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
#pragma once | ||
|
||
#include "Backend.hpp" | ||
#include "xnnpack/XpInterface.hpp" | ||
namespace mllm::xnnpack { | ||
|
||
class XpTransposeFunction : public TensorFunction, public XpTensorDefineInterface<XpTransposeFunction> { | ||
public: | ||
void setup(vector<Tensor *> outputs, vector<Tensor *> inputs, vector<float> args) override; | ||
|
||
void execute(vector<Tensor *> outputs, vector<Tensor *> inputs, vector<float> args) override; | ||
}; | ||
|
||
} // namespace mllm::xnnpack |
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,84 @@ | ||
#include "backends/xnnpack/Ops/XpTranspose.hpp" | ||
#include "Types.hpp" | ||
#include "xnnpack.h" | ||
#include <utility> | ||
|
||
namespace mllm::xnnpack { | ||
|
||
ErrorCode XpTranspose::setUp(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) { | ||
return MLLM_NO_ERROR; | ||
} | ||
|
||
ErrorCode XpTranspose::reshape(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) { | ||
// inputs[0]->transShape(SEQUENCE, DIMENSION); | ||
if (axis0_ == SEQUENCE && axis1_ == DIMENSION) { | ||
if (inputs[0]->ctype() == BSHD) { | ||
outputs[0]->reshape(inputs[0]->batch(), inputs[0]->head(), inputs[0]->dimension(), inputs[0]->sequence()); | ||
} | ||
} else if (axis0_ == THW && axis1_ == CHANNLE) { | ||
if (inputs[0]->ctype() == BCTHW) { | ||
outputs[0]->reshape(inputs[0]->batch(), inputs[0]->time(), inputs[0]->height(), inputs[0]->width(), inputs[0]->channel()); | ||
} | ||
} else if (axis0_ == BATCH && axis1_ == SEQUENCE) { | ||
if (inputs[0]->ctype() == BSHD) { | ||
outputs[0]->reshape(inputs[0]->sequence(), inputs[0]->head(), inputs[0]->batch(), inputs[0]->dimension()); | ||
} | ||
} | ||
return MLLM_NO_ERROR; | ||
} | ||
|
||
ErrorCode XpTranspose::execute(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) { | ||
auto xpb = (XnnpackBackend *)inputs[0]->backend(); | ||
tryDefineAllXpTensors(xpb, inputs); | ||
tryDefineAllXpTensors(xpb, outputs); | ||
|
||
std::array<size_t, 4> perm{3, 2, 1, 0}; | ||
|
||
// inputs[0]->transShape(SEQUENCE, DIMENSION); | ||
if (axis0_ == SEQUENCE && axis1_ == DIMENSION) { | ||
if (inputs[0]->ctype() == BSHD) { | ||
std::swap(perm[2], perm[3]); | ||
} else { | ||
Log::error("XpTransposeFunction NYI"); | ||
exit(-1); | ||
} | ||
} else if (axis0_ == THW && axis1_ == CHANNLE) { | ||
if (inputs[0]->ctype() == BCTHW) { | ||
Log::error("XpTransposeFunction NYI"); | ||
exit(-1); | ||
outputs[0]->reshape(inputs[0]->batch(), inputs[0]->time(), inputs[0]->height(), inputs[0]->width(), inputs[0]->channel()); | ||
} else { | ||
Log::error("XpTransposeFunction NYI"); | ||
exit(-1); | ||
} | ||
} else if (axis0_ == BATCH && axis1_ == SEQUENCE) { | ||
if (inputs[0]->ctype() == BSHD) { | ||
Log::error("XpTransposeFunction NYI"); | ||
exit(-1); | ||
outputs[0] | ||
->reshape(inputs[0]->sequence(), inputs[0]->head(), inputs[0]->batch(), inputs[0]->dimension()); | ||
} else { | ||
Log::error("XpTransposeFunction NYI"); | ||
exit(-1); | ||
} | ||
} else { | ||
Log::error("XpTransposeFunction NYI"); | ||
exit(-1); | ||
} | ||
|
||
auto status = xnn_define_static_transpose(xpb->getXnnSubgraph(), 4, perm.data(), inputs[0]->uuid(), outputs[0]->uuid(), 0); | ||
|
||
if (status != xnn_status_success) { | ||
Log::error("XpGeLU::execute Error"); | ||
exit(-1); | ||
} | ||
|
||
return MLLM_NO_ERROR; | ||
} | ||
|
||
Op *XpTransposeCreator::create(OpParam op_param, Backend *bk, const string &name, int thread_count) const { | ||
int axis0 = (int)op_param["axis0"]; | ||
int axis1 = (int)op_param["axis1"]; | ||
return new XpTranspose(bk, axis0, axis1, name, thread_count); | ||
} | ||
} // namespace mllm::xnnpack |
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,44 @@ | ||
/** | ||
* @file XpTranspose.hpp | ||
* @author Chenghua Wang (chenghua.wang.edu@gmail.com) | ||
* @brief | ||
* @version 0.1 | ||
* @date 2024-10-16 | ||
* | ||
* @copyright Copyright (c) 2024 | ||
* | ||
*/ | ||
#pragma once | ||
|
||
#include "Backend.hpp" | ||
#include "Op.hpp" | ||
#include "backends/xnnpack/XnnpackBackend.hpp" | ||
#include "backends/xnnpack/XpInterface.hpp" | ||
|
||
namespace mllm::xnnpack { | ||
|
||
class XpTranspose final : public Op, public XpTensorDefineInterface<XpTranspose> { | ||
public: | ||
XpTranspose(Backend *bk, int axis0, int axis1, const std::string &op_name, int thread_count) : | ||
Op(bk, op_name), axis0_(axis0), axis1_(axis1), thread_count_(thread_count) { | ||
} | ||
|
||
~XpTranspose() override = default; | ||
|
||
ErrorCode setUp(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override; | ||
|
||
ErrorCode reshape(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override; | ||
|
||
ErrorCode execute(vector<shared_ptr<Tensor>> inputs, vector<shared_ptr<Tensor>> outputs) override; | ||
|
||
private: | ||
int axis0_; | ||
int axis1_; | ||
int thread_count_ = 4; | ||
}; | ||
|
||
struct XpTransposeCreator : public XnnpackBackend::Creator { | ||
Op *create(OpParam op_param, Backend *bk, const string &name, int thread_count) const override; | ||
}; | ||
|
||
} // namespace mllm::xnnpack |
Oops, something went wrong.