Skip to content
Snippets Groups Projects
Commit 19dab4fa authored by Grégoire Kubler's avatar Grégoire Kubler
Browse files

chore : moved broadcasting files from backend_cpu to core

parent 1f26e48b
No related branches found
No related tags found
No related merge requests found
Pipeline #54494 passed
/********************************************************************************
* Copyright (c) 2024 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_DATA_BROADCASTING_H_
#define AIDGE_CORE_DATA_BROADCASTING_H_
#include <vector>
namespace Aidge {
// Function to broadCast an input dims vector into the same size as an outputDims vector
/**
* @brief Broadcast an input dims vector into the same size as an outputDims vector
* @details The missing dimensions would be completed by 1
* @example T1 dims are [1,3,1,5]
* T2 dims are [5]
* To simplify the computation we would like to have the same number of dimensions to
* @param outputDims The vector of dimensions to follow
* @param dimsToBroadcast The vecotr of dimensions to braodcast
* @return std::vector<std::size_t> a broadcasted vector by addding 1 on the missing dimensions.
*/
std::vector<std::size_t> getBroadcastedDims(const std::vector<std::size_t>& outputDims, const std::vector<std::size_t>& dimsToBroadcast);
/**
* @brief Get a vector of indexes along the dimensions vector from a flattened index
* @param dimensions The vector of dimensions we want the indexes on
* @param idx The flattened index
* @return std::vector<std::size_t> vector of indexes along dimensions.
*/
std::vector<std::size_t> getMultiDimIndices(const std::vector<std::size_t>& dimensions, std::size_t idx);
// Function to get a flattened index from multi-dimensional indices
/**
* @brief Get a flattened index the dimensions vector from a given vector of indices on a broadcasted vector
* @param dimensions The vector of dimensions we want the flattened index on
* @param indices The vector of indices we want to flatten
* @return std::size_t The flattened index on the dimensions vector
*/
std::size_t getFlattenedIndex(const std::vector<std::size_t>& dimensions, const std::vector<std::size_t>& indices);
} // namespace Aidge
#endif // AIDGE_CORE_DATA_BROADCASTING_H_
/********************************************************************************
* Copyright (c) 2024 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 "aidge/data/Broadcasting.hpp"
#include <vector>
namespace Aidge {
std::vector<std::size_t>
getBroadcastedDims(const std::vector<std::size_t> &outputDims,
const std::vector<std::size_t> &dimsToBroadcast) {
std::vector<std::size_t> broadcastedDims(outputDims.size(), 1);
for (int j = dimsToBroadcast.size() - 1; j >= 0; --j) {
std::size_t idx = outputDims.size() - (dimsToBroadcast.size() - j);
broadcastedDims[idx] = dimsToBroadcast[j];
}
return broadcastedDims;
}
std::vector<std::size_t>
getMultiDimIndices(const std::vector<std::size_t> &dimensions,
std::size_t idx) {
std::vector<std::size_t> indices(dimensions.size(), 0);
for (int i = dimensions.size() - 1; i >= 0; --i) {
indices[i] = idx % dimensions[i];
idx /= dimensions[i];
}
return indices;
}
std::size_t getFlattenedIndex(const std::vector<std::size_t> &dimensions,
const std::vector<std::size_t> &indices) {
std::size_t flattenedIdx = 0;
std::size_t stride = 1;
for (int i = dimensions.size() - 1; i >= 0; --i) {
std::size_t idx = dimensions[i] > 1 ? indices[i] : 0;
flattenedIdx += idx * stride;
stride *= dimensions[i];
}
return flattenedIdx;
}
} // namespace Aidge
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