Skip to content
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

Add support for torch.set_default_device when initializing model parameters #1158

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
12 changes: 6 additions & 6 deletions fairscale/nn/model_parallel/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def __init__(
self.num_embeddings_per_partition = self.vocab_end_index - self.vocab_start_index

# Allocate weights.
self.weight = Parameter(torch.Tensor(self.num_embeddings_per_partition, self.embedding_dim))
self.weight = Parameter(torch.empty(self.num_embeddings_per_partition, self.embedding_dim))
# And initialize.
_initialize_affine_weight(
self.weight, self.num_embeddings, self.embedding_dim, self.num_embeddings_per_partition, 0, init_method
Expand Down Expand Up @@ -187,7 +187,7 @@ def __init__(
self.embedding_dim_per_partition = divide_and_check_no_remainder(self.embedding_dim, world_size)

# Allocate weights.
self.weight = Parameter(torch.Tensor(self.num_embeddings, self.embedding_dim_per_partition))
self.weight = Parameter(torch.empty(self.num_embeddings, self.embedding_dim_per_partition))
# And initialize.
_initialize_affine_weight(
self.weight,
Expand Down Expand Up @@ -259,9 +259,9 @@ def __init__(
# Parameters.
# Note: torch.nn.functional.linear performs XA^T + b and as a result
# we allocate the transpose.
self.weight = Parameter(torch.Tensor(self.output_size_per_partition, self.in_features))
self.weight = Parameter(torch.empty(self.output_size_per_partition, self.in_features))
if bias:
self.bias = Parameter(torch.Tensor(self.output_size_per_partition))
self.bias = Parameter(torch.empty(self.output_size_per_partition))
# Always initialize bias to zero.
with torch.no_grad():
self.bias.zero_()
Expand Down Expand Up @@ -346,9 +346,9 @@ def __init__(
# Parameters.
# Note: torch.nn.functional.linear performs XA^T + b and as a result
# we allocate the transpose.
self.weight = Parameter(torch.Tensor(self.out_features, self.input_size_per_partition))
self.weight = Parameter(torch.empty(self.out_features, self.input_size_per_partition))
if bias:
self.bias = Parameter(torch.Tensor(self.out_features))
self.bias = Parameter(torch.empty(self.out_features))
# Always initialize bias to zero.
with torch.no_grad():
self.bias.zero_()
Expand Down