Skip to content

Commit

Permalink
Support clang format and format almost all C++ code (#423)
Browse files Browse the repository at this point in the history
* refactor: format almost all code

* fix: update IncludeCategories

* fix: make sure structmember.h is before Python.h
  • Loading branch information
wiryls authored Nov 15, 2023
1 parent 9ce743e commit fa1f0c8
Show file tree
Hide file tree
Showing 119 changed files with 6,606 additions and 6,240 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
Language: Cpp
BasedOnStyle: Google
22 changes: 22 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: code format

on:
workflow_dispatch:
pull_request:
push:
branches:
- main

jobs:
cpp-linter:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cpp-linter/cpp-linter-action@v2
id: linter
with:
style: file
tidy-checks: '-*' # disable clang tidy at this stage
- name: Fail test
if: steps.linter.outputs.checks-failed > 0
run: echo "Some files failed the linting checks!" && exit 1
3 changes: 3 additions & 0 deletions dicp/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
DisableFormat: true
SortIncludes: Never
36 changes: 36 additions & 0 deletions dipu/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
BasedOnStyle: InheritParentConfig
IncludeCategories:
- Regex: '^("|<)csrc_dipu/'
Priority: 90
CaseSensitive: false
- Regex: '^("|<)diopi/'
Priority: 80
CaseSensitive: false
- Regex: '^("|<)(c10|aten|torch)/'
Priority: 40
CaseSensitive: false
- Regex: '^("|<)Python\.h'
Priority: 50
CaseSensitive: false
- Regex: '^("|<)(frameobject|structmember)\.h'
Priority: 50
SortPriority: 51
CaseSensitive: false
- Regex: '^("|<)(pybind11)'
Priority: 50
SortPriority: 52
CaseSensitive: false
- Regex: '^<((ext/.*)|pthread)\.h'
Priority: 20
SortPriority: 21
CaseSensitive: false
- Regex: '^("|<)(cuda|su|cn|(..?ccl)|(.*_runtime)).*\.h'
Priority: 30
CaseSensitive: false
- Regex: '^<.*'
Priority: 20
CaseSensitive: false
- Regex: '.*'
Priority: 100
CaseSensitive: false
25 changes: 13 additions & 12 deletions dipu/tests/cpp/test_relu.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// Copyright (c) 2023, DeepLink.
#include <torch/torch.h>
#include <iostream>

void testTensorRelu(at::Tensor& self) {
std::cout << self << std::endl;
std::cout << torch::relu(self) << std::endl;
std::cout << self << std::endl;
#include <torch/torch.h>

void testTensorRelu(at::Tensor &self) {
std::cout << self << std::endl;
std::cout << torch::relu(self) << std::endl;
std::cout << self << std::endl;

std::cout << torch::relu_(self) << std::endl;
std::cout << self << std::endl;
std::cout << torch::relu_(self) << std::endl;
std::cout << self << std::endl;
}

int main() {
torch::Tensor tensor = torch::randn(10).cuda();
testTensorRelu(tensor);
torch::Tensor tensor = torch::randn(10).cuda();
testTensorRelu(tensor);

torch::Tensor tensor_cpu = torch::randn(10);
testTensorRelu(tensor_cpu);
return 0;
torch::Tensor tensor_cpu = torch::randn(10);
testTensorRelu(tensor_cpu);
return 0;
}
22 changes: 11 additions & 11 deletions dipu/tests/cpp/test_tensor_add.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// Copyright (c) 2023, DeepLink.
#include <iostream>

#include <ATen/Tensor.h>
#include <ATen/ATen.h>
#include <ATen/Tensor.h>

void testTensorAdd(const at::Tensor& lhs, const at::Tensor& rhs) {
at::Tensor result = lhs + rhs;
std::cout << lhs << std::endl;
std::cout << rhs << std::endl;
std::cout << result << std::endl;
void testTensorAdd(const at::Tensor &lhs, const at::Tensor &rhs) {
at::Tensor result = lhs + rhs;
std::cout << lhs << std::endl;
std::cout << rhs << std::endl;
std::cout << result << std::endl;
}

int main() {
at::Tensor t0 = at::randn({2, 2}).cuda();
at::Tensor t1 = at::randn({2}).cuda();
testTensorAdd(t0, t1);
testTensorAdd(t0.cpu(), t1.cpu());
return 0;
at::Tensor t0 = at::randn({2, 2}).cuda();
at::Tensor t1 = at::randn({2}).cuda();
testTensorAdd(t0, t1);
testTensorAdd(t0.cpu(), t1.cpu());
return 0;
}
9 changes: 5 additions & 4 deletions dipu/tests/cpp/testrt.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) 2023, DeepLink.
#include <torch/torch.h>
#include <iostream>
#include <csrc_dipu/runtime/devproxy/deviceproxy.h>

#include <torch/torch.h>

#include <csrc_dipu/runtime/core/DIPUStream.h>
#include <csrc_dipu/runtime/devproxy/deviceproxy.h>

using namespace dipu;
void testcopy() {
Expand Down Expand Up @@ -53,12 +55,11 @@ void testStream1() {
auto stream3 = getCurrentDIPUStream();
rawStream = stream3.rawstream();
std::cout << "current stream =" << rawStream << std::endl;

}

// need change to use gtest.
int main() {
for(int i=0; i<3; i++) {
for (int i = 0; i < 3; i++) {
// testcopy();
testDeviceSwitch();
// testStream1();
Expand Down
3 changes: 3 additions & 0 deletions dipu/third_party/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
DisableFormat: true
SortIncludes: Never
Loading

0 comments on commit fa1f0c8

Please sign in to comment.