Skip to content
Snippets Groups Projects
Commit 7215ade7 authored by Maxence Naud's avatar Maxence Naud
Browse files

Merge branch 'dev' into vit_operators

parents 03a58652 e4fb24ee
No related branches found
No related tags found
2 merge requests!50version 0.2.0,!20Vit operators
Pipeline #38490 failed
include:
- remote: 'https://gitlab.eclipse.org/eclipse/aidge/gitlab_shared_files/-/raw/main/.gitlab/ci/shared_script.gitlab-ci.yml'
build:ubuntu_cpp: build:ubuntu_cpp:
stage: build stage: build
needs: [] needs: []
...@@ -6,9 +9,9 @@ build:ubuntu_cpp: ...@@ -6,9 +9,9 @@ build:ubuntu_cpp:
script: script:
# Download dependencies # Download dependencies
# aidge_core # aidge_core
- 'curl --location --output build_artifacts.zip "https://gitlab.eclipse.org/api/v4/projects/5139/jobs/artifacts/main/download?job=build:ubuntu_cpp"' - DEPENDENCY_NAME="aidge_core"
- unzip -o build_artifacts.zip -d . - DEPENDENCY_JOB="build:ubuntu_cpp"
- rm -rf build_cpp - !reference [.download_dependency, script]
# Build current module # Build current module
- export CMAKE_PREFIX_PATH=../install_cpp - export CMAKE_PREFIX_PATH=../install_cpp
...@@ -32,9 +35,9 @@ build:ubuntu_cpp_g++10: ...@@ -32,9 +35,9 @@ build:ubuntu_cpp_g++10:
script: script:
# Download dependencies # Download dependencies
# aidge_core # aidge_core
- 'curl --location --output build_artifacts.zip "https://gitlab.eclipse.org/api/v4/projects/5139/jobs/artifacts/main/download?job=build:ubuntu_cpp"' - DEPENDENCY_NAME="aidge_core"
- unzip -o build_artifacts.zip -d . - DEPENDENCY_JOB="build:ubuntu_cpp"
- rm -rf build_cpp - !reference [.download_dependency, script]
# Build current module # Build current module
- export CMAKE_PREFIX_PATH=../install_cpp - export CMAKE_PREFIX_PATH=../install_cpp
...@@ -55,9 +58,9 @@ build:ubuntu_cpp_g++12: ...@@ -55,9 +58,9 @@ build:ubuntu_cpp_g++12:
script: script:
# Download dependencies # Download dependencies
# aidge_core # aidge_core
- 'curl --location --output build_artifacts.zip "https://gitlab.eclipse.org/api/v4/projects/5139/jobs/artifacts/main/download?job=build:ubuntu_cpp"' - DEPENDENCY_NAME="aidge_core"
- unzip -o build_artifacts.zip -d . - DEPENDENCY_JOB="build:ubuntu_cpp"
- rm -rf build_cpp - !reference [.download_dependency, script]
# Build current module # Build current module
- export CMAKE_PREFIX_PATH=../install_cpp - export CMAKE_PREFIX_PATH=../install_cpp
...@@ -78,9 +81,9 @@ build:ubuntu_cpp_clang12: ...@@ -78,9 +81,9 @@ build:ubuntu_cpp_clang12:
script: script:
# Download dependencies # Download dependencies
# aidge_core # aidge_core
- 'curl --location --output build_artifacts.zip "https://gitlab.eclipse.org/api/v4/projects/5139/jobs/artifacts/main/download?job=build:ubuntu_cpp"' - DEPENDENCY_NAME="aidge_core"
- unzip -o build_artifacts.zip -d . - DEPENDENCY_JOB="build:ubuntu_cpp"
- rm -rf build_cpp - !reference [.download_dependency, script]
# Build current module # Build current module
- export CMAKE_PREFIX_PATH=../install_cpp - export CMAKE_PREFIX_PATH=../install_cpp
...@@ -101,9 +104,9 @@ build:ubuntu_cpp_clang15: ...@@ -101,9 +104,9 @@ build:ubuntu_cpp_clang15:
script: script:
# Download dependencies # Download dependencies
# aidge_core # aidge_core
- 'curl --location --output build_artifacts.zip "https://gitlab.eclipse.org/api/v4/projects/5139/jobs/artifacts/main/download?job=build:ubuntu_cpp"' - DEPENDENCY_NAME="aidge_core"
- unzip -o build_artifacts.zip -d . - DEPENDENCY_JOB="build:ubuntu_cpp"
- rm -rf build_cpp - !reference [.download_dependency, script]
# Build current module # Build current module
- export CMAKE_PREFIX_PATH=../install_cpp - export CMAKE_PREFIX_PATH=../install_cpp
...@@ -120,86 +123,92 @@ build:ubuntu_python: ...@@ -120,86 +123,92 @@ build:ubuntu_python:
needs: [] needs: []
tags: tags:
- docker - docker
script: script:
# Download dependencies # Download dependencies
# aidge_core (Python) # aidge_core (Python)
- 'curl --location --output build_artifacts.zip "https://gitlab.eclipse.org/api/v4/projects/5139/jobs/artifacts/main/download?job=build:ubuntu_python"' - DEPENDENCY_NAME="aidge_core"
- unzip -o build_artifacts.zip -d . - DEPENDENCY_JOB="build:ubuntu_python"
- !reference [.download_dependency, script]
- python3 -m pip install virtualenv - python3 -m pip install virtualenv
- virtualenv venv - virtualenv venv
- source venv/bin/activate - source venv/bin/activate
- python3 -m pip install -r requirements.txt - python3 -m pip install -r requirements.txt
- python3 -m pip install . - python3 -m pip install .
- python3 -m pip install numpy unittest-xml-reporting
- python3 -m pip list
artifacts: artifacts:
expire_in: 1 week expire_in: 1 week
paths: paths:
- venv/ - venv/
# build:windows_cpp: build:windows_cpp:
# stage: build stage: build
# needs: [] needs: []
# tags: tags:
# - windows - windows
# image: buildtools image: buildtools
# before_script: before_script:
# # Install Chocolatey # Install Chocolatey
# - Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) - Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
# # Install dependencies # Install dependencies
# - choco install cmake.install --installargs '"ADD_CMAKE_TO_PATH=System"' -Y - choco install cmake.install --installargs '"ADD_CMAKE_TO_PATH=System"' -Y
# - choco install git -Y - choco install git -Y
# - choco install python -Y - choco install python -Y
# # Update PATH # Update PATH
# - $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") - $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
# script: script:
# # Download dependencies # Download dependencies
# # aidge_core # aidge_core
# - 'curl "https://gitlab.eclipse.org/api/v4/projects/5139/jobs/artifacts/main/download?job=build:windows_cpp" -o build_artifacts.zip' - $DEPENDENCY_NAME="aidge_core"
# - Expand-Archive -Path .\build_artifacts.zip -DestinationPath . -Force - $DEPENDENCY_JOB="build:windows_cpp"
# - Remove-Item .\build_cpp\ -Recurse - !reference [.download_dependency_windows, script]
- Remove-Item .\build_cpp\ -Recurse -Force -ErrorAction Ignore
# - $env:CMAKE_PREFIX_PATH = '../install_cpp'
# - mkdir -p build_cpp - $env:CMAKE_PREFIX_PATH = '../install_cpp'
# - cd build_cpp - mkdir -p build_cpp
# - cmake -DCMAKE_INSTALL_PREFIX:PATH=../install_cpp -DCMAKE_BUILD_TYPE=Debug .. - cd build_cpp
# - cmake --build . -j2 - cmake -DCMAKE_INSTALL_PREFIX:PATH=../install_cpp -DCMAKE_BUILD_TYPE=Debug ..
# - cmake --install . --config Debug - cmake --build . -j2
- cmake --install . --config Debug
# artifacts:
# expire_in: 1 week artifacts:
# paths: expire_in: 1 week
# - build_cpp/ paths:
# - install_cpp/ - build_cpp/
- install_cpp/
# build:windows_python:
# stage: build build:windows_python:
# needs: [] stage: build
# tags: needs: []
# - windows tags:
- windows
# image: buildtools
# before_script: image: buildtools
# # Install Chocolatey before_script:
# - Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) # Install Chocolatey
# # Install dependencies - Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
# - choco install cmake.install --installargs '"ADD_CMAKE_TO_PATH=System"' -Y # Install dependencies
# - choco install git -Y - choco install cmake.install --installargs '"ADD_CMAKE_TO_PATH=System"' -Y
# - choco install python -Y - choco install git -Y
# # Update PATH - choco install python -Y
# - $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") # Update PATH
# script: - $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
# # Download dependencies script:
# # aidge_core (Python) # Download dependencies
# - 'curl "https://gitlab.eclipse.org/api/v4/projects/5139/jobs/artifacts/main/download?job=build:windows_python" -o build_artifacts.zip' # aidge_core (Python)
# - Expand-Archive -Path .\build_artifacts.zip -DestinationPath . -Force - $DEPENDENCY_NAME="aidge_core"
- $DEPENDENCY_JOB="build:windows_python"
# - python -m pip install virtualenv - !reference [.download_dependency_windows, script]
# - virtualenv venv
# - venv\Scripts\Activate.ps1 - python -m pip install virtualenv
# - python -m pip install -r requirements.txt - virtualenv venv
# - python -m pip install . - venv\Scripts\Activate.ps1
# artifacts: - python -m pip install -r requirements.txt
# expire_in: 1 week - python -m pip install .
# paths: artifacts:
# - venv/ expire_in: 1 week
paths:
- venv/
...@@ -18,32 +18,31 @@ test:ubuntu_python: ...@@ -18,32 +18,31 @@ test:ubuntu_python:
script: script:
- source venv/bin/activate - source venv/bin/activate
- cd ${CI_PROJECT_NAME} - cd ${CI_PROJECT_NAME}
- python3 -m pip install numpy unittest-xml-reporting
- python3 -m pip list # Run on discovery all tests located in core/unit_tests/python and discard the stdout
# Run on discovery all tests located in core/unit_tests/python and discard the stdout
# only to show the errors/warnings and the results of the tests # only to show the errors/warnings and the results of the tests
- python3 -m xmlrunner discover -s unit_tests/ -v -b --output-file xmlrunner-results.xml - python3 -m xmlrunner discover -s unit_tests/ -v -b --output-file xmlrunner-results.xml
artifacts: artifacts:
reports: reports:
junit: ${CI_PROJECT_NAME}/xmlrunner-results.xml junit: ${CI_PROJECT_NAME}/xmlrunner-results.xml
# test:windows_cpp: test:windows_cpp:
# stage: test stage: test
# needs: ["build:windows_cpp"] needs: ["build:windows_cpp"]
# tags: tags:
# - windows - windows
# image: buildtools image: buildtools
# before_script: before_script:
# # Install Chocolatey # Install Chocolatey
# - Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) - Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
# # Install dependencies # Install dependencies
# - choco install cmake.install --installargs '"ADD_CMAKE_TO_PATH=System"' -Y - choco install cmake.install --installargs '"ADD_CMAKE_TO_PATH=System"' -Y
# - choco install python -Y - choco install python -Y
# # Update PATH # Update PATH
# - $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") - $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
# script: script:
# - cd build_cpp - cd build_cpp
# - ctest --output-junit ctest-results.xml --output-on-failure - ctest --output-junit ctest-results.xml --output-on-failure
# artifacts: artifacts:
# reports: reports:
# junit: build_cpp/ctest-results.xml junit: build_cpp/ctest-results.xml
...@@ -16,7 +16,8 @@ ...@@ -16,7 +16,8 @@
namespace Aidge { namespace Aidge {
inline void *getCPUPtr(std::shared_ptr<Aidge::Data> const &data) { inline void *getCPUPtr(std::shared_ptr<Aidge::Data> const &data) {
return std::static_pointer_cast<Tensor>(data)->getImpl()->rawPtr(); const auto tensor = std::static_pointer_cast<Tensor>(data);
return tensor->getImpl()->hostPtr(tensor->getImplOffset());
} }
} // namespace Aidge } // namespace Aidge
......
...@@ -25,8 +25,6 @@ namespace Aidge { ...@@ -25,8 +25,6 @@ namespace Aidge {
template <class T> template <class T>
class TensorImpl_cpu : public TensorImpl { class TensorImpl_cpu : public TensorImpl {
private: private:
const Tensor &mTensor; // Impl needs to access Tensor information, but is not
// supposed to change it!
/// Pointer to the data and its capacity /// Pointer to the data and its capacity
future_std::span<T> mData; future_std::span<T> mData;
/// If this instance own the data, std::unique_ptr manages it /// If this instance own the data, std::unique_ptr manages it
...@@ -35,88 +33,87 @@ private: ...@@ -35,88 +33,87 @@ private:
public: public:
static constexpr const char *Backend = "cpu"; static constexpr const char *Backend = "cpu";
TensorImpl_cpu(const Tensor &tensor) : TensorImpl(Backend), mTensor(tensor) {} TensorImpl_cpu(DeviceIdx_t device, NbElts_t length) : TensorImpl(Backend, device, length) {}
bool operator==(const TensorImpl &otherImpl) const override final { bool operator==(const TensorImpl &otherImpl) const override final {
const auto& typedOtherImpl = reinterpret_cast<const TensorImpl_cpu<T> &>(otherImpl); const auto& typedOtherImpl = reinterpret_cast<const TensorImpl_cpu<T> &>(otherImpl);
AIDGE_INTERNAL_ASSERT(typedOtherImpl.size() >= mTensor.size()); AIDGE_INTERNAL_ASSERT(typedOtherImpl.size() >= mNbElts);
std::size_t i = 0; std::size_t i = 0;
for (; i < mTensor.size() && for (; i < mNbElts &&
*(mData.data()+i) == *static_cast<const T*>(typedOtherImpl.rawPtr(i)); *(mData.data()+i) == *static_cast<const T*>(typedOtherImpl.rawPtr(i));
++i) { ++i) {
} }
return i == mTensor.size(); return i == mNbElts;
} }
static std::unique_ptr<TensorImpl_cpu> create(const Tensor &tensor) { static std::shared_ptr<TensorImpl_cpu> create(DeviceIdx_t device, NbElts_t length) {
return std::make_unique<TensorImpl_cpu<T>>(tensor); return std::make_shared<TensorImpl_cpu<T>>(device, length);
} }
inline std::size_t size() const noexcept override final { return mData.size(); }
inline std::size_t scalarSize() const noexcept override final { return sizeof(T); } inline std::size_t scalarSize() const noexcept override final { return sizeof(T); }
void setDevice(DeviceIdx_t device) override final {
AIDGE_ASSERT(device == 0, "device cannot be != 0 for CPU backend");
}
void copy(const void *src, NbElts_t length, NbElts_t offset = 0) override final { void copy(const void *src, NbElts_t length, NbElts_t offset = 0) override final {
AIDGE_ASSERT(length <= mData.size() || length <= mTensor.size(), "copy length is above capacity"); const T* srcT = static_cast<const T *>(src);
std::copy(static_cast<const T *>(src), static_cast<const T *>(src) + length, T* dstT = static_cast<T *>(rawPtr(offset));
static_cast<T *>(rawPtr()) + offset);
AIDGE_ASSERT(length <= mData.size() || length <= mNbElts, "copy length is above capacity");
AIDGE_ASSERT(dstT < srcT || dstT >= srcT + length, "overlapping copy is not supported");
std::copy(srcT, srcT + length, dstT);
} }
void copyCast(const void *src, NbElts_t length, const DataType srcDt) override final { void copyCast(const void *src, const DataType srcDt, NbElts_t length, NbElts_t offset = 0) override final {
if (length == 0) { if (length == 0) {
return; return;
} }
AIDGE_ASSERT(length <= mData.size() || length <= mTensor.size(), "copy length is above capacity"); T* dstT = static_cast<T *>(rawPtr(offset));
AIDGE_ASSERT(length <= mData.size() || length <= mNbElts, "copy length is above capacity");
switch (srcDt) switch (srcDt)
{ {
case DataType::Float64: case DataType::Float64:
std::copy(static_cast<const double*>(src), static_cast<const double*>(src) + length, std::copy(static_cast<const double*>(src), static_cast<const double*>(src) + length,
static_cast<T *>(rawPtr())); dstT);
break; break;
case DataType::Float32: case DataType::Float32:
std::copy(static_cast<const float*>(src), static_cast<const float*>(src) + length, std::copy(static_cast<const float*>(src), static_cast<const float*>(src) + length,
static_cast<T *>(rawPtr())); dstT);
break; break;
case DataType::Float16: case DataType::Float16:
std::copy(static_cast<const half_float::half*>(src), static_cast<const half_float::half*>(src) + length, std::copy(static_cast<const half_float::half*>(src), static_cast<const half_float::half*>(src) + length,
static_cast<T *>(rawPtr())); dstT);
break; break;
case DataType::Int64: case DataType::Int64:
std::copy(static_cast<const int64_t*>(src), static_cast<const int64_t*>(src) + length, std::copy(static_cast<const int64_t*>(src), static_cast<const int64_t*>(src) + length,
static_cast<T *>(rawPtr())); dstT);
break; break;
case DataType::UInt64: case DataType::UInt64:
std::copy(static_cast<const uint64_t*>(src), static_cast<const uint64_t*>(src) + length, std::copy(static_cast<const uint64_t*>(src), static_cast<const uint64_t*>(src) + length,
static_cast<T *>(rawPtr())); dstT);
break; break;
case DataType::Int32: case DataType::Int32:
std::copy(static_cast<const int32_t*>(src), static_cast<const int32_t*>(src) + length, std::copy(static_cast<const int32_t*>(src), static_cast<const int32_t*>(src) + length,
static_cast<T *>(rawPtr())); dstT);
break; break;
case DataType::UInt32: case DataType::UInt32:
std::copy(static_cast<const uint32_t*>(src), static_cast<const uint32_t*>(src) + length, std::copy(static_cast<const uint32_t*>(src), static_cast<const uint32_t*>(src) + length,
static_cast<T *>(rawPtr())); dstT);
break; break;
case DataType::Int16: case DataType::Int16:
std::copy(static_cast<const int16_t*>(src), static_cast<const int16_t*>(src) + length, std::copy(static_cast<const int16_t*>(src), static_cast<const int16_t*>(src) + length,
static_cast<T *>(rawPtr())); dstT);
break; break;
case DataType::UInt16: case DataType::UInt16:
std::copy(static_cast<const uint16_t*>(src), static_cast<const uint16_t*>(src) + length, std::copy(static_cast<const uint16_t*>(src), static_cast<const uint16_t*>(src) + length,
static_cast<T *>(rawPtr())); dstT);
break; break;
case DataType::Int8: case DataType::Int8:
std::copy(static_cast<const int8_t*>(src), static_cast<const int8_t*>(src) + length, std::copy(static_cast<const int8_t*>(src), static_cast<const int8_t*>(src) + length,
static_cast<T *>(rawPtr())); dstT);
break; break;
case DataType::UInt8: case DataType::UInt8:
std::copy(static_cast<const uint8_t*>(src), static_cast<const uint8_t*>(src) + length, std::copy(static_cast<const uint8_t*>(src), static_cast<const uint8_t*>(src) + length,
static_cast<T *>(rawPtr())); dstT);
break; break;
default: default:
AIDGE_THROW_OR_ABORT(std::runtime_error, "Unsupported data type."); AIDGE_THROW_OR_ABORT(std::runtime_error, "Unsupported data type.");
...@@ -124,21 +121,20 @@ public: ...@@ -124,21 +121,20 @@ public:
} }
} }
void copyFromDevice(const void *src, NbElts_t length, const std::pair<std::string, DeviceIdx_t>& device) override final { void copyFromDevice(const void *src, const std::pair<std::string, DeviceIdx_t>& device, NbElts_t length, NbElts_t offset = 0) override final {
AIDGE_ASSERT(device.first == Backend, "backend must match"); AIDGE_ASSERT(device.first == Backend, "backend must match");
AIDGE_ASSERT(device.second == 0, "device cannot be != 0 for CPU backend"); AIDGE_ASSERT(device.second == 0, "device cannot be != 0 for CPU backend");
copy(src, length); copy(src, length, offset);
} }
inline void copyFromHost(const void *src, NbElts_t length) override final { inline void copyFromHost(const void *src, NbElts_t length, NbElts_t offset = 0) override final {
copy(src, length); copy(src, length, offset);
} }
void copyToHost(void *dst, NbElts_t length) const override final { void copyToHost(void *dst, NbElts_t length, NbElts_t offset = 0) const override final {
AIDGE_ASSERT(length <= mData.size() || length <= mTensor.size(), "copy length is above capacity"); const T* src = static_cast<const T*>(rawPtr(offset));
const T* src = static_cast<const T*>(rawPtr()); AIDGE_ASSERT(length <= mData.size() || length <= mNbElts, "copy length is above capacity");
std::copy(static_cast<const T *>(src), static_cast<const T *>(src) + length, std::copy(src, src + length, static_cast<T *>(dst));
static_cast<T *>(dst));
} }
void *rawPtr(NbElts_t offset = 0) override final { void *rawPtr(NbElts_t offset = 0) override final {
...@@ -147,7 +143,7 @@ public: ...@@ -147,7 +143,7 @@ public:
}; };
const void *rawPtr(NbElts_t offset = 0) const override final { const void *rawPtr(NbElts_t offset = 0) const override final {
AIDGE_ASSERT(mData.size() >= mTensor.size(), "accessing uninitialized const rawPtr"); AIDGE_ASSERT(mData.size() >= mNbElts, "accessing uninitialized const rawPtr");
return (mData.data() + offset); return (mData.data() + offset);
}; };
...@@ -157,12 +153,12 @@ public: ...@@ -157,12 +153,12 @@ public:
}; };
const void *hostPtr(NbElts_t offset = 0) const override final { const void *hostPtr(NbElts_t offset = 0) const override final {
AIDGE_ASSERT(mData.size() >= mTensor.size(), "accessing uninitialized const hostPtr"); AIDGE_ASSERT(mData.size() >= mNbElts, "accessing uninitialized const hostPtr");
return (mData.data() + offset); return (mData.data() + offset);
}; };
void setRawPtr(void *ptr, NbElts_t length) override final { void setRawPtr(void *ptr, NbElts_t length) override final {
AIDGE_ASSERT(length >= mTensor.size(), "trying to set raw pointer of insufficient capacity"); AIDGE_ASSERT(length >= mNbElts, "trying to set raw pointer of insufficient capacity");
mData = future_std::span<T>(static_cast<T *>(ptr), length); mData = future_std::span<T>(static_cast<T *>(ptr), length);
mDataOwner.reset(); mDataOwner.reset();
}; };
...@@ -171,11 +167,11 @@ public: ...@@ -171,11 +167,11 @@ public:
private: private:
void lazyInit() { void lazyInit() {
if (mData.size() < mTensor.size()) { if (mData.size() < mNbElts) {
// Need more data, a re-allocation will occur // Need more data, a re-allocation will occur
AIDGE_ASSERT(mData.empty() || mDataOwner != nullptr, "trying to enlarge non-owned data"); AIDGE_ASSERT(mData.empty() || mDataOwner != nullptr, "trying to enlarge non-owned data");
mDataOwner.reset(new T[mTensor.size()]); mDataOwner.reset(new T[mNbElts]);
mData = future_std::span<T>(mDataOwner.get(), mTensor.size()); mData = future_std::span<T>(mDataOwner.get(), mNbElts);
} }
} }
}; };
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include "aidge/data/Tensor.hpp" #include "aidge/data/Tensor.hpp"
#include "aidge/utils/TensorUtils.hpp"
#include "aidge/backend/cpu/data/TensorImpl.hpp" #include "aidge/backend/cpu/data/TensorImpl.hpp"
using namespace Aidge; using namespace Aidge;
...@@ -57,3 +58,43 @@ TEST_CASE("Tensor creation") { ...@@ -57,3 +58,43 @@ TEST_CASE("Tensor creation") {
} }
} }
} }
TEST_CASE("Tensor methods") {
Tensor x = Array3D<int, 2, 2, 2>{{
{{1, 2},
{3, 4}},
{{5, 6},
{7, 8}}
}};
Tensor xCopy = Array3D<int, 2, 2, 2>{{{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}};
Tensor xFloat =
Array3D<float, 2, 2, 2>{{{{1., 2.}, {3., 4.}}, {{5., 6.}, {7., 8.}}}};
SECTION("Tensor sharing") {
Tensor xCopyCtor(x);
REQUIRE(xCopyCtor.getImpl() == x.getImpl());
Tensor xEqOp = x;
REQUIRE(xEqOp.getImpl() == x.getImpl());
Tensor xCloned = x.clone();
REQUIRE(xCloned.getImpl() != x.getImpl());
REQUIRE(xCloned == x);
}
SECTION("Tensor extract") {
Tensor y = x.extract({0, 1});
REQUIRE(y.getImpl() == x.getImpl());
REQUIRE(approxEq<int>(y, Array1D<int, 2>{{3, 4}}));
REQUIRE(y.isContiguous());
Tensor y2 = x.extract({0, 1, 1}, {2, 1, 1});
REQUIRE(y2.getImpl() == x.getImpl());
REQUIRE(!y2.isContiguous());
Tensor y3 = y2.clone();
REQUIRE(y3.isContiguous());
REQUIRE(approxEq<int>(y3, Array3D<int, 2, 1, 1>{{{{4}}, {{8}}}}));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment