!8768 fix reshape dynamic and emb

From: @fangzehua
Reviewed-by: 
Signed-off-by:
pull/8768/MERGE
mindspore-ci-bot 4 years ago committed by Gitee
commit 266e419bb7

@ -67,8 +67,12 @@ void AssignCPUKernel::LaunchKernel(const std::vector<AddressPtr> &inputs,
const std::vector<kernel::AddressPtr> &outputs) {
T *input_x = reinterpret_cast<T *>(inputs[0]->addr);
T *input_y = reinterpret_cast<T *>(inputs[1]->addr);
auto max_size = inputs[0]->size;
size_t total_size = input_x_dtype_size_ * batch_size_;
if (total_size > max_size) {
MS_LOG(EXCEPTION) << "Memcpy size must <= max_size, but got memcpy size is : " << total_size
<< ", max size is : " << max_size;
}
int ret = memcpy_s(input_x, total_size, input_y, total_size);
if (ret != 0) {
MS_LOG(EXCEPTION) << "memcpy_s error, errorno" << ret;

@ -49,6 +49,9 @@ void CacheSwapHashmapCPUKernel::InitKernel(const CNodePtr &kernel_node) {
}
hashmap_length_ = hashmap_shape[0];
if (hashmap_length_ <= 0) {
MS_LOG(EXCEPTION) << "Hashmap length must > 0";
}
dtype_ = AnfAlgo::GetPrevNodeOutputInferDataType(kernel_node, 0);
}

@ -85,6 +85,9 @@ void MapCacheIdxCPUKernel::InitKernel(const CNodePtr &kernel_node) {
MS_LOG(EXCEPTION) << "Dimension of HashMap must be 2, (n, 4)";
}
hashmap_length_ = hashmap_shape[0];
if (hashmap_length_ <= 0) {
MS_LOG(EXCEPTION) << "Hashmap length must > 0";
}
dtype_ = AnfAlgo::GetPrevNodeOutputInferDataType(kernel_node, 0);
}
@ -154,9 +157,14 @@ void MapCacheIdxCPUKernel::LaunchKernel(const std::vector<AddressPtr> &inputs,
hashmap[tmp_entry].step = step_[0];
}
}
MS_LOG(INFO) << "Miss count: " << miss_count;
MS_LOG(INFO) << "Avg search count: " << total_count / count_size;
MS_LOG(INFO) << "Cache hit rate: " << hit_count / count_size;
if (miss_count != 0) {
MS_LOG(INFO) << "Miss count: " << miss_count;
}
if (count_size != 0) {
MS_LOG(INFO) << "Avg search count: " << total_count / count_size;
MS_LOG(INFO) << "Cache hit rate: " << hit_count / count_size;
}
float total_insert_count = 0;
float total_delete_count = 0;
// swap hash map
@ -193,8 +201,10 @@ void MapCacheIdxCPUKernel::LaunchKernel(const std::vector<AddressPtr> &inputs,
total_delete_count += (compress_count + delete_count);
total_insert_count += tag_count;
}
MS_LOG(INFO) << "Insert count: " << total_insert_count / miss_count;
MS_LOG(INFO) << "Delete count: " << total_delete_count / miss_count;
if (miss_count != 0) {
MS_LOG(INFO) << "Insert count: " << total_insert_count / miss_count;
MS_LOG(INFO) << "Delete count: " << total_delete_count / miss_count;
}
// update step
step_[0] += 1;
// update cache idx

@ -21,7 +21,7 @@ namespace kernel {
void ReshapeCPUKernel::InitKernel(const CNodePtr &kernel_node) {
MS_EXCEPTION_IF_NULL(kernel_node);
node_ = kernel_node;
x_data_type_ = AnfAlgo::GetPrevNodeOutputInferDataType(kernel_node, 0);
x_data_type_ = AnfAlgo::GetInputDeviceDataType(kernel_node, 0);
type_size_ = GetTypeByte(TypeIdToType(x_data_type_));
}

@ -33,6 +33,9 @@ void SearchCacheIdxCPUKernel::InitKernel(const CNodePtr &kernel_node) {
}
hashmap_length_ = hashmap_shape[0];
if (hashmap_length_ <= 0) {
MS_LOG(EXCEPTION) << "Hashmap length must > 0";
}
dtype_ = AnfAlgo::GetPrevNodeOutputInferDataType(kernel_node, 0);
}
@ -96,8 +99,10 @@ void SearchCacheIdxCPUKernel::LaunchKernel(const std::vector<AddressPtr> &inputs
output_miss_emb_idx[i] = -1;
}
}
MS_LOG(INFO) << "avg search count: " << total_count / count_size;
MS_LOG(INFO) << "cache hit rate: " << hit_count / count_size;
if (count_size != 0) {
MS_LOG(INFO) << "avg search count: " << total_count / count_size;
MS_LOG(INFO) << "cache hit rate: " << hit_count / count_size;
}
}
} // namespace kernel
} // namespace mindspore

@ -72,13 +72,18 @@ void UpdateCacheCPUKernel::LaunchKernel(const std::vector<AddressPtr> &inputs,
max_num_ = *reinterpret_cast<T *>(inputs[3]->addr);
size_t one_length_size = input_x_dtype_size_ * update_length_;
auto max_size = inputs[0]->size;
for (size_t i = 0; i < batch_size_; ++i) {
if (indices[i] < 0 || indices[i] >= max_num_) continue;
char *tmp = update + i * one_length_size;
int ret = memcpy_s(input_x + indices[i] * one_length_size, one_length_size, tmp, one_length_size);
if (ret != 0) {
MS_LOG(EXCEPTION) << "memcpy_s error, errorno" << ret;
if (indices[i] * one_length_size + one_length_size <= max_size) {
int ret = memcpy_s(input_x + indices[i] * one_length_size, one_length_size, tmp, one_length_size);
if (ret != 0) {
MS_LOG(EXCEPTION) << "memcpy_s error, errorno" << ret;
}
} else {
MS_LOG(EXCEPTION) << "Memcpy out of size";
}
}
}

@ -43,7 +43,8 @@ const AnfNodePtr ConvertConstInputToAttr::Process(const FuncGraphPtr &, const An
todos.push_back(node);
}
std::set<string> DynamicShapeConstInputToAttr = {kCastOpName, kExpandDimsOpName};
std::set<string> DynamicShapeConstInputToAttr = {kCastOpName, kExpandDimsOpName, kReshapeOpName,
kEmbeddingLookupOpName};
for (auto &t : todos) {
CNodePtr cnode = t->cast<CNodePtr>();
ConstInputToAttrInfoRegister reg;

@ -80,6 +80,7 @@ constexpr auto kUnsortedSegmentProdOpName = "UnsortedSegmentProd";
constexpr auto kUnsortedSegmentMinOpName = "UnsortedSegmentMin";
constexpr auto kFlattenGradOpName = "FlattenGrad";
constexpr auto kExpandDimsOpName = "ExpandDims";
constexpr auto kReshapeOpName = "Reshape";
constexpr auto kSplitOpName = "Split";
constexpr auto kSplitVOpName = "SplitV";
constexpr auto kSparseApplyAdagradOpName = "SparseApplyAdagrad";

@ -597,7 +597,7 @@ AbstractBasePtr InferImplReshape(const AnalysisEnginePtr &, const PrimitivePtr &
auto reshape_tuple = reshape_value_tuple->value();
(void)std::transform(std::begin(reshape_tuple), std::end(reshape_tuple), std::back_inserter(shape),
[](const ValuePtr &e) -> int { return GetValue<int>(e); });
[](const ValuePtr &e) -> int64_t { return GetValue<int64_t>(e); });
auto max_shape = shape;
auto min_shape = shape;

@ -445,14 +445,6 @@ class Reshape(PrimitiveWithInfer):
validator.check_subclass("x", x['dtype'], mstype.tensor, self.name)
validator.check_value_type("shape", shape_v, [tuple], self.name)
shape_v = list(shape_v)
if 'max_shape' in x:
x_max_shape = x['max_shape']
else:
x_max_shape = x['shape']
if 'min_shape' in x:
x_min_shape = x['min_shape']
else:
x_min_shape = x['shape']
neg_index = -1
dim_prod = 1
for i, shp_i in enumerate(shape_v):
@ -464,34 +456,49 @@ class Reshape(PrimitiveWithInfer):
else:
dim_prod *= shp_i
arr_prod = np.prod(x_shp)
max_arr_prod = np.prod(x_max_shape)
min_arr_prod = np.prod(x_min_shape)
if dim_prod <= 0 or arr_prod % dim_prod != 0:
raise ValueError(f'For \'{self.name}\' input_x\'s shape is {x_shp}, input_shape\'s value is {shape_v}.'
f'The product of input_x\'s shape should > 0, '
f'and can be divided by product of input_shape, '
f'but product of input_x\'s shape is {arr_prod}, product of input_shape is {dim_prod}.')
max_shape = list(shape_v)
min_shape = list(shape_v)
if neg_index != -1:
shape_v[neg_index] = int(arr_prod / dim_prod)
max_shape[neg_index] = int(max_arr_prod / dim_prod)
min_shape[neg_index] = int(min_arr_prod / dim_prod)
dim_prod *= shape_v[neg_index]
if dim_prod != arr_prod:
raise ValueError(f'For \'{self.name}\' input_x\'s shape is {x_shp}, input_shape\'s value is {shape_v}.'
f'The product of input_x\'s shape should be equal to product of input_shape, '
f'but product of input_x\'s shape is {arr_prod}, product of input_shape is {dim_prod}.')
value = None
if x['value'] is not None:
value = Tensor(x['value'].asnumpy().reshape(shape_v))
out = {'shape': tuple(shape_v),
'dtype': x['dtype'],
'value': value,
'max_shape': tuple(max_shape),
'min_shape': tuple(min_shape)}
if arr_prod <= 0:
if 'max_shape' in x:
x_max_shape = x['max_shape']
else:
x_max_shape = x['shape']
if 'min_shape' in x:
x_min_shape = x['min_shape']
else:
x_min_shape = x['shape']
max_arr_prod = np.prod(x_max_shape)
min_arr_prod = np.prod(x_min_shape)
max_shape = list(shape_v)
min_shape = list(shape_v)
if neg_index != -1:
max_shape[neg_index] = int(max_arr_prod / dim_prod)
min_shape[neg_index] = int(min_arr_prod / dim_prod)
else:
raise ValueError(f'For dynamic shape, Reshape must have neg index')
out = {'shape': shape['value'],
'dtype': x['dtype'],
'value': None,
'max_shape': tuple(max_shape),
'min_shape': tuple(min_shape)}
else:
if dim_prod <= 0 or arr_prod % dim_prod != 0:
raise ValueError(f'For \'{self.name}\' input_x\'s shape is {x_shp}, input_shape\'s value is {shape_v}.'
f'The product of input_x\'s shape should > 0, '
f'and can be divided by product of input_shape, but '
f'product of input_x\'s shape is {arr_prod}, product of input_shape is {dim_prod}.')
if neg_index != -1:
shape_v[neg_index] = int(arr_prod / dim_prod)
dim_prod *= shape_v[neg_index]
if dim_prod != arr_prod:
raise ValueError(f'For \'{self.name}\' input_x\'s shape is {x_shp}, input_shape\'s value is {shape_v}.'
f'The product of input_x\'s shape should be equal to product of input_shape, but '
f'product of input_x\'s shape is {arr_prod}, product of input_shape is {dim_prod}.')
value = None
if x['value'] is not None:
value = Tensor(x['value'].asnumpy().reshape(shape_v))
out = {'shape': tuple(shape_v),
'dtype': x['dtype'],
'value': value}
return out
@ -4267,6 +4274,8 @@ class EmbeddingLookup(PrimitiveWithInfer):
validator.check_tensor_dtype_valid("indices", indices['dtype'], mstype.int_type, self.name)
validator.check_subclass("offset", offset['dtype'], mstype.int_, self.name)
params_shp = params['shape']
if len(params_shp) > 2:
raise ValueError("The dimension of 'params' in EmbeddingLookup must <= 2, but got %d." % len(params_shp))
out_shape = indices['shape'] + params_shp[1:]
if 'max_shape' in indices:
out_max_shape = indices['max_shape'] + params_shp[1:]

Loading…
Cancel
Save