fix: remove inline keyword in class; rename getter and setter for inputOf and outputOf

This commit is contained in:
whjthu 2023-03-25 12:04:24 +08:00
parent aff2b538ce
commit d9886e9de3
11 changed files with 53 additions and 55 deletions

View File

@ -63,7 +63,7 @@ class GraphObj : public Object {
inline TensorVec getInputs() const { inline TensorVec getInputs() const {
TensorVec ret; TensorVec ret;
for (const auto &t : tensors) for (const auto &t : tensors)
if (!t->getOutputOf()) if (!t->getSource())
ret.emplace_back(t); ret.emplace_back(t);
return ret; return ret;
} }
@ -74,7 +74,7 @@ class GraphObj : public Object {
inline TensorVec getOutputs() const { inline TensorVec getOutputs() const {
TensorVec ret; TensorVec ret;
for (const auto &t : tensors) for (const auto &t : tensors)
if (t->getInputOf().empty()) if (t->getTargets().empty())
ret.emplace_back(t); ret.emplace_back(t);
return ret; return ret;
} }

View File

@ -19,10 +19,10 @@ class TensorObj : public TensorBaseObj {
Fuid fuid; // Cloned tensors share the same id. Tensors constructed from Fuid fuid; // Cloned tensors share the same id. Tensors constructed from
// scratch have a new id. // scratch have a new id.
inline void copyin(const void *ptr, size_t size) { void copyin(const void *ptr, size_t size) {
runtime->copyBlobFromCPU(getRawDataPtr<void *>(), ptr, size); runtime->copyBlobFromCPU(getRawDataPtr<void *>(), ptr, size);
} }
inline void copyout(void *ptr, size_t size) const { void copyout(void *ptr, size_t size) const {
runtime->copyBlobToCPU(ptr, getRawDataPtr<void *>(), size); runtime->copyBlobToCPU(ptr, getRawDataPtr<void *>(), size);
} }
@ -31,33 +31,33 @@ class TensorObj : public TensorBaseObj {
virtual ~TensorObj() {} virtual ~TensorObj() {}
string toString() const override; string toString() const override;
inline size_t size() const { return _size; } size_t size() const { return _size; }
inline size_t getBytes() const { return _size * dtype.getSize(); } size_t getBytes() const { return _size * dtype.getSize(); }
Shape getDims() const { return shape; } Shape getDims() const { return shape; }
vector<size_t> getStride() const; vector<size_t> getStride() const;
size_t getOffset(const vector<int> &ds) const; size_t getOffset(const vector<int> &ds) const;
void dataMalloc(); void dataMalloc();
inline UidBaseType getFuid() const { return fuid; } UidBaseType getFuid() const { return fuid; }
void load(std::string file_path); void load(std::string file_path);
void save(std::string file_path); void save(std::string file_path);
// Copy elements from `data`. // Copy elements from `data`.
template <typename T> inline void copyin(const vector<T> &data) { template <typename T> void copyin(const vector<T> &data) {
IT_ASSERT(DataType::get<T>() == dtype); IT_ASSERT(DataType::get<T>() == dtype);
IT_ASSERT(data.size() >= _size); IT_ASSERT(data.size() >= _size);
copyin(data.data(), getBytes()); copyin(data.data(), getBytes());
} }
// Copy all the elements to a vector. // Copy all the elements to a vector.
template <typename T> inline auto copyout() const { template <typename T> auto copyout() const {
IT_ASSERT(DataType::get<T>() == dtype); IT_ASSERT(DataType::get<T>() == dtype);
std::vector<T> ans(_size); std::vector<T> ans(_size);
copyout(ans.data(), getBytes()); copyout(ans.data(), getBytes());
return ans; return ans;
} }
// Copy the element at `pos`. // Copy the element at `pos`.
template <typename T> inline auto copyOne(const vector<int> &pos) const { template <typename T> auto copyOne(const vector<int> &pos) const {
IT_ASSERT(DataType::get<T>() == dtype); IT_ASSERT(DataType::get<T>() == dtype);
auto offset = getOffset(pos); auto offset = getOffset(pos);
auto bytes = dtype.getSize(); auto bytes = dtype.getSize();

View File

@ -45,14 +45,14 @@ class TensorBaseObj : public Object {
DataType getDType() const { return dtype; } DataType getDType() const { return dtype; }
Runtime getRuntime() const { return runtime; } Runtime getRuntime() const { return runtime; }
void addInputOf(const Operator &op) { targets.emplace_back(op); } void addTarget(const Operator &op) { targets.emplace_back(op); }
void setOutputOf(const Operator &op) { source = op; } void setSource(const Operator &op) { source = op; }
bool hasTarget() const { return !targets.empty(); } bool hasTarget() const { return !targets.empty(); }
OpVec getInputOf() const { return wrefs_to_refs(targets); } OpVec getTargets() const { return wrefs_to_refs(targets); }
Operator getOutputOf() const { return source.lock(); } Operator getSource() const { return source.lock(); }
// std::pair<Operator *, int> getOutputOfWithIndex(); // std::pair<Operator *, int> getSourceWithIndex();
// bool setScalar(VType val) { // bool setScalar(VType val) {
// if (data == nullptr || !dims.empty()) // if (data == nullptr || !dims.empty())

View File

@ -39,11 +39,11 @@ class BatchNormObj : public OperatorObj {
std::string toString() const override; std::string toString() const override;
// output size will be 3 when training // output size will be 3 when training
inline int numInputs() const override { return 5; } int numInputs() const override { return 5; }
inline int numOutputs() const override { return outputs.size(); } int numOutputs() const override { return outputs.size(); }
inline float getMomentum() const { return momentum; } float getMomentum() const { return momentum; }
inline float getEps() const { return eps; } float getEps() const { return eps; }
inline bool getTraining() const { return training; } bool getTraining() const { return training; }
private: private:
vector<int> getWorkloadVector() const override; vector<int> getWorkloadVector() const override;

View File

@ -39,22 +39,20 @@ class PoolingObj : public OperatorObj {
optional<vector<Shape>> inferShape(const TensorVec &inputs) const override; optional<vector<Shape>> inferShape(const TensorVec &inputs) const override;
std::string toString() const override; std::string toString() const override;
inline int numInputs() const override { return 1; } int numInputs() const override { return 1; }
inline int numOutputs() const override { return 1; } int numOutputs() const override { return 1; }
inline int getKh() const { return kh; } int getKh() const { return kh; }
inline int getKw() const { return kw; } int getKw() const { return kw; }
inline int getDh() const { return dh; } int getDh() const { return dh; }
inline int getDw() const { return dw; } int getDw() const { return dw; }
inline int getPh() const { return ph; } int getPh() const { return ph; }
inline int getPw() const { return pw; } int getPw() const { return pw; }
inline int getSh() const { return sh; } int getSh() const { return sh; }
inline int getSw() const { return sw; } int getSw() const { return sw; }
inline auto getPadStrideDilation() const { auto getPadStrideDilation() const { return tuple(ph, pw, sh, sw, dh, dw); }
return tuple(ph, pw, sh, sw, dh, dw); auto getNCHWRS() const { return tuple(n, c, h, w, kh, kw); }
}
inline auto getNCHWRS() const { return tuple(n, c, h, w, kh, kw); }
private: private:
vector<int> getWorkloadVector() const override; vector<int> getWorkloadVector() const override;

View File

@ -33,15 +33,15 @@ void GraphObj::addOperatorAndConnect(const Operator &op) {
sorted = false; sorted = false;
ops.push_back(op); ops.push_back(op);
for (auto &input : op->getInputs()) { for (auto &input : op->getInputs()) {
input->addInputOf(op); input->addTarget(op);
if (auto pred = input->getOutputOf()) { if (auto pred = input->getSource()) {
pred->addSuccessors(op); pred->addSuccessors(op);
op->addPredecessors(pred); op->addPredecessors(pred);
} }
} }
for (auto &output : op->getOutputs()) { for (auto &output : op->getOutputs()) {
output->setOutputOf(op); output->setSource(op);
for (auto &succ : output->getInputOf()) { for (auto &succ : output->getTargets()) {
succ->addPredecessors(op); succ->addPredecessors(op);
op->addSuccessors(succ); op->addSuccessors(succ);
} }
@ -87,7 +87,7 @@ bool GraphObj::topo_sort() {
// this node is a head node. // this node is a head node.
const auto is_head = std::all_of( const auto is_head = std::all_of(
this_inputs.begin(), this_inputs.end(), [&](const auto &input) { this_inputs.begin(), this_inputs.end(), [&](const auto &input) {
auto src = input->getOutputOf(); auto src = input->getSource();
return src // If the source node is in the waiting list, return src // If the source node is in the waiting list,
// means that this node is not the head node. // means that this node is not the head node.
? waiting.find(src) == waiting.end() ? waiting.find(src) == waiting.end()

View File

@ -18,14 +18,14 @@ string TensorObj::toString() const {
string ret = "Tensor " + std::to_string(guid) + ", Fuid " + string ret = "Tensor " + std::to_string(guid) + ", Fuid " +
std::to_string(fuid) + ", shape " + vecToString(shape) + std::to_string(fuid) + ", shape " + vecToString(shape) +
", dtype " + dtype.toString(); ", dtype " + dtype.toString();
vector<UidBaseType> inputOfGuid; vector<UidBaseType> targetGuids;
for (const auto &op : targets) for (const auto &op : targets)
inputOfGuid.emplace_back(op.lock()->getGuid()); targetGuids.emplace_back(op.lock()->getGuid());
if (auto o = source.lock()) if (auto o = source.lock())
ret += ", source " + std::to_string(o->getGuid()); ret += ", source " + std::to_string(o->getGuid());
else else
ret += ", source None"; ret += ", source None";
ret += ", targets " + vecToString(inputOfGuid); ret += ", targets " + vecToString(targetGuids);
return ret; return ret;
} }

View File

@ -184,7 +184,7 @@ void init_graph_builder(py::module &m) {
.def("copyout_int32", &TensorObj::copyout<int32_t>, policy::move) .def("copyout_int32", &TensorObj::copyout<int32_t>, policy::move)
.def("copyout_int64", &TensorObj::copyout<int64_t>, policy::move) .def("copyout_int64", &TensorObj::copyout<int64_t>, policy::move)
.def("has_target", &TensorObj::hasTarget, policy::automatic) .def("has_target", &TensorObj::hasTarget, policy::automatic)
.def("src", &TensorObj::getOutputOf, policy::move); .def("src", &TensorObj::getSource, policy::move);
py::class_<OperatorObj, std::shared_ptr<OperatorObj>>(m, "Operator") py::class_<OperatorObj, std::shared_ptr<OperatorObj>>(m, "Operator")
.def("op_type", &OperatorObj::getOpType, policy::automatic) .def("op_type", &OperatorObj::getOpType, policy::automatic)
.def("inputs", py::overload_cast<>(&OperatorObj::getInputs, py::const_), .def("inputs", py::overload_cast<>(&OperatorObj::getInputs, py::const_),

View File

@ -19,13 +19,13 @@ TEST(Graph, build_and_run) {
w0->copyin(vector<uint32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); w0->copyin(vector<uint32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
auto matmul = g->addOpWithOutputs<MatmulObj>(i0, w0, o0); auto matmul = g->addOpWithOutputs<MatmulObj>(i0, w0, o0);
g->print(); g->print();
// check inputOf and outputsOf for tensor // check targets and source for tensor
EXPECT_EQ(i0->getInputOf().size(), 1u); EXPECT_EQ(i0->getTargets().size(), 1u);
EXPECT_EQ(w0->getInputOf().size(), 1u); EXPECT_EQ(w0->getTargets().size(), 1u);
EXPECT_EQ(o0->getInputOf().size(), 0u); EXPECT_EQ(o0->getTargets().size(), 0u);
EXPECT_EQ(i0->getOutputOf(), nullptr); EXPECT_EQ(i0->getSource(), nullptr);
EXPECT_EQ(w0->getOutputOf(), nullptr); EXPECT_EQ(w0->getSource(), nullptr);
EXPECT_NE(o0->getOutputOf(), nullptr); EXPECT_NE(o0->getSource(), nullptr);
EXPECT_EQ(matmul->getPredecessors().size(), 0u); EXPECT_EQ(matmul->getPredecessors().size(), 0u);
EXPECT_EQ(matmul->getSuccessors().size(), 0u); EXPECT_EQ(matmul->getSuccessors().size(), 0u);
@ -139,8 +139,8 @@ TEST(Graph, test_OpVec_ctor) {
map<pair<int, int>, int> inputOutput2Cnt = { map<pair<int, int>, int> inputOutput2Cnt = {
{{1, 0}, 2}, {{1, 1}, 1}, {{0, 1}, 1}}; {{1, 0}, 2}, {{1, 1}, 1}, {{0, 1}, 1}};
for (auto t : g2->getTensors()) { for (auto t : g2->getTensors()) {
pair<int, int> key = {t->getInputOf().size(), pair<int, int> key = {t->getTargets().size(),
t->getOutputOf() != nullptr}; t->getSource() != nullptr};
EXPECT_GE(inputOutput2Cnt[key], 0); EXPECT_GE(inputOutput2Cnt[key], 0);
inputOutput2Cnt[key]--; inputOutput2Cnt[key]--;
} }

View File

@ -23,7 +23,7 @@ namespace infini {
// w0->copyin(vector<uint32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); // w0->copyin(vector<uint32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
// auto matmul = g->addOpWithOutputs<MatmulObj>(i0, w0, o0); // auto matmul = g->addOpWithOutputs<MatmulObj>(i0, w0, o0);
// g->print(); // g->print();
// // check inputOf and outputsOf for tensor // // check targets and source for tensor
// SearchEngine searchEngine(runtime, make_ref<NMutator>()); // SearchEngine searchEngine(runtime, make_ref<NMutator>());
// searchEngine.run(g); // searchEngine.run(g);
// // check execution results // // check execution results
@ -46,7 +46,7 @@ TEST(Graph, search_withdm) {
auto conv1 = g->addOpWithOutputs<ConvObj>(t3, w3, t4, 1, 1); auto conv1 = g->addOpWithOutputs<ConvObj>(t3, w3, t4, 1, 1);
auto add1 = g->addOpWithOutputs<AddObj>(t4, t5, t6); auto add1 = g->addOpWithOutputs<AddObj>(t4, t5, t6);
g->dataMalloc(); g->dataMalloc();
// check inputOf and outputsOf for tensor // check targets and source for tensor
SearchEngine searchEngine(runtime, make_ref<DummyMutator>(10)); SearchEngine searchEngine(runtime, make_ref<DummyMutator>(10));
searchEngine.run(g); searchEngine.run(g);
// check execution results // check execution results

View File

@ -64,7 +64,7 @@ TEST(CUDA_Inception_v3_block, run) {
// check connection // check connection
EXPECT_EQ(maxpool->getSuccessors().size(), 4u); EXPECT_EQ(maxpool->getSuccessors().size(), 4u);
EXPECT_EQ(chainInput->getInputOf().size(), 4u); EXPECT_EQ(chainInput->getTargets().size(), 4u);
for (const auto &chainOps : ops) { for (const auto &chainOps : ops) {
for (size_t i = 1; i < chainOps.size(); i++) { for (size_t i = 1; i < chainOps.size(); i++) {
auto prev = chainOps[i - 1]; auto prev = chainOps[i - 1];