Skip to content

Fix special scalar handling for addcdiv and addcmul #3953

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 2 commits into from
Sep 13, 2022
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
15 changes: 15 additions & 0 deletions test/test_operations_hlo.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ def test_expand(self):
hlo_text = torch_xla._XLAC._get_xla_tensors_text([b])
assert 'aten::expand' in hlo_text

def test_special_scalars_addcdiv_addcmul(self):
a = torch.rand(5, 5).to(xm.xla_device())
b = torch.rand(5, 5).to(xm.xla_device())
c = torch.rand(5, 5).to(xm.xla_device())
for op in [torch.addcdiv, torch.addcmul]:
out = op(a, b, c, value=1.0)
hlo_text = torch_xla._XLAC._get_xla_tensors_text([out])
instructions = hlo_text.split('\n')
const_hlo = instructions[1]
root_hlo = instructions[5]
assert 'prim::Constant()' in const_hlo
assert 'xla::device_data()' not in const_hlo
assert 'f32' in root_hlo
assert 'f64' not in root_hlo


if __name__ == '__main__':
torch.set_default_tensor_type('torch.FloatTensor')
Expand Down
3 changes: 3 additions & 0 deletions torch_xla/csrc/init_python_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,9 @@ absl::flat_hash_map<std::string, absl::variant<int>> ConvertDictToMap(
void MapXlaEnvVarsToLazy() {
static bool wants_frames = xla::sys_util::GetEnvBool("XLA_IR_DEBUG", false);
FLAGS_torch_lazy_ir_debug = wants_frames;
static bool no_scalars =
xla::sys_util::GetEnvBool("XLA_NO_SPECIAL_SCALARS", false);
FLAGS_torch_lazy_handle_special_scalars = !no_scalars;
}

std::string GetPyTypeString(py::handle obj) {
Expand Down
1 change: 1 addition & 0 deletions torch_xla/csrc/xla_lower_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,7 @@ xla::XlaOp BuildRoll(xla::XlaOp input, absl::Span<const int64_t> shifts,

xla::XlaOp BuildAddcdiv(xla::XlaOp input, xla::XlaOp t1, xla::XlaOp t2,
xla::XlaOp val) {
val = MaybeConvertTo(val, XlaHelpers::ShapeOfXlaOp(t1).element_type());
return XlaHelpers::PromotedAdd(
input, XlaHelpers::PromotedMul(XlaHelpers::PromotedDiv(t1, t2), val));
}
Expand Down