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

[r2.3] Include auto-sharding configs as part of hash (#6753) #6763

Merged
merged 1 commit into from
Mar 17, 2024
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
22 changes: 21 additions & 1 deletion test/spmd/test_xla_auto_sharding.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,30 @@

class XlaAutoShardingTest(test_xla_sharding_base.XlaShardingTest):

# initialized in setUpClass
hash_no_auto = ""

@classmethod
def setUpClass(cls):
xr.use_spmd(auto=True)
super().setUpClass()
assert not torch_xla._XLAC._xla_get_auto_sharding()
cls.init_test_variables(cls)
assert cls.hash_no_auto
xr.use_spmd(auto=True)

def init_test_variables(cls):
xt_no_auto = torch.ones(2, 2).to(xm.xla_device())
cls.hash_no_auto = torch_xla._XLAC._get_graph_hash([xt_no_auto + 0])

def test_auto_sharding_hashing(self):
xt = torch.ones(2, 2).to(xm.xla_device())
assert torch_xla._XLAC._xla_get_auto_sharding()
hash_auto_spmd = torch_xla._XLAC._get_graph_hash([xt + 0])
self.assertNotEqual(hash_auto_spmd, self.hash_no_auto)
os.environ['XLA_AUTO_SPMD_MESH'] = '2,2'
hash_auto_spmd2 = torch_xla._XLAC._get_graph_hash([xt + 0])
self.assertNotEqual(hash_auto_spmd, hash_auto_spmd2)
del os.environ['XLA_AUTO_SPMD_MESH']

@unittest.skipUnless(xr.device_type() in ["TPU", "CPU"],
"Auto-sharding currently supports TPU & CPU backends.")
Expand Down
18 changes: 18 additions & 0 deletions torch_xla/csrc/xla_graph_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,15 @@ torch::lazy::hash_t XLAGraphExecutor::GetGraphHash(
res_hash, torch::lazy::Hash(buffer_donor_index));
}
}
{
// Auto-sharding configs
res_hash = torch::lazy::HashCombine(
res_hash, torch::lazy::MHash(ShardingUtil::GetAutoSharding()));
res_hash = torch::lazy::HashCombine(
res_hash,
torch::lazy::StringHash(
runtime::sys_util::GetEnvString("XLA_AUTO_SPMD_MESH", "").c_str()));
}
DeviceContextArena::Get()->SaveOutputShapes(res_hash,
std::move(output_shapes));
DeviceContextArena::Get()->SaveGraphAsString(res_hash, tensors,
Expand Down Expand Up @@ -1492,6 +1501,15 @@ XLAGraphExecutor::SyncTensorsGraphInternal(
coll.hash, torch::lazy::Hash(buffer_donor_index));
}
}
{
// Auto-sharding configs
coll.hash = torch::lazy::HashCombine(
coll.hash, torch::lazy::MHash(ShardingUtil::GetAutoSharding()));
coll.hash = torch::lazy::HashCombine(
coll.hash,
torch::lazy::StringHash(
runtime::sys_util::GetEnvString("XLA_AUTO_SPMD_MESH", "").c_str()));
}

DebugUtil::SaveGraphHash(coll.hash);
TF_VLOG(4) << "Parameter sequence graph hash "
Expand Down
Loading