Skip to content

Commit 7d7e89a

Browse files
authored
Arm backend: Initial bfloat16 support. (pytorch#16740)
Add the necessary infrastructure and add support for some initial ops. Currently, only the TOSA backend supports the dtype. Signed-off-by: Erik Lundell <erik.lundell@arm.com>
1 parent a63ca92 commit 7d7e89a

67 files changed

Lines changed: 329 additions & 229 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎backends/arm/_passes/decompose_avg_pool2d_pass.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def call_operator(self, op, args, kwargs, meta):
5151
full_op, cat_op, avgpool_op, mul_op = get_decomposition(op)
5252

5353
x = args[0]
54-
full_kwargs = {"device": x.data.device}
54+
full_kwargs = {"device": x.data.device, "dtype": x.data.dtype}
5555
kernel_h, kernel_w = args[1]
5656
kernel_size = kernel_h * kernel_w
5757
if len(args) > 2 and args[2] is not None:

‎backends/arm/_passes/fuse_equal_placeholders_pass.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ def call(self, graph_module: torch.fx.GraphModule) -> PassResult:
5454
# ensure we don't merge any special case int48_t tensors with int32_t tensors
5555
# since int48_t tensors needs to be instantiated separately.
5656
is_special_dtype = node.meta.get(TosaSpecialDtype.meta_key(), None)
57-
t_cpu = tensor.detach().cpu().contiguous()
57+
t_cpu = tensor.cpu().contiguous().flatten().view(dtype=torch.uint8)
5858
data_bytes = t_cpu.numpy().tobytes()
5959
key = (
6060
is_special_dtype,
61-
str(t_cpu.dtype),
62-
tuple(t_cpu.shape),
61+
str(tensor.dtype),
62+
tuple(tensor.shape),
6363
hashlib.sha1(data_bytes, usedforsecurity=False).hexdigest(),
6464
)
65-
hash_buckets[key].append((node, t_cpu))
65+
hash_buckets[key].append((node, tensor))
6666

6767
# For each bucket with more than one entry, fuse:
6868
for nodes_tensors in hash_buckets.values():

‎backends/arm/_passes/match_arg_dtype_pass.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Arm Limited and/or its affiliates.
1+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
@@ -19,8 +19,9 @@
1919
torch.int32: 4,
2020
torch.int64: 5,
2121
torch.float16: 6,
22-
torch.float32: 7,
23-
torch.float64: 8,
22+
torch.bfloat16: 7,
23+
torch.float32: 8,
24+
torch.float64: 9,
2425
}
2526

2627

‎backends/arm/operators/op_abs.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Arm Limited and/or its affiliates.
1+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
@@ -43,8 +43,8 @@ def define_node(
4343
validate_valid_dtype(
4444
self.target,
4545
[*inputs, output],
46-
[ts.DType.INT32, ts.DType.FP32],
47-
output.tosa_spec,
46+
[ts.DType.INT32, ts.DType.FP32, ts.DType.BF16],
47+
self.tosa_spec,
4848
)
4949

5050
attr = ts.TosaSerializerAttribute()

‎backends/arm/operators/op_add.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023-2025 Arm Limited and/or its affiliates.
1+
# Copyright 2023-2026 Arm Limited and/or its affiliates.
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
@@ -43,8 +43,8 @@ def define_node(
4343
validate_valid_dtype(
4444
self.target,
4545
[*inputs, output],
46-
[ts.DType.INT32, ts.DType.FP32],
47-
output.tosa_spec,
46+
[ts.DType.INT32, ts.DType.FP32, ts.DType.BF16],
47+
self.tosa_spec,
4848
)
4949

5050
attr = ts.TosaSerializerAttribute()

‎backends/arm/operators/op_amax.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Arm Limited and/or its affiliates.
1+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
@@ -42,7 +42,7 @@ def define_node(
4242
self.target,
4343
[inputs[0], output],
4444
[ts.DType.INT8, ts.DType.INT16, ts.DType.INT32, ts.DType.FP32],
45-
output.tosa_spec,
45+
self.tosa_spec,
4646
)
4747

4848
input = inputs[0]

‎backends/arm/operators/op_amin.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Arm Limited and/or its affiliates.
1+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
@@ -42,7 +42,7 @@ def define_node(
4242
self.target,
4343
[inputs[0], output],
4444
[ts.DType.INT8, ts.DType.INT16, ts.DType.INT32, ts.DType.FP32],
45-
output.tosa_spec,
45+
self.tosa_spec,
4646
)
4747

4848
input = inputs[0]

‎backends/arm/operators/op_any.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Arm Limited and/or its affiliates.
1+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
@@ -37,7 +37,7 @@ def define_node(
3737
validate_num_inputs(self.target, inputs, 3)
3838
validate_same_dtype(self.target, [inputs[0], output], ts)
3939
validate_valid_dtype(
40-
self.target, [inputs[0], output], ts.DType.BOOL, output.tosa_spec
40+
self.target, [inputs[0], output], ts.DType.BOOL, self.tosa_spec
4141
)
4242

4343
input_shape = list(inputs[0].shape)

‎backends/arm/operators/op_avg_pool2d.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023-2025 Arm Limited and/or its affiliates.
1+
# Copyright 2023-2026 Arm Limited and/or its affiliates.
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
@@ -115,14 +115,14 @@ def define_node(
115115
) -> None:
116116
validate_num_inputs(self.target, inputs, [3, 4, 5, 6, 7])
117117
validate_same_dtype(self.target, [inputs[0], output], ts)
118-
supported_dtypes = [ts.DType.INT8, ts.DType.FP32]
118+
supported_dtypes = [ts.DType.INT8, ts.DType.FP32, ts.DType.BF16]
119119
if self.tosa_spec.support_extension("int16"):
120120
supported_dtypes.append(ts.DType.INT16)
121121
validate_valid_dtype(
122122
self.target,
123123
[inputs[0], output],
124124
supported_dtypes,
125-
output.tosa_spec,
125+
self.tosa_spec,
126126
)
127127

128128
if inputs[0].dtype == ts.DType.INT8 or inputs[0].dtype == ts.DType.INT16:

‎backends/arm/operators/op_bitwise_not.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Arm Limited and/or its affiliates.
1+
# Copyright 2025-2026 Arm Limited and/or its affiliates.
22
#
33
# This source code is licensed under the BSD-style license found in the
44
# LICENSE file in the root directory of this source tree.
@@ -46,7 +46,7 @@ def define_node(
4646
self.target,
4747
[*inputs, output],
4848
[ts.DType.INT8, ts.DType.INT16, ts.DType.INT32],
49-
output.tosa_spec,
49+
self.tosa_spec,
5050
)
5151

5252
attr = ts.TosaSerializerAttribute()

0 commit comments

Comments
 (0)