Skip to content

Commit 85e513b

Browse files
committed
fix quantizer dtype
1 parent 7f788a3 commit 85e513b

5 files changed

Lines changed: 43 additions & 0 deletions

File tree

transformer_engine/plugin/core/backends/vendor/cuda/cuda.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ def quantize(
166166
noop: Optional[torch.Tensor] = None,
167167
) -> Any:
168168
tex = self._get_tex()
169+
# Quantizer.dtype may originate from a different TE extension module
170+
# (e.g. transformer_engine_torch vs transformer_engine_torch_nv) which leads
171+
# to a pybind enum cast failure. Normalize it to this backend's `tex.DType`.
172+
try:
173+
if quantizer is not None and hasattr(quantizer, "dtype") and hasattr(tex, "DType"):
174+
qdtype = quantizer.dtype
175+
if qdtype is not None:
176+
quantizer.dtype = tex.DType(int(qdtype))
177+
except Exception:
178+
pass
179+
169180
return tex.quantize(tensor, quantizer, output, noop)
170181

171182
def dequantize(

transformer_engine/plugin/core/backends/vendor/hygon/hygon.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ def quantize(
145145
noop: Optional[torch.Tensor] = None,
146146
) -> Any:
147147
tex = self._get_tex()
148+
try:
149+
if quantizer is not None and hasattr(quantizer, "dtype") and hasattr(tex, "DType"):
150+
qdtype = quantizer.dtype
151+
if qdtype is not None:
152+
if qdtype.__class__ is not tex.DType:
153+
quantizer.dtype = tex.DType(int(qdtype))
154+
except Exception:
155+
pass
148156
return tex.quantize(tensor, quantizer, output, noop)
149157

150158
def dequantize(

transformer_engine/plugin/core/backends/vendor/iluvatar/iluvatar.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ def quantize(
171171
noop: Optional[torch.Tensor] = None,
172172
) -> Any:
173173
tex = self._get_tex()
174+
try:
175+
if quantizer is not None and hasattr(quantizer, "dtype") and hasattr(tex, "DType"):
176+
qdtype = quantizer.dtype
177+
if qdtype is not None:
178+
if qdtype.__class__ is not tex.DType:
179+
quantizer.dtype = tex.DType(int(qdtype))
180+
except Exception:
181+
pass
174182
return tex.quantize(tensor, quantizer, output, noop)
175183

176184
def dequantize(

transformer_engine/plugin/core/backends/vendor/metax/metax.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ def quantize(
126126
noop: Optional[torch.Tensor] = None,
127127
) -> Any:
128128
tex = self._get_tex()
129+
try:
130+
if quantizer is not None and hasattr(quantizer, "dtype") and hasattr(tex, "DType"):
131+
qdtype = quantizer.dtype
132+
if qdtype is not None:
133+
if qdtype.__class__ is not tex.DType:
134+
quantizer.dtype = tex.DType(int(qdtype))
135+
except Exception:
136+
pass
129137
return tex.quantize(tensor, quantizer, output, noop)
130138

131139
def dequantize(

transformer_engine/plugin/core/backends/vendor/musa/musa.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ def quantize(
138138
noop: Optional[torch.Tensor] = None,
139139
) -> Any:
140140
tex = self._get_tex()
141+
try:
142+
if quantizer is not None and hasattr(quantizer, "dtype") and hasattr(tex, "DType"):
143+
qdtype = quantizer.dtype
144+
if qdtype is not None:
145+
if qdtype.__class__ is not tex.DType:
146+
quantizer.dtype = tex.DType(int(qdtype))
147+
except Exception:
148+
pass
141149
return tex.quantize(tensor, quantizer, output, noop)
142150

143151
def dequantize(

0 commit comments

Comments
 (0)