Skip to content

Delete context and get_context from Type. #11001

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

Closed
wants to merge 5 commits into from
Closed
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
4 changes: 2 additions & 2 deletions aten/src/ATen/UndefinedType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace at {

UndefinedType::UndefinedType(Context* context)
: Type(context, UndefinedTensorId(), /*is_variable=*/false, /*is_undefined=*/true) {}
UndefinedType::UndefinedType()
: Type(UndefinedTensorId(), /*is_variable=*/false, /*is_undefined=*/true) {}
ScalarType UndefinedType::scalarType() const {
return ScalarType::Undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/UndefinedType.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace at {

struct UndefinedType final : public Type {
explicit UndefinedType(Context* context);
explicit UndefinedType();
virtual ScalarType scalarType() const override;
virtual Backend backend() const override;
virtual bool is_cuda() const override;
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def check_all_files_written(self):
TYPE_REGISTER = CodeTemplate("""\
context->type_registry[static_cast<int>(Backend::${backend})]
[static_cast<int>(ScalarType::${scalar_type})]
.reset(new ${type_name}(context));
.reset(new ${type_name}());
detail::getVariableHooks().registerVariableTypeFor(context, Backend::${backend}, ScalarType::${scalar_type});
""")

Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/native/cuda/Gesv.cu
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void magmaGesvBatched<double>(
}

static magma_queue_t createMagmaQueue(const Tensor& tensor) {
auto& context = tensor.type().get_context();
auto& context = at::globalContext();
magma_queue_t magma_queue;
magma_queue_create_from_cuda(
tensor.get_device(),
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/templates/RegisterCPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace at {
void register_cpu_types(Context * context) {
${cpu_type_registrations}
context->type_registry[static_cast<int>(Backend::Undefined)]
[static_cast<int>(ScalarType::Undefined)].reset(new UndefinedType(context));
[static_cast<int>(ScalarType::Undefined)].reset(new UndefinedType());
}

} // namespace at
6 changes: 3 additions & 3 deletions aten/src/ATen/templates/SparseTypeDerived.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

namespace at {

${Type}::${Type}(Context* context)
: Type(context, ${Backend}TensorId(), /*is_variable=*/false, /*is_undefined=*/false) {}
${Type}::${Type}()
: Type(${Backend}TensorId(), /*is_variable=*/false, /*is_undefined=*/false) {}
ScalarType ${Type}::scalarType() const {
return ScalarType::${ScalarName};
}
Expand Down Expand Up @@ -58,7 +58,7 @@ Storage ${Type}::unsafeStorageFromTH(void * th_pointer, bool retain) const {
AT_ERROR("unsafeTensorFromTH not supported on sparse");
}
std::unique_ptr<Generator> ${Type}::generator() const {
return std::unique_ptr<Generator>(new ${Generator}(context));
return std::unique_ptr<Generator>(new ${Generator}(&at::globalContext()));
}

const char * ${Type}::toString() const {
Expand Down
4 changes: 2 additions & 2 deletions aten/src/ATen/templates/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ Tensor Type::copy(const Tensor & src, bool non_blocking) const {
}

Type & Type::toBackend(Backend b) const {
return context->getType(b,scalarType());
return at::globalContext().getType(b,scalarType());
}
Type & Type::toScalarType(ScalarType s) const {
return context->getType(backend(),s);
return at::globalContext().getType(backend(),s);
}
static std::vector<int64_t> defaultStrides(IntList sizes) {
std::vector<int64_t> strides(sizes.size());
Expand Down
7 changes: 2 additions & 5 deletions aten/src/ATen/templates/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ enum class TypeID {
};

struct AT_API Type {
explicit Type(Context* context, TensorTypeId type_id, bool is_variable, bool is_undefined)
: context(context), type_id_(type_id), is_variable_(is_variable), is_undefined_(is_undefined) {}
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 Down Expand Up @@ -79,8 +79,6 @@ struct AT_API Type {
Type & cuda() const {
return this->toBackend(at::backendToCUDA(this->backend()));
}
Context& get_context() const { return *context; }

// contiguous IDs for all types in the system
// for external dispatch
virtual TypeID ID() const = 0;
Expand Down Expand Up @@ -111,7 +109,6 @@ struct AT_API Type {
// virtual Tensor * add(Tensor & a, Tensor & b) = 0;
${type_method_declarations}
protected:
Context* context;
TensorTypeId type_id_;
bool is_variable_;
bool is_undefined_;
Expand Down
6 changes: 3 additions & 3 deletions aten/src/ATen/templates/TypeDerived.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ static int getPointerDevice(void* ptr) {
}
#endif

${Type}::${Type}(Context* context)
: Type(context, ${Backend}TensorId(), /*is_variable=*/false, /*is_undefined=*/false) {}
${Type}::${Type}()
: Type(${Backend}TensorId(), /*is_variable=*/false, /*is_undefined=*/false) {}
ScalarType ${Type}::scalarType() const {
return ScalarType::${ScalarName};
}
Expand Down Expand Up @@ -99,7 +99,7 @@ Storage ${Type}::unsafeStorageFromTH(void * th_pointer, bool retain) const {
return Storage((${THStorage}*) th_pointer);
}
std::unique_ptr<Generator> ${Type}::generator() const {
return std::unique_ptr<Generator>(new ${Generator}(context));
return std::unique_ptr<Generator>(new ${Generator}(&at::globalContext()));
}

const char * ${Type}::toString() const {
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/templates/TypeDerived.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace at {

struct ${Type} final : public Type {
explicit ${Type}(Context* context);
explicit ${Type}();
virtual ScalarType scalarType() const override;
virtual Backend backend() const override;
virtual bool is_cuda() const override;
Expand Down
2 changes: 1 addition & 1 deletion tools/autograd/templates/VariableType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ using namespace torch::autograd::generated;
namespace torch { namespace autograd {

VariableType::VariableType(Context* context, Type* baseType)
: Type(context, baseType->type_id(), /*is_variable=*/true, /*is_undefined=*/false)
: Type(baseType->type_id(), /*is_variable=*/true, /*is_undefined=*/false)
, baseType(baseType)
, id_(context->freshTypeID()) {
str = std::string("Variable[") + baseType->toString() + "]";
Expand Down