Skip to content

Commit

Permalink
Merge pull request PaddlePaddle#22 from Superjomn/fea/placeholder-dsl
Browse files Browse the repository at this point in the history
initialize placeholder dsl
  • Loading branch information
Superjomn authored Feb 10, 2020
2 parents 7b15a98 + 50f7e6f commit 2a535b0
Show file tree
Hide file tree
Showing 63 changed files with 364 additions and 35 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.0)
project(CINN)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 14)
set(THIRD_PARTY_PATH "${CMAKE_BINARY_DIR}/thirds")

option(WITH_TESTING "Compile with Unittests" ON)
Expand Down
1 change: 1 addition & 0 deletions cinn/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_subdirectory(utils)
add_subdirectory(arithmetic)
add_subdirectory(poly)
add_subdirectory(schedule)
add_subdirectory(runtime)
add_subdirectory(ir)
add_subdirectory(lang)
add_subdirectory(optim)
1 change: 1 addition & 0 deletions cinn/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cc_library(common
SRCS shared.cc pod_value.cc type.cc
object.cc
graph_utils.cc
context.cc
DEPS boost utils)

cc_test(test_pod_value SRCS pod_value_test.cc DEPS common ir)
Expand Down
12 changes: 9 additions & 3 deletions cinn/common/common.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "cinn/common/context.h"
#include "cinn/common/domain.h"
#include "cinn/common/graph_utils.h"
#include "cinn/common/pod_value.h"
Expand All @@ -9,10 +10,15 @@
namespace cinn {

// export some general concepts.
using common::Float;
using common::Int;
using common::Context;
using common::make_shared;
using common::Object;
using common::ref_count;
using common::Shared;
using common::make_shared;

// Type related.
using common::Float;
using common::Int;
using common::type_of;

} // namespace cinn
5 changes: 5 additions & 0 deletions cinn/common/context.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "cinn/common/context.h"

namespace cinn {
namespace common {} // namespace common
} // namespace cinn
42 changes: 42 additions & 0 deletions cinn/common/context.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once
#include <string>
#include <vector>

namespace cinn {
namespace common {

struct ID {
size_t id() const { return cur_; }
size_t New() { return ++cur_; }
void Reset() { cur_ = 0; }

private:
size_t cur_;
};

struct NameGenerator {
std::string New(const std::string& name_hint) { return name_hint + "_" + std::to_string(id_.New()); }

private:
ID id_;
};

class Context {
public:
static Context& Global() {
static Context x;
return x;
}
/**
* Generate a new unique name.
* @param name_hint The prefix.
*/
std::string NewName(const std::string& name_hint) { return name_generator_.New(name_hint); }

private:
Context() = default;
NameGenerator name_generator_;
};

} // namespace common
} // namespace cinn
2 changes: 2 additions & 0 deletions cinn/common/graph_utils.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "cinn/common/graph_utils.h"

#include <glog/logging.h>

#include <functional>
#include <set>
#include <stack>
Expand Down
1 change: 1 addition & 0 deletions cinn/common/graph_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! \file This file contains the utilities of graph.

#include <glog/logging.h>

#include <list>
#include <map>
#include <string>
Expand Down
1 change: 1 addition & 0 deletions cinn/common/graph_utils_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <gtest/gtest.h>

#include "cinn/common/common.h"

namespace cinn {
Expand Down
1 change: 1 addition & 0 deletions cinn/common/pod_value.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cinn/common/pod_value.h"

#include "cinn/ir/node.h"

namespace cinn {
Expand Down
1 change: 1 addition & 0 deletions cinn/common/pod_value.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include <glog/logging.h>

#include <boost/variant/get.hpp>
#include <boost/variant/variant.hpp>

Expand Down
1 change: 1 addition & 0 deletions cinn/common/pod_value_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cinn/common/pod_value.h"

#include <gtest/gtest.h>

namespace cinn {
Expand Down
20 changes: 12 additions & 8 deletions cinn/common/shared.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#pragma once
#include <atomic>
#include <string>
#include <type_traits>

namespace cinn {
namespace common {

class RefCount {
std::atomic<uint32_t> count{0};

public:
using value_type = uint32_t;
using value_type = int32_t;
RefCount() = default;

value_type Inc() { return ++count; }
value_type Dec() { return --count; }
bool is_zero() const { return 0 == count; }
value_type Inc() { return ++count_; }
value_type Dec() { return --count_; }
bool is_zero() const { return 0 == count_; }
std::string to_string() { return std::to_string(count_.load()); }
int32_t val() const { return count_; }

private:
std::atomic<value_type> count_{0};
};

class Object;
Expand All @@ -34,8 +38,8 @@ void Destroy(const T* t) {
template <typename T>
struct Shared {
Shared() = default;
Shared(T* p) : p_(p) {}
Shared(const Shared& other) : p_(other.p_) {}
Shared(T* p) : p_(p) { IncRef(p); }
Shared(const Shared& other) : p_(other.p_) { IncRef(p_); }
Shared(Shared&& other) : p_(other.p_) { other.p_ = nullptr; }
Shared<T>& operator=(const Shared<T>& other);

Expand Down
21 changes: 18 additions & 3 deletions cinn/common/shared_test.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
#include "cinn/common/shared.h"

#include <glog/logging.h>
#include <gtest/gtest.h>

#include "cinn/common/object.h"
#include "cinn/common/shared.h"

namespace cinn {
namespace common {

class A : public Object {};
struct A : public Object {
const char *type_info() const override { return "A"; }
};
class B : public Object {};

TEST(Shared, test) {}
TEST(Shared, test) {
Shared<A> a_ref(make_shared<A>());
ASSERT_EQ(ref_count(a_ref.get()).val(), 1);

{ // local copy
Shared<A> b = a_ref;
EXPECT_EQ(ref_count(a_ref.get()).val(), 2);
ASSERT_EQ(ref_count(b.get()).val(), 2);
}

ASSERT_EQ(ref_count(a_ref.get()).val(), 1);
}

} // namespace common
} // namespace cinn
1 change: 1 addition & 0 deletions cinn/ir/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ cc_test(test_ir_printer SRCS ir_printer_test.cc DEPS ir)
cc_test(test_ir_operators SRCS ir_operators_test.cc DEPS ir)
#cc_test(test_tensor SRCS tensor_test.cc DEPS ir)
cc_test(test_function SRCS function_test.cc DEPS ir)
cc_test(test_buffer SRCS buffer_test.cc DEPS ir)
3 changes: 2 additions & 1 deletion cinn/ir/buffer.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "cinn/ir/buffer.h"

#include "cinn/ir/ir_visitor.h"

namespace cinn {
namespace ir {

const _Buffer_ *Buffer::operator->() const { return As<_Buffer_>(); }
const _Buffer_ *Buffer::operator->() const { return IrNodeRef::As<_Buffer_>(); }

Buffer _Buffer_::Make(Var data,
Type dtype,
Expand Down
4 changes: 2 additions & 2 deletions cinn/ir/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Buffer : public IrNodeRef {
Buffer() = default;
explicit Buffer(IrNode* n) : IrNodeRef(n) {}

inline const _Buffer_* operator->() const;
const _Buffer_* operator->() const;
};

class _Buffer_ : public IrNode {
Expand Down Expand Up @@ -68,7 +68,7 @@ class _Buffer_ : public IrNode {
void Accept(IrVisitor* v) const override;
IrNodeTy node_type() const override;

static const IrNodeTy _node_type_ = IrNodeTy::_Range_;
static const IrNodeTy _node_type_ = IrNodeTy::_Buffer_;
};

} // namespace ir
Expand Down
32 changes: 32 additions & 0 deletions cinn/ir/buffer_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "cinn/ir/buffer.h"

#include <gtest/gtest.h>

#include <vector>

#include "cinn/common/common.h"

namespace cinn {
namespace ir {

TEST(Buffer, basic) {
Var ptr("buff", Float(32));
std::vector<Expr> shape({Expr(100), Expr(20)});
Var i("i"), j("j");
std::vector<Expr> strides({Expr(0), Expr(0)});
auto buffer = _Buffer_::Make(ptr, ptr->type(), shape, strides, Expr(0), "buf", "", 0, 0);

// Check shared
ASSERT_EQ(ref_count(buffer.get()).val(), 1);

{
auto buffer1 = buffer;
ASSERT_EQ(ref_count(buffer.get()).val(), 2);
ASSERT_EQ(ref_count(buffer1.get()).val(), 2);
}

ASSERT_EQ(ref_count(buffer.get()).val(), 1);
}

} // namespace ir
} // namespace cinn
1 change: 1 addition & 0 deletions cinn/ir/function_definition.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <memory>

#include "cinn/ir/ir.h"

namespace cinn {
Expand Down
1 change: 1 addition & 0 deletions cinn/ir/function_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cinn/ir/function.h"

#include <gtest/gtest.h>

namespace cinn {
Expand Down
1 change: 1 addition & 0 deletions cinn/ir/ir.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cinn/ir/ir.h"

#include "cinn/common/pod_value.h"
#include "cinn/ir/ir_visitor.h"
#include "cinn/lang/tensor.h"
Expand Down
1 change: 1 addition & 0 deletions cinn/ir/ir_operators_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cinn/ir/ir_operators.h"

#include <gtest/gtest.h>

namespace cinn {
Expand Down
10 changes: 10 additions & 0 deletions cinn/ir/ir_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ void IrPrinter::Visit(const _Range_ *x) {
os_ << ")";
}

void IrPrinter::Visit(const _Buffer_ *x) { os_ << "_Buffer_(" << x->name << ")"; }
void IrPrinter::Visit(const _Tensor_ *x) {
os_ << "Tensor(";
for (auto &i : x->shape) {
Print(i);
os_ << ",";
}
os_ << ")";
}

std::ostream &operator<<(std::ostream &os, Expr a) {
std::stringstream ss;
IrPrinter printer(ss);
Expand Down
4 changes: 2 additions & 2 deletions cinn/ir/ir_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ struct IrPrinter : public IrVisitor {
void Visit(const Free *x) override;
void Visit(const _Range_ *x) override;
void Visit(const _IterVar_ *x) override {}
void Visit(const _Buffer_ *x) override {}
void Visit(const _Tensor_ *x) override {}
void Visit(const _Buffer_ *x) override;
void Visit(const _Tensor_ *x) override;

private:
std::ostream &os_;
Expand Down
2 changes: 2 additions & 0 deletions cinn/ir/ir_printer_test.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "cinn/ir/ir_printer.h"

#include <gtest/gtest.h>

#include <sstream>

namespace cinn {
Expand Down
1 change: 1 addition & 0 deletions cinn/ir/ir_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cinn/ir/ir.h"

#include <gtest/gtest.h>

#include "cinn/utils/string.h"
Expand Down
2 changes: 2 additions & 0 deletions cinn/ir/node.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "cinn/ir/node.h"

#include "cinn/common/pod_value.h"
#include "cinn/ir/buffer.h"
#include "cinn/ir/ir.h"
Expand Down Expand Up @@ -47,6 +48,7 @@ std::ostream &operator<<(std::ostream &os, IrNodeTy type) {
}

Expr::Expr(const Var &var) { *static_cast<IrNodeRef *>(this) = *static_cast<const IrNodeRef *>(&var); }
Expr::Expr(const Buffer &buffer) { *static_cast<IrNodeRef *>(this) = *static_cast<const IrNodeRef *>(&buffer); }

} // namespace ir
} // namespace cinn
Loading

0 comments on commit 2a535b0

Please sign in to comment.