MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / SparsifyWeights

Function SparsifyWeights

tensorflow/tools/graph_transforms/sparsify_gather.cc:40–79  ·  view source on GitHub ↗

Sparsify Tensor of shape [N, 1]. Return the indices and values vectors for non-zero tensor content.

Source from the content-addressed store, hash-verified

38// Sparsify Tensor of shape [N, 1]. Return the indices and values vectors for
39// non-zero tensor content.
40Status SparsifyWeights(const Tensor& tensor, Tensor* indices_tensor,
41 Tensor* values_tensor) {
42 if (tensor.dims() != 2 || tensor.dim_size(1) != 1) {
43 return tensorflow::errors::FailedPrecondition(
44 "Transform only applicable to subgraph with 'Const' with "
45 "tensor of shape [N, 1]. But instead get shape ",
46 tensor.shape().DebugString(), ".");
47 }
48
49 auto flat = tensor.flat<float>();
50 std::vector<int64> indices;
51 std::vector<float> values;
52
53 for (int64 i = 0; i < flat.size(); i++) {
54 float val = flat(i);
55 if (std::abs(val) >= 1.0e-5) {
56 indices.push_back(i);
57 values.push_back(val);
58 }
59 }
60
61 // During model initialization, InitializeTableOp makes use of
62 // KeyValueTensorIterator, which does not accept empty keys or values.
63 // Consequently, adding a dummy pair of indices and values as a walkaround.
64 if (indices.empty() || values.empty()) {
65 indices.push_back(0);
66 values.push_back(0);
67 }
68 *indices_tensor = Tensor(DataTypeToEnum<int64>::value,
69 {static_cast<int64>(indices.size())});
70 std::copy_n(indices.begin(), indices.size(),
71 indices_tensor->flat<int64>().data());
72
73 *values_tensor =
74 Tensor(DataTypeToEnum<float>::value, {static_cast<int64>(values.size())});
75 std::copy_n(values.begin(), values.size(),
76 values_tensor->flat<float>().data());
77
78 return Status::OK();
79}
80
81void CreateConstNode(const Tensor& tensor, const string& name,
82 NodeDef* node_def) {

Callers 1

SparsifyGatherInternalFunction · 0.85

Calls 12

FailedPreconditionFunction · 0.85
absFunction · 0.50
TensorStruct · 0.50
dimsMethod · 0.45
dim_sizeMethod · 0.45
DebugStringMethod · 0.45
shapeMethod · 0.45
sizeMethod · 0.45
push_backMethod · 0.45
emptyMethod · 0.45
beginMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected