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

Feature improve tensor interface #41

Merged
merged 8 commits into from
Aug 8, 2024
Merged

Conversation

ewanwm
Copy link
Owner

@ewanwm ewanwm commented Aug 8, 2024

Slightly tidier way if building tensor objects and some new constructors to simplify things a bit

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

Click here for the full clang-format patch
diff --git a/nuTens/tensors/tensor.hpp b/nuTens/tensors/tensor.hpp
index e3a587a..2b0eaac 100644
--- a/nuTens/tensors/tensor.hpp
+++ b/nuTens/tensors/tensor.hpp
@@ -56 +56 @@ class Tensor
-    Tensor(const std::vector<float>& values, NTdtypes::scalarType type = NTdtypes::kFloat,
+    Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,

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

Click here for the full clang-tidy patch
diff --git a/benchmarks/benchmarks.cpp b/benchmarks/benchmarks.cpp
index ebd71ca..cb6f4c3 100644
--- a/benchmarks/benchmarks.cpp
+++ b/benchmarks/benchmarks.cpp
@@ -99 +99 @@ static void BM_constMatterOscillations(benchmark::State &state)
-    std::unique_ptr<BaseMatterSolver> matterSolver = std::make_unique<ConstDensityMatterSolver>(3, 2.6);
+    std::unique_ptr<BaseMatterSolver> matterSolver = 0 = std::make_unique<ConstDensityMatterSolver>(3, 2.6);
diff --git a/nuTens/propagator/const-density-solver.hpp b/nuTens/propagator/const-density-solver.hpp
index 5aa5b34..19470ea 100644
--- a/nuTens/propagator/const-density-solver.hpp
+++ b/nuTens/propagator/const-density-solver.hpp
@@ -82 +82 @@ class ConstDensityMatterSolver : public BaseMatterSolver
-    void calculateEigenvalues(const Tensor &energies, Tensor &eigenvectors, Tensor &eigenvalues);
+    void calculateEigenvalues(const Tensor &energies, Tensor &eigenvectors, Tensor &eigenvalues) override;
diff --git a/nuTens/tensors/tensor.hpp b/nuTens/tensors/tensor.hpp
index e3a587a..f825ea5 100644
--- a/nuTens/tensors/tensor.hpp
+++ b/nuTens/tensors/tensor.hpp
@@ -52 +52 @@ class Tensor
-    Tensor(){};
+    Tensor()= default;;
@@ -56 +56 @@ class Tensor
-    Tensor(const std::vector<float>& values, NTdtypes::scalarType type = NTdtypes::kFloat,
+    Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,
diff --git a/nuTens/tensors/torch-tensor.cpp b/nuTens/tensors/torch-tensor.cpp
index 3324317..cf91698 100644
--- a/nuTens/tensors/torch-tensor.cpp
+++ b/nuTens/tensors/torch-tensor.cpp
@@ -20 +20 @@ std::string Tensor::getTensorLibrary()
-Tensor::Tensor(std::vector<float> values, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)
+Tensor::Tensor(const std::vector<float>& values, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)

Have any feedback or feature suggestions? Share it here.


/// @brief Construct a 1-d array with specified values
/// @arg values The values to include in the tensor
Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,
Copy link
Contributor

Choose a reason for hiding this comment

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

clang-format suggestions

Suggested change
Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,
Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,

bool requiresGrad = true);
/// @brief Initialise this tensor with ones
/// @brief Default constructor with no initialisation
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 diagnostics

Suggested change
Tensor(){};
Tensor()= default;;


/// @brief Construct a 1-d array with specified values
/// @arg values The values to include in the tensor
Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,
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 suggestions

Suggested change
Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,
Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,

@@ -17,45 +17,72 @@ std::string Tensor::getTensorLibrary()
return "PyTorch";
}

Tensor &Tensor::ones(int length, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)
Tensor::Tensor(std::vector<float> values, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)
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 diagnostics

Suggested change
Tensor::Tensor(std::vector<float> values, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)
Tensor::Tensor(const std::vector<float>& values, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)

@codecov-commenter
Copy link

codecov-commenter commented Aug 8, 2024

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

Codecov Report

Attention: Patch coverage is 72.72727% with 15 lines in your changes missing coverage. Please review.

Files Patch % Lines
nuTens/tensors/torch-tensor.cpp 61.76% 12 Missing and 1 partial ⚠️
nuTens/propagator/propagator.cpp 50.00% 2 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Files Coverage Δ
nuTens/propagator/const-density-solver.cpp 100.00% <100.00%> (ø)
nuTens/propagator/const-density-solver.hpp 100.00% <100.00%> (+7.69%) ⬆️
nuTens/tensors/tensor.hpp 100.00% <ø> (+12.50%) ⬆️
tests/tensor-basic.cpp 92.98% <100.00%> (ø)
tests/two-flavour-const-matter.cpp 100.00% <100.00%> (ø)
tests/two-flavour-vacuum.cpp 100.00% <100.00%> (ø)
nuTens/propagator/propagator.cpp 65.00% <50.00%> (-7.23%) ⬇️
nuTens/tensors/torch-tensor.cpp 65.16% <61.76%> (+0.97%) ⬆️

Copy link
Contributor

github-actions bot commented Aug 8, 2024

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-tidy reports: 7 concern(s)
  • benchmarks/benchmarks.cpp:2:10: error: [clang-diagnostic-error]

    'benchmark/benchmark.h' file not found

    #include <benchmark/benchmark.h>
             ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:61:29: warning: 0.1 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor masses = Tensor({0.1, 0.2, 0.3}, NTdtypes::kFloat).requiresGrad(false).addBatchDim();
                                ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:61:34: warning: 0.2 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor masses = Tensor({0.1, 0.2, 0.3}, NTdtypes::kFloat).requiresGrad(false).addBatchDim();
                                     ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:61:39: warning: 0.3 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor masses = Tensor({0.1, 0.2, 0.3}, NTdtypes::kFloat).requiresGrad(false).addBatchDim();
                                          ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:63:30: warning: 0.23 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor theta23 = Tensor({0.23}).dType(NTdtypes::kComplexFloat).requiresGrad(false);
                                 ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:64:30: warning: 0.13 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor theta13 = Tensor({0.13}).dType(NTdtypes::kComplexFloat).requiresGrad(false);
                                 ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:65:30: warning: 0.12 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor theta12 = Tensor({0.12}).dType(NTdtypes::kComplexFloat).requiresGrad(false);
                                 ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:66:30: warning: 0.5 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor deltaCP = Tensor({0.5}).dType(NTdtypes::kComplexFloat).requiresGrad(false);
                                 ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:76:16: warning: 123 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        std::srand(123);
                   ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:89:29: warning: 0.1 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor masses = Tensor({0.1, 0.2, 0.3}, NTdtypes::kFloat).requiresGrad(false).addBatchDim();
                                ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:89:34: warning: 0.2 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor masses = Tensor({0.1, 0.2, 0.3}, NTdtypes::kFloat).requiresGrad(false).addBatchDim();
                                     ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:89:39: warning: 0.3 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor masses = Tensor({0.1, 0.2, 0.3}, NTdtypes::kFloat).requiresGrad(false).addBatchDim();
                                          ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:91:30: warning: 0.23 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor theta23 = Tensor({0.23}).dType(NTdtypes::kComplexFloat).requiresGrad(false);
                                 ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:92:30: warning: 0.13 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor theta13 = Tensor({0.13}).dType(NTdtypes::kComplexFloat).requiresGrad(false);
                                 ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:93:30: warning: 0.12 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor theta12 = Tensor({0.12}).dType(NTdtypes::kComplexFloat).requiresGrad(false);
                                 ^
    /home/runner/work/nuTens/nuTens/benchmarks/benchmarks.cpp:94:30: warning: 0.5 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers]
        Tensor deltaCP = Tensor({0.5}).dType(NTdtypes::kComplexFloat).requiresGrad(false);
                                 ^
  • benchmarks/benchmarks.cpp:100:39: warning: [cppcoreguidelines-init-variables]

    variable 'matterSolver' is not initialized

        std::unique_ptr<BaseMatterSolver> matterSolver = std::make_unique<ConstDensityMatterSolver>(3, 2.6);
                                          ^
                                                       = 0
  • benchmarks/benchmarks.cpp:116:11: warning: [cppcoreguidelines-avoid-non-const-global-variables]

    variable 'BM_vacuumOscillations' is non-const and globally accessible, consider making it const

    BENCHMARK(BM_vacuumOscillations)->Name("Vacuum Oscillations")->Args({1 << 10, 1 << 10});
              ^
  • benchmarks/benchmarks.cpp:119:11: warning: [cppcoreguidelines-avoid-non-const-global-variables]

    variable 'BM_constMatterOscillations' is non-const and globally accessible, consider making it const

    BENCHMARK(BM_constMatterOscillations)->Name("Const Density Oscillations")->Args({1 << 10, 1 << 10});
              ^
  • nuTens/propagator/const-density-solver.hpp:82:10: warning: [clang-diagnostic-inconsistent-missing-override]

    'calculateEigenvalues' overrides a member function but is not marked 'override'

        void calculateEigenvalues(const Tensor &energies, Tensor &eigenvectors, Tensor &eigenvalues);
             ^
    /home/runner/work/nuTens/nuTens/nuTens/propagator/base-matter-solver.hpp:20:18: note: overridden virtual function is here
        virtual void calculateEigenvalues(const Tensor &energies, Tensor &eigenvectors, Tensor &eigenvalues) = 0;
                     ^
    /home/runner/work/nuTens/nuTens/nuTens/propagator/const-density-solver.hpp:82:10: warning: annotate this function with 'override' or (rarely) 'final' [cppcoreguidelines-explicit-virtual-functions,modernize-use-override]
        void calculateEigenvalues(const Tensor &energies, Tensor &eigenvectors, Tensor &eigenvalues);
             ^
                                                                                                     override
  • nuTens/tensors/tensor.hpp:52:5: warning: [modernize-use-equals-default]

    use '= default' to define a trivial default constructor

        Tensor(){};
        ^       ~~
                = default;
  • nuTens/tensors/torch-tensor.cpp:20:35: warning: [performance-unnecessary-value-param]

    the parameter 'values' is copied for each invocation but only used as a const reference; consider making it a const reference

    Tensor::Tensor(std::vector<float> values, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)
                                      ^
                   const             &

Have any feedback or feature suggestions? Share it here.

@github-actions github-actions bot dismissed their stale review August 8, 2024 23:10

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

Click here for the full clang-format patch
diff --git a/nuTens/tensors/tensor.hpp b/nuTens/tensors/tensor.hpp
index e3a587a..2b0eaac 100644
--- a/nuTens/tensors/tensor.hpp
+++ b/nuTens/tensors/tensor.hpp
@@ -56 +56 @@ class Tensor
-    Tensor(const std::vector<float>& values, NTdtypes::scalarType type = NTdtypes::kFloat,
+    Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,

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

Click here for the full clang-tidy patch
diff --git a/benchmarks/benchmarks.cpp b/benchmarks/benchmarks.cpp
index 2f729d2..87fcbe1 100644
--- a/benchmarks/benchmarks.cpp
+++ b/benchmarks/benchmarks.cpp
@@ -100 +100 @@ static void BM_constMatterOscillations(benchmark::State &state)
-    std::unique_ptr<BaseMatterSolver> matterSolver = std::make_unique<ConstDensityMatterSolver>(3, 2.6);
+    std::unique_ptr<BaseMatterSolver> matterSolver = 0 = std::make_unique<ConstDensityMatterSolver>(3, 2.6);
diff --git a/nuTens/propagator/const-density-solver.hpp b/nuTens/propagator/const-density-solver.hpp
index 5aa5b34..19470ea 100644
--- a/nuTens/propagator/const-density-solver.hpp
+++ b/nuTens/propagator/const-density-solver.hpp
@@ -82 +82 @@ class ConstDensityMatterSolver : public BaseMatterSolver
-    void calculateEigenvalues(const Tensor &energies, Tensor &eigenvectors, Tensor &eigenvalues);
+    void calculateEigenvalues(const Tensor &energies, Tensor &eigenvectors, Tensor &eigenvalues) override;
diff --git a/nuTens/tensors/tensor.hpp b/nuTens/tensors/tensor.hpp
index e3a587a..f825ea5 100644
--- a/nuTens/tensors/tensor.hpp
+++ b/nuTens/tensors/tensor.hpp
@@ -52 +52 @@ class Tensor
-    Tensor(){};
+    Tensor()= default;;
@@ -56 +56 @@ class Tensor
-    Tensor(const std::vector<float>& values, NTdtypes::scalarType type = NTdtypes::kFloat,
+    Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,
diff --git a/nuTens/tensors/torch-tensor.cpp b/nuTens/tensors/torch-tensor.cpp
index 3324317..cf91698 100644
--- a/nuTens/tensors/torch-tensor.cpp
+++ b/nuTens/tensors/torch-tensor.cpp
@@ -20 +20 @@ std::string Tensor::getTensorLibrary()
-Tensor::Tensor(std::vector<float> values, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)
+Tensor::Tensor(const std::vector<float>& values, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)

Have any feedback or feature suggestions? Share it here.


/// @brief Construct a 1-d array with specified values
/// @arg values The values to include in the tensor
Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,
Copy link
Contributor

Choose a reason for hiding this comment

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

clang-format suggestions

Suggested change
Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,
Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,

bool requiresGrad = true);
/// @brief Initialise this tensor with ones
/// @brief Default constructor with no initialisation
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 diagnostics

Suggested change
Tensor(){};
Tensor()= default;;


/// @brief Construct a 1-d array with specified values
/// @arg values The values to include in the tensor
Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,
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 suggestions

Suggested change
Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,
Tensor(std::vector<float> values, NTdtypes::scalarType type = NTdtypes::kFloat,

@@ -17,45 +17,72 @@ std::string Tensor::getTensorLibrary()
return "PyTorch";
}

Tensor &Tensor::ones(int length, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)
Tensor::Tensor(std::vector<float> values, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)
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 diagnostics

Suggested change
Tensor::Tensor(std::vector<float> values, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)
Tensor::Tensor(const std::vector<float>& values, NTdtypes::scalarType type, NTdtypes::deviceType device, bool requiresGrad)

Copy link
Contributor

github-actions bot commented Aug 8, 2024

🐰Bencher

ReportThu, August 8, 2024 at 23:10:49 UTC
ProjectnuTens
Branchfeature_improve_tensor_interface
Testbedubuntu-latest

⚠️ WARNING: The following Measure does not have a Threshold. Without a Threshold, no Alerts will ever be generated!

  • Latency (latency)

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 CLI flag.

Click to view all benchmark results
BenchmarkLatencyLatency Results
nanoseconds (ns)
Const Density Oscillations/1024/1024➖ (view plot)4,689,771,572.00
Const Density Oscillations/1024/1024_cv➖ (view plot)0.00
Const Density Oscillations/1024/1024_mean➖ (view plot)4,686,469,794.37
Const Density Oscillations/1024/1024_median➖ (view plot)4,678,853,041.00
Const Density Oscillations/1024/1024_stddev➖ (view plot)17,161,362.70
Vacuum Oscillations/1024/1024➖ (view plot)262,678,301.33
Vacuum Oscillations/1024/1024_cv➖ (view plot)0.04
Vacuum Oscillations/1024/1024_mean➖ (view plot)267,452,796.25
Vacuum Oscillations/1024/1024_median➖ (view plot)262,925,851.50
Vacuum Oscillations/1024/1024_stddev➖ (view plot)10,305,432.72

Bencher - Continuous Benchmarking
View Public Perf Page
Docs | Repo | Chat | Help

@ewanwm ewanwm merged commit 41ae3ec into main Aug 8, 2024
2 of 3 checks passed
@ewanwm ewanwm deleted the feature_improve_tensor_interface branch August 8, 2024 23:19
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