-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-introduce "XLA_USE_32BIT_LONG" flag
- Loading branch information
1 parent
1c89675
commit 6186c82
Showing
3 changed files
with
105 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,91 @@ | ||
import os | ||
import sys | ||
import unittest | ||
|
||
import torch | ||
import torch_xla | ||
import torch_xla.core.xla_model as xm | ||
import torch_xla.utils.utils as xu | ||
import unittest | ||
|
||
class XlaDataTypeTest(unittest.TestCase): | ||
def setUp(cls): | ||
cls.original_env = { | ||
'XLA_USE_BF16': os.environ.get('XLA_USE_BF16'), | ||
'XLA_DOWNCAST_BF16': os.environ.get('XLA_DOWNCAST_BF16'), | ||
'XLA_USE_FP16': os.environ.get('XLA_USE_FP16'), | ||
'XLA_DOWNCAST_FP16': os.environ.get('XLA_DOWNCAST_FP16'), | ||
'XLA_USE_32BIT_LONG': os.environ.get('XLA_USE_32BIT_LONG') | ||
} | ||
|
||
def check_env_flag(name, default=''): | ||
return os.getenv(name, default).upper() in ['ON', '1', 'YES', 'TRUE', 'Y'] | ||
def tearDown(self): | ||
for key, value in self.original_env.items(): | ||
if value is None: | ||
os.environ.pop(key, None) | ||
else: | ||
os.environ[key] = value | ||
|
||
def _set_env(self, **kwargs): | ||
for key, value in kwargs.items(): | ||
os.environ[key] = value | ||
|
||
class XlaDataTypeTest(unittest.TestCase): | ||
def _test_datatype(self, dtype, expected_type, op): | ||
t1 = torch.tensor([2, 3], dtype=dtype, device=xm.xla_device()) | ||
t2 = torch.tensor([2, 3], dtype=dtype, device=xm.xla_device()) | ||
t3 = op(t1, t2) | ||
self.assertEqual(t3.dtype, dtype) | ||
|
||
hlo_text = torch_xla._XLAC._get_xla_tensors_text([t3]) | ||
device_data_hlo = hlo_text.split('\n')[2] | ||
self.assertIn('xla::device_data', device_data_hlo) | ||
self.assertIn(expected_type, device_data_hlo) | ||
|
||
def test_datatype_f32(self): | ||
t1 = torch.tensor([2.0, 3.0], dtype=torch.float, device=xm.xla_device()) | ||
t2 = torch.tensor([2.0, 3.0], dtype=torch.float, device=xm.xla_device()) | ||
t3 = torch.div(t1, t2, rounding_mode='floor') | ||
assert t3.dtype == torch.float | ||
def test_datatype_use_bf16(self): | ||
self._set_env(XLA_USE_BF16='1') | ||
self._test_datatype(torch.double, 'bf16', torch.floor_divide) | ||
self._test_datatype(torch.float, 'bf16', torch.floor_divide) | ||
|
||
hlo_text = torch_xla._XLAC._get_xla_tensors_text([t3]) | ||
device_data_hlo = hlo_text.split('\n')[1] | ||
assert 'xla::device_data' in device_data_hlo, device_data_hlo | ||
if check_env_flag('XLA_USE_BF16') or check_env_flag('XLA_DOWNCAST_BF16'): | ||
assert 'bf16' in device_data_hlo, device_data_hlo | ||
elif check_env_flag('XLA_USE_FP16') or check_env_flag('XLA_DOWNCAST_FP16'): | ||
assert 'f16' in device_data_hlo, device_data_hlo | ||
else: | ||
assert 'f32' in device_data_hlo, device_data_hlo | ||
def test_datatype_use_fp16(self): | ||
self._set_env(XLA_USE_FP16='1') | ||
self._test_datatype(torch.double, 'bf16', torch.floor_divide) | ||
self._test_datatype(torch.float, 'f16', torch.floor_divide) | ||
|
||
def test_datatype_f64(self): | ||
t1 = torch.tensor([2.0, 3.0], dtype=torch.double, device=xm.xla_device()) | ||
t2 = torch.tensor([2.0, 3.0], dtype=torch.double, device=xm.xla_device()) | ||
t3 = torch.div(t1, t2, rounding_mode='floor') | ||
assert t3.dtype == torch.double | ||
def test_datatype_downcast_bf16(self): | ||
self._set_env(XLA_DOWNCAST_BF16='1') | ||
self._test_datatype(torch.double, 'bf16', torch.floor_divide) | ||
self._test_datatype(torch.float, 'bf16', torch.floor_divide) | ||
|
||
hlo_text = torch_xla._XLAC._get_xla_tensors_text([t3]) | ||
device_data_hlo = hlo_text.split('\n')[1] | ||
assert 'xla::device_data' in device_data_hlo, device_data_hlo | ||
if check_env_flag('XLA_USE_BF16'): | ||
assert 'bf16' in device_data_hlo, device_data_hlo | ||
elif check_env_flag('XLA_USE_FP16'): | ||
assert 'f16' in device_data_hlo, device_data_hlo | ||
elif check_env_flag('XLA_DOWNCAST_BF16') or check_env_flag( | ||
'XLA_DOWNCAST_FP16'): | ||
assert 'f32' in device_data_hlo, device_data_hlo | ||
else: | ||
assert 'f64' in device_data_hlo, device_data_hlo | ||
def test_datatype_downcast_fp16(self): | ||
self._set_env(XLA_DOWNCAST_FP16='1') | ||
self._test_datatype(torch.double, 'f16', torch.floor_divide) | ||
self._test_datatype(torch.float, 'f16', torch.floor_divide) | ||
|
||
def test_module_to_dtype(self): | ||
device = torch_xla.device() | ||
linear = torch.nn.Linear( | ||
5, 10, dtype=torch.float32).to(device).to(torch.bfloat16) | ||
input = torch.randn( | ||
10, | ||
5, | ||
).to(device).to(torch.bfloat16) | ||
xm.mark_step() | ||
res = linear(input) | ||
def test_datatype_use_32bit_long(self): | ||
self._set_env(XLA_USE_32BIT_LONG='1') | ||
self._test_datatype(torch.int64, 's32', torch.add) | ||
self._test_datatype(torch.uint64, 'u32', torch.add) | ||
|
||
hlo_text = torch_xla._XLAC._get_xla_tensors_text([res]) | ||
res_hlo = hlo_text.split('\n')[-3] | ||
assert 'bf16' in res_hlo, res_hlo | ||
def test_module_to_dtype(self): | ||
device = torch_xla.device() | ||
linear = torch.nn.Linear(5, 10, dtype=torch.float32).to(device).to(torch.bfloat16) | ||
input = torch.randn(10, 5).to(device).to(torch.bfloat16) | ||
xm.mark_step() | ||
res = linear(input) | ||
|
||
linear_weight_hlo = torch_xla._XLAC._get_xla_tensors_text([linear.weight | ||
]).split('\n')[-3] | ||
assert 'bf16' in linear_weight_hlo, linear_weight_hlo | ||
hlo_text = torch_xla._XLAC._get_xla_tensors_text([res]) | ||
res_hlo = hlo_text.split('\n')[-3] | ||
self.assertIn('bf16', res_hlo) | ||
|
||
linear_weight_hlo = torch_xla._XLAC._get_xla_tensors_text([linear.weight]).split('\n')[-3] | ||
self.assertIn('bf16', linear_weight_hlo) | ||
|
||
if __name__ == '__main__': | ||
test = unittest.main() | ||
sys.exit(0 if test.result.wasSuccessful() else 1) | ||
suite = unittest.TestSuite() | ||
suite.addTest(XlaDataTypeTest("test_datatype_use_bf16")) | ||
suite.addTest(XlaDataTypeTest("test_datatype_use_fp16")) | ||
suite.addTest(XlaDataTypeTest("test_datatype_downcast_bf16")) | ||
suite.addTest(XlaDataTypeTest("test_datatype_downcast_fp16")) | ||
suite.addTest(XlaDataTypeTest("test_datatype_use_32bit_long")) | ||
suite.addTest(XlaDataTypeTest("test_module_to_dtype")) | ||
runner = unittest.TextTestRunner(failfast=True) | ||
result = runner.run(suite) | ||
sys.exit(0 if result.wasSuccessful() else 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters