From 96916e41d683400b41610e510d5d8ee3f82a6c03 Mon Sep 17 00:00:00 2001 From: Olivier BICHLER <olivier.bichler@cea.fr> Date: Sun, 3 Dec 2023 18:56:20 +0100 Subject: [PATCH] Initial concept for a Convert operator (UNTESTED) --- include/aidge/backend/TensorImpl.hpp | 71 +++++++++++++++++- include/aidge/data/Tensor.hpp | 18 +++-- include/aidge/graph/GraphView.hpp | 4 +- include/aidge/operator/Add.hpp | 6 +- include/aidge/operator/AvgPooling.hpp | 6 +- include/aidge/operator/BatchNorm.hpp | 12 +-- include/aidge/operator/Concat.hpp | 6 +- include/aidge/operator/Conv.hpp | 8 +- include/aidge/operator/ConvDepthWise.hpp | 8 +- include/aidge/operator/Convert.hpp | 87 ++++++++++++++++++++++ include/aidge/operator/Div.hpp | 8 +- include/aidge/operator/FC.hpp | 10 +-- include/aidge/operator/GenericOperator.hpp | 2 +- include/aidge/operator/Identity.hpp | 2 +- include/aidge/operator/LeakyReLU.hpp | 6 +- include/aidge/operator/MatMul.hpp | 8 +- include/aidge/operator/MaxPooling.hpp | 6 +- include/aidge/operator/MetaOperator.hpp | 4 +- include/aidge/operator/Mul.hpp | 8 +- include/aidge/operator/Operator.hpp | 2 +- include/aidge/operator/Pad.hpp | 6 +- include/aidge/operator/Pow.hpp | 8 +- include/aidge/operator/Producer.hpp | 4 +- include/aidge/operator/ReLU.hpp | 6 +- include/aidge/operator/Scaling.hpp | 6 +- include/aidge/operator/Slice.hpp | 6 +- include/aidge/operator/Softmax.hpp | 6 +- include/aidge/operator/Sqrt.hpp | 6 +- include/aidge/operator/Sub.hpp | 8 +- src/graph/GraphView.cpp | 8 +- src/operator/Convert.cpp | 83 +++++++++++++++++++++ 31 files changed, 336 insertions(+), 93 deletions(-) create mode 100644 include/aidge/operator/Convert.hpp create mode 100644 src/operator/Convert.cpp diff --git a/include/aidge/backend/TensorImpl.hpp b/include/aidge/backend/TensorImpl.hpp index dfe3d932a..1a036a0d5 100644 --- a/include/aidge/backend/TensorImpl.hpp +++ b/include/aidge/backend/TensorImpl.hpp @@ -14,15 +14,81 @@ #include <cstddef> #include <cstdio> +#include "aidge/data/Data.hpp" #include "aidge/utils/Types.h" namespace Aidge { class TensorImpl { public: TensorImpl() = delete; - TensorImpl(const char *backend) : mBackend(backend){}; + TensorImpl(const char *backend, int device = 0) : mBackend(backend), mDevice(device){}; + + /** + * Return the (backend, device) pair for this implementation. + */ + std::pair<std::string, int> device() const { return std::make_pair(mBackend, mDevice); } + + /** + * Set the device ID for current backend. + * @param device New device ID on current backend. + */ + virtual void setDevice(int device) = 0; + + /** + * Copy data from the same device. + * @param src Pointer on current implementation device. + * @param length Number of bytes to copy. + */ virtual void copy(const void *src, NbElts_t length) = 0; - virtual void *rawPtr() = 0; + + /** + * Copy-convert data from the same device. + * @param srcDt Source data type. + * @param src Pointer on current implementation device. + * @param length Number of bytes to copy. + */ + virtual void copyCast(const void *src, NbElts_t length, const DataType srcDt) = 0; + + /** + * Copy data from an other device on the same backend. + * @param device (backend, device) pair to copy from. The backend must match current implementation backend. + * @param src Pointer on current implementation backend. + * @param length Number of bytes to copy. + */ + virtual void copyFromDevice(const void *src, NbElts_t length, const std::pair<std::string, int>& device) = 0; + + /** + * Copy data from host. + * @param src Host pointer to copy from. + * @param length Number of bytes to copy. + */ + virtual void copyFromHost(const void *src, NbElts_t length) = 0; + + /** + * Copy data to host. + * @param src Host pointer to copy to. + * @param length Number of bytes to copy. + */ + virtual void copyToHost(void *dst, NbElts_t length) = 0; + + /** + * Return the raw device pointer. + * The raw pointer is garanteed to be valid only on the *same* device. + */ + virtual void* rawPtr() = 0; + + /** + * Return the host pointer. + * If the implementation does not have a valid host pointer, nullptr is returned. + */ + virtual void* hostPtr() = 0; + + /** + * Sets the device pointer. + * UNSAFE: directly setting the device pointer may lead to undefined behavior + * if it does not match the required storage. + * @param ptr A valid device pointer. + */ virtual void setRawPtr(void* /*ptr*/) { printf("Cannot set raw pointer for backend %s\n", mBackend); @@ -37,6 +103,7 @@ public: private: const char *mBackend; + int mDevice; }; } // namespace Aidge diff --git a/include/aidge/data/Tensor.hpp b/include/aidge/data/Tensor.hpp index f8c3a48f7..9fabd2d4e 100644 --- a/include/aidge/data/Tensor.hpp +++ b/include/aidge/data/Tensor.hpp @@ -301,7 +301,7 @@ class Tensor : public Data, resize(t.dims()); setDataType(t.dataType()); if (t.hasImpl()) { - setBackend(t.mImpl->backend()); + setBackend(t.mImpl->backend(), t.mImpl->device().second); mImpl->copy(t.mImpl->rawPtr(), size()); } else { @@ -327,17 +327,23 @@ class Tensor : public Data, * @details Create and initialized an implementation if non was associated. * @param name */ - inline void setBackend(const std::string &name) { + inline void setBackend(const std::string &name, int device = 0) { if (mImpl) { - if (strcmp(mImpl->backend(), name.c_str()) != 0) { + if (mImpl->device() != std::make_pair(name, device)) { // Backend change: create new impl, copy from old to new and replace // impl std::unique_ptr<TensorImpl> newImpl = Registrar<Tensor>::create({name, mDataType})(*this); - newImpl->copy(mImpl->rawPtr(), size()); - mImpl = std::move(newImpl); + newImpl->setDevice(device); + + //TODO: FIXME: copy() work only on same device! + //newImpl->copy(mImpl->rawPtr(), size()); + //mImpl = std::move(newImpl); } - } else + } + else { mImpl = Registrar<Tensor>::create({name, mDataType})(*this); + mImpl->setDevice(device); + } } /** diff --git a/include/aidge/graph/GraphView.hpp b/include/aidge/graph/GraphView.hpp index bf23ef9f0..2a367b4da 100644 --- a/include/aidge/graph/GraphView.hpp +++ b/include/aidge/graph/GraphView.hpp @@ -203,7 +203,7 @@ public: * If not, add a Transpose Operator. * 4 - Propagate Tensor dimensions through the consecutive Operators. */ - void compile(const std::string& backend, const Aidge::DataType datatype); + void compile(const std::string& backend, const Aidge::DataType datatype, int device = 0); /** * @brief Compute dimensions of input/output Tensors for each Operator of the @@ -212,7 +212,7 @@ public: void forwardDims(); /** @brief Set the same backend for each Operator of the GraphView object's Nodes. */ - void setBackend(const std::string &backend); + void setBackend(const std::string &backend, int device = 0); /** @brief Set the same backend for each Operator of the GraphView object's Nodes. */ void setDataType(const DataType &datatype); diff --git a/include/aidge/operator/Add.hpp b/include/aidge/operator/Add.hpp index 0c2854029..59188fcf2 100644 --- a/include/aidge/operator/Add.hpp +++ b/include/aidge/operator/Add.hpp @@ -76,13 +76,13 @@ public: // } - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<Add_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround for (std::size_t i = 0; i < nbInputs(); ++i) { - getInput(i)->setBackend(name); + getInput(i)->setBackend(name, device); } } diff --git a/include/aidge/operator/AvgPooling.hpp b/include/aidge/operator/AvgPooling.hpp index f0f9f6c54..483f8a60b 100644 --- a/include/aidge/operator/AvgPooling.hpp +++ b/include/aidge/operator/AvgPooling.hpp @@ -130,12 +130,12 @@ public: // } - void setBackend(const std::string &name) override { + void setBackend(const std::string &name, int device = 0) override { mImpl = Registrar<AvgPooling_Op<DIM>>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); + getInput(0)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/BatchNorm.hpp b/include/aidge/operator/BatchNorm.hpp index 09a9bb9ef..43a096534 100644 --- a/include/aidge/operator/BatchNorm.hpp +++ b/include/aidge/operator/BatchNorm.hpp @@ -94,15 +94,15 @@ public: } } - void setBackend(const std::string &name) override { + void setBackend(const std::string &name, int device = 0) override { mImpl = Registrar<BatchNorm_Op<DIM>>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(1)->setBackend(name); - getInput(2)->setBackend(name); - getInput(3)->setBackend(name); - getInput(4)->setBackend(name); + getInput(1)->setBackend(name, device); + getInput(2)->setBackend(name, device); + getInput(3)->setBackend(name, device); + getInput(4)->setBackend(name, device); } static const std::vector<std::string> getInputsName() { diff --git a/include/aidge/operator/Concat.hpp b/include/aidge/operator/Concat.hpp index 01d590aa7..c6b679cf5 100644 --- a/include/aidge/operator/Concat.hpp +++ b/include/aidge/operator/Concat.hpp @@ -101,13 +101,13 @@ public: } } - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<Concat_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround for (std::size_t i = 0; i < nbInputs(); ++i) { - getInput(i)->setBackend(name); + getInput(i)->setBackend(name, device); } } diff --git a/include/aidge/operator/Conv.hpp b/include/aidge/operator/Conv.hpp index 4f0fb1ea2..8ae56e9b9 100644 --- a/include/aidge/operator/Conv.hpp +++ b/include/aidge/operator/Conv.hpp @@ -169,13 +169,13 @@ public: // AIDGE_THROW_OR_ABORT(std::runtime_error, "Given outputDim out of range or output dim not forwarded yet."); // } - void setBackend(const std::string &name) override { + void setBackend(const std::string &name, int device = 0) override { mImpl = Registrar<Conv_Op<DIM>>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(1)->setBackend(name); - getInput(2)->setBackend(name); + getInput(1)->setBackend(name, device); + getInput(2)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/ConvDepthWise.hpp b/include/aidge/operator/ConvDepthWise.hpp index ca6401e0e..591f7c6ed 100644 --- a/include/aidge/operator/ConvDepthWise.hpp +++ b/include/aidge/operator/ConvDepthWise.hpp @@ -151,13 +151,13 @@ public: // AIDGE_THROW_OR_ABORT(std::runtime_error, "Given outputDim out of range or output dim not forwarded yet."); // } - void setBackend(const std::string &name) override { + void setBackend(const std::string &name, int device = 0) override { mImpl = Registrar<ConvDepthWise_Op<DIM>>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(1)->setBackend(name); - getInput(2)->setBackend(name); + getInput(1)->setBackend(name, device); + getInput(2)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/Convert.hpp b/include/aidge/operator/Convert.hpp new file mode 100644 index 000000000..19ffb9a0d --- /dev/null +++ b/include/aidge/operator/Convert.hpp @@ -0,0 +1,87 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#ifndef AIDGE_CORE_OPERATOR_CONVERT_H_ +#define AIDGE_CORE_OPERATOR_CONVERT_H_ + +#include <cassert> +#include <memory> +#include <vector> + +#include "aidge/utils/Registrar.hpp" +#include "aidge/operator/OperatorTensor.hpp" +#include "aidge/backend/OperatorImpl.hpp" +#include "aidge/data/Tensor.hpp" +#include "aidge/graph/Node.hpp" +#include "aidge/utils/Types.h" + +namespace Aidge { + +class Convert_Op : public OperatorTensor, + public Registrable<Convert_Op, std::tuple<std::string, std::string>, std::unique_ptr<OperatorImpl>(const Convert_Op&)> { +public: + static constexpr const char* Type = "Convert"; + + Convert_Op() : OperatorTensor(Type, 1, 0, 1) {} + + /** + * @brief Copy-constructor. Copy the operator attributes and its output tensor(s), but not its input tensors (the new operator has no input associated). + * @param op Operator to copy. + */ + Convert_Op(const Convert_Op& op) + : OperatorTensor(op) + { + mImpl = op.mImpl ? Registrar<Convert_Op>::create({mInputs[0]->getImpl()->backend(), mOutputs[0]->getImpl()->backend()})(*this) : nullptr; + } + + /** + * @brief Clone the operator using its copy-constructor. + * @see Operator::Convert_Op + */ + std::shared_ptr<Operator> clone() const override { + return std::make_shared<Convert_Op>(*this); + } + + void setBackend(const std::string& name, int device = 0) override { + if (Registrar<Convert_Op>::exists({mInputs[0]->getImpl()->backend(), name})) { + mImpl = Registrar<Convert_Op>::create({mInputs[0]->getImpl()->backend(), name})(*this); + } + mOutputs[0]->setBackend(name, device); + } + + void setDataType(const DataType& dataType) const override { + mOutputs[0]->setDataType(dataType); + } + + void forward() override; + + static const std::vector<std::string> getInputsName(){ + return {"data_input"}; + } + static const std::vector<std::string> getOutputsName(){ + return {"data_output"}; + } + +private: + /// @brief Store the data to the right type on input device + /// Required for any type conversion. + std::shared_ptr<Tensor> mConvertedInput; + /// @brief Store the data to the right type on host + /// Required if there is no direct link between input and output devices + std::shared_ptr<Tensor> mHostBuffer; +}; + +inline std::shared_ptr<Node> Convert(const std::string& name = "") { + return std::make_shared<Node>(std::make_shared<Convert_Op>(), name); +} +} + +#endif /* AIDGE_CORE_OPERATOR_CONVERT_H_ */ \ No newline at end of file diff --git a/include/aidge/operator/Div.hpp b/include/aidge/operator/Div.hpp index ba76c0bde..933e2e092 100644 --- a/include/aidge/operator/Div.hpp +++ b/include/aidge/operator/Div.hpp @@ -54,13 +54,13 @@ public: void computeOutputDims() override final; - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<Div_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); - getInput(1)->setBackend(name); + getInput(0)->setBackend(name, device); + getInput(1)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/FC.hpp b/include/aidge/operator/FC.hpp index 4cece292c..23f2459af 100644 --- a/include/aidge/operator/FC.hpp +++ b/include/aidge/operator/FC.hpp @@ -95,14 +95,14 @@ public: } - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<FC_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); - getInput(1)->setBackend(name); - getInput(2)->setBackend(name); + getInput(0)->setBackend(name, device); + getInput(1)->setBackend(name, device); + getInput(2)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/GenericOperator.hpp b/include/aidge/operator/GenericOperator.hpp index 505c53449..56e57a14a 100644 --- a/include/aidge/operator/GenericOperator.hpp +++ b/include/aidge/operator/GenericOperator.hpp @@ -97,7 +97,7 @@ public: ~GenericOperator_Op() = default; - void setBackend(const std::string & /*name*/) override { printf("setBackend: not available yet.\n"); } + void setBackend(const std::string & /*name*/, int /*device*/ = 0) override { printf("setBackend: not available yet.\n"); } void setDataType(const DataType& /*datatype*/) const override { printf("setDataType: not available yet.\n"); } void forward() override final { if(mImpl){ diff --git a/include/aidge/operator/Identity.hpp b/include/aidge/operator/Identity.hpp index c5cd9bb62..55d7f492f 100644 --- a/include/aidge/operator/Identity.hpp +++ b/include/aidge/operator/Identity.hpp @@ -103,7 +103,7 @@ public: } return mInputs[outputIdx]; } - void setBackend(const std::string& name) override final { + void setBackend(const std::string& name, int device = 0) override final { // setBackend do nothing, Identity node has no backend it just pass the same Tensor } void setDataType(const DataType& dataType) const override final { diff --git a/include/aidge/operator/LeakyReLU.hpp b/include/aidge/operator/LeakyReLU.hpp index 800c8c61d..d8a608331 100644 --- a/include/aidge/operator/LeakyReLU.hpp +++ b/include/aidge/operator/LeakyReLU.hpp @@ -67,12 +67,12 @@ public: - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<LeakyReLU_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); + getInput(0)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/MatMul.hpp b/include/aidge/operator/MatMul.hpp index 23c12d458..e69708947 100644 --- a/include/aidge/operator/MatMul.hpp +++ b/include/aidge/operator/MatMul.hpp @@ -83,13 +83,13 @@ public: } - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<MatMul_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); - getInput(1)->setBackend(name); + getInput(0)->setBackend(name, device); + getInput(1)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/MaxPooling.hpp b/include/aidge/operator/MaxPooling.hpp index ad50a27a9..01658bb04 100644 --- a/include/aidge/operator/MaxPooling.hpp +++ b/include/aidge/operator/MaxPooling.hpp @@ -104,12 +104,12 @@ public: } - void setBackend(const std::string &name) override { + void setBackend(const std::string &name, int device = 0) override { mImpl = Registrar<MaxPooling_Op<DIM>>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); + getInput(0)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/MetaOperator.hpp b/include/aidge/operator/MetaOperator.hpp index 991c1c60d..3427890b0 100644 --- a/include/aidge/operator/MetaOperator.hpp +++ b/include/aidge/operator/MetaOperator.hpp @@ -70,7 +70,7 @@ public: } - void setBackend(const std::string &name) override { + void setBackend(const std::string &name, int device = 0) override { if (Registrar<MetaOperator_Op>::exists({name, type()})) { // A custom implementation exists for this meta operator mImpl = Registrar<MetaOperator_Op>::create({name, type()})(*this); @@ -79,7 +79,7 @@ public: // The micro-graph should always be set to the right backend, since it // shares input/output tensors. // Input/output tensors backend are updated here. - mGraph->setBackend(name); + mGraph->setBackend(name, device); } void setDataType(const DataType &datatype) const override { diff --git a/include/aidge/operator/Mul.hpp b/include/aidge/operator/Mul.hpp index 5b9ab4eb8..7eb384f35 100644 --- a/include/aidge/operator/Mul.hpp +++ b/include/aidge/operator/Mul.hpp @@ -56,13 +56,13 @@ public: void computeOutputDims() override final; - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<Mul_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); - getInput(1)->setBackend(name); + getInput(0)->setBackend(name, device); + getInput(1)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/Operator.hpp b/include/aidge/operator/Operator.hpp index b0f8435bd..1dd7c4beb 100644 --- a/include/aidge/operator/Operator.hpp +++ b/include/aidge/operator/Operator.hpp @@ -114,7 +114,7 @@ public: // IMPLEMENTATION /////////////////////////////////////////////////////// - virtual void setBackend(const std::string& name) = 0; + virtual void setBackend(const std::string& name, int device = 0) = 0; virtual void setDataType(const DataType& dataType) const = 0; /** diff --git a/include/aidge/operator/Pad.hpp b/include/aidge/operator/Pad.hpp index 279b8b3d2..ab54853a3 100644 --- a/include/aidge/operator/Pad.hpp +++ b/include/aidge/operator/Pad.hpp @@ -97,12 +97,12 @@ public: } } - void setBackend(const std::string &name) override { + void setBackend(const std::string &name, int device = 0) override { mImpl = Registrar<Pad_Op<DIM>>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); + getInput(0)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/Pow.hpp b/include/aidge/operator/Pow.hpp index 0b0ae82f0..d437f1e4e 100644 --- a/include/aidge/operator/Pow.hpp +++ b/include/aidge/operator/Pow.hpp @@ -54,13 +54,13 @@ public: void computeOutputDims() override final; - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<Pow_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); - getInput(1)->setBackend(name); + getInput(0)->setBackend(name, device); + getInput(1)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/Producer.hpp b/include/aidge/operator/Producer.hpp index fb6a20403..3c37b3d61 100644 --- a/include/aidge/operator/Producer.hpp +++ b/include/aidge/operator/Producer.hpp @@ -73,9 +73,9 @@ public: inline const std::vector<DimSize_t> dims() const noexcept { return mOutputs[0]->dims(); } - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<Producer_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/ReLU.hpp b/include/aidge/operator/ReLU.hpp index 3444c25fc..97156fda9 100644 --- a/include/aidge/operator/ReLU.hpp +++ b/include/aidge/operator/ReLU.hpp @@ -51,12 +51,12 @@ public: } - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<ReLU_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); + getInput(0)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/Scaling.hpp b/include/aidge/operator/Scaling.hpp index fd6d6bcfc..e56901e1e 100644 --- a/include/aidge/operator/Scaling.hpp +++ b/include/aidge/operator/Scaling.hpp @@ -66,11 +66,11 @@ public: return std::make_shared<Scaling_Op>(*this); } - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<Scaling_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - mInputs[0]->setBackend(name); + mInputs[0]->setBackend(name, device); } static const std::vector<std::string> getInputsName() { diff --git a/include/aidge/operator/Slice.hpp b/include/aidge/operator/Slice.hpp index 7bdbd8099..c635ea3f5 100644 --- a/include/aidge/operator/Slice.hpp +++ b/include/aidge/operator/Slice.hpp @@ -95,12 +95,12 @@ public: mOutputs[0]->resize(outputDims); } - void setBackend(const std::string &name) { + void setBackend(const std::string &name, int device = 0) { mImpl = Registrar<Slice_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); + getInput(0)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/Softmax.hpp b/include/aidge/operator/Softmax.hpp index cc19cb821..08500b2fa 100644 --- a/include/aidge/operator/Softmax.hpp +++ b/include/aidge/operator/Softmax.hpp @@ -51,12 +51,12 @@ public: return std::make_shared<Softmax_Op>(*this); } - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<Softmax_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); + getInput(0)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/Sqrt.hpp b/include/aidge/operator/Sqrt.hpp index a4069b59b..7df04f776 100644 --- a/include/aidge/operator/Sqrt.hpp +++ b/include/aidge/operator/Sqrt.hpp @@ -56,12 +56,12 @@ public: return std::make_shared<Sqrt_Op>(*this); } - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<Sqrt_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); + getInput(0)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/include/aidge/operator/Sub.hpp b/include/aidge/operator/Sub.hpp index becf98926..d4c8c7ad0 100644 --- a/include/aidge/operator/Sub.hpp +++ b/include/aidge/operator/Sub.hpp @@ -59,13 +59,13 @@ public: void computeOutputDims() override final; - void setBackend(const std::string& name) override { + void setBackend(const std::string& name, int device = 0) override { mImpl = Registrar<Sub_Op>::create(name)(*this); - mOutputs[0]->setBackend(name); + mOutputs[0]->setBackend(name, device); // FIXME: temporary workaround - getInput(0)->setBackend(name); - getInput(1)->setBackend(name); + getInput(0)->setBackend(name, device); + getInput(1)->setBackend(name, device); } static const std::vector<std::string> getInputsName(){ diff --git a/src/graph/GraphView.cpp b/src/graph/GraphView.cpp index 0b64c518c..01de48b41 100644 --- a/src/graph/GraphView.cpp +++ b/src/graph/GraphView.cpp @@ -247,10 +247,10 @@ Aidge::GraphView::inputs(std::string name) const { return mNodeRegistry.at(name)->inputs(); } -void Aidge::GraphView::compile(const std::string& backend, const Aidge::DataType datatype) { +void Aidge::GraphView::compile(const std::string& backend, const Aidge::DataType datatype, int device) { // Backend // TODO: add Backend attribute to Operator - setBackend(backend); + setBackend(backend, device); // Data type // TODO: manage Datatype attribute in OperatorImpl setDataType(datatype); @@ -320,9 +320,9 @@ void Aidge::GraphView::_forwardDims(std::set<std::shared_ptr<Node>> listNodes) { } } -void Aidge::GraphView::setBackend(const std::string &backend) { +void Aidge::GraphView::setBackend(const std::string &backend, int device) { for (auto node : getNodes()) { - node->getOperator()->setBackend(backend); + node->getOperator()->setBackend(backend, device); } } diff --git a/src/operator/Convert.cpp b/src/operator/Convert.cpp new file mode 100644 index 000000000..d88fcfed4 --- /dev/null +++ b/src/operator/Convert.cpp @@ -0,0 +1,83 @@ +/******************************************************************************** + * Copyright (c) 2023 CEA-List + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * SPDX-License-Identifier: EPL-2.0 + * + ********************************************************************************/ + +#include <cassert> +#include <cstddef> +#include <vector> +#include <utility> + +#include "aidge/backend/OperatorImpl.hpp" +#include "aidge/operator/Convert.hpp" +#include "aidge/utils/Types.h" +#include "aidge/utils/ErrorHandling.hpp" + +void Aidge::Convert_Op::forward() { + if (mImpl) { + mImpl->forward(); + } + else { + // mConvertedInput stores data to the desired (output) type + if (mInputs[0]->dataType() != mOutputs[0]->dataType()) { + // Different type: create a new tensor on same input device + if (!mConvertedInput) { + mConvertedInput = std::make_shared<Tensor>(mOutputs[0]->dataType()); + } + + mConvertedInput->setDataType(mOutputs[0]->dataType()); + const auto device = mInputs[0]->getImpl()->device(); + mConvertedInput->setBackend(device.first, device.second); + mConvertedInput->resize(mInputs[0]->dims()); + + // Copy convert input to mConvertedInput + mConvertedInput->getImpl()->copyCast(mInputs[0]->getImpl()->rawPtr(), mInputs[0]->size(), mInputs[0]->dataType()); + } + else { + // Same type: mConvertedInput *is* the input + mConvertedInput = mInputs[0]; + } + + // Copy to output device, if necessary + if (mConvertedInput->getImpl()->device() != mOutputs[0]->getImpl()->device()) { + if (mConvertedInput->getImpl()->backend() == mOutputs[0]->getImpl()->backend()) { + // Same backend, but different device + mOutputs[0]->getImpl()->copyFromDevice(mConvertedInput->getImpl()->rawPtr(), mConvertedInput->size(), mConvertedInput->getImpl()->device()); + } + else if (mConvertedInput->getImpl()->hostPtr() != nullptr) { + // Different backend, but input is valid on host + mOutputs[0]->getImpl()->copyFromHost(mConvertedInput->getImpl()->hostPtr(), mConvertedInput->size()); + } + else if (mOutputs[0]->getImpl()->hostPtr() != nullptr) { + // Different backend, but output is valid on host + mConvertedInput->getImpl()->copyToHost(mOutputs[0]->getImpl()->hostPtr(), mConvertedInput->size()); + } + else { + // No direct link possible from input to output device + // SLOW SOLUTION: must pass through the host, requires TWO copies + const auto availableBackends = Tensor::getAvailableBackends(); + AIDGE_ASSERT(availableBackends.find("cpu") != availableBackends.end(), "Conversion requires CPU backend"); + + if (!mHostBuffer) { + mHostBuffer = std::make_shared<Tensor>(mOutputs[0]->dataType()); + mHostBuffer->setBackend("cpu"); + } + + mConvertedInput->getImpl()->copyToHost(mHostBuffer->getImpl()->rawPtr(), mConvertedInput->size()); + mOutputs[0]->getImpl()->copyFromHost(mHostBuffer->getImpl()->rawPtr(), mConvertedInput->size()); + } + } + else { + // Same device: simple copy on device + mConvertedInput->getImpl()->copy(mConvertedInput->getImpl()->rawPtr(), mConvertedInput->size()); + } + } + + runHooks(); +} -- GitLab