Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Add.cpp 2.26 KiB
/********************************************************************************
 * 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 <cstddef>  // std::size_t
#include <string>
#include <vector>

#include "aidge/operator/Add.hpp"
#include "aidge/utils/Types.h"
#include "aidge/utils/ErrorHandling.hpp"

const std::string Aidge::Add_Op::Type = "Add";

void Aidge::Add_Op::computeOutputDims() {
    // check inputs have been associated
    bool associated = (nbInputs() > 0); // do not compute anything if no input
    for (IOIndex_t i = 0; i < nbInputs(); ++i) {
        if (!getInput(i)) {
            AIDGE_THROW_OR_ABORT(std::runtime_error, "Every input should be associated with a Tensor");
        }
        associated &= !(getInput(i)->empty());
    }
    if (associated) {
        std::vector<std::vector<std::size_t>> inputsDims(nbInputs());
        for (std::size_t i = 0; i < nbInputs(); i++) {
            inputsDims[i] = getInput(i)->dims();
        }

        std::size_t outNbDims = 1;
        for(std::size_t i = 0; i < nbInputs(); ++i) {
            outNbDims = (inputsDims[i].size() > outNbDims) ? inputsDims[i].size() : outNbDims;
        }

        std::vector<std::size_t> outDims(outNbDims, 1);

        for (auto it = outDims.rbegin(); it != outDims.rend(); ++it) {
            for (size_t i = 0; i < inputsDims.size(); i++) {
                if(!inputsDims[i].empty()) {
                    std::size_t dim = inputsDims[i].back();
                    inputsDims[i].pop_back();
                    if (*it != dim) {
                        if(dim != 1) {
                            if (*it != 1) {
                                AIDGE_THROW_OR_ABORT(std::runtime_error, "Unsopported Tensor shape for Add operation");
                            }
                            else {
                                *it = dim;
                            }
                        }
                    }
                }
            }
        }
        mOutputs[0]->resize(outDims);
    }
}