diff --git a/include/aidge/backend/TensorImpl.hpp b/include/aidge/backend/TensorImpl.hpp index dfe3d932ac68929acfd26ecf7126e07c4707bcfc..1a036a0d53bdda4265ca09bae8be19c51e4f0bba 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 f8c3a48f7d5169dfee2cdceff37465f61bbb546c..9fabd2d4e2faa6d71ded929b3bf8f0910b5e6d1d 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 bf23ef9f0957f15538986739a8f3086373879efc..2a367b4da29b4e1cfd1f4019c7b205a359a059fb 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 0c285402929ab7b071d732180891de1b738dc4a8..59188fcf2fa9b3536f5c4f967d32bfc05ca2725a 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 f0f9f6c54ed1953ed31b713ce19edc7a8e594d4a..483f8a60b2927bc0a84e6ef71d6b75a9eff9d907 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 09a9bb9efac81431673ef3449f717fbcb9af5108..43a09653407eae9a2928d19cad5eb6c9b4da6d33 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 01d590aa7425cb62ab665c0078019a6c8ab4a66a..c6b679cf5d59c1da71ce8cc71a98ec58ecca91d6 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 4f0fb1ea2717c1fdf4443c450000ec3a56bb9b5b..8ae56e9b9d066cce21644d8fc0432bd47f5d1cbc 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 ca6401e0ed3ac888f12858853f0d8f494c226041..591f7c6ed6aca142a64d771ef0b03efbdbf838ab 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 0000000000000000000000000000000000000000..19ffb9a0de59b8f33e51736032625c9a1b0098f7 --- /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 ba76c0bdecfaf86644a3336a1076064b96b36046..933e2e092c0a6147cde76bcb630803a684125758 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 4cece292cb322c0a58f96380eb0f0083771d3c19..23f2459af4daceb2a493c4ce07965cba6b0aa305 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 505c5344990453c8f4ab84fa3893e75b216d7a54..56e57a14ac2a22c91fd9f300c9e85fda998285fc 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 c5cd9bb62e0097c9a0e646caaf14cddd73bf512d..55d7f492ffd1e64ace2ab820749356144d675e9c 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 800c8c61d876b6f33cce1af3365179b7eb14b68d..d8a608331d45bbbfa5292dec3bdd7c730f069396 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 23c12d45802e25f29891c48164acfb2d3ad137ac..e69708947297d2e592a60efb6e841856670240a4 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 ad50a27a94a2217c94445fb556c84ec7f121c6b9..01658bb04aaa0b9f453722b26a7d2869cfb3175d 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 991c1c60dbd7eab79c132447b78cc429a928426b..3427890b048b74c7ba1968c6fe3ea7369883c1c6 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 5b9ab4eb8c3924133f32ddfeb0a5f05963381771..7eb384f35311965b6deec389bb4a6b6c82045bf0 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 b0f8435bd0126cf3fba9f956a432017585a4d873..1dd7c4beb98c7fc4c65b3f0783ac7c47d0396069 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 279b8b3d2c173d18c65c17e50385954a88fde77e..ab54853a32586caafbdf70ce18d3966b1c4927b2 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 0b0ae82f012eace8b5a2d5eb362a359386495b79..d437f1e4e61912bb750ce3e52504bc2d8a0c0c42 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 fb6a20403adc1ee5cddb5869fd9d39ef59fb776e..3c37b3d6178a5a7a4e74b97c79796953e09b5a5a 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 3444c25fc2e1572e78a1377b3273580f494ac8f9..97156fda9e8d26ad6158a8062e4765a6e8c91763 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 fd6d6bcfccc36829671538e1f2e31b13644e3938..e56901e1e7c5a10ff43effa83f9cb6c994a54100 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 7bdbd8099ab79c9f1714989dc41cfc0893427bc9..c635ea3f51856e0ffb04544e1a9c5ae89d5ccf22 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 cc19cb8210af516f349de124f65cdd55308609fb..08500b2fa64c09ba6aede45b8d8ff328b11855ae 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 a4069b59bbe7e7586d02b71a39d811d9bf972b77..7df04f776e13ace53c175b5a646c081133592945 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 becf98926d2da777c6551e8ed2fbd7b5fcf50017..d4c8c7ad0e28e6a204bafbee7bd3ed1d19aa47a6 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 0b64c518cd5aad7d4ae6841dea53d828c4c85923..01de48b418f8188077a5f5291dbcc2f9a5a5748d 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 0000000000000000000000000000000000000000..d88fcfed4ab5191a1d6b1bf2850cffa236e211eb --- /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(); +}