Open
feat(optim): 为 npu_bert_apply_adam 增加 Ascend950 ACLNN 通路 #6194
feat(optim): 为 npu_bert_apply_adam 增加 Ascend950 ACLNN 通路 #6194
Open
陈展熹Add on 11 days ago
Total: 3 files changed+157-0
@@ -5989,10 +5989,12 @@ custom:
5989 5989 
5990 - func: npu_bert_apply_adam(Scalar lr, Scalar beta1, Scalar beta2, Scalar epsilon, Tensor grad, Scalar max_grad_norm, Scalar global_grad_norm, Scalar weight_decay, Scalar? step_size=None, int adam_mode=0) -> (Tensor var, Tensor m, Tensor v)5990 - func: npu_bert_apply_adam(Scalar lr, Scalar beta1, Scalar beta2, Scalar epsilon, Tensor grad, Scalar max_grad_norm, Scalar global_grad_norm, Scalar weight_decay, Scalar? step_size=None, int adam_mode=0) -> (Tensor var, Tensor m, Tensor v)
5991 acl_op: all_version5991 acl_op: all_version
5992+ op_api: all_version
5992 exposed: all_version5993 exposed: all_version
5993 5994 
5994 - func: npu_bert_apply_adam.out(Scalar lr, Scalar beta1, Scalar beta2, Scalar epsilon, Tensor grad, Scalar max_grad_norm, Scalar global_grad_norm, Scalar weight_decay, Scalar? step_size=None, int adam_mode=0, *, Tensor(a!) var, Tensor(b!) m, Tensor(c!) v) -> (Tensor(a!), Tensor(b!), Tensor(c!))5995 - func: npu_bert_apply_adam.out(Scalar lr, Scalar beta1, Scalar beta2, Scalar epsilon, Tensor grad, Scalar max_grad_norm, Scalar global_grad_norm, Scalar weight_decay, Scalar? step_size=None, int adam_mode=0, *, Tensor(a!) var, Tensor(b!) m, Tensor(c!) v) -> (Tensor(a!), Tensor(b!), Tensor(c!))
5995 acl_op: all_version5996 acl_op: all_version
5997+ op_api: all_version
5996 exposed: all_version5998 exposed: all_version
5997 5999 
5998 - func: npu_binary_cross_entropy_with_logits_backward(Tensor grad_output, Tensor self, Tensor target, Tensor? weight_opt, Tensor? pos_weight_opt, int reduction) -> Tensor6000 - func: npu_binary_cross_entropy_with_logits_backward(Tensor grad_output, Tensor self, Tensor target, Tensor? weight_opt, Tensor? pos_weight_opt, int reduction) -> Tensor
@@ -0,0 +1,88 @@
1+// Copyright (c) 2026 Huawei Technologies Co., Ltd
2+// All rights reserved.
3+//
4+// Licensed under the BSD 3-Clause License (the "License");
5+// you may not use this file except in compliance with the License.
6+// You may obtain a copy of the License at
7+//
8+// https://opensource.org/licenses/BSD-3-Clause
9+//
10+// Unless required by applicable law or agreed to in writing, software
11+// distributed under the License is distributed on an "AS IS" BASIS,
12+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+// See the License for the specific language governing permissions and
14+// limitations under the License.
15+ 
16+#include "op_plugin/OpApiInterface.h"
17+#include "op_plugin/utils/op_api_common.h"
18+#include "op_plugin/AclOpsInterface.h"
19+ 
20+namespace op_api {
21+using npu_preparation = at_npu::native::OpPreparation;
22+ 
23+std::tuple<at::Tensor, at::Tensor, at::Tensor> npu_bert_apply_adam(
24+ const at::Scalar& lr,
25+ const at::Scalar& beta1,
26+ const at::Scalar& beta2,
27+ const at::Scalar& epsilon,
28+ const at::Tensor& grad,
29+ const at::Scalar& max_grad_norm,
30+ const at::Scalar& global_grad_norm,
31+ const at::Scalar& weight_decay,
32+ const c10::optional<at::Scalar>& step_size,
33+ int64_t adam_mode) {
34+ TORCH_CHECK(false, "npu_bert_apply_adam is not implemented for Tensor" + OPS_ERROR(ErrCode::PARAM));
35+}
36+ 
37+std::tuple<at::Tensor&, at::Tensor&, at::Tensor&> npu_bert_apply_adam_out(
38+ const at::Scalar& lr,
39+ const at::Scalar& beta1,
40+ const at::Scalar& beta2,
41+ const at::Scalar& epsilon,
42+ const at::Tensor& grad,
43+ const at::Scalar& max_grad_norm,
44+ const at::Scalar& global_grad_norm,
45+ const at::Scalar& weight_decay,
46+ const c10::optional<at::Scalar>& step_size,
47+ int64_t adam_mode,
48+ at::Tensor& var,
49+ at::Tensor& m,
50+ at::Tensor& v) {
51+ DO_COMPATIBILITY(
52+ aclnnApplyAdamV2,
53+ acl_op::npu_bert_apply_adam_out(
54+ lr, beta1, beta2, epsilon, grad, max_grad_norm, global_grad_norm,
55+ weight_decay, step_size, adam_mode, var, m, v));
56+ 
57+ if (!c10_npu::IsAclnnOnly()) {
58+ return acl_op::npu_bert_apply_adam_out(
59+ lr, beta1, beta2, epsilon, grad, max_grad_norm, global_grad_norm,
60+ weight_decay, step_size, adam_mode, var, m, v);
61+ }
62+ 
63+ const auto scalar_type = var.scalar_type();
64+ const auto device = var.device();
65+ at::Tensor lr_tensor = npu_preparation::copy_scalar_to_device(lr, scalar_type, device);
66+ at::Tensor beta1_tensor = npu_preparation::copy_scalar_to_device(beta1, scalar_type, device);
67+ at::Tensor beta2_tensor = npu_preparation::copy_scalar_to_device(beta2, scalar_type, device);
68+ at::Tensor epsilon_tensor = npu_preparation::copy_scalar_to_device(epsilon, scalar_type, device);
69+ at::Tensor max_grad_norm_tensor = npu_preparation::copy_scalar_to_device(max_grad_norm, scalar_type, device);
70+ at::Tensor global_grad_norm_tensor = npu_preparation::copy_scalar_to_device(global_grad_norm, scalar_type, device);
71+ at::Tensor weight_decay_tensor = npu_preparation::copy_scalar_to_device(weight_decay, scalar_type, device);
72+ c10::optional<at::Tensor> step_size_tensor;
73+ if (step_size.has_value()) {
74+ step_size_tensor = npu_preparation::copy_scalar_to_device(step_size.value(), scalar_type, device);
75+ } else {
76+ // mBART defaults to lr; Adam ignores this input. Older tiling requires its shape.
77+ step_size_tensor = lr_tensor;
78+ }
79+ std::string adam_mode_str = adam_mode == 0 ? "adam" : "mbart_adam";
80+ char* adam_mode_ptr = const_cast<char*>(adam_mode_str.c_str());
81+ 
82+ EXEC_NPU_CMD(
83+ aclnnApplyAdamV2, var, m, v, lr_tensor, beta1_tensor, beta2_tensor, epsilon_tensor,
84+ grad, max_grad_norm_tensor, global_grad_norm_tensor, weight_decay_tensor,
85+ step_size_tensor, adam_mode_ptr);
86+ return std::tie(var, m, v);
87+}
88+} // namespace op_api
@@ -2,9 +2,76 @@ import torch
2import torch_npu2import torch_npu
3 3 
4from torch_npu.testing.testcase import TestCase, run_tests4from torch_npu.testing.testcase import TestCase, run_tests
5+from torch_npu.testing.common_utils import SupportedDevices
5 6 
6 7 
7class TestNpuBertApplyAdam(TestCase):8class TestNpuBertApplyAdam(TestCase):
9+ def test_npu_bert_apply_adam_updates_states_in_place(self):
10+ # beta1=beta2=0.5 keeps the expected moments exact in both dtypes.
11+ # With grad=[2, -4], m=[0, 0], v=[4, 16], the update is [0.4, -4/9].
12+ cases = (
13+ # mode, step_size, weight_decay, max_norm, global_norm, var, m, v
14+ (0, None, 0.0, -1.0, 0.0, [1.9, -35.0 / 9.0], [1.0, -2.0], [4.0, 16.0]),
15+ (0, None, 0.5, -1.0, 0.0, [1.65, -61.0 / 18.0], [1.0, -2.0], [4.0, 16.0]),
16+ (0, None, 0.0, 2.0, 2.0,
17+ [2.0 - 0.125 / (2.5 ** 0.5 + 0.5), -4.0 + 0.25 / (10.0 ** 0.5 + 0.5)],
18+ [0.5, -1.0], [2.5, 10.0]),
19+ (1, 0.125, 0.0, -1.0, 0.0, [1.95, -71.0 / 18.0], [1.0, -2.0], [4.0, 16.0]),
20+ (2, 0.125, 0.0, -1.0, 0.0, [1.95, -71.0 / 18.0], [1.0, -2.0], [4.0, 16.0]),
21+ )
22+ for dtype in (torch.float16, torch.float32):
23+ for mode, step_size, decay, max_norm, global_norm, var_expected, m_expected, v_expected in cases:
24+ with self.subTest(dtype=dtype, mode=mode, step_size=step_size, decay=decay, max_norm=max_norm):
25+ var = torch.tensor([2.0, -4.0], dtype=dtype, device="npu")
26+ m = torch.tensor([0.0, 0.0], dtype=dtype, device="npu")
27+ v = torch.tensor([4.0, 16.0], dtype=dtype, device="npu")
28+ grad = torch.tensor([2.0, -4.0], dtype=dtype, device="npu")
29+ states = (var, m, v)
30+ pointers = tuple(state.data_ptr() for state in states)
31+ 
32+ outputs = torch_npu.npu_bert_apply_adam(
33+ 0.25, 0.5, 0.5, 0.5, grad, max_norm, global_norm, decay,
34+ step_size=step_size, adam_mode=mode, out=states)
35+ 
36+ for output, state, pointer, expected in zip(
37+ outputs, states, pointers, (var_expected, m_expected, v_expected)):
38+ self.assertEqual(output.data_ptr(), pointer)
39+ self.assertEqual(state.data_ptr(), pointer)
40+ self.assertEqual(output.dtype, dtype)
41+ self.assertRtolEqual(state.cpu(), torch.tensor(expected, dtype=dtype))
42+ self.assertRtolEqual(grad.cpu(), torch.tensor([2.0, -4.0], dtype=dtype))
43+ 
44+ @SupportedDevices(["Ascend950"])
45+ def test_npu_bert_apply_adam_aclnn_mode_semantics(self):
46+ # Legacy ACL kernels differ for Adam's explicit step_size and mBART decay.
47+ # These numerical expectations apply to the Ascend950 ACLNN implementation.
48+ cases = (
49+ # mode, step_size, max_norm, global_norm, decay, var
50+ (0, 0.125, -1.0, 0.0, 0.0, [1.9, -35.0 / 9.0]),
51+ (1, 0.125, 1.0, 2.0, 0.5, [1.7, -31.0 / 9.0]),
52+ (1, None, 1.0, 2.0, 0.5, [1.65, -61.0 / 18.0]),
53+ )
54+ for dtype in (torch.float16, torch.float32):
55+ for mode, step_size, max_norm, global_norm, decay, var_expected in cases:
56+ with self.subTest(dtype=dtype, mode=mode, step_size=step_size, decay=decay):
57+ states = tuple(torch.tensor(values, dtype=dtype, device="npu")
58+ for values in ([2.0, -4.0], [0.0, 0.0], [4.0, 16.0]))
59+ pointers = tuple(state.data_ptr() for state in states)
60+ grad = torch.tensor([2.0, -4.0], dtype=dtype, device="npu")
61+ outputs = torch_npu.npu_bert_apply_adam(
62+ 0.25, 0.5, 0.5, 0.5, grad, max_norm, global_norm, decay,
63+ step_size=step_size, adam_mode=mode, out=states)
64+ expected = (var_expected, [1.0, -2.0], [4.0, 16.0])
65+ for output, state, pointer, values in zip(outputs, states, pointers, expected):
66+ self.assertEqual(output.data_ptr(), pointer)
67+ self.assertEqual(state.data_ptr(), pointer)
68+ self.assertRtolEqual(state.cpu(), torch.tensor(values, dtype=dtype))
69+ 
70+ def test_npu_bert_apply_adam_requires_out(self):
71+ grad = torch.tensor([1.0], device="npu")
72+ with self.assertRaisesRegex(RuntimeError, "npu_bert_apply_adam is not implemented for Tensor"):
73+ torch_npu.npu_bert_apply_adam(0.25, 0.5, 0.5, 0.5, grad, -1.0, 0.0, 0.0)
74+ 
8 def test_npu_bert_apply_adam(self):75 def test_npu_bert_apply_adam(self):
9 seed = 376 seed = 3
10 torch.manual_seed(seed)77 torch.manual_seed(seed)