Skip to content

Commit e684565

Browse files
committed
Add option to use bf16 in PT sdp in examples
The new option --sdp_on_bf16 allows pyTorch to use reduced precision in sdp in the math backend.
1 parent f488ab6 commit e684565

4 files changed

Lines changed: 22 additions & 0 deletions

File tree

examples/stable-diffusion/text_to_image_generation.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ def main():
226226
),
227227
)
228228
parser.add_argument("--bf16", action="store_true", help="Whether to perform generation in bf16 precision.")
229+
parser.add_argument(
230+
"--sdp_on_bf16", action="store_true", help="Allow pyTorch to use reduced precision in the SDPA math backend"
231+
)
229232
parser.add_argument(
230233
"--ldm3d", action="store_true", help="Use LDM3D to generate an image and a depth map from a given text prompt."
231234
)
@@ -316,6 +319,7 @@ def main():
316319
"use_habana": args.use_habana,
317320
"use_hpu_graphs": args.use_hpu_graphs,
318321
"gaudi_config": args.gaudi_config_name,
322+
"sdp_on_bf16": args.sdp_on_bf16,
319323
}
320324

321325
if scheduler is not None:

optimum/habana/diffusers/pipelines/pipeline_utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class GaudiDiffusionPipeline(DiffusionPipeline):
112112
bf16_full_eval (bool, defaults to `False`):
113113
Whether to use full bfloat16 evaluation instead of 32-bit.
114114
This will be faster and save memory compared to fp32/mixed precision but can harm generated images.
115+
sdp_on_bf16 (bool, defaults to `False`):
116+
Whether to allow PyTorch to use reduced precision in the SDPA math backend.
115117
"""
116118

117119
def __init__(
@@ -120,9 +122,13 @@ def __init__(
120122
use_hpu_graphs: bool = False,
121123
gaudi_config: Union[str, GaudiConfig] = None,
122124
bf16_full_eval: bool = False,
125+
sdp_on_bf16: bool = False,
123126
):
124127
DiffusionPipeline.__init__(self)
125128

129+
if sdp_on_bf16:
130+
torch._C._set_math_sdp_allow_fp16_bf16_reduction(True)
131+
126132
self.use_habana = use_habana
127133
if self.use_habana:
128134
self.use_hpu_graphs = use_hpu_graphs

optimum/habana/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ class GaudiStableDiffusionPipeline(GaudiDiffusionPipeline, StableDiffusionPipeli
131131
bf16_full_eval (bool, defaults to `False`):
132132
Whether to use full bfloat16 evaluation instead of 32-bit.
133133
This will be faster and save memory compared to fp32/mixed precision but can harm generated images.
134+
sdp_on_bf16 (bool, defaults to `False`):
135+
Whether to allow PyTorch to use reduced precision in the SDPA math backend.
134136
"""
135137

136138
def __init__(
@@ -148,13 +150,15 @@ def __init__(
148150
use_hpu_graphs: bool = False,
149151
gaudi_config: Union[str, GaudiConfig] = None,
150152
bf16_full_eval: bool = False,
153+
sdp_on_bf16: bool = False,
151154
):
152155
GaudiDiffusionPipeline.__init__(
153156
self,
154157
use_habana,
155158
use_hpu_graphs,
156159
gaudi_config,
157160
bf16_full_eval,
161+
sdp_on_bf16,
158162
)
159163

160164
# Workaround for Synapse 1.11 for full bf16

optimum/habana/transformers/training_args.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ class GaudiTrainingArguments(TrainingArguments):
300300
},
301301
)
302302

303+
sdp_on_bf16: bool = field(
304+
default=False,
305+
metadata={"help": "Allow pyTorch to use reduced precision in the SDPA math backend"},
306+
)
307+
303308
fp8: Optional[bool] = field(
304309
default=False,
305310
metadata={"help": "Whether to use fp8 for training."},
@@ -842,6 +847,9 @@ def _setup_devices(self) -> "torch.device":
842847
):
843848
gaudi_config.declare_autocast_bf16_fp32_ops()
844849

850+
if self.sdp_on_bf16:
851+
torch._C._set_math_sdp_allow_fp16_bf16_reduction(True)
852+
845853
logger.info("PyTorch: setting up devices")
846854
if not is_accelerate_available():
847855
raise ImportError(

0 commit comments

Comments
 (0)