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
2 changes: 1 addition & 1 deletion doc/excuter/op-mem-cuda/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
|-----------|--------|------------|--------------|----------------|
| 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) | T2 = T1.transpose(dimorder=[1,0]) | transpose(tensor<any> A, vector<int32> dim_order)->(tensor<any> C) |
| reshape | miaobyte | reshape(tensor<any> A, vector<int32> shape)->() | T2=T1.reshape(shape) | reshape(tensor<any> A, vector<int32> shape)->() |
| reshape | miaobyte | reshape(tensor<any> A, vector<int32> shape)->(tensor<any> B) | T1.reshape(shape)->T2 | reshape(tensor<any> A, vector<int32> shape)->(tensor<any> B) |
| matmul | cublas | matmul(tensor<any> A, tensor<any> B)->(tensor<any> C) | T3=T1 @ T2 | matmul(tensor<any> A, tensor<any> B)->(tensor<any> C) |
| comparescalar | miaobyte | comparescalar(tensor<any> A, var<any> scalar)->(tensor<int8> mask) | mask=compare(T1, scalar) | comparescalar(tensor<any> A, var<any> scalar)->(tensor<int8> mask) |
| add | cublas | add(tensor<any> a, tensor<any> b)->(tensor<any> c) | T3=T1+T2 | add(tensor<any> a, tensor<any> b)->(tensor<any> c) |
Expand Down
6 changes: 3 additions & 3 deletions excuter/cpp-common/src/deepx/tensorfunc/changeshape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace deepx::tensorfunc
template <typename Author, typename T>
struct reshapeDispatcher
{
static void reshape(Tensor<T> &tensor, const std::vector<int> &new_shape) = delete;
static void reshape(const Tensor<T> &tensor, const std::vector<int> &new_shape,Tensor<T> &output) = delete;
};

// A.reshape(new_shape)
template <typename Author, typename T>
void reshape(Tensor<T> &tensor, const std::vector<int> &new_shape)
void reshape(const Tensor<T> &tensor, const std::vector<int> &new_shape,Tensor<T> &output)
{
reshapeDispatcher<Author, T>::reshape(tensor, new_shape);
reshapeDispatcher<Author, T>::reshape(tensor, new_shape,output);
}

template <typename Author, typename T>
Expand Down
2 changes: 1 addition & 1 deletion excuter/cpp-common/src/deepx/tf/tf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ namespace deepx::tf
}
std::stringstream ss(textvalue);
std::string item;
while (std::getline(ss, item, ','))
while (std::getline(ss, item, ' '))
{
result.push_back(to<T>(item));
}
Expand Down
5 changes: 4 additions & 1 deletion excuter/op-mem-cuda/src/client/tfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,10 @@ namespace deepx::tf
Param("A", DataCategory::Tensor, Precision::Any),
Param("shape", DataCategory::Vector, Precision::Int32),
}),
vector<Param>()));
vector<Param>(
{
Param("B", DataCategory::Tensor, Precision::Any),
})));

tffactory.add_tf(std::make_shared<Transpose<miaobyte>>(vector<Param>(
{
Expand Down
87 changes: 52 additions & 35 deletions excuter/op-mem-cuda/src/deepx/tensorfunc/changeshape_miaobyte.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,30 @@ namespace deepx::tensorfunc
template <typename T>
struct reshapeDispatcher<miaobyte, T>
{
static void reshape(Tensor<T> &tensor, const std::vector<int> &new_shape)
static void reshape(const Tensor<T> &tensor, const std::vector<int> &shape, Tensor<T> &output)
{
if (tensor.shape.dim != new_shape.size())
int new_prod = 1;
for (int dim : shape)
{
throw std::runtime_error("Tensor shapes must match for reshape");
new_prod *= dim;
}

if (tensor.shape.size != new_prod)
{
throw std::invalid_argument("Shape size mismatch");
}
Shape newshape(shape);
if (tensor.data == output.data)
{
output.shape.shape=newshape.shape;
output.shape.strides=newshape.strides;
}
else
{
output.shape.shape=newshape.shape;
output.shape.strides=newshape.strides;
output.copyer(tensor.data, output.data, tensor.shape.size);
}
tensor.shape = Shape(new_shape);
}
};

Expand All @@ -35,46 +52,46 @@ namespace deepx::tensorfunc
}
auto [actual_blocks, optimal_block_size] = BestDims(tensor.shape.size);
launch_transpose<T>(actual_blocks, optimal_block_size,
tensor.data, tensor.shape.strides.data(),
output.data, output.shape.strides.data(),
tensor.shape.dim, tensor.shape.size, dim_order.data());
tensor.data, tensor.shape.strides.data(),
output.data, output.shape.strides.data(),
tensor.shape.dim, tensor.shape.size, dim_order.data());
}
};

template <typename T>
struct concatDispatcher<miaobyte, T>
{
static void concat(const vector<Tensor<T>*> tensors, const int axis, Tensor<T> &C)
{
//checkshape
if (!checkShapeConcat(tensors, axis, C))
{
throw TensorShapeError("Output tensor shape size must match the sum of input tensor shape sizes for concat");
}
static void concat(const vector<Tensor<T> *> tensors, const int axis, Tensor<T> &C)
{
// checkshape
if (!checkShapeConcat(tensors, axis, C))
{
throw TensorShapeError("Output tensor shape size must match the sum of input tensor shape sizes for concat");
}

vector<const T *> tensorsData(tensors.size());
for (int i = 0; i < tensors.size(); i++)
{
tensorsData[i] = tensors[i]->data;
}

vector<const T*> tensorsData(tensors.size());
for (int i = 0; i < tensors.size(); i++)
{
tensorsData[i] = tensors[i]->data;
}
vector<int> inputStrides;
for (int i = 0; i < tensors.size(); i++)
{
std::copy(tensors[i]->shape.strides.data(), tensors[i]->shape.strides.data() + tensors[i]->shape.dim, std::back_inserter(inputStrides));
}

vector< int> inputStrides;
for (int i = 0; i < tensors.size(); i++)
{
std::copy(tensors[i]->shape.strides.data(), tensors[i]->shape.strides.data() + tensors[i]->shape.dim, std::back_inserter(inputStrides));
}

vector<int> shapeAtAxis(tensors.size());
for (int i = 0; i < tensors.size(); i++)
{
shapeAtAxis[i] = tensors[i]->shape[axis];
}
vector<int> shapeAtAxis(tensors.size());
for (int i = 0; i < tensors.size(); i++)
{
shapeAtAxis[i] = tensors[i]->shape[axis];
}

launch_concat<T>(tensorsData.data(), inputStrides.data(),
C.data, C.shape.strides.data(),
C.shape.dim,
C.shape.size,
axis, tensors.size(), shapeAtAxis.data());
launch_concat<T>(tensorsData.data(), inputStrides.data(),
C.data, C.shape.strides.data(),
C.shape.dim,
C.shape.size,
axis, tensors.size(), shapeAtAxis.data());
};
};
}
Expand Down
14 changes: 7 additions & 7 deletions excuter/op-mem-cuda/src/deepx/tf/changeshape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace deepx::tf

string math_formula() const override
{
return "T2=T1.reshape(shape)";
return "T1.reshape(shape)->T2";
}

shared_ptr<TF> clone() const override
Expand All @@ -47,22 +47,22 @@ namespace deepx::tf
switch (input_type)
{
case Precision::Float64:
reshape<Author, double>(*mem->gettensor<double>(this->args[0].textvalue), shape);
reshape<Author, double>(*mem->gettensor<double>(this->args[0].textvalue), shape, *mem->gettensor<double>(this->returns[0].textvalue));
break;
case Precision::Float32:
reshape<Author, float>(*mem->gettensor<float>(this->args[0].textvalue), shape);
reshape<Author, float>(*mem->gettensor<float>(this->args[0].textvalue), shape, *mem->gettensor<float>(this->returns[0].textvalue));
break;
case Precision::Int64:
reshape<Author, int64_t>(*mem->gettensor<int64_t>(this->args[0].textvalue), shape);
reshape<Author, int64_t>(*mem->gettensor<int64_t>(this->args[0].textvalue), shape, *mem->gettensor<int64_t>(this->returns[0].textvalue));
break;
case Precision::Int32:
reshape<Author, int32_t>(*mem->gettensor<int32_t>(this->args[0].textvalue), shape);
reshape<Author, int32_t>(*mem->gettensor<int32_t>(this->args[0].textvalue), shape, *mem->gettensor<int32_t>(this->returns[0].textvalue));
break;
case Precision::Int16:
reshape<Author, int16_t>(*mem->gettensor<int16_t>(this->args[0].textvalue), shape);
reshape<Author, int16_t>(*mem->gettensor<int16_t>(this->args[0].textvalue), shape, *mem->gettensor<int16_t>(this->returns[0].textvalue));
break;
case Precision::Int8:
reshape<Author, int8_t>(*mem->gettensor<int8_t>(this->args[0].textvalue), shape);
reshape<Author, int8_t>(*mem->gettensor<int8_t>(this->args[0].textvalue), shape, *mem->gettensor<int8_t>(this->returns[0].textvalue));
break;
default:
error = "Unsupported type: " + precision_str(input_type);
Expand Down
5 changes: 4 additions & 1 deletion excuter/op-mem-ompsimd/src/client/tfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,10 @@ namespace deepx::tf
Param("A", DataCategory::Tensor, Precision::Any),
Param("shape", DataCategory::Vector, Precision::Int32),
}),
vector<Param>()));
vector<Param>(
{
Param("B", DataCategory::Tensor, Precision::Any),
})));

tffactory.add_tf(std::make_shared<Transpose<miaobyte>>(vector<Param>(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ namespace deepx::tensorfunc
template <typename T>
struct reshapeDispatcher<miaobyte, T>
{
static void reshape(Tensor<T> &tensor, const std::vector<int> &shape)
static void reshape(const Tensor<T> &tensor, const std::vector<int> &shape,Tensor<T> &output)
{ // 参数改为单个tensor引用

int new_prod = 1;
for (int dim : shape)
{
Expand All @@ -28,7 +28,18 @@ namespace deepx::tensorfunc
{
throw std::invalid_argument("Shape size mismatch");
}
tensor.shape = Shape(shape);
Shape newshape(shape);
if (tensor.data == output.data)
{
output.shape.shape=newshape.shape;
output.shape.strides=newshape.strides;
}
else
{
output.shape.shape=newshape.shape;
output.shape.strides=newshape.strides;
output.copyer(tensor.data, output.data, tensor.shape.size);
}
}
};

Expand Down
16 changes: 8 additions & 8 deletions excuter/op-mem-ompsimd/src/deepx/tf/changeshape.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace deepx::tf

string math_formula() const override
{
return "T2=T1.reshape(shape)";
return "T1.reshape(shape)->T2";
}

shared_ptr<TF> clone() const override
Expand All @@ -46,22 +46,22 @@ namespace deepx::tf
switch (input_type)
{
case Precision::Float64:
reshape<Author, double>(*mem->gettensor<double>(this->args[0].textvalue), shape);
reshape<Author, double>(*mem->gettensor<double>(this->args[0].textvalue), shape, *mem->gettensor<double>(this->returns[0].textvalue));
break;
case Precision::Float32:
reshape<Author, float>(*mem->gettensor<float>(this->args[0].textvalue), shape);
reshape<Author, float>(*mem->gettensor<float>(this->args[0].textvalue), shape, *mem->gettensor<float>(this->returns[0].textvalue));
break;
case Precision::Int64:
reshape<Author, int64_t>(*mem->gettensor<int64_t>(this->args[0].textvalue), shape);
reshape<Author, int64_t>(*mem->gettensor<int64_t>(this->args[0].textvalue), shape, *mem->gettensor<int64_t>(this->returns[0].textvalue));
break;
case Precision::Int32:
reshape<Author, int32_t>(*mem->gettensor<int32_t>(this->args[0].textvalue), shape);
reshape<Author, int32_t>(*mem->gettensor<int32_t>(this->args[0].textvalue), shape, *mem->gettensor<int32_t>(this->returns[0].textvalue));
break;
case Precision::Int16:
reshape<Author, int16_t>(*mem->gettensor<int16_t>(this->args[0].textvalue), shape);
reshape<Author, int16_t>(*mem->gettensor<int16_t>(this->args[0].textvalue), shape, *mem->gettensor<int16_t>(this->returns[0].textvalue));
break;
case Precision::Int8:
reshape<Author, int8_t>(*mem->gettensor<int8_t>(this->args[0].textvalue), shape);
reshape<Author, int8_t>(*mem->gettensor<int8_t>(this->args[0].textvalue), shape, *mem->gettensor<int8_t>(this->returns[0].textvalue));
break;
default:
error = "Unsupported type: " + precision_str(input_type);
Expand All @@ -85,7 +85,7 @@ namespace deepx::tf

string math_formula() const override
{
return "T2 = T1.transpose(dimorder=[1,0])";
return "T1.transpose(dimorder=[1,0])->T2";
}

shared_ptr<TF> clone() const override
Expand Down
4 changes: 2 additions & 2 deletions front/py/deepx/nn/deepxir.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def __init__(self,
"""

self._name = name
self._args = args
self._returns = returns
self._args = [arg if isinstance(arg, Param) else Param(arg) for arg in args]
self._returns = [ret if isinstance(ret, Param) else Param(ret) for ret in returns]
self._author = author
self._id=None
self._created_at=time.time()
Expand Down
4 changes: 2 additions & 2 deletions front/py/deepx/nn/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .matmul import matmul
from .init import *
from .reduce import reduce_max,reduce_min,sum,prod,mean
from .changeshape import transpose,reshape,broadcast_shape,broadcast_to,unsqueeze
from .changeshape import *
from .activite import relu,sigmoid,swish
__all__ = [
"newtensor",
Expand All @@ -13,7 +13,7 @@
"add","sub","mul","div","sqrt","pow","exp","log","rsqrt",
"matmul",
"max","min","sum","prod","mean",
"transpose","reshape","broadcast_shape","broadcast_to","unsqueeze",
"reshape","permute","transpose",
"relu","sigmoid","swish",

]
Loading