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 {
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;
}

View File

@ -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<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);
}
@ -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<size_t> getStride() const;
size_t getOffset(const vector<int> &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 <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(data.size() >= _size);
copyin(data.data(), getBytes());
}
// 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);
std::vector<T> ans(_size);
copyout(ans.data(), getBytes());
return ans;
}
// 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);
auto offset = getOffset(pos);
auto bytes = dtype.getSize();

View File

@ -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<Operator *, int> getOutputOfWithIndex();
OpVec getTargets() const { return wrefs_to_refs(targets); }
Operator getSource() const { return source.lock(); }
// std::pair<Operator *, int> getSourceWithIndex();
// bool setScalar(VType val) {
// if (data == nullptr || !dims.empty())

View File

@ -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<int> getWorkloadVector() const override;

View File

@ -39,22 +39,20 @@ class PoolingObj : public OperatorObj {
optional<vector<Shape>> 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<int> getWorkloadVector() const override;

View File

@ -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()

View File

@ -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<UidBaseType> inputOfGuid;
vector<UidBaseType> 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;
}

View File

@ -184,7 +184,7 @@ void init_graph_builder(py::module &m) {
.def("copyout_int32", &TensorObj::copyout<int32_t>, policy::move)
.def("copyout_int64", &TensorObj::copyout<int64_t>, policy::move)
.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")
.def("op_type", &OperatorObj::getOpType, policy::automatic)
.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});
auto matmul = g->addOpWithOutputs<MatmulObj>(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<pair<int, int>, int> inputOutput2Cnt = {
{{1, 0}, 2}, {{1, 1}, 1}, {{0, 1}, 1}};
for (auto t : g2->getTensors()) {
pair<int, int> key = {t->getInputOf().size(),
t->getOutputOf() != nullptr};
pair<int, int> key = {t->getTargets().size(),
t->getSource() != nullptr};
EXPECT_GE(inputOutput2Cnt[key], 0);
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});
// auto matmul = g->addOpWithOutputs<MatmulObj>(i0, w0, o0);
// g->print();
// // check inputOf and outputsOf for tensor
// // check targets and source for tensor
// SearchEngine searchEngine(runtime, make_ref<NMutator>());
// searchEngine.run(g);
// // check execution results
@ -46,7 +46,7 @@ TEST(Graph, search_withdm) {
auto conv1 = g->addOpWithOutputs<ConvObj>(t3, w3, t4, 1, 1);
auto add1 = g->addOpWithOutputs<AddObj>(t4, t5, t6);
g->dataMalloc();
// check inputOf and outputsOf for tensor
// check targets and source for tensor
SearchEngine searchEngine(runtime, make_ref<DummyMutator>(10));
searchEngine.run(g);
// check execution results

View File

@ -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];