Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Div.cpp 2.08 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 <algorithm>
#include <cassert>
#include <cstddef>
#include <string>
#include <vector>
#include <utility>

#include "aidge/backend/OperatorImpl.hpp"
#include "aidge/operator/Div.hpp"
#include "aidge/utils/Types.h"
#include "aidge/utils/ErrorHandling.hpp"

const std::string Aidge::Div_Op::Type = "Div";

void Aidge::Div_Op::computeOutputDims() {
    // check inputs have been associated
    if (!getInput(0) || !getInput(1)) {
        AIDGE_THROW_OR_ABORT(std::runtime_error, "At least one input was not connected");
    }

    if (!getInput(0)->empty() && !getInput(1)->empty()) {

        std::vector<std::vector<std::size_t>> inputsDims{getInput(0)->dims(), getInput(1)->dims()};

        std::vector<std::size_t> outDims = (inputsDims[0].size() >= inputsDims[1].size()) ?
                                    		inputsDims[0] : inputsDims[1];

        std::vector<std::size_t>::iterator it = outDims.end();
        while (it != outDims.begin()) {
            --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 Div Operation");
                            }
                            else {
                                *it = dim;
                            }
                        }
                    }
                }
            }
        }
        mOutputs[0]->resize(outDims);
    }
}