Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/excuter/op-mem-ompsimd/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

| Operation | Author | Func Def | Math Formula | IR Instruction |
|-----------|--------|------------|--------------|----------------|
| reducemax | miaobyte | reducemax(tensor<any> A, vector<int32> axis, var<bool> keepdims)->(tensor<any> B) | B = reducemax(A, axis=[1 2], keepdims=false) | reducemax(tensor<any> A, vector<int32> axis, var<bool> keepdims)->(tensor<any> B) |
| broadcastTo | miaobyte | broadcastTo(tensor<any> A, vector<int32> new_shape)->(tensor<any> B) | T2 = T1.broadcastTo(new_shape=[4,3,2]) | broadcastTo(tensor<any> A, vector<int32> new_shape)->(tensor<any> B) |
| concat | miaobyte | concat(listtensor<any> tensors, var<int32> axis)->(tensor<any> result) | Tresult = concat([T1, T2...], axis=3) | concat(listtensor<any> tensors, var<int32> axis)->(tensor<any> result) |
| transpose | miaobyte | transpose(tensor<any> A, vector<int32> dim_order)->(tensor<any> C) | T1.transpose(dimorder=[1,0])->T2 | transpose(tensor<any> A, vector<int32> dim_order)->(tensor<any> C) |
Expand All @@ -21,8 +22,10 @@
| newtensor | none | newtensor(vector<int32> shape)->(tensor<any> tensor1) | T1 =Tensor(shape=[...]) | newtensor(vector<int32> shape)->(tensor<any> tensor1) |
| newtensor | none | newtensor(var<string> shape)->(tensor<any> tensor1) | T1 =Tensor(shape=[...]) | newtensor(var<string> shape)->(tensor<any> tensor1) |
| vecset | none | vecset(vector<any> value)->(vector<any> name) | shape = [3 4 5] | vecset(vector<any> value)->(vector<any> name) |
| reducemin | miaobyte | reducemin(tensor<any> A, vector<int32> axis, var<bool> keepdims)->(tensor<any> B) | B = reducemin(A, axis=[1 2], keepdims=false) | reducemin(tensor<any> A, vector<int32> axis, var<bool> keepdims)->(tensor<any> B) |
| subscalar | miaobyte | subscalar(tensor<any> a, var<any> scalar)->(tensor<any> c) | T3=T1-scalar | subscalar(tensor<any> a, var<any> scalar)->(tensor<any> c) |
| sqrt | miaobyte | sqrt(tensor<any> A)->(tensor<any> C) | T3=sqrt(T1) | sqrt(tensor<any> A)->(tensor<any> C) |
| sum | miaobyte | sum(tensor<any> A, vector<int32> axis, var<bool> keepdims)->(tensor<any> B) | B = sum(A, axis=[1 2], keepdims=false) | sum(tensor<any> A, vector<int32> axis, var<bool> keepdims)->(tensor<any> B) |
| argset | none | argset(var<any> value)->(var<any> name) | var argname = argvalue | argset(var<any> value)->(var<any> name) |
| sub | miaobyte | sub(tensor<any> a, tensor<any> b)->(tensor<any> c) | T3=T1-T2 | sub(tensor<any> a, tensor<any> b)->(tensor<any> c) |
| mulscalar | miaobyte | mulscalar(tensor<any> A, var<any> b)->(tensor<any> C) | T3=T1*scalar | mulscalar(tensor<any> A, var<any> b)->(tensor<any> C) |
Expand All @@ -36,6 +39,7 @@
| exp | miaobyte | exp(tensor<any> A)->(tensor<any> C) | T3=exp(T1) | exp(tensor<any> A)->(tensor<any> C) |
| rdivscalar | miaobyte | rdivscalar(var<any> scalar, tensor<any> A)->(tensor<any> C) | T3=scalar/T1 | rdivscalar(var<any> scalar, tensor<any> A)->(tensor<any> C) |
| minscalar | miaobyte | minscalar(tensor<any> A, var<any> scalar)->(tensor<any> C) | T3=min(T1,scalar) | minscalar(tensor<any> A, var<any> scalar)->(tensor<any> C) |
| prod | miaobyte | prod(tensor<any> A, vector<int32> axis, var<bool> keepdims)->(tensor<any> B) | B = prod(A, axis=[1 2], keepdims=false) | prod(tensor<any> A, vector<int32> axis, var<bool> keepdims)->(tensor<any> B) |
| min | miaobyte | min(tensor<any> A, tensor<any> B)->(tensor<any> C) | T3=min(T1,T2) | min(tensor<any> A, tensor<any> B)->(tensor<any> C) |
| compare | miaobyte | compare(tensor<any> A, tensor<any> B)->(tensor<float32> mask) | mask=compare(T1,T2) | compare(tensor<any> A, tensor<any> B)->(tensor<float32> mask) |
| matmul | cblas | matmul(tensor<float64|float32> A, tensor<float64|float32> B)->(tensor<float64|float32> C) | T3=T1 @ T2 | matmul(tensor<float64|float32> A, tensor<float64|float32> B)->(tensor<float64|float32> C) |
Expand Down
21 changes: 0 additions & 21 deletions excuter/cpp-common/src/deepx/shape_broadcast.hpp

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
#include <vector>
#include <stdexcept>

#include "deepx/shape_broadcast.hpp"
#include "deepx/shape_changeshape.hpp"

namespace deepx
{

//transpose

std::vector<int> swaplastTwoDimOrder(const std::vector<int> &shape)
{
vector<int> dimOrder = shape;
std::iota(dimOrder.begin(), dimOrder.end(), 0);
swap(dimOrder[dimOrder.size() - 1], dimOrder[dimOrder.size() - 2]);
return dimOrder;
}
std::vector<int> transposeShape(const std::vector<int> &shape, const std::vector<int> &dimOrder)
{
if (dimOrder.size() != shape.size())
{
throw std::invalid_argument("dimOrder size does not match the number of dimensions in the TensorCPU.");
}
std::vector<int> newShape = shape;
for (size_t i = 0; i < dimOrder.size(); ++i)
{
newShape[i] =shape[dimOrder[i]];
}
return newShape;
}

//concat

Shape concatShape(const std::vector<Shape> &shapes,const int axis){
std::vector<int> outputShape(shapes[0].dim);
outputShape=shapes[0].shape;
for (int i = 1; i < shapes.size(); ++i)
{
if (shapes[i].dim != outputShape.size())
{
throw std::invalid_argument("All tensors must have the same number of dimensions.");
}
for (size_t j = 0; j < outputShape.size(); ++j)
{
if (j == axis)
{
outputShape[j] += shapes[i].shape[j];
}
else if (shapes[i].shape[j] != outputShape[j])
{
throw std::invalid_argument("Shapes of tensors must match except in the concatenation axis.");
}
}
}
return Shape(outputShape);
}

//broadcast
std::vector<int> broadcastShape(const std::vector<int> &a, const std::vector<int> &b)
{
int len1 = a.size();
Expand Down
76 changes: 76 additions & 0 deletions excuter/cpp-common/src/deepx/shape_changeshape.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#ifndef DEEPX_SHAPE_CHANGESHAPE_HPP
#define DEEPX_SHAPE_CHANGESHAPE_HPP

#include <vector>
#include <numeric>
#include <algorithm>
#include <stdexcept>
#include "deepx/tensor.hpp"
#include "deepx/shape.hpp"
#include "stdutil/error.hpp"

namespace deepx
{
// transpose

using namespace std;
std::vector<int> swaplastTwoDimOrder(const std::vector<int> &shape);

std::vector<int> transposeShape(const std::vector<int> &shape, const std::vector<int> &dimOrder);

// concat

Shape concatShape(const std::vector<Shape> &shapes, const int axis);

template <typename T>
Shape concatShape(const std::vector<Tensor<T> *> &tensors, const int axis)
{
std::vector<Shape> shapes;
for (int i = 0; i < tensors.size(); ++i)
{
shapes.push_back(tensors[i]->shape);
}
return concatShape(shapes, axis);
}

template <typename T>
bool checkShapeConcat(const std::vector<Tensor<T> *> &tensors, const int axis, const Tensor<T> &output)
{
int axisDim = 0;
for (int i = 0; i < tensors.size(); i++)
{
if (tensors[i]->shape.dim != output.shape.dim)
{
throw TensorShapeError("All input tensors must have the same dimension size for concat");
}
for (int j = 0; j < tensors[i]->shape.dim; j++)
{
if (j != axis)
{
if (tensors[i]->shape[j] != output.shape[j])
{
throw TensorShapeError("All input tensors must have the same dimension size for concat");
}
}
else
{
axisDim += tensors[i]->shape[j];
}
}
}
return axisDim == output.shape[axis];
}

// broadcast
std::vector<int> broadcastShape(const std::vector<int> &a, const std::vector<int> &b);
enum BroadcastMap
{
xTox = 0,
nullTo1 = 1,
xTo1 = 2,
};
std::vector<BroadcastMap> broadcastMap(const std::vector<int> &a, const std::vector<int> &b);

}

#endif // DEEPX_SHAPE_CHANGESHAPE_HPP
30 changes: 0 additions & 30 deletions excuter/cpp-common/src/deepx/shape_concat.cpp

This file was deleted.

50 changes: 0 additions & 50 deletions excuter/cpp-common/src/deepx/shape_concat.hpp

This file was deleted.

78 changes: 47 additions & 31 deletions excuter/cpp-common/src/deepx/shape_reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,65 @@
#include <algorithm>
#include <stdexcept>

#include "stdutil/error.hpp"
#include "deepx/shape_reduce.hpp"

namespace deepx
{
std::vector<int> reduceDimMap(const Shape &shape, const std::vector<int> &dims)
std::vector<int> checkedDims(const std::vector<int> &inputshape, const std::vector<int> &dims)
{
// Step 1: 确定输出形状
std::vector<int> sumDims;
std::vector<int> checkeddims;
// 如果dims为空,则求和所有维度
if (dims.empty())
{
for (int i = 0; i < shape.dim; ++i)
for (int i = 0; i < inputshape.size(); ++i)
{
sumDims.push_back(i);
checkeddims.push_back(i);
}
}
else
{
sumDims = std::vector<int>(dims.data(), dims.data() + dims.size());
}
std::sort(sumDims.begin(), sumDims.end());
// 去重
sumDims.erase(std::unique(sumDims.begin(), sumDims.end()), sumDims.end());

// 验证维度
for (int d : sumDims)
{
if (d < 0 || d >= shape.dim)
{
// 验证维度
for (int d : dims)
{
throw std::invalid_argument("Dimension out of range in sum");
if (d < 0)
{
d = inputshape.size() + d;
}
if (d >= inputshape.size())
{
throw TensorShapeError("Dimension out of range in sum");
}
checkeddims.push_back(d);
}
}

// 创建一个映射数组,标记哪些维度需要求和
std::vector<int> sumMap(shape.dim, 0);
for (int dim : sumDims)
{
sumMap[dim] = 1;
}
return sumMap;
// 排序
std::sort(checkeddims.begin(), checkeddims.end());
// 去重
checkeddims.erase(std::unique(checkeddims.begin(), checkeddims.end()), checkeddims.end());

return checkeddims;
}
std::vector<int> reduceShape(const Shape &a, const std::vector<int> &dims)

std::vector<int> reducedShape(const std::vector<int> &inputshape, const std::vector<int> &dims, const bool keepdim)
{

// 创建一个映射数组,标记哪些维度需要求和
std::vector<int> reduceMap = reduceDimMap(a, dims);
std::vector<int> reducedims = reducedDim(inputshape, dims);

// 计算输出形状
std::vector<int> outputShape;

for (size_t i = 0; i < a.dim; ++i)
for (size_t i = 0; i < inputshape.size(); ++i)
{
if (reduceMap[i] == 0)
if (reducedims[i] == 0)
{
outputShape.push_back(inputshape[i]);
}
else if (keepdim)
{
outputShape.push_back(a[i]);
outputShape.push_back(1);
}
}

Expand All @@ -66,5 +71,16 @@ namespace deepx
outputShape.push_back(1);
}
return outputShape;
}
}

std::vector<int> reducedDim(const std::vector<int> &shape, const std::vector<int> &dims)
{
// 创建一个映射数组,标记哪些维度需要求和
std::vector<int> sumMap(shape.size(), 0);
for (int dim : dims)
{
sumMap[dim] = 1;
}
return sumMap;
}
}
Loading