Skip to content

Simplify Internal Tensor Creation#159

Merged
ewanwm merged 2 commits intomainfrom
tidy/internal-tensor-instantiation
Sep 11, 2025
Merged

Simplify Internal Tensor Creation#159
ewanwm merged 2 commits intomainfrom
tidy/internal-tensor-instantiation

Conversation

@ewanwm
Copy link
Owner

@ewanwm ewanwm commented Sep 11, 2025

Add constructor taking a pytorch tensor so we can simplify the internal creation of tensors in e.g. arithmetic functions

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-linter Review

Used clang-tidy v16.0.6

Only 50 out of 58 clang-tidy concerns fit within this pull request's diff.

Have any feedback or feature suggestions? Share it here.

ret.setTensor(tensor);

return ret;
return Tensor(tensor);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/tensor.hpp:438:16: warning: [modernize-return-braced-init-list]

avoid repeating the return type from the declaration; use a braced initializer list instead

        return Tensor(tensor);
               ^

}

protected:
torch::Tensor _tensor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/tensor.hpp:509:19: warning: [cppcoreguidelines-non-private-member-variables-in-classes]

member variable '_tensor' has protected visibility

    torch::Tensor _tensor;
                  ^


Tensor ret;
ret.setTensor(torch::eye(n, torch::TensorOptions()
return Tensor(torch::eye(n, torch::TensorOptions()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:26:12: warning: [modernize-return-braced-init-list]

avoid repeating the return type from the declaration; use a braced initializer list instead

    return Tensor(torch::eye(n, torch::TensorOptions()
           ^


Tensor ret;
ret.setTensor(torch::rand(c10::IntArrayRef(shape), torch::TensorOptions()
return Tensor(torch::rand(c10::IntArrayRef(shape), torch::TensorOptions()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:37:12: warning: [modernize-return-braced-init-list]

avoid repeating the return type from the declaration; use a braced initializer list instead

    return Tensor(torch::rand(c10::IntArrayRef(shape), torch::TensorOptions()
           ^

ret._dType = diag._dType;
ret._device = diag._device;
return ret;
return Tensor(torch::diag(diag._tensor));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:48:12: warning: [modernize-return-braced-init-list]

avoid repeating the return type from the declaration; use a braced initializer list instead

    return Tensor(torch::diag(diag._tensor));
           ^

Tensor ret;
ret.setTensor(_tensor.grad());
return ret;
return Tensor(_tensor.grad());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:572:12: warning: [modernize-return-braced-init-list]

avoid repeating the return type from the declaration; use a braced initializer list instead

    return Tensor(_tensor.grad());
           ^

return Tensor(_tensor.grad());
}

Tensor Tensor::sin(const Tensor &t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:575:34: warning: [readability-identifier-length]

parameter name 't' is too short, expected at least 3 characters

Tensor Tensor::sin(const Tensor &t)
                                 ^

Tensor ret;
ret.setTensor(torch::sin(t._tensor));
return ret;
return Tensor(torch::sin(t._tensor));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:579:12: warning: [modernize-return-braced-init-list]

avoid repeating the return type from the declaration; use a braced initializer list instead

    return Tensor(torch::sin(t._tensor));
           ^

return Tensor(torch::sin(t._tensor));
}

Tensor Tensor::cos(const Tensor &t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:582:34: warning: [readability-identifier-length]

parameter name 't' is too short, expected at least 3 characters

Tensor Tensor::cos(const Tensor &t)
                                 ^

Tensor ret;
ret.setTensor(torch::cos(t._tensor));
return ret;
return Tensor(torch::cos(t._tensor));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:586:12: warning: [modernize-return-braced-init-list]

avoid repeating the return type from the declaration; use a braced initializer list instead

    return Tensor(torch::cos(t._tensor));
           ^

@codecov-commenter
Copy link

codecov-commenter commented Sep 11, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 77.77778% with 10 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
nuTens/tensors/torch-tensor.cpp 75.60% 10 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Files with missing lines Coverage Δ
nuTens/tensors/tensor.hpp 92.50% <100.00%> (+1.07%) ⬆️
nuTens/tensors/torch-tensor.cpp 56.56% <75.60%> (-3.61%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 11, 2025

🐰 Bencher Report

Branchtidy/internal-tensor-instantiation
Testbedubuntu-latest

⚠️ WARNING: No Threshold found!

Without a Threshold, no Alerts will ever be generated.

Click here to create a new Threshold
For more information, see the Threshold documentation.
To only post results if a Threshold exists, set the --ci-only-thresholds flag.

Click to view all benchmark results
BenchmarkLatencynanoseconds (ns)
Const Density Oscillations/1024/1024📈 view plot
⚠️ NO THRESHOLD
1,861,378,777.00 ns
Const Density Oscillations/1024/1024_cv📈 view plot
⚠️ NO THRESHOLD
0.01 ns
Const Density Oscillations/1024/1024_mean📈 view plot
⚠️ NO THRESHOLD
1,863,611,138.75 ns
Const Density Oscillations/1024/1024_median📈 view plot
⚠️ NO THRESHOLD
1,861,016,887.00 ns
Const Density Oscillations/1024/1024_stddev📈 view plot
⚠️ NO THRESHOLD
11,576,951.17 ns
DP Propagator Const Density Oscillations/1024/1024📈 view plot
⚠️ NO THRESHOLD
989,017,945.00 ns
DP Propagator Const Density Oscillations/1024/1024_cv📈 view plot
⚠️ NO THRESHOLD
0.00 ns
DP Propagator Const Density Oscillations/1024/1024_mean📈 view plot
⚠️ NO THRESHOLD
987,943,249.56 ns
DP Propagator Const Density Oscillations/1024/1024_median📈 view plot
⚠️ NO THRESHOLD
988,070,096.50 ns
DP Propagator Const Density Oscillations/1024/1024_stddev📈 view plot
⚠️ NO THRESHOLD
2,832,584.36 ns
Vacuum Oscillations/1024/1024📈 view plot
⚠️ NO THRESHOLD
395,982,577.00 ns
Vacuum Oscillations/1024/1024_cv📈 view plot
⚠️ NO THRESHOLD
0.00 ns
Vacuum Oscillations/1024/1024_mean📈 view plot
⚠️ NO THRESHOLD
396,102,845.78 ns
Vacuum Oscillations/1024/1024_median📈 view plot
⚠️ NO THRESHOLD
396,140,479.75 ns
Vacuum Oscillations/1024/1024_stddev📈 view plot
⚠️ NO THRESHOLD
1,070,827.57 ns
🐰 View full continuous benchmarking report in Bencher

@github-actions github-actions bot dismissed their stale review September 11, 2025 10:31

outdated suggestion

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-linter Review

Used clang-tidy v16.0.6

Only 14 out of 21 clang-tidy concerns fit within this pull request's diff.

Have any feedback or feature suggestions? Share it here.

ret.setTensor(tensor);

return ret;
return Tensor(tensor);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/tensor.hpp:438:16: warning: [modernize-return-braced-init-list]

avoid repeating the return type from the declaration; use a braced initializer list instead

        return Tensor(tensor);
               ^

}

protected:
torch::Tensor _tensor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/tensor.hpp:509:19: warning: [cppcoreguidelines-non-private-member-variables-in-classes]

member variable '_tensor' has protected visibility

    torch::Tensor _tensor;
                  ^

return {torch::matmul(t1._tensor, t2._tensor)};
}

Tensor Tensor::outer(const Tensor &t1, const Tensor &t2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:238:36: warning: [readability-identifier-length]

parameter name 't1' is too short, expected at least 3 characters

Tensor Tensor::outer(const Tensor &t1, const Tensor &t2)
                                   ^

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:238:54: warning: [readability-identifier-length]

parameter name 't2' is too short, expected at least 3 characters

Tensor Tensor::outer(const Tensor &t1, const Tensor &t2)
                                                     ^

return {torch::outer(t1._tensor, t2._tensor)};
}

Tensor Tensor::mul(const Tensor &t1, const Tensor &t2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:245:34: warning: [readability-identifier-length]

parameter name 't1' is too short, expected at least 3 characters

Tensor Tensor::mul(const Tensor &t1, const Tensor &t2)
                                 ^

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:245:52: warning: [readability-identifier-length]

parameter name 't2' is too short, expected at least 3 characters

Tensor Tensor::mul(const Tensor &t1, const Tensor &t2)
                                                   ^

return {torch::mul(t1._tensor, t2._tensor)};
}

Tensor Tensor::div(const Tensor &t1, const Tensor &t2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:252:34: warning: [readability-identifier-length]

parameter name 't1' is too short, expected at least 3 characters

Tensor Tensor::div(const Tensor &t1, const Tensor &t2)
                                 ^

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:252:52: warning: [readability-identifier-length]

parameter name 't2' is too short, expected at least 3 characters

Tensor Tensor::div(const Tensor &t1, const Tensor &t2)
                                                   ^

Tensor ret;
ret.setTensor(torch::transpose(t._tensor, dim1, dim2));
return ret;
return {torch::transpose(t._tensor, dim1, dim2)};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:284:13: warning: [readability-suspicious-call-argument]

2nd argument 'dim1' (passed to 'dim0') looks like it might be swapped with the 3rd, 'dim2' (passed to 'dim1')

    return {torch::transpose(t._tensor, dim1, dim2)};
            ^                           ~~~~  ~~~~
/home/runner/.local/lib/python3.10/site-packages/torch/include/ATen/ops/transpose.h:26:19: note: in the call to 'transpose', declared here
inline at::Tensor transpose(const at::Tensor & self, int64_t dim0, int64_t dim1) {
                  ^                                          ~~~~          ~~~~

return {torch::transpose(t._tensor, dim1, dim2)};
}

Tensor Tensor::scale(const Tensor &t, float s)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:287:36: warning: [readability-identifier-length]

parameter name 't' is too short, expected at least 3 characters

Tensor Tensor::scale(const Tensor &t, float s)
                                   ^

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:287:45: warning: [readability-identifier-length]

parameter name 's' is too short, expected at least 3 characters

Tensor Tensor::scale(const Tensor &t, float s)
                                            ^

return {torch::multiply(t._tensor, s)};
}

Tensor Tensor::scale(const Tensor &t, double s)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:294:36: warning: [readability-identifier-length]

parameter name 't' is too short, expected at least 3 characters

Tensor Tensor::scale(const Tensor &t, double s)
                                   ^

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:294:46: warning: [readability-identifier-length]

parameter name 's' is too short, expected at least 3 characters

Tensor Tensor::scale(const Tensor &t, double s)
                                             ^

return {_tensor.grad()};
}

Tensor Tensor::sin(const Tensor &t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:575:34: warning: [readability-identifier-length]

parameter name 't' is too short, expected at least 3 characters

Tensor Tensor::sin(const Tensor &t)
                                 ^

return {torch::sin(t._tensor)};
}

Tensor Tensor::cos(const Tensor &t)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy diagnostic

nuTens/tensors/torch-tensor.cpp:582:34: warning: [readability-identifier-length]

parameter name 't' is too short, expected at least 3 characters

Tensor Tensor::cos(const Tensor &t)
                                 ^

@ewanwm ewanwm merged commit 480a3b5 into main Sep 11, 2025
6 of 7 checks passed
@ewanwm ewanwm deleted the tidy/internal-tensor-instantiation branch September 11, 2025 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants