Skip to content

Merge from upstream #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions aten/src/ATen/UndefinedType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace at {

UndefinedType::UndefinedType()
: Type(UndefinedTensorId(), /*is_variable=*/false, /*is_undefined=*/true) {}
: TypeDefault(UndefinedTensorId(), /*is_variable=*/false, /*is_undefined=*/true) {}
ScalarType UndefinedType::scalarType() const {
return ScalarType::Undefined;
}
Expand Down Expand Up @@ -50,13 +50,13 @@ size_t UndefinedType::elementSizeInBytes() const {

Type & UndefinedType::toBackend(Backend b) const {
if (b == Backend::Undefined) {
return Type::toBackend(b);
return TypeDefault::toBackend(b);
}
AT_ERROR("toBackend not implemented for UndefinedType to non-UndefinedType");
}
Type & UndefinedType::toScalarType(ScalarType s) const {
if (s == ScalarType::Undefined) {
return Type::toScalarType(s);
return TypeDefault::toScalarType(s);
}
AT_ERROR("toScalarType not implemented for UndefinedType to non-UndefinedType");
}
Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/UndefinedType.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "ATen/Type.h"
#include "ATen/TypeDefault.h"
#include "ATen/CheckGenerator.h"

#ifdef _MSC_VER
Expand All @@ -11,7 +11,7 @@

namespace at {

struct UndefinedType final : public Type {
struct UndefinedType final : public TypeDefault {
explicit UndefinedType();
virtual ScalarType scalarType() const override;
virtual Backend backend() const override;
Expand Down
46 changes: 33 additions & 13 deletions aten/src/ATen/function_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def TypedDict(name, attrs, total=True): # type: ignore
# declaration under Type.h (right now, we call this template
# BROADCAST but it also handles default arguments)
TYPE_METHOD_DECLARATION_BROADCAST = CodeTemplate("""\
${return_type} ${api_name}(${type_method_formals_with_defaults}) const;
${return_type} ${api_name}(${type_method_formals_with_defaults}) const override;
""")
# 2. broadcasting functions are implemented in Type.cpp
TYPE_METHOD_DEFINITION_BROADCAST = CodeTemplate("""\
${return_type} Type::${api_name}(${type_method_formals}) const {
${return_type} TypeDefault::${api_name}(${type_method_formals}) const {
${device_guard_declaration}
Tensor ${broadcast_returns};
std::tie(${broadcast_returns}) = ${broadcast_function}(${broadcast_actuals}, "${api_name}");
Expand All @@ -59,36 +59,44 @@ def TypedDict(name, attrs, total=True): # type: ignore
# actual implementation. At the moment, this situation *only* occurs
# for 'native' declarations (so the native dispatch is hardcoded into
# the template here.)
PURE_VIRTUAL_TYPE_METHOD_DECLARATION = CodeTemplate("""\
virtual ${return_type} ${method_prefix_derived}${api_name}(${type_method_formals_with_defaults}) const = 0;
""")
DEPRECATED_PURE_VIRTUAL_TYPE_METHOD_DECLARATION = CodeTemplate("""\
AT_DEPRECATED(virtual ${return_type} \
${method_prefix_derived}${api_name}(${type_method_formals_with_defaults}) const = 0);
""")
PURE_VIRTUAL_TYPE_METHOD_DECLARATION_BROADCAST = CodeTemplate("""\
virtual ${return_type} ${api_name}(${type_method_formals_with_defaults}) const = 0;
""")

TYPE_METHOD_DECLARATION_ABSTRACT = CodeTemplate("""\
virtual ${return_type} ${method_prefix_derived}${api_name}(${type_method_formals_with_defaults}) const;
${return_type} ${method_prefix_derived}${api_name}(${type_method_formals_with_defaults}) const override;
""")
TYPE_METHOD_DEFINITION_ABSTRACT = CodeTemplate("""\
${return_type} Type::${method_prefix_derived}${api_name}(${type_method_formals}) const {
${return_type} TypeDefault::${method_prefix_derived}${api_name}(${type_method_formals}) const {
AT_ERROR("${method_prefix_derived}${api_name} is not implemented for type ", toString());
}
""")
TYPE_METHOD_DECLARATION_CONCRETE = CodeTemplate("""\
virtual ${return_type} ${api_name}(${type_method_formals_with_defaults}) const;
""")
DEPRECATED_TYPE_METHOD_DECLARATION_CONCRETE = CodeTemplate("""\
AT_DEPRECATED(virtual ${return_type} ${api_name}(${type_method_formals_with_defaults}) const);
${return_type} ${api_name}(${type_method_formals_with_defaults}) const override;
""")
TYPE_METHOD_DEFINITION_CONCRETE = CodeTemplate("""\
${return_type} Type::${api_name}(${type_method_formals}) const {
${return_type} TypeDefault::${api_name}(${type_method_formals}) const {
${device_guard_declaration}
${type_definition_body}
}
""")
DEPRECATED_TYPE_METHOD_DEFINITION_CONCRETE = CodeTemplate("""\
${return_type} Type::${api_name}(${type_method_formals}) const {
${return_type} TypeDefault::${api_name}(${type_method_formals}) const {
TensorOptions options(*this);
${device_guard_declaration}
return at::native::${api_name}(${type_method_actuals}, options);
}
""")
# 4. add virtual override to TypeDerived.h
TYPE_DERIVED_DECLARATION = CodeTemplate("""\
virtual ${return_type} ${method_prefix_derived}${api_name}(${type_method_formals}) const override;
${return_type} ${method_prefix_derived}${api_name}(${type_method_formals}) const override;
""")
# 5. add override definition to TypeDerived.cpp
TYPE_DERIVED_DEFINITION = CodeTemplate("""\
Expand Down Expand Up @@ -382,6 +390,7 @@ def __getitem__(self, x):
TopEnvironment = TypedDict('TopEnvironment', {
'type_registrations': List[str],
'type_headers': List[str],
'pure_virtual_type_method_declarations': List[str],
'type_method_declarations': List[str],
'type_method_definitions': List[str],
'type_method_inline_definitions': List[str],
Expand Down Expand Up @@ -815,18 +824,26 @@ def process_option(option, output_options):
# NN function with no _forward/_backward suffix don't have cimpls.
# They call the _forward function and discard any buffer returns
abstract = False
top_env['pure_virtual_type_method_declarations'].append(
PURE_VIRTUAL_TYPE_METHOD_DECLARATION.substitute(env))
top_env['type_method_declarations'].append(
TYPE_METHOD_DECLARATION_CONCRETE.substitute(env))
body = emit_nn_body(option)
top_env['type_method_definitions'].append(
TYPE_METHOD_DEFINITION_CONCRETE.substitute(
env, type_definition_body=body))
elif broadcast_arg is None:
top_env['pure_virtual_type_method_declarations'].append(
PURE_VIRTUAL_TYPE_METHOD_DECLARATION.substitute(env))
top_env['type_method_declarations'].append(
TYPE_METHOD_DECLARATION_ABSTRACT.substitute(env))
top_env['type_method_definitions'].append(
TYPE_METHOD_DEFINITION_ABSTRACT.substitute(env))
else:
top_env['pure_virtual_type_method_declarations'].append(
PURE_VIRTUAL_TYPE_METHOD_DECLARATION_BROADCAST.substitute(env))
top_env['pure_virtual_type_method_declarations'].append(
PURE_VIRTUAL_TYPE_METHOD_DECLARATION.substitute(env))
top_env['type_method_declarations'].append(
TYPE_METHOD_DECLARATION_BROADCAST.substitute(env))
top_env['type_method_declarations'].append(
Expand Down Expand Up @@ -1031,9 +1048,12 @@ def find_formal(formal_name, formals):
# Factory methods are not dispatched over `Type`.
if not is_factory_method:
if option['deprecated']:
top_env['type_method_declarations'].append(DEPRECATED_TYPE_METHOD_DECLARATION_CONCRETE.substitute(env))
top_env['pure_virtual_type_method_declarations'].append(
DEPRECATED_PURE_VIRTUAL_TYPE_METHOD_DECLARATION.substitute(env))
else:
top_env['type_method_declarations'].append(TYPE_METHOD_DECLARATION_CONCRETE.substitute(env))
top_env['pure_virtual_type_method_declarations'].append(
PURE_VIRTUAL_TYPE_METHOD_DECLARATION.substitute(env))
top_env['type_method_declarations'].append(TYPE_METHOD_DECLARATION_CONCRETE.substitute(env))
dispatch = option['type_method_definition_dispatch']
option['native_type_method_dispatch'] = dispatch

Expand Down
9 changes: 6 additions & 3 deletions aten/src/ATen/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ def check_all_files_written(self):
SPARSE_TYPE_DERIVED_CPP = CodeTemplate.from_file(TEMPLATE_PATH + "/SparseTypeDerived.cpp")
TYPE_DERIVED_H = CodeTemplate.from_file(TEMPLATE_PATH + "/TypeDerived.h")
TYPE_H = CodeTemplate.from_file(TEMPLATE_PATH + "/Type.h")
TYPE_CPP = CodeTemplate.from_file(TEMPLATE_PATH + "/Type.cpp")
TYPE_DEFAULT_H = CodeTemplate.from_file(TEMPLATE_PATH + "/TypeDefault.h")
TYPE_DEFAULT_CPP = CodeTemplate.from_file(TEMPLATE_PATH + "/TypeDefault.cpp")

REGISTER_CPU_H = CodeTemplate.from_file(TEMPLATE_PATH + "/RegisterCPU.h")
REGISTER_CPU_CPP = CodeTemplate.from_file(TEMPLATE_PATH + "/RegisterCPU.cpp")
Expand Down Expand Up @@ -166,6 +167,7 @@ def check_all_files_written(self):
'cpu_type_headers': [],
'cuda_type_registrations': [],
'cuda_type_headers': [],
'pure_virtual_type_method_declarations': [],
'type_method_declarations': [],
'type_method_definitions': [],
'type_method_inline_definitions': [],
Expand Down Expand Up @@ -329,7 +331,7 @@ def iterate_types():
# so that the script runs quickly when we are just querying the
# outputs
def declare_outputs():
files = ['Declarations.yaml', 'Type.h', 'Type.cpp', 'Tensor.h',
files = ['Declarations.yaml', 'Type.h', 'TypeDefault.cpp', 'TypeDefault.h', 'Tensor.h',
'TensorMethods.h', 'Functions.h',
'CPUCopy.cpp', 'NativeFunctions.h',
'RegisterCPU.cpp', 'RegisterCPU.h']
Expand Down Expand Up @@ -399,7 +401,8 @@ def generate_outputs():
backend, density, scalar_type, declarations))

file_manager.write('Type.h', TYPE_H, top_env)
file_manager.write('Type.cpp', TYPE_CPP, top_env)
file_manager.write('TypeDefault.h', TYPE_DEFAULT_H, top_env)
file_manager.write('TypeDefault.cpp', TYPE_DEFAULT_CPP, top_env)

file_manager.write('RegisterCPU.h', REGISTER_CPU_H, top_env)
file_manager.write('RegisterCPU.cpp', REGISTER_CPU_CPP, top_env)
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/templates/SparseTypeDerived.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
namespace at {

${Type}::${Type}()
: Type(${Backend}TensorId(), /*is_variable=*/false, /*is_undefined=*/false) {}
: TypeDefault(${Backend}TensorId(), /*is_variable=*/false, /*is_undefined=*/false) {}
ScalarType ${Type}::scalarType() const {
return ScalarType::${ScalarName};
}
Expand Down
29 changes: 17 additions & 12 deletions aten/src/ATen/templates/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum class TypeID {
struct AT_API Type {
explicit Type(TensorTypeId type_id, bool is_variable, bool is_undefined)
: type_id_(type_id), is_variable_(is_variable), is_undefined_(is_undefined) {}

virtual ~Type() {}
virtual ScalarType scalarType() const = 0;
virtual Backend backend() const = 0;
Expand All @@ -65,8 +66,8 @@ struct AT_API Type {
virtual Storage unsafeStorageFromTH(void * th_pointer, bool retain) const = 0;
virtual const char * toString() const = 0;
virtual size_t elementSizeInBytes() const = 0;
virtual Type & toBackend(Backend b) const;
virtual Type & toScalarType(ScalarType s) const;
virtual Type & toBackend(Backend b) const = 0;
virtual Type & toScalarType(ScalarType s) const = 0;
Type & toSparse() const {
return this->toBackend(at::toSparse(this->backend()));
}
Expand All @@ -91,23 +92,27 @@ struct AT_API Type {
return backendToDeviceType(backend());
}

Tensor copy(const Tensor & src, bool non_blocking=false) const;
Tensor & copy_(Tensor & self, const Tensor & src, bool non_blocking=false) const;
virtual Tensor copy(const Tensor & src, bool non_blocking=false) const = 0;
virtual Tensor & copy_(Tensor & self, const Tensor & src, bool non_blocking=false) const = 0;
virtual Tensor & s_copy_(Tensor & self, const Tensor & src, bool non_blocking) const = 0;
virtual Tensor & _s_copy_from(const Tensor & self, Tensor & dst, bool non_blocking) const = 0;

Tensor tensorFromBlob(void * data, IntList sizes, const std::function<void(void*)> & deleter=noop_deleter) const;
Tensor tensorFromBlob(void * data, IntList sizes, IntList strides, const std::function<void(void*)> & deleter=noop_deleter) const;
Tensor tensorWithAllocator(IntList sizes, Allocator* allocator) const;
Tensor tensorWithAllocator(IntList sizes, IntList strides, Allocator* allocator) const;
Tensor scalarTensor(Scalar s) const;
virtual Tensor tensorFromBlob(void * data, IntList sizes, const std::function<void(void*)> & deleter=noop_deleter) const = 0;
virtual Tensor tensorFromBlob(void * data, IntList sizes, IntList strides, const std::function<void(void*)> & deleter=noop_deleter) const = 0;
virtual Tensor tensorWithAllocator(IntList sizes, Allocator* allocator) const = 0;
virtual Tensor tensorWithAllocator(IntList sizes, IntList strides, Allocator* allocator) const = 0;
virtual Tensor scalarTensor(Scalar s) const = 0;

bool operator==(const Type& other) const;
bool operator!=(const Type& other) const;
bool operator==(const Type& other) const {
return this == &other;
}
bool operator!=(const Type& other) const {
return this != &other;
}

// example
// virtual Tensor * add(Tensor & a, Tensor & b) = 0;
${type_method_declarations}
${pure_virtual_type_method_declarations}
protected:
TensorTypeId type_id_;
bool is_variable_;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ATen/Type.h"
#include "ATen/TypeDefault.h"

// ${generated_comment}

Expand All @@ -13,13 +13,13 @@

namespace at {

Tensor & Type::copy_(Tensor & self, const Tensor & src, bool non_blocking) const {
Tensor & TypeDefault::copy_(Tensor & self, const Tensor & src, bool non_blocking) const {
Tensor b_src;
std::tie(b_src) = expand_inplace(self, src, "copy");
return s_copy_(self, b_src, non_blocking);
}

Tensor Type::copy(const Tensor & src, bool non_blocking) const {
Tensor TypeDefault::copy(const Tensor & src, bool non_blocking) const {
// TODO(psag): have a DeviceGuard here
AT_CHECK(src.defined(), "attempt to copy an undefined tensor");
if (is_sparse()) {
Expand All @@ -37,10 +37,10 @@ Tensor Type::copy(const Tensor & src, bool non_blocking) const {
}
}

Type & Type::toBackend(Backend b) const {
Type & TypeDefault::toBackend(Backend b) const {
return at::globalContext().getNonVariableType(b,scalarType());
}
Type & Type::toScalarType(ScalarType s) const {
Type & TypeDefault::toScalarType(ScalarType s) const {
return at::globalContext().getNonVariableType(backend(),s);
}
static std::vector<int64_t> defaultStrides(IntList sizes) {
Expand All @@ -64,31 +64,24 @@ static int64_t computeStorageSize(IntList sizes, IntList strides) {
}
return size;
}
Tensor Type::tensorFromBlob(void * data, IntList sizes, const std::function<void(void*)> & deleter) const {
Tensor TypeDefault::tensorFromBlob(void * data, IntList sizes, const std::function<void(void*)> & deleter) const {
return tensorFromBlob(data, sizes, defaultStrides(sizes), deleter);
}
Tensor Type::tensorFromBlob(void * data, IntList sizes, IntList strides, const std::function<void(void*)> & deleter) const {
Tensor TypeDefault::tensorFromBlob(void * data, IntList sizes, IntList strides, const std::function<void(void*)> & deleter) const {
auto storage = storageFromBlob(data, computeStorageSize(sizes, strides), deleter);
return tensor(storage, 0, sizes, strides);
}
Tensor Type::tensorWithAllocator(IntList sizes, Allocator* allocator) const {
Tensor TypeDefault::tensorWithAllocator(IntList sizes, Allocator* allocator) const {
return tensorWithAllocator(sizes, defaultStrides(sizes), std::move(allocator));
}
Tensor Type::tensorWithAllocator(IntList sizes, IntList strides, Allocator* allocator) const {
Tensor TypeDefault::tensorWithAllocator(IntList sizes, IntList strides, Allocator* allocator) const {
auto storage = storageWithAllocator(computeStorageSize(sizes, strides), std::move(allocator));
return tensor(storage, 0, sizes, strides);
}
Tensor Type::scalarTensor(Scalar s) const {
Tensor TypeDefault::scalarTensor(Scalar s) const {
return tensor({}).fill_(s);
}

bool Type::operator==(const Type& other) const {
return this == &other;
}
bool Type::operator!=(const Type& other) const {
return this != &other;
}

${type_method_definitions}

}
36 changes: 36 additions & 0 deletions aten/src/ATen/templates/TypeDefault.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

// ${generated_comment}

#include "ATen/Type.h"

namespace at {

struct AT_API TypeDefault : public Type {
explicit TypeDefault(TensorTypeId type_id, bool is_variable, bool is_undefined)
: Type(type_id, is_variable, is_undefined) {}

// Make sure overload resolution considers the nullary virtual method.
// (A single argument overload is generated in the list.)
bool is_cuda() const override = 0;
bool is_sparse() const override = 0;
bool is_distributed() const override = 0;

Type & toBackend(Backend b) const override;
Type & toScalarType(ScalarType s) const override;

Tensor copy(const Tensor & src, bool non_blocking=false) const override;
Tensor & copy_(Tensor & self, const Tensor & src, bool non_blocking=false) const override;

Tensor tensorFromBlob(void * data, IntList sizes, const std::function<void(void*)> & deleter=noop_deleter) const override;
Tensor tensorFromBlob(void * data, IntList sizes, IntList strides, const std::function<void(void*)> & deleter=noop_deleter) const override;
Tensor tensorWithAllocator(IntList sizes, Allocator* allocator) const override;
Tensor tensorWithAllocator(IntList sizes, IntList strides, Allocator* allocator) const override;
Tensor scalarTensor(Scalar s) const override;

// example
// virtual Tensor * add(Tensor & a, Tensor & b) = 0;
${type_method_declarations}
};

} // namespace at
2 changes: 1 addition & 1 deletion aten/src/ATen/templates/TypeDerived.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static int getPointerDevice(void* ptr) {
#endif

${Type}::${Type}()
: Type(${Backend}TensorId(), /*is_variable=*/false, /*is_undefined=*/false) {}
: TypeDefault(${Backend}TensorId(), /*is_variable=*/false, /*is_undefined=*/false) {}
ScalarType ${Type}::scalarType() const {
return ScalarType::${ScalarName};
}
Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/templates/TypeDerived.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// ${generated_comment}

#include "ATen/Type.h"
#include "ATen/TypeDefault.h"
#include "ATen/Context.h"
#include "ATen/TensorMethods.h"
#include "ATen/CheckGenerator.h"
Expand All @@ -15,7 +15,7 @@

namespace at {

struct ${Type} final : public Type {
struct ${Type} final : public TypeDefault {
explicit ${Type}();
virtual ScalarType scalarType() const override;
virtual Backend backend() const override;
Expand Down
Loading