From d9886e9de302f126f998ef23ebd3cbdd3087d393 Mon Sep 17 00:00:00 2001 From: whjthu Date: Sat, 25 Mar 2023 12:04:24 +0800 Subject: [PATCH] fix: remove inline keyword in class; rename getter and setter for inputOf and outputOf --- include/core/graph.h | 4 ++-- include/core/tensor.h | 16 +++++++-------- include/core/tensor_base.h | 10 ++++----- include/operators/batch_norm.h | 10 ++++----- include/operators/pooling.h | 26 +++++++++++------------- src/core/graph.cc | 10 ++++----- src/core/tensor.cc | 6 +++--- src/ffi/ffi_infinitensor.cc | 2 +- test/core/test_graph.cc | 18 ++++++++-------- test/core/test_search.cc | 4 ++-- test/kernels/cuda/test_cuda_inception.cc | 2 +- 11 files changed, 53 insertions(+), 55 deletions(-) diff --git a/include/core/graph.h b/include/core/graph.h index 8e317a8b..4e654caa 100644 --- a/include/core/graph.h +++ b/include/core/graph.h @@ -63,7 +63,7 @@ class GraphObj : public Object { inline TensorVec getInputs() const { TensorVec ret; for (const auto &t : tensors) - if (!t->getOutputOf()) + if (!t->getSource()) ret.emplace_back(t); return ret; } @@ -74,7 +74,7 @@ class GraphObj : public Object { inline TensorVec getOutputs() const { TensorVec ret; for (const auto &t : tensors) - if (t->getInputOf().empty()) + if (t->getTargets().empty()) ret.emplace_back(t); return ret; } diff --git a/include/core/tensor.h b/include/core/tensor.h index ed95f5f5..72a3b007 100644 --- a/include/core/tensor.h +++ b/include/core/tensor.h @@ -19,10 +19,10 @@ class TensorObj : public TensorBaseObj { Fuid fuid; // Cloned tensors share the same id. Tensors constructed from // 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(), ptr, size); } - inline void copyout(void *ptr, size_t size) const { + void copyout(void *ptr, size_t size) const { runtime->copyBlobToCPU(ptr, getRawDataPtr(), size); } @@ -31,33 +31,33 @@ class TensorObj : public TensorBaseObj { virtual ~TensorObj() {} string toString() const override; - inline size_t size() const { return _size; } - inline size_t getBytes() const { return _size * dtype.getSize(); } + size_t size() const { return _size; } + size_t getBytes() const { return _size * dtype.getSize(); } Shape getDims() const { return shape; } vector getStride() const; size_t getOffset(const vector &ds) const; void dataMalloc(); - inline UidBaseType getFuid() const { return fuid; } + UidBaseType getFuid() const { return fuid; } void load(std::string file_path); void save(std::string file_path); // Copy elements from `data`. - template inline void copyin(const vector &data) { + template void copyin(const vector &data) { IT_ASSERT(DataType::get() == dtype); IT_ASSERT(data.size() >= _size); copyin(data.data(), getBytes()); } // Copy all the elements to a vector. - template inline auto copyout() const { + template auto copyout() const { IT_ASSERT(DataType::get() == dtype); std::vector ans(_size); copyout(ans.data(), getBytes()); return ans; } // Copy the element at `pos`. - template inline auto copyOne(const vector &pos) const { + template auto copyOne(const vector &pos) const { IT_ASSERT(DataType::get() == dtype); auto offset = getOffset(pos); auto bytes = dtype.getSize(); diff --git a/include/core/tensor_base.h b/include/core/tensor_base.h index 4c73094a..61b8d032 100644 --- a/include/core/tensor_base.h +++ b/include/core/tensor_base.h @@ -45,14 +45,14 @@ class TensorBaseObj : public Object { DataType getDType() const { return dtype; } Runtime getRuntime() const { return runtime; } - void addInputOf(const Operator &op) { targets.emplace_back(op); } - void setOutputOf(const Operator &op) { source = op; } + void addTarget(const Operator &op) { targets.emplace_back(op); } + void setSource(const Operator &op) { source = op; } bool hasTarget() const { return !targets.empty(); } - OpVec getInputOf() const { return wrefs_to_refs(targets); } - Operator getOutputOf() const { return source.lock(); } - // std::pair getOutputOfWithIndex(); + OpVec getTargets() const { return wrefs_to_refs(targets); } + Operator getSource() const { return source.lock(); } + // std::pair getSourceWithIndex(); // bool setScalar(VType val) { // if (data == nullptr || !dims.empty()) diff --git a/include/operators/batch_norm.h b/include/operators/batch_norm.h index 8e41a043..fbee21fd 100644 --- a/include/operators/batch_norm.h +++ b/include/operators/batch_norm.h @@ -39,11 +39,11 @@ class BatchNormObj : public OperatorObj { std::string toString() const override; // output size will be 3 when training - inline int numInputs() const override { return 5; } - inline int numOutputs() const override { return outputs.size(); } - inline float getMomentum() const { return momentum; } - inline float getEps() const { return eps; } - inline bool getTraining() const { return training; } + int numInputs() const override { return 5; } + int numOutputs() const override { return outputs.size(); } + float getMomentum() const { return momentum; } + float getEps() const { return eps; } + bool getTraining() const { return training; } private: vector getWorkloadVector() const override; diff --git a/include/operators/pooling.h b/include/operators/pooling.h index 1a1a6edf..c14bb8ad 100644 --- a/include/operators/pooling.h +++ b/include/operators/pooling.h @@ -39,22 +39,20 @@ class PoolingObj : public OperatorObj { optional> inferShape(const TensorVec &inputs) const override; std::string toString() const override; - inline int numInputs() const override { return 1; } - inline int numOutputs() const override { return 1; } + int numInputs() const override { return 1; } + int numOutputs() const override { return 1; } - inline int getKh() const { return kh; } - inline int getKw() const { return kw; } - inline int getDh() const { return dh; } - inline int getDw() const { return dw; } - inline int getPh() const { return ph; } - inline int getPw() const { return pw; } - inline int getSh() const { return sh; } - inline int getSw() const { return sw; } + int getKh() const { return kh; } + int getKw() const { return kw; } + int getDh() const { return dh; } + int getDw() const { return dw; } + int getPh() const { return ph; } + int getPw() const { return pw; } + int getSh() const { return sh; } + int getSw() const { return sw; } - inline auto getPadStrideDilation() const { - return tuple(ph, pw, sh, sw, dh, dw); - } - inline auto getNCHWRS() const { return tuple(n, c, h, w, kh, kw); } + auto getPadStrideDilation() const { return tuple(ph, pw, sh, sw, dh, dw); } + auto getNCHWRS() const { return tuple(n, c, h, w, kh, kw); } private: vector getWorkloadVector() const override; diff --git a/src/core/graph.cc b/src/core/graph.cc index 04ce2581..800f4ca5 100644 --- a/src/core/graph.cc +++ b/src/core/graph.cc @@ -33,15 +33,15 @@ void GraphObj::addOperatorAndConnect(const Operator &op) { sorted = false; ops.push_back(op); for (auto &input : op->getInputs()) { - input->addInputOf(op); - if (auto pred = input->getOutputOf()) { + input->addTarget(op); + if (auto pred = input->getSource()) { pred->addSuccessors(op); op->addPredecessors(pred); } } for (auto &output : op->getOutputs()) { - output->setOutputOf(op); - for (auto &succ : output->getInputOf()) { + output->setSource(op); + for (auto &succ : output->getTargets()) { succ->addPredecessors(op); op->addSuccessors(succ); } @@ -87,7 +87,7 @@ bool GraphObj::topo_sort() { // this node is a head node. const auto is_head = std::all_of( 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, // means that this node is not the head node. ? waiting.find(src) == waiting.end() diff --git a/src/core/tensor.cc b/src/core/tensor.cc index b8a41728..362c3e76 100644 --- a/src/core/tensor.cc +++ b/src/core/tensor.cc @@ -18,14 +18,14 @@ string TensorObj::toString() const { string ret = "Tensor " + std::to_string(guid) + ", Fuid " + std::to_string(fuid) + ", shape " + vecToString(shape) + ", dtype " + dtype.toString(); - vector inputOfGuid; + vector targetGuids; for (const auto &op : targets) - inputOfGuid.emplace_back(op.lock()->getGuid()); + targetGuids.emplace_back(op.lock()->getGuid()); if (auto o = source.lock()) ret += ", source " + std::to_string(o->getGuid()); else ret += ", source None"; - ret += ", targets " + vecToString(inputOfGuid); + ret += ", targets " + vecToString(targetGuids); return ret; } diff --git a/src/ffi/ffi_infinitensor.cc b/src/ffi/ffi_infinitensor.cc index 627be8bf..7d080548 100644 --- a/src/ffi/ffi_infinitensor.cc +++ b/src/ffi/ffi_infinitensor.cc @@ -184,7 +184,7 @@ void init_graph_builder(py::module &m) { .def("copyout_int32", &TensorObj::copyout, policy::move) .def("copyout_int64", &TensorObj::copyout, policy::move) .def("has_target", &TensorObj::hasTarget, policy::automatic) - .def("src", &TensorObj::getOutputOf, policy::move); + .def("src", &TensorObj::getSource, policy::move); py::class_>(m, "Operator") .def("op_type", &OperatorObj::getOpType, policy::automatic) .def("inputs", py::overload_cast<>(&OperatorObj::getInputs, py::const_), diff --git a/test/core/test_graph.cc b/test/core/test_graph.cc index 8140ea43..85c012b9 100644 --- a/test/core/test_graph.cc +++ b/test/core/test_graph.cc @@ -19,13 +19,13 @@ TEST(Graph, build_and_run) { w0->copyin(vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); auto matmul = g->addOpWithOutputs(i0, w0, o0); g->print(); - // check inputOf and outputsOf for tensor - EXPECT_EQ(i0->getInputOf().size(), 1u); - EXPECT_EQ(w0->getInputOf().size(), 1u); - EXPECT_EQ(o0->getInputOf().size(), 0u); - EXPECT_EQ(i0->getOutputOf(), nullptr); - EXPECT_EQ(w0->getOutputOf(), nullptr); - EXPECT_NE(o0->getOutputOf(), nullptr); + // check targets and source for tensor + EXPECT_EQ(i0->getTargets().size(), 1u); + EXPECT_EQ(w0->getTargets().size(), 1u); + EXPECT_EQ(o0->getTargets().size(), 0u); + EXPECT_EQ(i0->getSource(), nullptr); + EXPECT_EQ(w0->getSource(), nullptr); + EXPECT_NE(o0->getSource(), nullptr); EXPECT_EQ(matmul->getPredecessors().size(), 0u); EXPECT_EQ(matmul->getSuccessors().size(), 0u); @@ -139,8 +139,8 @@ TEST(Graph, test_OpVec_ctor) { map, int> inputOutput2Cnt = { {{1, 0}, 2}, {{1, 1}, 1}, {{0, 1}, 1}}; for (auto t : g2->getTensors()) { - pair key = {t->getInputOf().size(), - t->getOutputOf() != nullptr}; + pair key = {t->getTargets().size(), + t->getSource() != nullptr}; EXPECT_GE(inputOutput2Cnt[key], 0); inputOutput2Cnt[key]--; } diff --git a/test/core/test_search.cc b/test/core/test_search.cc index 5f531c21..5f354fdb 100644 --- a/test/core/test_search.cc +++ b/test/core/test_search.cc @@ -23,7 +23,7 @@ namespace infini { // w0->copyin(vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}); // auto matmul = g->addOpWithOutputs(i0, w0, o0); // g->print(); -// // check inputOf and outputsOf for tensor +// // check targets and source for tensor // SearchEngine searchEngine(runtime, make_ref()); // searchEngine.run(g); // // check execution results @@ -46,7 +46,7 @@ TEST(Graph, search_withdm) { auto conv1 = g->addOpWithOutputs(t3, w3, t4, 1, 1); auto add1 = g->addOpWithOutputs(t4, t5, t6); g->dataMalloc(); - // check inputOf and outputsOf for tensor + // check targets and source for tensor SearchEngine searchEngine(runtime, make_ref(10)); searchEngine.run(g); // check execution results diff --git a/test/kernels/cuda/test_cuda_inception.cc b/test/kernels/cuda/test_cuda_inception.cc index 1e691576..31a7b888 100644 --- a/test/kernels/cuda/test_cuda_inception.cc +++ b/test/kernels/cuda/test_cuda_inception.cc @@ -64,7 +64,7 @@ TEST(CUDA_Inception_v3_block, run) { // check connection EXPECT_EQ(maxpool->getSuccessors().size(), 4u); - EXPECT_EQ(chainInput->getInputOf().size(), 4u); + EXPECT_EQ(chainInput->getTargets().size(), 4u); for (const auto &chainOps : ops) { for (size_t i = 1; i < chainOps.size(); i++) { auto prev = chainOps[i - 1];