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

Added pow to autocast policy and unit test #8684

Merged
merged 2 commits into from
Feb 7, 2025
Merged

Added pow to autocast policy and unit test #8684

merged 2 commits into from
Feb 7, 2025

Conversation

avizon-aws
Copy link
Collaborator

Pow is a non linear operation and must be done in higher precision i.e. FP32.

Before this CR:

import torch
import torch_xla.core.xla_model as xm
input=torch.rand((5,5), dtype=torch.bfloat16).to(xm.xla_device())
with torch.autocast("xla"):
    output=input.pow(2) // this should happen in FP32
xm.mark_step()

HloModule SyncTensorsGraph.6, entry_computation_layout={(bf16[5,5]{1,0})->(bf16[5,5]{1,0})}

ENTRY %SyncTensorsGraph.6 (p0.2: bf16[5,5]) -> (bf16[5,5]) {
  %p0.2 = bf16[5,5]{1,0} parameter(0), frontend_attributes={neff_input_names="input0"}, metadata={op_type="xla__device_data" op_name="xla__device_data" source_file="/shared/avizon/avi_trial_exps/test.py" source_line=4}
  %multiply = bf16[5,5]{1,0} multiply(bf16[5,5]{1,0} %p0.2, bf16[5,5]{1,0} %p0.2), metadata={op_type="aten__pow" op_name="aten__pow" source_file="/shared/avizon/avi_trial_exps/test.py" source_line=7}
  ROOT %tuple.5 = (bf16[5,5]{1,0}) tuple(bf16[5,5]{1,0} %multiply), frontend_attributes={neff_output_names="output0"}
}

//Its happening in BF16, which is wrong

after the CR:

HloModule SyncTensorsGraph.7, entry_computation_layout={(bf16[5,5]{1,0})->(f32[5,5]{1,0})}

ENTRY %SyncTensorsGraph.7 (p0.2: bf16[5,5]) -> (f32[5,5]) {
  %p0.2 = bf16[5,5]{1,0} parameter(0), frontend_attributes={neff_input_names="input0"}, metadata={op_type="xla__device_data" op_name="xla__device_data" source_file="/shared/avizon/avi_trial_exps/test.py" source_line=4}
  %convert.3 = f32[5,5]{1,0} convert(bf16[5,5]{1,0} %p0.2), metadata={op_type="xla__cast" op_name="xla__cast" source_file="/shared/avizon/avi_trial_exps/test.py" source_line=7}
  %multiply = f32[5,5]{1,0} multiply(f32[5,5]{1,0} %convert.3, f32[5,5]{1,0} %convert.3), metadata={op_type="aten__pow" op_name="aten__pow" source_file="/shared/avizon/avi_trial_exps/test.py" source_line=7}
  ROOT %tuple.6 = (f32[5,5]{1,0}) tuple(f32[5,5]{1,0} %multiply), frontend_attributes={neff_output_names="output0"}
}

//The inputs have been upcasted

@lsy323 lsy323 merged commit afaf0d0 into master Feb 7, 2025
12 checks passed
@lsy323 lsy323 deleted the autocast_pow branch February 7, 2025 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants