Skip to content

Automated sync from github.com/tensorflow/tensorflow #3124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
71 changes: 40 additions & 31 deletions tensorflow/lite/core/c/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,41 @@ void TfLiteVarArrayFree(T* a) {

#ifndef TF_LITE_STATIC_MEMORY

TfLiteSparsity TfLiteSparsityClone(const TfLiteSparsity& src) {
TfLiteSparsity dst = src;
dst.traversal_order = TfLiteIntArrayCopy(src.traversal_order);
dst.block_map = TfLiteIntArrayCopy(src.block_map);
if (src.dim_metadata) {
dst.dim_metadata = reinterpret_cast<TfLiteDimensionMetadata*>(
calloc(1, sizeof(TfLiteDimensionMetadata) * src.dim_metadata_size));
for (int i = 0; i < src.dim_metadata_size; ++i) {
dst.dim_metadata[i] = src.dim_metadata[i];
dst.dim_metadata[i].array_segments =
TfLiteIntArrayCopy(src.dim_metadata[i].array_segments);
dst.dim_metadata[i].array_indices =
TfLiteIntArrayCopy(src.dim_metadata[i].array_indices);
}
}
return dst;
}

// Clones the source sparsity to a newly allocated object.
TfLiteSparsity* TfLiteSparsityClone(const TfLiteSparsity* const src) {
if (!src) {
return nullptr;
}
TfLiteSparsity* dst =
reinterpret_cast<TfLiteSparsity*>(calloc(1, sizeof(TfLiteSparsity)));
*dst = TfLiteSparsityClone(*src);
return dst;
}

#endif // TF_LITE_STATIC_MEMORY

} // namespace

#ifndef TF_LITE_STATIC_MEMORY

TfLiteQuantization TfLiteQuantizationClone(const TfLiteQuantization& src) {
TfLiteQuantization dst;
dst.type = src.type;
Expand Down Expand Up @@ -136,39 +171,8 @@ TfLiteQuantization TfLiteQuantizationClone(const TfLiteQuantization& src) {
return dst;
}

TfLiteSparsity TfLiteSparsityClone(const TfLiteSparsity& src) {
TfLiteSparsity dst = src;
dst.traversal_order = TfLiteIntArrayCopy(src.traversal_order);
dst.block_map = TfLiteIntArrayCopy(src.block_map);
if (src.dim_metadata) {
dst.dim_metadata = reinterpret_cast<TfLiteDimensionMetadata*>(
calloc(1, sizeof(TfLiteDimensionMetadata) * src.dim_metadata_size));
for (int i = 0; i < src.dim_metadata_size; ++i) {
dst.dim_metadata[i] = src.dim_metadata[i];
dst.dim_metadata[i].array_segments =
TfLiteIntArrayCopy(src.dim_metadata[i].array_segments);
dst.dim_metadata[i].array_indices =
TfLiteIntArrayCopy(src.dim_metadata[i].array_indices);
}
}
return dst;
}

// Clones the source sparsity to a newly allocated object.
TfLiteSparsity* TfLiteSparsityClone(const TfLiteSparsity* const src) {
if (!src) {
return nullptr;
}
TfLiteSparsity* dst =
reinterpret_cast<TfLiteSparsity*>(calloc(1, sizeof(TfLiteSparsity)));
*dst = TfLiteSparsityClone(*src);
return dst;
}

#endif // TF_LITE_STATIC_MEMORY

} // namespace

extern "C" {

size_t TfLiteIntArrayGetSizeInBytes(int size) {
Expand Down Expand Up @@ -247,6 +251,11 @@ void TfLiteQuantizationFree(TfLiteQuantization* quantization) {
}
free(q_params);
}
if (quantization->type == kTfLiteBlockwiseQuantization) {
TfLiteBlockwiseQuantization* q_params =
reinterpret_cast<TfLiteBlockwiseQuantization*>(quantization->params);
free(q_params);
}
quantization->params = nullptr;
quantization->type = kTfLiteNoQuantization;
}
Expand Down
4 changes: 4 additions & 0 deletions tensorflow/lite/core/c/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ TfLiteStatus TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor);
/// If all dimensions are known, this is the same as `t->dims`.
/// (`dims_signature` is NULL or empty if all dimensions are known.)
const TfLiteIntArray* TfLiteTensorGetDimsSignature(const TfLiteTensor* t);

#endif // TF_LITE_STATIC_MEMORY

/// WARNING: This is an experimental interface that is subject to change.
Expand Down Expand Up @@ -1633,5 +1634,8 @@ TfLiteStatus TfLiteTensorVariantRealloc(TfLiteTensor* t,
return kTfLiteOk;
}

// Returns a copy of the quantization parameters of the tensor.
TfLiteQuantization TfLiteQuantizationClone(const TfLiteQuantization& src);

#endif // __cplusplus
#endif // TENSORFLOW_LITE_CORE_C_COMMON_H_
Loading