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

Align diopiStd to device with torch version>=2.0 #921

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions dipu/scripts/autogen_diopi_wrapper/autogen_diopi_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1203,10 +1203,25 @@ def main():
continue

# filter torch version
in_torch_vers = merged_fun_config.get("torch_ver", None)
supported_torch_ver_list = merged_fun_config.get("torch_ver", None)
cur_torch_ver = merged_fun_config.get("current_torch_ver", None)
if in_torch_vers is not None and cur_torch_ver not in in_torch_vers:
continue

if supported_torch_ver_list != None:
exclude_torch_ver_list = []
include_torch_ver_list = []
all_include = False
for supported_torch_ver in supported_torch_ver_list:
if supported_torch_ver.startswith("-"):
exclude_torch_ver_list.append(supported_torch_ver[1:])
elif supported_torch_ver == "all":
all_include = True
else:
include_torch_ver_list.append(supported_torch_ver)

if (cur_torch_ver in exclude_torch_ver_list) or (
all_include == False and (cur_torch_ver not in include_torch_ver_list)
):
continue

fun_code, register_code = functions_code_gen(merged_fun_config)

Expand Down
28 changes: 18 additions & 10 deletions dipu/scripts/autogen_diopi_wrapper/diopi_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -838,32 +838,40 @@
custom_code_at_the_beginning: |
c10::DimVector output_shape = infer_reduce_op_shape(self.sizes(), dim.value_or(c10::DimVector()), keepdim);
auto out = nodispatch::empty(output_shape, self.options());
bool unbiased = correction.value_or(1) == 1;
::diopiScalar_t correctionDiopiScalar;
const ::diopiScalar_t* correctionDiopiScalarPtr = nullptr;
if (correction.has_value()) {
correctionDiopiScalar = dipu::diopi_helper::toDiopiScalar(correction.value());
correctionDiopiScalarPtr = &correctionDiopiScalar;
}
::diopiSize_t diopi_size = toDiopiSize(dim);
interface: diopiStd(ctx, out, self, diopi_size, unbiased);
interface: diopiStd(ctx, out, self, diopi_size, correctionDiopiScalarPtr);

- schema: "std.correction_out(Tensor self, int[1]? dim=None, *, int? correction=None, bool keepdim=False, Tensor(a!) out) -> Tensor(a!)"
torch_ver: ["20000",]
custom_code_at_the_beginning: |
::diopiSize_t diopi_size = toDiopiSize(dim);
bool unbiased = correction.value_or(1) == 1;
interface: diopiStd(ctx, out, self, diopi_size, unbiased);
::diopiScalar_t correctionDiopiScalar;
const ::diopiScalar_t* correctionDiopiScalarPtr = nullptr;
if (correction.has_value()) {
correctionDiopiScalar = dipu::diopi_helper::toDiopiScalar(correction.value());
correctionDiopiScalarPtr = &correctionDiopiScalar;
}
interface: diopiStd(ctx, out, self, diopi_size, correctionDiopiScalarPtr);

- schema: "std.correction(Tensor self, int[1]? dim=None, *, Scalar? correction=None, bool keepdim=False) -> Tensor"
torch_ver: ["20100", "20101", "20202"]
torch_ver: [all, "-20000"]
custom_code_at_the_beginning: |
c10::DimVector output_shape = infer_reduce_op_shape(self.sizes(), dim.value_or(c10::DimVector()), keepdim);
auto out = nodispatch::empty(output_shape, self.options());
bool unbiased = correction.value_or(1).toLong() == 1;
::diopiSize_t diopi_size = toDiopiSize(dim);
interface: diopiStd(ctx, out, self, diopi_size, unbiased);
interface: diopiStd(ctx, out, self, diopi_size, correctionDiopiScalarPtr);

- schema: "std.correction_out(Tensor self, int[1]? dim=None, *, Scalar? correction=None, bool keepdim=False, Tensor(a!) out) -> Tensor(a!)"
torch_ver: ["20100", "20101", "20202"]
torch_ver: [all, "-20000"]
custom_code_at_the_beginning: |
::diopiSize_t diopi_size = toDiopiSize(dim);
bool unbiased = correction.value_or(1).toLong() == 1;
interface: diopiStd(ctx, out, self, diopi_size, unbiased);
interface: diopiStd(ctx, out, self, diopi_size, correctionDiopiScalarPtr)

- schema: "linear_backward(Tensor input, Tensor grad_output, Tensor weight, bool[3] output_mask) -> (Tensor grad_input, Tensor grad_weight, Tensor grad_bias)"
device: [all, -cuda, -muxi, -ascend]
Expand Down
13 changes: 12 additions & 1 deletion dipu/tests/python/unittests/test_mean_std.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) 2023, DeepLink.
import torch
import torch_dipu
from torch_dipu.testing._internal.common_utils import TestCase, run_tests
from torch_dipu.testing._internal.common_utils import TestCase, run_tests, skipOn


class TestMeanStd(TestCase):
Expand Down Expand Up @@ -61,6 +61,17 @@ def test_std(self):
)
)

@skipOn("MLU", "camb does not support this type")
def test_std_correction(self):
self.assertTrue(
torch.allclose(
torch.std(self.a, dim=-1, correction=20).cpu(),
torch.std(self.a.cpu(), dim=-1, correction=20),
atol=1e-3,
rtol=1e-3,
)
)


if __name__ == "__main__":
run_tests()
2 changes: 0 additions & 2 deletions dipu/tests/pytorch_config_cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
"TestReductionsDIPU": {
"test_ref_large_input_1D",
"test_ref_large_input_64bit_indexing",
# will fail because diopiStd not align with torch.std, will fix later
"test_warn_invalid_degrees_of_freedom",
},
}

Expand Down
Loading